Exemplo n.º 1
0
        }                                         // Like a linked list, points to the next else (could be an 'else if' if it has a Condition)

        public IfStatement(SourceSpan span, Expressions.Expression condition, InstructionStatement body, IfStatement elseStatement)
        {
            this.Span          = span;
            this.Condition     = condition;
            this.Body          = body;
            this.ElseStatement = elseStatement;
        }
Exemplo n.º 2
0
 public ForEachStatement(SourceSpan span, Token variable, Expressions.Expression collection, InstructionStatement body)
 {
     this.Span       = span;
     this.Variable   = variable;
     this.Collection = collection;
     this.Body       = body;
 }
Exemplo n.º 3
0
        private ForEachStatement ParseForEachStatement()
        {
            Token      forEachKeyword = ConsumeType(TokenType.KeywordForEach, "Expected foreach keyword.", "LSS028");
            SourceSpan span           = forEachKeyword.Span;

            SkipWhitespace();
            ConsumeType(TokenType.OpenParenthesis, "Expected open parenthesis after foreach keyword.", "LSS029");
            SkipWhitespace();
            ConsumeType(TokenType.KeywordVar, "Expected var keyword when declaring iteration variable.", "LSS030");
            SkipWhitespace();
            Token variableName = ConsumeType(TokenType.Symbol, "Expected name of iteration variable.", "LSS031");

            SkipWhitespace();
            ConsumeType(TokenType.Colon, "Expected colon after iteration variable.", "LSS032");
            SkipWhitespace();
            Expression collection = ParseExpression();

            SkipWhitespace();
            ConsumeType(TokenType.CloseParenthesis, "Expected close parenthesis after collection.", "LSS033");
            SkipWhitespace();
            InstructionStatement body = ParseInstructionStatement();

            span += body.Span;
            return(new ForEachStatement(span, variableName, collection, body));
        }
Exemplo n.º 4
0
        private IfStatement ParseIfStatement()
        {
            Token      ifKeyword = ConsumeType(TokenType.KeywordIf, "Expected if keyword.", "LSS025");
            SourceSpan span      = ifKeyword.Span;

            SkipWhitespace();
            ConsumeType(TokenType.OpenParenthesis, "Expected an open parenthesis after if keyword.", "LSS026");
            Expression condition = ParseExpression();

            SkipWhitespace();
            ConsumeType(TokenType.CloseParenthesis, "Expected a close parenthesis after if condition.", "LSS027");
            InstructionStatement body = ParseInstructionStatement();

            span = span + body.Span;
            SkipWhitespace();
            IfStatement elseStatement = null;

            if (ConsumeIfType(out Token elseKeyword, TokenType.KeywordElse))
            {
                SkipWhitespace();
                if (Peek().Type == TokenType.KeywordIf)
                {
                    elseStatement = ParseIfStatement();
                }
                else
                {
                    InstructionStatement elseBody = ParseInstructionStatement();
                    elseStatement = new IfStatement(elseKeyword.Span + elseBody.Span, null, elseBody, null);
                }
                span = span + elseStatement.Span;
                SkipWhitespace();
            }
            return(new IfStatement(span, condition, body, elseStatement));
        }
Exemplo n.º 5
0
        private DoWhileStatement ParseDoWhileStatement()
        {
            Token doKeyword = ConsumeType(TokenType.KeywordDo, "", "");

            SkipWhitespace();
            InstructionStatement body = ParseInstructionStatement();

            SkipWhitespace();
            ConsumeType(TokenType.KeywordWhile, "Expected do statement to have a while keyword after the body.", "LSS034");
            SkipWhitespace();
            ConsumeType(TokenType.OpenParenthesis, "Expected do statement to have a condition after the while", "LSS035");
            Expression condition = ParseExpression();

            SkipWhitespace();
            Token endParenthesis = ConsumeType(TokenType.CloseParenthesis, "Expected do statement condition to have a closing parenthesis after the condition.", "LSS036");

            return(new DoWhileStatement(doKeyword.Span + endParenthesis.Span, body, condition));
        }
Exemplo n.º 6
0
 public DoWhileStatement(SourceSpan span, InstructionStatement body, Expressions.Expression condition)
 {
     this.Span      = span;
     this.Condition = condition;
     this.Body      = body;
 }
Exemplo n.º 7
0
 private InstructionStatement ParseInstructionStatement()
 {
     // { means block
     SkipWhitespace();
     if (Peek().Type == TokenType.OpenBrace)
     {
         return(ParseBlockStatement());
     }
     else if (Peek().Type == TokenType.KeywordIf)
     {
         return(ParseIfStatement());
     }
     else if (Peek().Type == TokenType.KeywordForEach)
     {
         return(ParseForEachStatement());
     }
     else if (Peek().Type == TokenType.KeywordDo)
     {
         return(ParseDoWhileStatement());
     }
     else if (ConsumeIfType(out Token whileKeyword, TokenType.KeywordWhile))
     {
         SkipWhitespace();
         ConsumeType(TokenType.OpenParenthesis, "Expected an open parenthesis after while keyword.", "LSS037");
         Expression condition = ParseExpression();
         SkipWhitespace();
         ConsumeType(TokenType.CloseParenthesis, "Expected a close parenthesis after while condition.", "LSS038");
         InstructionStatement body = ParseInstructionStatement();
         return(new WhileStatement(whileKeyword.Span + body.Span, condition, body));
     }
     else if (ConsumeIfType(out Token returnKeyword, TokenType.KeywordReturn))
     {
         if (ConsumeIfType(out _, TokenType.Semicolon))
         {
             return(new ReturnStatement(returnKeyword, null));
         }
         else
         {
             Expression value = ParseExpression();
             SkipWhitespace();
             ConsumeType(TokenType.Semicolon, "Expected a semicolon after expression to return.", "LSS039");
             return(new ReturnStatement(returnKeyword, value));
         }
     }
     else if (ConsumeIfType(out Token varKeyword, TokenType.KeywordVar))
     {
         SkipWhitespace();
         Token      name        = ConsumeType(TokenType.Symbol, "Expected a name for variable declaration.", "LSS040");
         Expression initializer = null;
         SkipWhitespace();
         if (ConsumeIfType(out _, TokenType.Equals))
         {
             initializer = ParseExpression();
         }
         SkipWhitespace();
         ConsumeType(TokenType.Semicolon, "Expected a semicolon after variable initializer.", "LSS041");
         return(new VariableDeclarationStatement(varKeyword.Span + name.Span, name, initializer));
     }
     else
     {
         Expression value = ParseExpression();
         SkipWhitespace();
         if (ConsumeIfType(out Token equalsOperator, TokenType.Equals))
         {
             Expression rhs = ParseExpression();
             SkipWhitespace();
             ConsumeType(TokenType.Semicolon, "Expected semicolon after assignment statement.", "LSS042");
             return(new AssignmentStatement(value, rhs));
         }
         else
         {
             ConsumeType(TokenType.Semicolon, "Expected semicolon after expression statement.", "LSS043");
             return(new ExpressionStatement(value));
         }
     }
 }