예제 #1
0
 private static Statement ParseStatement(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("if"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement thenstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new IfStatement(condition,thenstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("while"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new WhileStatement(condition,loopstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("foreach"))
     {
         scanner.GetDelimiter("(");
         string variabletype;
         string variablename;
         scanner.GetIdentifier(out variabletype);
         scanner.GetIdentifier(out variablename);
         scanner.GetIdentifier("in");
         Expression collection = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new ForEachStatement(variabletype,variablename,collection,loopstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("var"))
     {
         string variablename;
         scanner.GetIdentifier(out variablename);
         scanner.GetDelimiter("=");
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return new VarStatement(variablename,expression);
     }
     else if (scanner.TryGetIdentifier("return"))
     {
         if (scanner.TryGetDelimiter(";"))
         {
             return new ReturnStatement(null);
         }
         else
         {
             Expression expression = ParseExpression(scanner);
             scanner.GetDelimiter(";");
             return new ReturnStatement(expression);
         }
     }
     else if (scanner.TryGetDelimiter("{"))
     {
         return CodeParser.ParseBlock(scanner.ScanAcross("{","}"));
     }
     else if (scanner.TryGetDelimiter(";"))
     {
         return new NullStatement();
     }
     else
     {
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return new ExpressionStatement(expression);
     }
 }
예제 #2
0
 private static Statement ParseStatement(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("if"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement thenstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new IfStatement(condition, thenstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("while"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new WhileStatement(condition, loopstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("foreach"))
     {
         scanner.GetDelimiter("(");
         string variabletype;
         string variablename;
         scanner.GetIdentifier(out variabletype);
         scanner.GetIdentifier(out variablename);
         scanner.GetIdentifier("in");
         Expression collection = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new ForEachStatement(variabletype, variablename, collection, loopstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("var"))
     {
         string variablename;
         scanner.GetIdentifier(out variablename);
         scanner.GetDelimiter("=");
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return(new VarStatement(variablename, expression));
     }
     else if (scanner.TryGetIdentifier("return"))
     {
         if (scanner.TryGetDelimiter(";"))
         {
             return(new ReturnStatement(null));
         }
         else
         {
             Expression expression = ParseExpression(scanner);
             scanner.GetDelimiter(";");
             return(new ReturnStatement(expression));
         }
     }
     else if (scanner.TryGetDelimiter("{"))
     {
         return(CodeParser.ParseBlock(scanner.ScanAcross("{", "}")));
     }
     else if (scanner.TryGetDelimiter(";"))
     {
         return(new NullStatement());
     }
     else
     {
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return(new ExpressionStatement(expression));
     }
 }