示例#1
0
        public static LinkedListNode <Lexeme> TryParseStruct(LinkedListNode <Lexeme> lexemes, out Stmt resultNode)
        {
            resultNode = null;
            var nextLexeme = DeclStmt.TryParse(lexemes, out var decl);

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

            return(lexemes);
        }
示例#2
0
文件: DeclStmt.cs 项目: paulens12/C--
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out DeclStmt resultNode)
        {
            resultNode = null;
            var nextLexeme = Type.TryParse(lexemes, out Type type);

            if (type == null)
            {
                return(lexemes);
            }
            if (nextLexeme.Value.Type != LT.IDENT)
            {
                return(lexemes);
            }
            var nameLexeme = nextLexeme.Value;

            nextLexeme = nextLexeme.Next;

            if (nextLexeme.Value.Type == LT.OP_ASSIGN)
            {
                nextLexeme = nextLexeme.Next;
                nextLexeme = Expr.TryParse(nextLexeme, out Expr expr);
                if (expr == null)
                {
                    throw new Exception($"Missing expression after assignment operator at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                }
                resultNode = new DeclAssignStmt {
                    Type = type, Value = expr, Token = nameLexeme
                };
            }
            else
            {
                resultNode = new DeclStmt {
                    Type = type, Token = nameLexeme
                };
            }

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

            return(nextLexeme.Next);
        }
示例#3
0
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out ForLoop resultNode)
        {
            resultNode = null;
            if (lexemes.Value.Type != LT.KW_FOR)
            {
                return(lexemes);
            }
            var nextLexeme = lexemes.Next;

            if (nextLexeme.Value.Type != LT.OP_PAREN_O)
            {
                throw new Exception($"Missing loop header at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            nextLexeme = nextLexeme.Next;
            nextLexeme = DeclStmt.TryParse(nextLexeme, out DeclStmt ds);
            if (ds == null || !(ds is DeclAssignStmt init))
            {
                throw new Exception($"Cannot parse loop initializer at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            nextLexeme = BinaryExpr.TryParseExpr(nextLexeme, out Expr cond);
            if (cond == null)
            {
                throw new Exception($"Cannot parse loop condition at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            if (nextLexeme.Value.Type != LT.OP_SC)
            {
                throw new Exception($"Missing semicolon after loop condition at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            nextLexeme = nextLexeme.Next;

            nextLexeme = AssignStmt.TryParse(nextLexeme, out AssignStmt upd, false);
            if (upd == null)
            {
                throw new Exception($"Cannot parse loop afterthought at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            if (nextLexeme.Value.Type != LT.OP_PAREN_C)
            {
                throw new Exception($"Missing closing parenthesis for loop header at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            nextLexeme = nextLexeme.Next;

            Stmt        stmt;
            List <Stmt> stmts = new List <Stmt>();

            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 loop 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 loop body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
                }
                stmts.Add(stmt);
            }

            resultNode = new ForLoop
            {
                Init      = init,
                Condition = cond,
                Update    = upd,
                Body      = new BlockBody(stmts)
            };
            return(nextLexeme);
        }