From 766a4b8e5236bffb40984439a79186c5ee823ff8 Mon Sep 17 00:00:00 2001 From: ov-4 Date: Fri, 30 Jan 2026 02:06:37 +0000 Subject: Added runtime check for array index and division --- lib/Parser/Parser.cc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'lib/Parser/Parser.cc') diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index a85b151..b9396ce 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -38,6 +38,7 @@ std::unique_ptr Parser::ParseNumberExpr() { std::unique_ptr Parser::ParseIdentifierExpr() { std::string IdName = Lex.IdentifierStr; + int Line = Lex.getLine(); getNextToken(); if (CurTok == '[') { @@ -60,7 +61,7 @@ std::unique_ptr Parser::ParseIdentifierExpr() { return nullptr; } getNextToken(); - return std::make_unique(IdName, std::move(Indices)); + return std::make_unique(IdName, std::move(Indices), Line); } return std::make_unique(IdName); @@ -84,7 +85,8 @@ std::unique_ptr Parser::ParsePrimary() { case tok_number: return ParseNumberExpr(); case '(': return ParseParenExpr(); default: - fprintf(stderr, "Error: unknown token when expecting an expression\n"); + fprintf(stderr, "Error: unknown token '%c' (%d) at line %d when expecting an expression\n", + (char)CurTok, CurTok, Lex.getLine()); return nullptr; } } @@ -95,6 +97,7 @@ std::unique_ptr Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr(BinOp, std::move(LHS), std::move(RHS)); + LHS = std::make_unique(BinOp, std::move(LHS), std::move(RHS), Line); } } @@ -209,6 +212,7 @@ std::unique_ptr Parser::ParseIfStmt() { while (CurTok != tok_else && CurTok != tok_endif && CurTok != tok_eof) { auto Stmt = ParseStatement(); if (Stmt) ThenStmts.push_back(std::move(Stmt)); + else if (CurTok != tok_eof && CurTok != tok_else && CurTok != tok_endif) getNextToken(); } std::vector> ElseStmts; @@ -217,6 +221,7 @@ std::unique_ptr Parser::ParseIfStmt() { while (CurTok != tok_endif && CurTok != tok_eof) { auto Stmt = ParseStatement(); if (Stmt) ElseStmts.push_back(std::move(Stmt)); + else if (CurTok != tok_eof && CurTok != tok_endif) getNextToken(); } } @@ -244,6 +249,7 @@ std::unique_ptr Parser::ParseWhileStmt() { while (CurTok != tok_endwhile && CurTok != tok_eof) { auto Stmt = ParseStatement(); if (Stmt) Body.push_back(std::move(Stmt)); + else if (CurTok != tok_eof && CurTok != tok_endwhile) getNextToken(); } if (CurTok != tok_endwhile) { @@ -262,6 +268,7 @@ std::unique_ptr Parser::ParseRepeatStmt() { while (CurTok != tok_until && CurTok != tok_eof) { auto Stmt = ParseStatement(); if (Stmt) Body.push_back(std::move(Stmt)); + else if (CurTok != tok_eof && CurTok != tok_until) getNextToken(); } if (CurTok != tok_until) { @@ -307,12 +314,14 @@ std::unique_ptr Parser::ParseForStmt() { if (CurTok == tok_step) { getNextToken(); Step = ParseExpression(); + if (!Step) return nullptr; } std::vector> Body; while (CurTok != tok_next && CurTok != tok_eof) { auto Stmt = ParseStatement(); if (Stmt) Body.push_back(std::move(Stmt)); + else if (CurTok != tok_eof && CurTok != tok_next) getNextToken(); } if (CurTok != tok_next) { @@ -342,6 +351,7 @@ std::unique_ptr Parser::ParseStatement() { } else if (CurTok == tok_identifier) { std::string Name = Lex.IdentifierStr; + int Line = Lex.getLine(); getNextToken(); if (CurTok == '[') { @@ -349,6 +359,7 @@ std::unique_ptr Parser::ParseStatement() { std::vector> Indices; while (true) { auto Exp = ParseExpression(); + if (!Exp) return nullptr; Indices.push_back(std::move(Exp)); if (CurTok == ']') break; if (CurTok == ',') getNextToken(); @@ -361,12 +372,14 @@ std::unique_ptr Parser::ParseStatement() { } getNextToken(); auto Expr = ParseExpression(); - return std::make_unique(Name, std::move(Indices), std::move(Expr)); + if (!Expr) return nullptr; + return std::make_unique(Name, std::move(Indices), std::move(Expr), Line); } if (CurTok != tok_assign) return nullptr; getNextToken(); auto Expr = ParseExpression(); + if (!Expr) return nullptr; return std::make_unique(Name, std::move(Expr)); } else if (CurTok == tok_input) { @@ -379,6 +392,7 @@ std::unique_ptr Parser::ParseStatement() { else if (CurTok == tok_output) { getNextToken(); auto Expr = ParseExpression(); + if (!Expr) return nullptr; return std::make_unique(std::move(Expr)); } else if (CurTok == tok_if) { -- cgit v1.3.1-4-g156e