aboutsummaryrefslogtreecommitdiff
path: root/lib/Parser/Parser.cc
diff options
context:
space:
mode:
authorov-4 <hausemasterstream@gmail.com>2026-01-29 10:41:27 +0000
committerov-4 <hausemasterstream@gmail.com>2026-01-29 10:41:27 +0000
commitf1fedc36ec54578be4983d2fc6abee999dbc51c2 (patch)
tree959fae06a583e96a219527b7032609f435f9108b /lib/Parser/Parser.cc
parentc2a3cecc1ab2d3f27dcea40a6f0f353b0306d66a (diff)
Added binary operations +-*/
Diffstat (limited to 'lib/Parser/Parser.cc')
-rw-r--r--lib/Parser/Parser.cc95
1 files changed, 78 insertions, 17 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc
index 90dc3a0..76b124a 100644
--- a/lib/Parser/Parser.cc
+++ b/lib/Parser/Parser.cc
@@ -2,6 +2,69 @@
using namespace cps;
+Parser::Parser(Lexer &L) : Lex(L) {
+ getNextToken();
+
+ BinopPrecedence['+'] = 20;
+ BinopPrecedence['-'] = 20;
+ BinopPrecedence['*'] = 40;
+ BinopPrecedence['/'] = 40;
+}
+
+int Parser::GetTokPrecedence() {
+ if (!isascii(CurTok)) return -1;
+ int TokPrec = BinopPrecedence[CurTok];
+ if (TokPrec <= 0) return -1;
+ return TokPrec;
+}
+
+std::unique_ptr<ExprAST> Parser::ParseParenExpr() {
+ getNextToken();
+ auto V = ParseExpression();
+ if (!V) return nullptr;
+ if (CurTok != ')') {
+ fprintf(stderr, "Error: expected ')'\n");
+ return nullptr;
+ }
+ getNextToken();
+ return V;
+}
+
+std::unique_ptr<ExprAST> Parser::ParsePrimary() {
+ switch (CurTok) {
+ case tok_identifier: return ParseIdentifierExpr();
+ case tok_number: return ParseNumberExpr();
+ case '(': return ParseParenExpr();
+ default:
+ fprintf(stderr, "Error: unknown token when expecting an expression\n");
+ return nullptr;
+ }
+}
+
+std::unique_ptr<ExprAST> Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) {
+ while (true) {
+ int TokPrec = GetTokPrecedence();
+
+ if (TokPrec < ExprPrec)
+ return LHS;
+
+ int BinOp = CurTok;
+ getNextToken();
+
+ auto RHS = ParsePrimary();
+ if (!RHS) return nullptr;
+
+ int NextPrec = GetTokPrecedence();
+ if (TokPrec < NextPrec) {
+ RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
+ if (!RHS) return nullptr;
+ }
+
+ LHS = std::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
+ }
+}
+
+
int Parser::getNextToken() {
return CurTok = Lex.gettok();
}
@@ -19,11 +82,9 @@ std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() {
}
std::unique_ptr<ExprAST> Parser::ParseExpression() {
- if (CurTok == tok_number)
- return ParseNumberExpr();
- if (CurTok == tok_identifier)
- return ParseIdentifierExpr();
- return nullptr;
+ auto LHS = ParsePrimary();
+ if (!LHS) return nullptr;
+ return ParseBinOpRHS(0, std::move(LHS));
}
std::vector<std::unique_ptr<StmtAST>> Parser::Parse() {
@@ -32,45 +93,45 @@ std::vector<std::unique_ptr<StmtAST>> Parser::Parse() {
while (CurTok != tok_eof) {
if (CurTok == tok_declare) {
// DECLARE <id> : INTEGER
- getNextToken(); // eat DECLARE
+ getNextToken();
if (CurTok != tok_identifier) break; // Error
std::string Name = Lex.IdentifierStr;
- getNextToken(); // eat Name
+ getNextToken();
- if (CurTok != tok_colon) break; // Error: expected ':'
- getNextToken(); // eat ':'
+ if (CurTok != tok_colon) break; // Error
+ getNextToken();
- if (CurTok != tok_integer_kw) break; // Error: expected INTEGER
- getNextToken(); // eat INTEGER
+ if (CurTok != tok_integer_kw) break; // Error
+ getNextToken();
Statements.push_back(std::make_unique<DeclareStmtAST>(Name));
}
else if (CurTok == tok_identifier) {
// <id> <- <expr>
std::string Name = Lex.IdentifierStr;
- getNextToken(); // eat Name
+ getNextToken();
- if (CurTok != tok_assign) break; // Error: expected '<-'
- getNextToken(); // eat '<-'
+ if (CurTok != tok_assign) break; // Error
+ getNextToken();
auto Expr = ParseExpression();
Statements.push_back(std::make_unique<AssignStmtAST>(Name, std::move(Expr)));
}
else if (CurTok == tok_input) {
// INPUT <id>
- getNextToken(); // eat INPUT
+ getNextToken();
if (CurTok != tok_identifier) break;
Statements.push_back(std::make_unique<InputStmtAST>(Lex.IdentifierStr));
getNextToken();
}
else if (CurTok == tok_output) {
// OUTPUT <expr>
- getNextToken(); // eat OUTPUT
+ getNextToken();
auto Expr = ParseExpression();
Statements.push_back(std::make_unique<OutputStmtAST>(std::move(Expr)));
}
else {
- getNextToken(); // Skip unknown
+ getNextToken();
}
}
return Statements;