コード例 #1
0
ファイル: Parser.cs プロジェクト: Stu042/SimpleC
 public object DoForRetCmd(RetCmdAST rc, object options = null)
 {
     PositionOutput(rc.token.indent);
     ConsoleEx.WriteLine("{0}(Return) {1}{2}", ConsoleColor.DarkBlue, ConsoleColor.Gray, rc.token.value);
     rc.expression.Accept(this, options);
     return(options);
 }
コード例 #2
0
ファイル: Compile.cs プロジェクト: Stu042/SimpleC
            public object DoForRetCmd(RetCmdAST rc, object options = null)
            {
                ObjExpr oe = (ObjExpr)rc.expression.Accept(this, options);

                code.Append($"  ret {curParent.llType.llName} ");                   // we're returning from the curParent (probably)
                // todo set new curParent
                code.Append(oe.val);
                return(options);
            }
コード例 #3
0
ファイル: Parser.cs プロジェクト: Stu042/SimpleC
        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()));
        }