Exemple #1
0
        private object Visit(BlockNode node)
        {
            object result = null;

            foreach (var child in node.Block)
            {
                if (child is ReturnNode)
                {
                    result = Visit(child);
                }
                else
                {
                    Visit(child);
                }
            }
            return(result);
        }
Exemple #2
0
 private SyntaxNode Statement(Parser.ParserInstance parser)
 {
     if (parser.Current.TypeId == Program.LBlock)
     {
         return(Block(parser));
     }
     else if (parser.Current.TypeId == Program.Variable)
     {
         parser.ConsumeToken();
         return(VariableDeclaration(parser, TypeNode.Inferred));
     }
     else if (parser.Current.TypeId == Program.Identifier)
     {
         var ident = (string)parser.Current.Value;
         parser.ConsumeToken(Program.Identifier);
         if (parser.Current.TypeId == Program.LParen) // FUNC-INVOKE
         {
             return(FunctionInvoke(parser, ident));
         }
         else if (parser.Current.TypeId == Program.Assign) // ASSIGN
         {
             return(Assignment(parser, new VariableNode(ident)));
         }
         else if (parser.Current.TypeId == Program.Identifier)
         {
             return(VariableDeclaration(parser, new TypeNode(ident)));
         }
         // increment-by, decrement-by, multiply-by, divide-by
     }
     else if (parser.Current.TypeId == Program.Func) // FUNC-DEFINE
     {
         parser.ConsumeToken();                      // consume the FUNC
         var returnType = new TypeNode((string)parser.Current.Value);
         parser.ConsumeToken();                      // Consume the Return Type
         var ident = (string)parser.Current.Value;
         parser.ConsumeToken(Program.Identifier);
         parser.ConsumeToken(Program.LParen);
         var parameters = new List <FunctionParameterDefinitionNode>();
         while (parser.Current.TypeId != Program.RParen)
         {
             if (parser.Current.TypeId == Program.Comma)
             {
                 parser.ConsumeToken();
             }
             var paramType = new TypeNode((string)parser.Current.Value);
             parser.ConsumeToken();
             var paramName = (string)parser.Current.Value;
             parser.ConsumeToken();
             parameters.Add(new FunctionParameterDefinitionNode(paramName, paramType));
         }
         parser.ConsumeToken(Program.RParen);
         BlockNode body = null;
         if (parser.Current.TypeId == Program.LBlock)
         {
             body = Block(parser);
         }
         else if (returnType.Name != "void" && parser.Current.TypeId != Program.Return)
         {
             body = new BlockNode(new ReturnNode(Expression(parser)));
         }
         else
         {
             body = new BlockNode(Statement(parser));
         }
         return(new FunctionDefinitionNode(ident, returnType, parameters.ToArray(), body));
     }
     else if (parser.Current.TypeId == Program.TypeOf)
     {
         parser.ConsumeToken();
         parser.ConsumeToken(Program.LParen);
         var expression = Expression(parser);
         parser.ConsumeToken(Program.RParen);
         return(new TypeOfNode(expression));
     }
     else if (parser.Current.TypeId == Program.Return)
     {
         parser.ConsumeToken();
         return(new ReturnNode(Expression(parser)));
     }
     else if (parser.Current.TypeId == Program.Branch)
     {
         parser.ConsumeToken();
         return(Branch(parser));
     }
     else if (parser.Current.TypeId == Program.Loop)
     {
         parser.ConsumeToken();
         var numberOfLoops = Expression(parser);
         if (parser.Current.TypeId == Program.LBlock)
         {
             return(new LoopNode(numberOfLoops, Block(parser)));
         }
         else
         {
             return(new LoopNode(numberOfLoops, Statement(parser)));
         }
     }
     else if (parser.Current.TypeId == Program.Print)
     {
         parser.ConsumeToken();
         return(new PrintNode(Expression(parser)));
     }
     return(new NoOpNode());
 }
Exemple #3
0
 public FunctionDefinitionNode(string name, TypeNode returnType, FunctionParameterDefinitionNode[] parameters, BlockNode body)
 {
     FunctionName = name;
     ReturnType   = returnType;
     Parameters   = parameters;
     Body         = body;
 }