public SilverFunction(string name, List<FunctionArgument> arguments, BlockExpression body, SilverScope context) { Name = name; Arguments = arguments; Body = body; Context = context; }
public override object Run(Scope scope) { var body = (Body as BlockExpression); body.Scope.MergeWithScope(Silver.Globals); body.Scope.MergeWithScope(scope); var visitor = new VariableNameVisitor(); visitor.Visit(body); body.SetChildrenScopes(body.Scope); dynamic block = CompilerServices.CreateLambdaForExpression(body); dynamic res = block(); if (res is Symbol) { var symval = new BlockExpression(new List<Expression> {new VariableExpression(res)}, body.Scope); res = CompilerServices.CreateLambdaForExpression(symval)(); } else if (res is SilverInstance) { var so = (SilverInstance) res; if (so is SilverBoxedInstance) { res = ((SilverBoxedInstance) so).BoxedObject; } } else if (res is SilverNumber) { res = SilverNumber.Convert(res); } else if (res is SilverString) { res = (string) res; } else if (res is SilverArray) { res = ConvertElements((SilverArray)res); } else if (res is SilverDictionary) { res = ConvertElements((SilverDictionary)res); } body.Scope.MergeIntoScope(scope); return res; }
internal object Run(SilverScope scope) { var body = (BlockExpression) Body; body.SetScope(scope); body.SetChildrenScopes(body.Scope); dynamic block = CompilerServices.CreateLambdaForExpression(Expression.Block(body)); dynamic res = block(); if (res is Symbol) { var symval = new BlockExpression(new List<Expression> {new VariableExpression(res)}, body.Scope); res = CompilerServices.CreateLambdaForExpression(symval)(); } return res; }
protected virtual Expression VisitBlock(BlockExpression node) { node.Body.ForEach(arg => Visit(arg)); return node; }