예제 #1
0
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out IfStmt resultNode)
        {
            resultNode = null;
            if (lexemes.Value.Type != LT.KW_IF)
            {
                return(lexemes);
            }

            var nextLexeme = lexemes.Next;

            if (nextLexeme.Value.Type != LT.OP_PAREN_O)
            {
                throw new Exception($"Missing if condition at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            nextLexeme = BinaryExpr.TryParseExpr(nextLexeme.Next, out Expr expr);
            if (expr == null)
            {
                throw new Exception($"Cannot parse if condition at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            if (nextLexeme.Value.Type != LT.OP_PAREN_C)
            {
                throw new Exception($"Missing closing parenthesis for if condition at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            nextLexeme = nextLexeme.Next;

            Stmt        stmt;
            List <Stmt> stmts       = new List <Stmt>();
            bool        elseAllowed = true;

            if (nextLexeme.Value.Type == LT.OP_BRACE_O)
            {
                // block body
                nextLexeme = nextLexeme.Next;
                nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                while (stmt != null)
                {
                    stmts.Add(stmt);
                    nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                }
                if (nextLexeme.Value.Type != LT.OP_BRACE_C)
                {
                    throw new Exception($"Missing closing bracket for if body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                }
                nextLexeme = nextLexeme.Next;
            }
            else
            {
                // single statement body
                nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                if (stmt == null)
                {
                    throw new Exception($"Missing if body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                }
                stmts.Add(stmt);
                if (stmt is IfStmt)
                {
                    elseAllowed = false;
                }
            }

            if (elseAllowed)
            {
                if (nextLexeme.Value.Type == LT.KW_ELSE)
                {
                    List <Stmt> elseStmts = new List <Stmt>();
                    nextLexeme = nextLexeme.Next;
                    if (nextLexeme.Value.Type == LT.OP_BRACE_O)
                    {
                        // block body
                        nextLexeme = nextLexeme.Next;
                        nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                        while (stmt != null)
                        {
                            elseStmts.Add(stmt);
                            nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                        }
                        if (nextLexeme.Value.Type != LT.OP_BRACE_C)
                        {
                            throw new Exception($"Missing closing bracket for else body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                        }
                        nextLexeme = nextLexeme.Next;
                    }
                    else
                    {
                        // single statement body
                        nextLexeme = Stmt.TryParse(nextLexeme, out stmt);
                        if (stmt == null)
                        {
                            throw new Exception($"Missing else body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                        }
                        elseStmts.Add(stmt);
                    }

                    resultNode = new IfStmt {
                        Condition = expr, Body = new BlockBody(stmts), ElseBody = new BlockBody(elseStmts), Token = lexemes.Value
                    };
                    return(nextLexeme);
                }
            }

            resultNode = new IfStmt {
                Condition = expr, Body = new BlockBody(stmts), ElseBody = null, Token = lexemes.Value
            };
            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);
        }