private GotoExpression GotoExpression( ExpressionType nodeType, System.Type type, JObject obj) { var value = this.Expression(this.Prop(obj, "value")); var kind = this.Enum <GotoExpressionKind>(this.Prop(obj, "kind")); var targetType = this.Type(this.Prop(obj, "targetType")); var targetName = this.Prop(obj, "targetName").Value <string>(); switch (kind) { case GotoExpressionKind.Break: return(Expr.Break(CreateLabelTarget(targetName, targetType), value)); case GotoExpressionKind.Continue: return(Expr.Continue(CreateLabelTarget(targetName, targetType))); case GotoExpressionKind.Goto: return(Expr.Goto(CreateLabelTarget(targetName, targetType), value)); case GotoExpressionKind.Return: return(Expr.Return(CreateLabelTarget(targetName, targetType), value)); default: throw new NotImplementedException(); } }
private static Func <int[], int[]> GenerateCopyExpression() { var ctor = typeof(int[]).GetConstructor(new[] { typeof(int) }); var get = typeof(int[]).GetMethod("Get", new[] { typeof(int) }); var set = typeof(int[]).GetMethod("Set", new[] { typeof(int), typeof(int) }); var p1 = Exp.Parameter(typeof(int[])); var v1 = Exp.Variable(typeof(int[])); var v2 = Exp.Variable(typeof(int)); var @break = Exp.Label(); var block = Exp.Block( new[] { v1, v2 }, Exp.Assign(v1, Exp.New(ctor, Exp.Property(p1, "Length"))), Exp.Assign(v2, Exp.Constant(0)), Exp.Loop( Exp.IfThenElse( Exp.LessThan(v2, Exp.Property(p1, "Length")), Exp.Block( Exp.Call(v1, set, v2, Exp.Call(p1, get, v2)), Exp.AddAssign(v2, Exp.Constant(1)) ), Exp.Break(@break) ), @break), v1 ); return(Exp.Lambda <Func <int[], int[]> >(block, new ParameterExpression[] { p1 }).Compile()); }
public void Factorial() { var value = LinqExpression.Parameter(typeof(int), "value"); var result = LinqExpression.Parameter(typeof(int), "result"); var label = LinqExpression.Label(typeof(int), "label"); var one = LinqExpression.Constant(1); var expression = LinqExpression.Block( new[] { result }, LinqExpression.Assign( result, one), LinqExpression.Loop( LinqExpression.Condition( LinqExpression.GreaterThan( value, one), LinqExpression.MultiplyAssign( result, LinqExpression.PostDecrementAssign( value)), LinqExpression.Break( label, result), typeof(void)), label)); ShouldRoundrip(expression); }
public void Break() { var expression = LinqExpression.Break( LinqExpression.Label( typeof(void))); ShouldRoundrip(expression); }
public Expr CompileStatement(ParseTreeNode parseNode) { switch (parseNode.Term.Name) { case "GlobalAssignment": case "LocalAssignment": case "FunctionCall": throw new NotImplementedException(); case "Label": return(CurrentScope.RegisterJumpTarget(parseNode.ChildNodes[0].Token.Text)); case "Goto": return(Expr.Goto(CurrentScope.GetJumpLabel(parseNode.ChildNodes[0].Token.Text))); case "Break": return(Expr.Break(CurrentScope.GetExitLabel())); case "Scope": return(CompileBlock(parseNode)); case "ForLoop_Enumerable": case "ForLoop_Iterable": case "WhileLoop": case "RepeatLoop": case "IfStatement": case "GlobalFunction": throw new NotImplementedException(); case "LocalFunction": return(CompileLocalFunction(parseNode)); case "ReturnStatement": return(Expr.Return(CurrentScope.GetReturnLabel(), GetMultiExpressionValue(parseNode.ChildNodes[0]))); default: throw new LithiumCompilerException("Unrecognized statement terminal '{0}' at {1}", parseNode.Term.Name, parseNode.Span.Location.ToUiString()); } }
/// <summary>for(loopVar = min; i < max; i++) { loopContent }</summary> public static E For(this ParameterExpression loopVar, int min, int max, E loopContent) { var initAssign = E.Assign(loopVar, E.Constant(min, typeof(int))); var breakLabel = E.Label("LoopBreak"); var loop = E.Block(new[] { loopVar }, initAssign, E.Loop( E.IfThenElse( E.LessThan(loopVar, E.Constant(max, typeof(int))), E.Block( loopContent, E.Assign(loopVar, E.Increment(loopVar)) ), E.Break(breakLabel) ), breakLabel) ); return(loop); }
public void Factorial() { var value = LinqExpression.Parameter(typeof(int)); var result = LinqExpression.Parameter(typeof(int)); var label = LinqExpression.Label(typeof(int)); var expected = LinqExpression.Block( new[] { result }, LinqExpression.Assign( result, LinqExpression.Constant(1)), LinqExpression.Loop( LinqExpression.Condition( LinqExpression.GreaterThan( value, LinqExpression.Constant(1)), LinqExpression.MultiplyAssign( result, LinqExpression.PostDecrementAssign( value)), LinqExpression.Break( label, result), typeof(void)), label)); using var g = new GraphEngine.Graph(); g.LoadFromString(@" @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <http://example.com/> . @prefix xt: <http://example.com/ExpressionTypes/> . _:constantValue :parameterType [ :typeName ""System.Int32"" ; ] ; . _:result :parameterType [ :typeName ""System.Int32"" ; ] ; . _:label :targetType [ :typeName ""System.Int32"" ; ] ; . _:one :constantValue ""1""^^xsd:int ; . :s :blockVariables ( _:result ) ; :blockExpressions ( [ :binaryExpressionType xt:Assign ; :binaryLeft _:result ; :binaryRight _:one ; ] [ :loopBody [ :conditionTest [ :binaryExpressionType xt:GreaterThan ; :binaryLeft _:constantValue ; :binaryRight _:one ; ] ; :conditionIfTrue [ :binaryExpressionType xt:MultiplyAssign ; :binaryLeft _:result ; :binaryRight [ :unaryExpressionType xt:PostDecrementAssign ; :unaryOperand _:constantValue ; ] ; ] ; :conditionIfFalse [ a :Break ; :gotoTarget _:label ; :gotoValue _:result ; ] ; :conditionType [ :typeName ""System.Void"" ; ] ; ] ; :loopBreak _:label ; ] ) ; . "); var s = g.GetUriNode(":s"); var actual = Expression.Parse(s).LinqExpression; Console.WriteLine(actual.GetDebugView()); actual.Should().Be(expected); }
private static LinqExpression[] CompileGroup(ExpressionGroup group, CompilationData data) { switch (group) { case Persist p: foreach (var s in p.State) { if (!(s.InitialValue is Constant)) { throw new Exception("Persist initial value is not constant"); } data.StateValues.Add(((Constant)s.InitialValue).Value); } var assigns = new List <LinqExpression>(); // TODO: Resolve parent scopes data.ResolveState = s => LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(s.Id)); for (var i = 0; i < p.NewValue.Count; i++) { var newValueExpr = Compile(p.NewValue[i], data); assigns.Add(LinqExpression.Assign( LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(i)), newValueExpr)); } data.Statements.AddRange(assigns); return(Enumerable.Range(0, p.Size) .Select(i => LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(i))) .ToArray()); case Loop l: var stateList = new List <ParameterExpression>(); foreach (var s in l.State) { var initial = Compile(s.InitialValue, data); var variable = LinqExpression.Variable(initial.Type); data.Statements.Add(LinqExpression.Assign(variable, initial)); data.Variables.Add(variable); stateList.Add(variable); } // TODO: Resolve parent scopes data.ResolveState = s => stateList[s.Id]; var parentStatements = data.Statements; // Make a new cache that copies in the old one, but won't leak State expressions data.Cache = new Dictionary <CachedExpression, ParameterExpression>(data.Cache); // Create a new statements list to put in the loop body var s1 = data.Statements = new List <LinqExpression>(); var condition = Compile(l.Condition, data); var s2 = data.Statements = new List <LinqExpression>(); var newState = l.Body.Select(e => Compile(e, data)).ToArray(); // Ensure that the entire state is only set at the end of the loop for (var i = 0; i < newState.Length; i++) { var s = newState[i]; if (!(s is ParameterExpression)) { var tmpVar = LinqExpression.Variable(s.Type); data.Variables.Add(tmpVar); s2.Add(LinqExpression.Assign(tmpVar, s)); newState[i] = tmpVar; } } var breakLabel = LinqExpression.Label(); var body = LinqExpression.Block(s1 .Concat(new[] { LinqExpression.IfThen( LinqExpression.LessThan(condition, LinqExpression.Constant(1f)), LinqExpression.Break(breakLabel)) }) .Concat(s2) .Concat(newState.Select((e, i) => LinqExpression.Assign(stateList[i], e)))); parentStatements.Add(LinqExpression.Loop(body, breakLabel)); return(stateList.ToArray()); default: throw new NotSupportedException(); } }
/// <summary> /// Transform as expression (value is read). /// </summary> protected internal override MAst Transform() { return MAst.Break(_target); }
public Expr ToExpression() { var exitLabel = Expr.Label(); var loopBody = Expr.Block(Body.ToExpression(), Expr.PreIncrementAssign(Iterator.ToExpression())); return(Expr.Loop(Expr.IfThenElse(Expr.LessThan(Iterator.ToExpression(), UpperBound.ToExpression()), loopBody, Expr.Break(exitLabel)), exitLabel)); }
public override Expr ToExpression() { var loopBody = new List <Expr>(); if (ReadExpression.Count > 0) { loopBody.Add(Expr.Assign(AccessExpression, ReadExpression[0])); } foreach (var child in Children) { loopBody.Add(child.ToExpression()); } loopBody.Add(Expr.PreIncrementAssign(Iterator)); var loopExitLabel = Expr.Label(); var loopCode = Expr.Loop(Expr.IfThenElse(LoopCondition, Expr.Block(loopBody), Expr.Break(loopExitLabel)), loopExitLabel); return(Expr.Block(new[] { Iterator }, IteratorInitializer, loopCode)); }