From 943ed04b775949e8bc4948bcb8d762a32ab14f76 Mon Sep 17 00:00:00 2001 From: ov-4 Date: Thu, 29 Jan 2026 14:24:05 +0000 Subject: Added array --- lib/Parser/Parser.cc | 121 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 112 insertions(+), 9 deletions(-) (limited to 'lib/Parser/Parser.cc') 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 Parser::ParseNumberExpr() { std::unique_ptr Parser::ParseIdentifierExpr() { std::string IdName = Lex.IdentifierStr; getNextToken(); + + if (CurTok == '[') { + getNextToken(); + std::vector> 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(IdName, std::move(Indices)); + } + return std::make_unique(IdName); } @@ -104,6 +128,72 @@ std::vector> Parser::Parse() { return Statements; } +std::unique_ptr 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::unique_ptr>> 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(Name, std::move(Bounds), "INTEGER"); + } + else if (CurTok == tok_integer_kw) { + getNextToken(); + return std::make_unique(Name); + } + + return nullptr; +} + std::unique_ptr Parser::ParseIfStmt() { getNextToken(); auto Cond = ParseExpression(); @@ -248,19 +338,32 @@ std::unique_ptr Parser::ParseForStmt() { std::unique_ptr 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(Name); + return ParseDeclare(); } else if (CurTok == tok_identifier) { std::string Name = Lex.IdentifierStr; getNextToken(); + + if (CurTok == '[') { + getNextToken(); + std::vector> 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(Name, std::move(Indices), std::move(Expr)); + } + if (CurTok != tok_assign) return nullptr; getNextToken(); auto Expr = ParseExpression(); -- cgit v1.3.1-4-g156e