diff options
| author | ov-4 <hausemasterstream@gmail.com> | 2026-01-29 14:24:05 +0000 |
|---|---|---|
| committer | ov-4 <hausemasterstream@gmail.com> | 2026-01-29 14:24:05 +0000 |
| commit | 943ed04b775949e8bc4948bcb8d762a32ab14f76 (patch) | |
| tree | 5c8d09bcd702558e1fba25e176712a1697a6c7e7 /lib/Parser | |
| parent | 4573880fcc32bd76a72a4ae713190cd997194583 (diff) | |
Added array
Diffstat (limited to 'lib/Parser')
| -rw-r--r-- | lib/Parser/Parser.cc | 121 |
1 files changed, 112 insertions, 9 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 7a1af89..a85b151 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -39,6 +39,30 @@ std::unique_ptr<ExprAST> Parser::ParseNumberExpr() { std::unique_ptr<ExprAST> Parser::ParseIdentifierExpr() { std::string IdName = Lex.IdentifierStr; getNextToken(); + + 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"); + return nullptr; + } + getNextToken(); + return std::make_unique<ArrayAccessExprAST>(IdName, std::move(Indices)); + } + return std::make_unique<VariableExprAST>(IdName); } @@ -104,6 +128,72 @@ std::vector<std::unique_ptr<StmtAST>> Parser::Parse() { return Statements; } +std::unique_ptr<StmtAST> Parser::ParseDeclare() { + getNextToken(); + if (CurTok != tok_identifier) return nullptr; + std::string Name = Lex.IdentifierStr; + getNextToken(); + + if (CurTok != tok_colon) return nullptr; + getNextToken(); + + if (CurTok == tok_array) { + getNextToken(); + if (CurTok != '[') { + fprintf(stderr, "Error: Expected '[' after ARRAY\n"); + return nullptr; + } + getNextToken(); + + std::vector<std::pair<std::unique_ptr<ExprAST>, std::unique_ptr<ExprAST>>> Bounds; + + while (true) { + auto Lower = ParseExpression(); + if (!Lower) return nullptr; + + if (CurTok != tok_colon) { + fprintf(stderr, "Error: Expected ':' in array range (e.g. 1:10)\n"); + return nullptr; + } + getNextToken(); + + auto Upper = ParseExpression(); + if (!Upper) return nullptr; + + Bounds.push_back({std::move(Lower), std::move(Upper)}); + + if (CurTok == ']') break; + if (CurTok == ',') { + getNextToken(); + continue; + } + fprintf(stderr, "Error: Expected ',' or ']' in array dims\n"); + return nullptr; + } + getNextToken(); + + if (CurTok != tok_of) { + fprintf(stderr, "Error: Expected OF after array dims\n"); + return nullptr; + } + getNextToken(); + + if (CurTok != tok_integer_kw) { + fprintf(stderr, "Error: Only INTEGER arrays supported currently\n"); + return nullptr; + } + getNextToken(); + + return std::make_unique<ArrayDeclareStmtAST>(Name, std::move(Bounds), "INTEGER"); + } + else if (CurTok == tok_integer_kw) { + getNextToken(); + return std::make_unique<DeclareStmtAST>(Name); + } + + return nullptr; +} + std::unique_ptr<StmtAST> Parser::ParseIfStmt() { getNextToken(); auto Cond = ParseExpression(); @@ -248,19 +338,32 @@ std::unique_ptr<StmtAST> Parser::ParseForStmt() { std::unique_ptr<StmtAST> Parser::ParseStatement() { if (CurTok == tok_declare) { - getNextToken(); - if (CurTok != tok_identifier) return nullptr; - std::string Name = Lex.IdentifierStr; - getNextToken(); - if (CurTok != tok_colon) return nullptr; - getNextToken(); - if (CurTok != tok_integer_kw) return nullptr; - getNextToken(); - return std::make_unique<DeclareStmtAST>(Name); + return ParseDeclare(); } else if (CurTok == tok_identifier) { std::string Name = Lex.IdentifierStr; getNextToken(); + + if (CurTok == '[') { + getNextToken(); + std::vector<std::unique_ptr<ExprAST>> Indices; + while (true) { + auto Exp = ParseExpression(); + Indices.push_back(std::move(Exp)); + if (CurTok == ']') break; + if (CurTok == ',') getNextToken(); + } + getNextToken(); + + if (CurTok != tok_assign) { + fprintf(stderr, "Error: Expected '<-' after array access in assignment\n"); + return nullptr; + } + getNextToken(); + auto Expr = ParseExpression(); + return std::make_unique<ArrayAssignStmtAST>(Name, std::move(Indices), std::move(Expr)); + } + if (CurTok != tok_assign) return nullptr; getNextToken(); auto Expr = ParseExpression(); |
