//context should be init block if init exists public static ForLoopNode Create(ContextNode context, CodeBlockNode init, ExpressionNode condition, CodeBlockNode increment, CodeBlockNode body, SequencePoint point) { if (init == null) { init = CodeBlockNode.Create(context, point); } if (condition == null) { condition = LiteralNode.Create(context, true, point); } if (increment == null) { increment = CodeBlockNode.Create(context, point); } if (!(condition.IsGettable && condition.ExpressionReturnType.IsAssignableTo(context.Parser.Bool))) { ErrorCode.InvalidCondition.ReportAndThrow(point, "Condition must be a gettable boolean expression"); } return(new ForLoopNode(init, condition, increment, body, point)); }
public static ParserNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode) { Contract.Requires(context != null); CodeBlockNode init = null; ExpressionNode condition = null; CodeBlockNode increment = null; IAbstractSyntaxTree initNode = lexerNode.Children[2]; IAbstractSyntaxTree conditionNode = lexerNode.Children[4]; IAbstractSyntaxTree incrementNode = lexerNode.Children[6]; IAbstractSyntaxTree bodyNode = lexerNode.Children[8]; if (initNode.Type != TokenType.Empty) { init = CodeBlockNode.Create(context, context.Parser.GetSequencePoint(initNode)); init.AddStatement(initNode); //makes init scope encompass for scope context = init; } if (conditionNode.Type != TokenType.Empty) { condition = ExpressionNode.Parse(context, conditionNode, context.Parser.Bool); } if (incrementNode.Type != TokenType.Empty) { increment = CodeBlockNode.Create(context, context.Parser.GetSequencePoint(incrementNode)); increment.AddStatement(incrementNode); } var body = CodeBlockNode.Parse(context, bodyNode); return(ForLoopNode.Create(context, init, condition, increment, body, context.Parser.GetSequencePoint(lexerNode))); }