diff options
| author | _ov4 <hausemasterstream@gmail.com> | 2026-01-30 13:11:20 +0000 |
|---|---|---|
| committer | _ov4 <hausemasterstream@gmail.com> | 2026-01-30 13:11:20 +0000 |
| commit | bd799e42b40613983c6e3cb9aedf58866ac68605 (patch) | |
| tree | 03fdd0dffba45fa2a1d9b8d4b0e78f49140c07e5 /lib/Parser/Parser.cc | |
| parent | 80420b0f92f42edc0a3cac4490a08adb8b2457dc (diff) | |
Added FUNCTION and PROCEDURE
Diffstat (limited to 'lib/Parser/Parser.cc')
| -rw-r--r-- | lib/Parser/Parser.cc | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index b9396ce..563dd9b 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -1,4 +1,5 @@ #include "cps/Parser.h" +#include "cps/FunctionAST.h" using namespace cps; @@ -40,24 +41,40 @@ std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() { std::string IdName = Lex.IdentifierStr; int Line = Lex.getLine(); getNextToken(); + + if (CurTok == '(') { + getNextToken(); + std::vector<std::unique_ptr<ExprAST>> Args; + if (CurTok != ')') { + while (true) { + if (auto Arg = ParseExpression()) + Args.push_back(std::move(Arg)); + else + return nullptr; + + if (CurTok == ')') break; + + if (CurTok != ',') { + fprintf(stderr, "Error: Expected ')' or ',' in function call.\n"); + return nullptr; + } + getNextToken(); + } + } + getNextToken(); + return std::make_unique<CallExprAST>(IdName, std::move(Args)); + } if (CurTok == '[') { getNextToken(); std::vector<std::unique_ptr<ExprAST>> Indices; - while (true) { auto Exp = ParseExpression(); if (!Exp) return nullptr; Indices.push_back(std::move(Exp)); - - if (CurTok == ']') { - break; - } - if (CurTok == ',') { - getNextToken(); - continue; - } - fprintf(stderr, "Error: Expected ',' or ']' in array index\n"); + if (CurTok == ']') break; + if (CurTok == ',') { getNextToken(); continue; } + fprintf(stderr, "Error: Expected ',' or ']'\n"); return nullptr; } getNextToken(); @@ -407,6 +424,18 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { else if (CurTok == tok_for) { return ParseForStmt(); } + else if (CurTok == tok_function) { + return ParseFunction(); + } + else if (CurTok == tok_procedure) { + return ParseProcedure(); + } + else if (CurTok == tok_call) { + return ParseCallStmt(); + } + else if (CurTok == tok_return) { + return ParseReturnStmt(); + } return nullptr; } |
