コード例 #1
0
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out ReturnStmt resultNode)
        {
            resultNode = null;
            if (lexemes.Value.Type != LT.KW_RETURN)
            {
                return(lexemes);
            }

            var nextLexeme = lexemes.Next;

            nextLexeme = BinaryExpr.TryParseExpr(nextLexeme, out Expr expr);

            if (nextLexeme.Value.Type != LT.OP_SC)
            {
                throw new Exception($"Missing semicolon after return statement at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            resultNode = new ReturnStmt
            {
                Value = expr,
                Token = nextLexeme.Value
            };
            nextLexeme = nextLexeme.Next;

            return(nextLexeme);
        }
コード例 #2
0
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out Stmt resultNode)
        {
            resultNode = null;
            if (lexemes.Value.Type == LT.OP_SC)
            {
                resultNode = new BlankStmt();
                return(lexemes.Next);
            }

            var nextLexeme = IfStmt.TryParse(lexemes, out IfStmt sif);

            if (sif != null)
            {
                resultNode = sif;
                return(nextLexeme);
            }

            nextLexeme = TryParseStruct(lexemes, out Stmt stmt);
            if (stmt != null)
            {
                resultNode = stmt;
                return(nextLexeme);
            }

            nextLexeme = FnCall.TryParse(lexemes, out FnCall fnc);
            if (fnc != null)
            {
                if (nextLexeme.Value.Type == LT.OP_SC)
                {
                    resultNode = new ExprStmt <FnCall>(fnc);
                    return(nextLexeme.Next);
                }
            }

            nextLexeme = ForLoop.TryParse(lexemes, out ForLoop fl);
            if (fl != null)
            {
                resultNode = fl;
                return(nextLexeme);
            }

            nextLexeme = WhileLoop.TryParse(lexemes, out WhileLoop wl);
            if (wl != null)
            {
                resultNode = wl;
                return(nextLexeme);
            }

            nextLexeme = AssignStmt.TryParse(lexemes, out AssignStmt sa);
            if (sa != null)
            {
                resultNode = sa;
                return(nextLexeme);
            }

            nextLexeme = ReturnStmt.TryParse(lexemes, out ReturnStmt sr);
            if (sr != null)
            {
                resultNode = sr;
                return(nextLexeme);
            }

            if (lexemes.Value.Type == LT.KW_BREAK)
            {
                if (lexemes.Next.Value.Type != LT.OP_SC)
                {
                    throw new Exception($"Missing semicolon after break keyword at {lexemes.Value.File}:{lexemes.Value.Line}");
                }
                resultNode = new BreakStmt {
                    Token = lexemes.Value
                };
                return(lexemes.Next.Next);
            }

            if (lexemes.Value.Type == LT.KW_CONT)
            {
                if (lexemes.Next.Value.Type != LT.OP_SC)
                {
                    throw new Exception($"Missing semicolon after continue keyword at {lexemes.Value.File}:{lexemes.Value.Line}");
                }
                resultNode = new ContinueStmt {
                    Token = lexemes.Value
                };
                return(lexemes.Next.Next);
            }

            return(lexemes);
        }