public override object Run(Scope scope) { var body = (Body as BlockExpression); body.Scope.MergeWithScope(Kuma.Globals); body.Scope.MergeWithScope(scope); var visitor = new VariableNameVisitor(); visitor.Visit(body); body.SetChildrenScopes(body.Scope); var block = CompilerServices.CreateLambdaForExpression(body); var 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 KumaInstance) { var so = (KumaInstance)res; if (so is KumaBoxedInstance) { res = ((KumaBoxedInstance)so).BoxedObject; } } else if (res is KumaNumber) { res = KumaNumber.Convert(res); } else if (res is KumaString) { res = (string)res; } else if (res is KumaArray) { res = ConvertElements((KumaArray)res); } else if (res is KumaDictionary) { res = ConvertElements((KumaDictionary)res); } body.Scope.MergeIntoScope(scope); return(res); }
internal object Run(KumaScope scope) { var body = (BlockExpression)Body; body.SetScope(scope); body.SetChildrenScopes(body.Scope); var block = CompilerServices.CreateLambdaForExpression(Expression.Block(body)); var res = block(); if (res is Symbol) { var symval = new BlockExpression(new List <Expression> { new VariableExpression(res) }, body.Scope); res = CompilerServices.CreateLambdaForExpression(symval)(); } return(res); }
public override Expression Reduce() { var forLabel = Label("<kuma_for>"); VariableExpression forReturn = null; LeftHandValueExpression forReturnLh = null; var useReturn = true; if (Body.Type == typeof(void)) { useReturn = false; } else { forReturn = Variable(Constant("<kuma_for_return>")); forReturnLh = LeftHandValue(forReturn); forReturn.Scope = ((KumaExpression)Body).Scope; forReturnLh.Scope = ((KumaExpression)Body).Scope; } var forTest = Variable(Constant("<kuma_for_test>")); var forTestLh = LeftHandValue(forTest); forTest.Scope = ((KumaExpression)Body).Scope; forTestLh.Scope = ((KumaExpression)Body).Scope; var realBody = new List <Expression> { Init, Label(forLabel), }; var testAssign = Assign(forTestLh, Test); realBody.Add(Label(KumaParser.RetryTarget)); testAssign.Scope = (Body as KumaExpression).Scope; realBody.Add(testAssign); IfExpression testIf; if (useReturn) { var returnAssign = Assign(forReturnLh, Body); returnAssign.Scope = (Body as KumaExpression).Scope; testIf = IfThen(forTest, returnAssign); } else { testIf = IfThen(forTest, Body); } testIf.Scope = ((KumaExpression)Body).Scope; realBody.Add(testIf); realBody.Add(Label(KumaParser.ContinueTarget)); realBody.Add(Step); realBody.Add(IfThen(forTest, Goto(forLabel))); realBody.Add(Label(KumaParser.BreakTarget)); if (useReturn) { realBody.Add(forReturn); } var block = new BlockExpression(realBody) { Scope = (Body as KumaExpression).Scope }; return(Convert(block, Type)); }
protected virtual Expression VisitBlock(BlockExpression node) { node.Body.ForEach(arg => Visit(arg)); return(node); }