From 40579ca508770aa144cf23d50aa20e4983030377 Mon Sep 17 00:00:00 2001 From: abdussamedulutas Date: Tue, 26 May 2026 00:03:19 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20Pratt=20parser=20NUD/LED=20ak=C4=B1?= =?UTF-8?q?=C5=9F=C4=B1,=20Token*=20slicing,=20null=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ParserToken artık Token* tutuyor (object slicing önlendi) - parseNullDenotation atom'ları tüketip ilerliyor - parseExpression while döngüsü currentToken() ile çalışıyor - IR: null Left/Right ve null ASTNode girişi korumalı - source.sqt: 1/(74-63+!1)-74*2/-0.7e+10 başarıyla parse ediliyor --- source.sqt | 2 +- src/ir/ir.hpp | 5 +++-- src/parser/parser.hpp | 10 ++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/source.sqt b/source.sqt index 19f5084..2c3ffcd 100644 --- a/source.sqt +++ b/source.sqt @@ -1 +1 @@ -1+2 +1 / (74 - 63 + !1) - 74 * 2 / -0.7e+10 diff --git a/src/ir/ir.hpp b/src/ir/ir.hpp index b91e478..d369221 100644 --- a/src/ir/ir.hpp +++ b/src/ir/ir.hpp @@ -51,6 +51,7 @@ public: std::vector IROpDatas; int parse(ASTNode* ast) { + if (!ast) return 0; switch (ast->kind) { case ASTKind::BinaryExpression: return parseBinaryExpr((BinaryExpressionNode*)ast); @@ -131,8 +132,8 @@ public: default: return 0; } - int left = parse(bin->Left); - int right = parse(bin->Right); + int left = bin->Left ? parse(bin->Left) : 0; + int right = bin->Right ? parse(bin->Right) : 0; IROpDatas.push_back({ op, diff --git a/src/parser/parser.hpp b/src/parser/parser.hpp index 8b5f1c2..aa74e34 100644 --- a/src/parser/parser.hpp +++ b/src/parser/parser.hpp @@ -420,16 +420,14 @@ inline ASTNode* Parser::parseExpression(uint16_t precedence) { if (!left) return nullptr; while (true) { - auto next = lookahead(1); + auto next = currentToken(); if (next.type == TokenType::RPAREN || next.type == TokenType::SEMICOLON || next.type == TokenType::RBRACE || next.type == TokenType::COMMA) break; - next = lookahead(1); if (precedence < next.getPowerOperator()) { - nextToken(); left = parseLeftDenotation(left); } else { break; @@ -472,8 +470,9 @@ inline ASTNode* Parser::parseNullDenotation() { return bin; } - // Numeric literal — NUD does NOT advance; loop handles it + // Numeric literal if (ct.type == TokenType::NUMBER) { + nextToken(); LiteralNode* lit = new LiteralNode(); lit->lexerToken = ct.token; lit->parserToken = ct; @@ -482,6 +481,7 @@ inline ASTNode* Parser::parseNullDenotation() { // String literal if (ct.type == TokenType::STRING) { + nextToken(); LiteralNode* lit = new LiteralNode(); lit->lexerToken = ct.token; lit->parserToken = ct; @@ -490,6 +490,7 @@ inline ASTNode* Parser::parseNullDenotation() { // Boolean / null literals if (ct.is({TokenType::KW_TRUE, TokenType::KW_FALSE, TokenType::KW_NULL})) { + nextToken(); LiteralNode* lit = new LiteralNode(); lit->lexerToken = ct.token; lit->parserToken = ct; @@ -498,6 +499,7 @@ inline ASTNode* Parser::parseNullDenotation() { // Identifier if (ct.type == TokenType::IDENTIFIER) { + nextToken(); IdentifierNode* id = new IdentifierNode(); id->lexerToken = ct.token; id->parserToken = ct;