Example #1
0
File: optim.go Project: cosim/lang
// evaluates lambdas
func (scope *Scope) lambda(tree *ast.Tree, args []*ast.Tree) *ast.Tree {
	sc := scope.childScope()
	if len(tree.Sub[0].Sub) >= len(args) {
		sc.Scope = append(sc.Scope, &variable.Var{
			Var:  "self",
			Tree: ast.CopyTree(tree, new(ast.Tree)),
		})
		//return nil
		for i := 0; i < len(tree.Sub[0].Sub); i++ {
			// populate scope
			sc.Scope = append(sc.Scope, &variable.Var{
				Var:  tree.Sub[0].Sub[i].Val.Var,
				Tree: scope.eval(args[i]),
			})
		}
		tr := sc.eval(tree.Sub[1])
		if tr != nil {
			return tr
		} else {
			return tree.Sub[1]
		}
	} else {
		errorf("lambda: arg number incorrect")
		return nil
	}
}
Example #2
0
File: optim.go Project: cosim/lang
func (scope *Scope) evalVar(tree *ast.Tree) *ast.Tree {
	t := ((*variable.Scope)(scope)).GetName(tree.Val.Var)
	if t != nil && t.Tree != nil {
		return ast.CopyTree(t.Tree, new(ast.Tree))
	} else {
		return nil
	}
}