Exemplo n.º 1
0
 public object DoForObjInst(ObjInstAST oi, object options = null)
 {
     PositionOutput(oi.instToken.indent);
     oi.typeAST.Accept(this, null);
     ConsoleEx.WriteLine("{0}(Inst) {1}{2}", ConsoleColor.DarkBlue, ConsoleColor.Gray, oi.name);
     if (oi.value != null)
     {
         oi.value.Accept(this, null);
     }
     return(null);
 }
Exemplo n.º 2
0
        BlockAST Statments(int startIndent = 0)
        {
            List <AST> stmts    = new List <AST>();
            Token      firstTok = tokens.current;

            do
            {
                string found = GetStmntMatch(tokens);
                switch (found)
                {
                case "Command": {
                    AST stmtAST = null;
                    switch (tokens.curTokenType)
                    {
                    case TokenType.Return:
                        Token   retTok = tokens.current;
                        ExprAST expAST = ParseExpr();
                        stmtAST = new RetCmdAST(retTok, expAST);
                        break;

                    default:
                        ShowError("Unknown command", tokens.current);
                        break;
                    }
                    if (stmtAST != null)
                    {
                        stmts.Add(stmtAST);
                    }
                    break;
                }

                case "Main": {
                    TypeAST  typeAST = new TypeAST(tokens.current);
                    Token    mainTok = tokens.Next();
                    ArgAST[] argsAST = ParseArgs();
                    symbols.AddParent(typeAST, mainTok.value, argsAST);
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    AST functAST = new FuncDefAST(mainTok, typeAST, argsAST, stmtAST);
                    stmts.Add(functAST);
                    break;
                }

                case "Function": {
                    TypeAST  typeAST = new TypeAST(tokens.current);
                    Token    instTok = tokens.Next();
                    ArgAST[] argsAST = ParseArgs();
                    symbols.AddParent(typeAST, instTok.value, argsAST);
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    AST functAST = new FuncDefAST(instTok, typeAST, argsAST, stmtAST);
                    stmts.Add(functAST);
                    break;
                }

                case "ObjectDef": {                             // TokenType.Identifier, TokenType.Colon
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.AddParent(typeAST, "", null);
                    tokens.Next();
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    ObjDefAST odAST = new ObjDefAST(tokens.current, stmtAST);
                    stmts.Add(odAST);
                    break;
                }

                case "SimpleObjectInst": {                          // TokenType.AllTypes, TokenType.Identifier
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.Add(typeAST, tokens.current.value, null);
                    ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null);
                    stmts.Add(odAST);
                    break;
                }

                case "ObjectInst": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.Add(typeAST, tokens.current.value, null);
                    ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null);
                    stmts.Add(odAST);
                    break;
                }

                case "SimpleObjectInstAssigned": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    Token   instTok = tokens.Next();
                    symbols.Add(typeAST, instTok.value, null);
                    tokens.Next();
                    NumAST     numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType);
                    ObjInstAST odAST  = new ObjInstAST(instTok, typeAST, numAST);
                    stmts.Add(odAST);
                    break;
                }

                case "ObjectInstAssigned": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    Token   instTok = tokens.Next();
                    symbols.Add(typeAST, instTok.value, null);
                    tokens.Next();
                    NumAST     numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType);
                    ObjInstAST odAST  = new ObjInstAST(instTok, typeAST, numAST);
                    stmts.Add(odAST);
                    break;
                }

                default:
                    ShowError("Unable to parse statement", tokens.current);
                    break;
                }
            } while (tokens.Remaining() > 0);
            return(new BlockAST(firstTok, stmts.ToArray()));
        }