aboutsummaryrefslogtreecommitdiff
path: root/lib/Parser
diff options
context:
space:
mode:
authorov-4 <hausemasterstream@gmail.com>2026-01-29 10:20:58 +0000
committerov-4 <hausemasterstream@gmail.com>2026-01-29 10:20:58 +0000
commitc2a3cecc1ab2d3f27dcea40a6f0f353b0306d66a (patch)
tree3adfa6ed51620adcf6b129eade1622b01527c8a9 /lib/Parser
parent3f48a1d3af588242cd745e344b6b0b02cfb26131 (diff)
Added INPUT, OUTPUT and DECLARE
Diffstat (limited to 'lib/Parser')
-rw-r--r--lib/Parser/Parser.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc
new file mode 100644
index 0000000..90dc3a0
--- /dev/null
+++ b/lib/Parser/Parser.cc
@@ -0,0 +1,77 @@
+#include "cps/Parser.h"
+
+using namespace cps;
+
+int Parser::getNextToken() {
+ return CurTok = Lex.gettok();
+}
+
+std::unique_ptr<ExprAST> Parser::ParseNumberExpr() {
+ auto Result = std::make_unique<NumberExprAST>(Lex.NumVal);
+ getNextToken();
+ return Result;
+}
+
+std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() {
+ std::string IdName = Lex.IdentifierStr;
+ getNextToken();
+ return std::make_unique<VariableExprAST>(IdName);
+}
+
+std::unique_ptr<ExprAST> Parser::ParseExpression() {
+ if (CurTok == tok_number)
+ return ParseNumberExpr();
+ if (CurTok == tok_identifier)
+ return ParseIdentifierExpr();
+ return nullptr;
+}
+
+std::vector<std::unique_ptr<StmtAST>> Parser::Parse() {
+ std::vector<std::unique_ptr<StmtAST>> Statements;
+
+ while (CurTok != tok_eof) {
+ if (CurTok == tok_declare) {
+ // DECLARE <id> : INTEGER
+ getNextToken(); // eat DECLARE
+ if (CurTok != tok_identifier) break; // Error
+ std::string Name = Lex.IdentifierStr;
+ getNextToken(); // eat Name
+
+ if (CurTok != tok_colon) break; // Error: expected ':'
+ getNextToken(); // eat ':'
+
+ if (CurTok != tok_integer_kw) break; // Error: expected INTEGER
+ getNextToken(); // eat INTEGER
+
+ Statements.push_back(std::make_unique<DeclareStmtAST>(Name));
+ }
+ else if (CurTok == tok_identifier) {
+ // <id> <- <expr>
+ std::string Name = Lex.IdentifierStr;
+ getNextToken(); // eat Name
+
+ if (CurTok != tok_assign) break; // Error: expected '<-'
+ getNextToken(); // eat '<-'
+
+ auto Expr = ParseExpression();
+ Statements.push_back(std::make_unique<AssignStmtAST>(Name, std::move(Expr)));
+ }
+ else if (CurTok == tok_input) {
+ // INPUT <id>
+ getNextToken(); // eat INPUT
+ 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
+ auto Expr = ParseExpression();
+ Statements.push_back(std::make_unique<OutputStmtAST>(std::move(Expr)));
+ }
+ else {
+ getNextToken(); // Skip unknown
+ }
+ }
+ return Statements;
+}