From fcb28cc03f93a4b00ec181a8492432dfa0e5fa11 Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Fri, 18 Apr 2025 06:00:25 +0000
Subject: [PATCH] replace hungarian comment to english
---
src/Parser/Parser.cpp | 35 ++++++-----
src/Parser/ParsedExpression.hpp | 14 ++--
src/Parser/Parser.hpp | 76 ++++++++++++-------------
3 files changed, 64 insertions(+), 61 deletions(-)
diff --git a/src/Parser/ParsedExpression.hpp b/src/Parser/ParsedExpression.hpp
index 54cce5d..7d7c8fb 100644
--- a/src/Parser/ParsedExpression.hpp
+++ b/src/Parser/ParsedExpression.hpp
@@ -21,12 +21,12 @@
Symbols::Value value;
std::string name;
- // Műveletekhez
+ // For operations
std::string op;
ParsedExpressionPtr lhs;
ParsedExpressionPtr rhs;
- // Konstruktor literálhoz
+ // Constructor for literal
static ParsedExpressionPtr makeLiteral(const Symbols::Value & val) {
auto expr = std::make_unique<ParsedExpression>();
expr->kind = Kind::Literal;
@@ -34,7 +34,7 @@
return expr;
}
- // Konstruktor változóhoz
+ // Constructor for variable
static ParsedExpressionPtr makeVariable(const std::string & name) {
auto expr = std::make_unique<ParsedExpression>();
expr->kind = Kind::Variable;
@@ -42,7 +42,7 @@
return expr;
}
- // Konstruktor binárishoz
+ // Constructor for binary operation
static ParsedExpressionPtr makeBinary(std::string op, ParsedExpressionPtr left, ParsedExpressionPtr right) {
auto expr = std::make_unique<ParsedExpression>();
expr->kind = Kind::Binary;
@@ -52,7 +52,7 @@
return expr;
}
- // Konstruktor unárishoz
+ // Constructor for unary operation
static ParsedExpressionPtr makeUnary(std::string op, ParsedExpressionPtr operand) {
auto expr = std::make_unique<ParsedExpression>();
expr->kind = Kind::Unary;
@@ -82,14 +82,14 @@
{
auto lhsType = lhs->value.getType();
//auto rhsType = rhs->value.getType();
- return lhsType; // Bináris kifejezésnél a típusok azonosak, tehát a bal oldali típust visszaadhatjuk
+ return lhsType; // In binary expressions, operand types match, so we can return the left-hand type
}
case Kind::Unary:
{
//auto operandType = op.
if (op == "!") {
- return Symbols::Variables::Type::BOOLEAN; // Mivel a '!' operátor bool típust vár
+ return Symbols::Variables::Type::BOOLEAN; // Because the '!' operator expects a boolean type
}
break;
}
diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp
index 38ff418..9e8ad75 100644
--- a/src/Parser/Parser.cpp
+++ b/src/Parser/Parser.cpp
@@ -4,7 +4,7 @@
#include "Interpreter/OperationsFactory.hpp"
#include "Lexer/Operators.hpp"
-// Más szükséges include-ok, ha kellenek
+// Additional necessary includes, if needed
namespace Parser {
const std::unordered_map<std::string, Lexer::Tokens::Type> Parser::keywords = {
@@ -14,11 +14,11 @@
{ "for", Lexer::Tokens::Type::KEYWORD },
{ "return", Lexer::Tokens::Type::KEYWORD_RETURN },
{ "function", Lexer::Tokens::Type::KEYWORD_FUNCTION_DECLARATION },
- // Régebbiek:
+ // Older keywords:
{ "const", Lexer::Tokens::Type::KEYWORD },
{ "true", Lexer::Tokens::Type::KEYWORD },
{ "false", Lexer::Tokens::Type::KEYWORD },
- // változó típusok
+ // variable types
{ "null", Lexer::Tokens::Type::KEYWORD_NULL },
{ "int", Lexer::Tokens::Type::KEYWORD_INT },
{ "double", Lexer::Tokens::Type::KEYWORD_DOUBLE },
@@ -26,7 +26,7 @@
{ "string", Lexer::Tokens::Type::KEYWORD_STRING },
{ "boolean", Lexer::Tokens::Type::KEYWORD_BOOLEAN },
{ "bool", Lexer::Tokens::Type::KEYWORD_BOOLEAN },
- // ... egyéb kulcsszavak ...
+ // ... other keywords ...
};
const std::unordered_map<Lexer::Tokens::Type, Symbols::Variables::Type> Parser::variable_types = {
@@ -69,29 +69,29 @@
if (currentToken().type != Lexer::Tokens::Type::PUNCTUATION || currentToken().value != ")") {
while (true) {
- // Paraméter típusa
- Symbols::Variables::Type param_type = parseType(); // Ez elfogyasztja a type tokent
+ // Parameter type
+ Symbols::Variables::Type param_type = parseType(); // This consumes the type token
- // Paraméter név ($variable)
+ // Parameter name ($variable)
Lexer::Tokens::Token param_id_token = expect(Lexer::Tokens::Type::VARIABLE_IDENTIFIER);
std::string param_name = param_id_token.value;
- if (!param_name.empty() && param_name[0] == '$') { // '$' eltávolítása
+ if (!param_name.empty() && param_name[0] == '$') { // remove '$'
param_name = param_name.substr(1);
}
param_infos.push_back({ param_name, param_type });
- // Vessző vagy zárójel következik?
+ // Expecting comma or closing parenthesis?
if (match(Lexer::Tokens::Type::PUNCTUATION, ",")) {
continue;
}
if (currentToken().type == Lexer::Tokens::Type::PUNCTUATION && currentToken().value == ")") {
- break; // Lista vége
+ break; // end of list
}
reportError("Expected ',' or ')' in parameter list");
}
}
- // Most a ')' következik
+ // Now expect ')'
expect(Lexer::Tokens::Type::PUNCTUATION, ")");
// check if we have a option return type: function name() type { ... }
@@ -182,8 +182,9 @@
expect(Lexer::Tokens::Type::PUNCTUATION, "}");
const std::string newns = Symbols::SymbolContainer::instance()->currentScopeName() + "." + function_name;
Symbols::SymbolContainer::instance()->create(newns);
- std::shared_ptr<Parser> parser = std::make_shared<Parser>();
- parser->parseScript(filtered_tokens, input_string, this->current_filename_);
+ // Parse function body using a stack‑allocated Parser (avoid heap allocations)
+ Parser innerParser;
+ innerParser.parseScript(filtered_tokens, input_string, this->current_filename_);
Symbols::SymbolContainer::instance()->enterPreviousScope();
// create function
Interpreter::OperationsFactory::defineFunction(
@@ -194,6 +195,10 @@
ParsedExpressionPtr Parser::parseParsedExpression(const Symbols::Variables::Type & expected_var_type) {
std::stack<std::string> operator_stack;
std::vector<ParsedExpressionPtr> output_queue;
+ // Reserve output queue to reduce reallocations
+ if (tokens_.size() > current_token_index_) {
+ output_queue.reserve(tokens_.size() - current_token_index_);
+ }
bool expect_unary = true;
@@ -238,7 +243,7 @@
std::string op = std::string(token.lexeme);
if (expect_unary && Lexer::isUnaryOperator(op)) {
- op = "u" + op; // pl. u-, u+ vagy u!
+ op = "u" + op; // e.g. u-, u+ or u!
}
while (!operator_stack.empty()) {
@@ -285,7 +290,7 @@
}
}
- // Kiürítjük az operator stack-et
+ // Empty the operator stack
while (!operator_stack.empty()) {
std::string op = operator_stack.top();
operator_stack.pop();
diff --git a/src/Parser/Parser.hpp b/src/Parser/Parser.hpp
index 92461bb..5996bab 100644
--- a/src/Parser/Parser.hpp
+++ b/src/Parser/Parser.hpp
@@ -32,7 +32,7 @@
formattedMessage_ = formatMessage();
}
- Exception(const std::string & msg, std::string & expected, int line, int col) {
+ Exception(const std::string & msg, const std::string & expected, int line, int col) {
rawMessage_ = msg;
if (expected.empty() == false) {
rawMessage_ += " (expected: " + expected + ")";
@@ -56,23 +56,23 @@
size_t current_token_index_;
std::string current_filename_;
- // Token Stream Kezelő és Hibakezelő segédfüggvények (változatlanok)
+ // Token stream handling and error-reporting helper functions (unchanged)
const Lexer::Tokens::Token & currentToken() const {
if (isAtEnd()) {
- // Technikailag itt már nem kellene lennünk, ha a parseProgram ciklus jól van megírva
- // De biztonsági ellenőrzésként jó lehet
+ // Technically we should never reach this if parseScript's loop is correct
+ // But it's useful as a safety check
if (!tokens_.empty() && tokens_.back().type == Lexer::Tokens::Type::END_OF_FILE) {
- return tokens_.back(); // Visszaadjuk az EOF tokent
+ return tokens_.back(); // return the EOF token
}
throw std::runtime_error("Unexpected end of token stream reached.");
}
return tokens_[current_token_index_];
}
- // Előre néz a token stream-ben
+ // Look ahead in the token stream
const Lexer::Tokens::Token & peekToken(size_t offset = 1) const {
if (current_token_index_ + offset >= tokens_.size()) {
- // EOF vagy azon túl vagyunk, adjuk vissza az utolsó tokent (ami EOF kell legyen)
+ // If at or beyond EOF, return the last token (should be EOF)
if (!tokens_.empty()) {
return tokens_.back();
}
@@ -81,7 +81,7 @@
return tokens_[current_token_index_ + offset];
}
- // Elfogyasztja (lépteti az indexet) az aktuális tokent és visszaadja azt
+ // Consume (advance past) the current token and return it
Lexer::Tokens::Token consumeToken() {
if (isAtEnd()) {
throw std::runtime_error("Cannot consume token at end of stream.");
@@ -89,8 +89,8 @@
return tokens_[current_token_index_++];
}
- // Ellenőrzi, hogy az aktuális token típusa megegyezik-e a várttal.
- // Ha igen, elfogyasztja és true-t ad vissza. Ha nem, false-t ad vissza.
+ // Check if current token type matches the expected type
+ // If so, consume it and return true; otherwise return false
bool match(Lexer::Tokens::Type expected_type) {
if (isAtEnd()) {
return false;
@@ -102,8 +102,8 @@
return false;
}
- // Ellenőrzi, hogy az aktuális token típusa és értéke megegyezik-e a várttal.
- // Csak OPERATOR és PUNCTUATION esetén érdemes használni az érték ellenőrzést.
+ // Check if current token type and value match the expected ones
+ // Only use value checking for operators and punctuation
bool match(Lexer::Tokens::Type expected_type, const std::string & expected_value) {
if (isAtEnd()) {
return false;
@@ -125,11 +125,11 @@
return consumeToken();
}
reportError("Expected token type " + Lexer::Tokens::TypeToString(expected_type));
- // A reportError dob, ez a return sosem fut le, de a fordító kedvéért kellhet:
- return token; // Vagy dobjon a reportError
+ // reportError throws; this return is never reached, but may satisfy the compiler
+ return token; // or let reportError throw
}
- // Mint az expect, de az értékét is ellenőrzi.
+ // Like expect, but also checks the token's value
Lexer::Tokens::Token expect(Lexer::Tokens::Type expected_type, const std::string & expected_value) {
if (isAtEnd()) {
reportError("Unexpected end of file, expected token: " + Lexer::Tokens::TypeToString(expected_type) +
@@ -141,18 +141,18 @@
}
reportError("Expected token " + Lexer::Tokens::TypeToString(expected_type) + " with value '" + expected_value +
"'");
- return token; // reportError dob
+ return token; // reportError throws
}
- // Ellenőrzi, hogy a releváns tokenek végére értünk-e (az EOF előtti utolsó tokenen vagyunk-e)
+ // Check if we've reached the end of relevant tokens (just before EOF)
bool isAtEnd() const {
- // Akkor vagyunk a végén, ha az index a tokenek méretével egyenlő,
- // vagy ha már csak az EOF token van hátra (ha az a lista utolsó eleme).
+ // We're at the end if the index equals the number of tokens,
+ // or if only the EOF token remains (as the last element)
return current_token_index_ >= tokens_.size() ||
(current_token_index_ == tokens_.size() - 1 && tokens_.back().type == Lexer::Tokens::Type::END_OF_FILE);
}
- [[noreturn]] void reportError(const std::string & message, const std::string expected = "") {
+ [[noreturn]] void reportError(const std::string & message, const std::string& expected = "") {
if (current_token_index_ < tokens_.size()) {
throw Exception(message, expected, tokens_[current_token_index_]);
}
@@ -161,7 +161,7 @@
throw Exception(message, expected, line, col);
}
- // parseStatement (változatlan)
+ // parseStatement (unchanged)
void parseStatement() {
const auto & token_type = currentToken().type;
@@ -170,11 +170,10 @@
return;
}
- for (const auto & _type : Parser::Parser::variable_types) {
- if (token_type == _type.first) {
- parseVariableDefinition();
- return;
- }
+ // Variable definition if leading token matches a type keyword
+ if (Parser::variable_types.find(token_type) != Parser::variable_types.end()) {
+ parseVariableDefinition();
+ return;
}
reportError("Unexpected token at beginning of statement");
@@ -183,19 +182,18 @@
void parseVariableDefinition();
void parseFunctionDefinition();
- // --- Elemzési Segédfüggvények ---
+ // --- Parsing helper functions ---
// type : KEYWORD_STRING | KEYWORD_INT | KEYWORD_DOUBLE
- // Visszaadja a megfelelő Symbols::Variables::Type enum értéket és elfogyasztja a tokent.
+ // Returns the corresponding Symbols::Variables::Type enum and consumes the token
Symbols::Variables::Type parseType() {
const auto & token = currentToken();
- for (const auto & _type : Parser::variable_types) {
- if (token.type == _type.first) {
- consumeToken();
- return _type.second;
- }
+ // Direct lookup for type keyword
+ auto it = Parser::variable_types.find(token.type);
+ if (it != Parser::variable_types.end()) {
+ consumeToken();
+ return it->second;
}
-
reportError("Expected type keyword (string, int, double, float)");
}
@@ -203,15 +201,15 @@
Lexer::Tokens::Token token = currentToken();
bool is_negative = false;
- // Előjel kezelése
+ // Handle unary sign
if (token.type == Lexer::Tokens::Type::OPERATOR_ARITHMETIC && (token.lexeme == "-" || token.lexeme == "+") &&
peekToken().type == Lexer::Tokens::Type::NUMBER) {
is_negative = (token.lexeme == "-");
token = peekToken();
- consumeToken(); // előjelet elfogyasztottuk
+ consumeToken(); // consumed the sign
}
- // STRING típus
+ // STRING type
if (expected_var_type == Symbols::Variables::Type::STRING) {
if (token.type == Lexer::Tokens::Type::STRING_LITERAL) {
consumeToken();
@@ -220,7 +218,7 @@
reportError("Expected string literal value");
}
- // BOOLEAN típus
+ // BOOLEAN type
if (expected_var_type == Symbols::Variables::Type::BOOLEAN) {
if (token.type == Lexer::Tokens::Type::KEYWORD && (token.value == "true" || token.value == "false")) {
consumeToken();
@@ -229,7 +227,7 @@
reportError("Expected boolean literal value (true or false)");
}
- // NUMERIC típusok
+ // NUMERIC types
if (expected_var_type == Symbols::Variables::Type::INTEGER ||
expected_var_type == Symbols::Variables::Type::DOUBLE ||
expected_var_type == Symbols::Variables::Type::FLOAT) {
--
Gitblit v1.9.3