diff options
Diffstat (limited to 'lib/Parser')
| -rw-r--r-- | lib/Parser/Parser.cc | 31 | ||||
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 16 |
2 files changed, 34 insertions, 13 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 322cdb9..049f99d 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -271,12 +271,18 @@ std::vector<std::unique_ptr<StmtAST>> Parser::Parse() { } std::unique_ptr<StmtAST> Parser::ParseDeclare() { - getNextToken(); - if (CurTok != tok_identifier) return nullptr; + getNextToken(); + if (CurTok != tok_identifier) { + fprintf(stderr, "Error: Expected identifier after DECLARE at line %d\n", Lex.getLine()); + return nullptr; + } std::string Name = Lex.IdentifierStr; getNextToken(); - - if (CurTok != tok_colon) return nullptr; + + if (CurTok != tok_colon) { + fprintf(stderr, "Error: Expected ':' after variable name '%s' at line %d\n", Name.c_str(), Lex.getLine()); + return nullptr; + } getNextToken(); if (CurTok == tok_array) { @@ -485,6 +491,12 @@ std::unique_ptr<StmtAST> Parser::ParseForStmt() { } std::unique_ptr<StmtAST> Parser::ParseStatement() { + auto Stmt = ParseStatementImpl(); + if (!Stmt) ++NumErrors; + return Stmt; +} + +std::unique_ptr<StmtAST> Parser::ParseStatementImpl() { if (CurTok == tok_declare) { return ParseDeclare(); } @@ -515,7 +527,10 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { return std::make_unique<ArrayAssignStmtAST>(Name, std::move(Indices), std::move(Expr), Line); } - if (CurTok != tok_assign) return nullptr; + if (CurTok != tok_assign) { + fprintf(stderr, "Error: Expected '<-' after identifier '%s' at line %d\n", Name.c_str(), Line); + return nullptr; + } getNextToken(); auto Expr = ParseExpression(); if (!Expr) return nullptr; @@ -523,7 +538,10 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { } else if (CurTok == tok_input) { getNextToken(); - if (CurTok != tok_identifier) return nullptr; + if (CurTok != tok_identifier) { + fprintf(stderr, "Error: Expected variable name after INPUT at line %d\n", Lex.getLine()); + return nullptr; + } std::string Name = Lex.IdentifierStr; getNextToken(); return std::make_unique<InputStmtAST>(Name); @@ -559,5 +577,6 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { return ParseReturnStmt(); } + fprintf(stderr, "Error: Unexpected token at line %d when expecting a statement\n", Lex.getLine()); return nullptr; } diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index 05b1e12..06348c4 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -5,7 +5,7 @@ using namespace cps; -std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeArgs() { +std::optional<std::vector<std::tuple<std::string, std::string, bool>>> Parser::ParsePrototypeArgs() { std::vector<std::tuple<std::string, std::string, bool>> Args; if (CurTok != '(') return Args; getNextToken(); @@ -24,21 +24,21 @@ std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeAr if (CurTok != tok_identifier) { fprintf(stderr, "Error: Expected argument name\n"); - return Args; + return std::nullopt; } std::string Name = Lex.IdentifierStr; getNextToken(); if (CurTok != tok_colon) { fprintf(stderr, "Error: Expected ':' after argument name\n"); - return Args; + return std::nullopt; } getNextToken(); std::string Type = ParseTypeName(false); if (Type.empty()) { fprintf(stderr, "Error: Expected argument type for '%s'\n", Name.c_str()); - return Args; + return std::nullopt; } Args.emplace_back(Name, Type, IsRef); @@ -46,7 +46,7 @@ std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeAr if (CurTok == ')') break; if (CurTok != ',') { fprintf(stderr, "Error: Expected ',' or ')'\n"); - return Args; + return std::nullopt; } getNextToken(); } @@ -65,6 +65,7 @@ std::unique_ptr<StmtAST> Parser::ParseFunction() { getNextToken(); auto Args = ParsePrototypeArgs(); + if (!Args) return nullptr; std::string RetType = "INTEGER"; if (CurTok == tok_returns) { @@ -76,7 +77,7 @@ std::unique_ptr<StmtAST> Parser::ParseFunction() { } } - auto Proto = std::make_unique<PrototypeAST>(Name, std::move(Args), RetType); + auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), RetType); std::vector<std::unique_ptr<StmtAST>> Body; while (CurTok != tok_endfunction && CurTok != tok_eof) { @@ -105,7 +106,8 @@ std::unique_ptr<StmtAST> Parser::ParseProcedure() { getNextToken(); auto Args = ParsePrototypeArgs(); - auto Proto = std::make_unique<PrototypeAST>(Name, std::move(Args), "VOID"); + if (!Args) return nullptr; + auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID"); std::vector<std::unique_ptr<StmtAST>> Body; while (CurTok != tok_endprocedure && CurTok != tok_eof) { |
