Implemented simple expressions
This commit is contained in:
parent
c437b1d0f8
commit
37e222e022
12 changed files with 232 additions and 52 deletions
|
@ -55,13 +55,13 @@ func (expr *Expression) Close() {
|
|||
}
|
||||
|
||||
// EachLeaf iterates through all leaves in the tree.
|
||||
func (expr *Expression) EachLeaf(callBack func(*Expression) error) error {
|
||||
func (expr *Expression) EachLeaf(call func(*Expression) error) error {
|
||||
if expr.IsLeaf() {
|
||||
return callBack(expr)
|
||||
return call(expr)
|
||||
}
|
||||
|
||||
for _, child := range expr.Children {
|
||||
err := child.EachLeaf(callBack)
|
||||
err := child.EachLeaf(call)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -71,6 +71,28 @@ func (expr *Expression) EachLeaf(callBack func(*Expression) error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// EachOperation iterates through all the operations in the tree.
|
||||
func (expr *Expression) EachOperation(call func(*Expression) error) error {
|
||||
if expr.IsLeaf() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Don't descend into the parameters of function calls
|
||||
if expr.Token.Text() == "λ" {
|
||||
return call(expr)
|
||||
}
|
||||
|
||||
for _, child := range expr.Children {
|
||||
err := child.EachOperation(call)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return call(expr)
|
||||
}
|
||||
|
||||
// RemoveChild removes a child from the expression.
|
||||
func (expr *Expression) RemoveChild(child *Expression) {
|
||||
for i, c := range expr.Children {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue