From 56d7b4abd2da3c3532db6a722fe866558ada6689 Mon Sep 17 00:00:00 2001 From: Nathanne Isip Date: Sun, 27 Oct 2024 05:55:11 +0800 Subject: [PATCH] Used shared pointers instead of unique pointers on source file implementations. --- src/zhivo/ast/expression/BinaryExpression.cpp | 2 +- .../VariableDeclarationExpression.cpp | 2 +- src/zhivo/core/DynamicObject.cpp | 4 +- src/zhivo/core/SymbolTable.cpp | 2 +- src/zhivo/parser/Parser.cpp | 394 +++++++++--------- src/zhivo/parser/Tokenizer.cpp | 4 +- 6 files changed, 204 insertions(+), 204 deletions(-) diff --git a/src/zhivo/ast/expression/BinaryExpression.cpp b/src/zhivo/ast/expression/BinaryExpression.cpp index 9e40ca8..5ef038d 100644 --- a/src/zhivo/ast/expression/BinaryExpression.cpp +++ b/src/zhivo/ast/expression/BinaryExpression.cpp @@ -51,7 +51,7 @@ DynamicObject BinaryExpression::visit(SymbolTable& symbols) { ); DynamicObject rValue = this->right->visit(symbols); - std::unique_ptr rValuePtr = std::make_unique(rValue); + std::shared_ptr rValuePtr = std::make_shared(rValue); arrayVal.setArrayElement( std::move(this->address), diff --git a/src/zhivo/ast/expression/VariableDeclarationExpression.cpp b/src/zhivo/ast/expression/VariableDeclarationExpression.cpp index 1cc9b5c..105b16b 100644 --- a/src/zhivo/ast/expression/VariableDeclarationExpression.cpp +++ b/src/zhivo/ast/expression/VariableDeclarationExpression.cpp @@ -59,7 +59,7 @@ DynamicObject VariableDeclarationExpression::visit(SymbolTable& symbols) { NativeFunction VariableDeclarationExpression::loadNativeFunction( std::string& libName, std::string& funcName, - std::unique_ptr address + std::shared_ptr address ) { void* handle; std::string library = std::string(libName.c_str()); diff --git a/src/zhivo/core/DynamicObject.cpp b/src/zhivo/core/DynamicObject.cpp index a716743..0988756 100644 --- a/src/zhivo/core/DynamicObject.cpp +++ b/src/zhivo/core/DynamicObject.cpp @@ -161,9 +161,9 @@ bool DynamicObject::booleanEquivalent() { } void DynamicObject::setArrayElement( - std::unique_ptr reference, + std::shared_ptr reference, size_t index, - std::unique_ptr object + std::shared_ptr object ) { if(!this->isArray()) #ifdef _MSC_VER diff --git a/src/zhivo/core/SymbolTable.cpp b/src/zhivo/core/SymbolTable.cpp index c547637..6e30ac5 100644 --- a/src/zhivo/core/SymbolTable.cpp +++ b/src/zhivo/core/SymbolTable.cpp @@ -30,7 +30,7 @@ SymbolTable& SymbolTable::operator=(const SymbolTable& other) { } DynamicObject SymbolTable::getSymbol( - std::unique_ptr reference, + std::shared_ptr reference, const std::string& name ) { if(this->hasSymbol(name)) diff --git a/src/zhivo/parser/Parser.cpp b/src/zhivo/parser/Parser.cpp index a705d24..a8f2fa5 100644 --- a/src/zhivo/parser/Parser.cpp +++ b/src/zhivo/parser/Parser.cpp @@ -65,7 +65,7 @@ #include Parser Parser::fromFile(const std::string& fileName) { - std::unique_ptr tokenizer = Tokenizer::loadFile(fileName); + std::shared_ptr tokenizer = Tokenizer::loadFile(fileName); tokenizer->scan(); return Parser(tokenizer->getTokens()); @@ -92,7 +92,7 @@ Token Parser::peek() { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(this->previous()), + std::make_shared(this->previous()), "Encountered end-of-file." ); @@ -121,7 +121,7 @@ Token Parser::consume(const std::string& image) { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(this->previous()), + std::make_shared(this->previous()), "Expecting \"" + image + "\", encountered end-of-code." ); @@ -132,7 +132,7 @@ Token Parser::consume(const std::string& image) { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(this->previous()), + std::make_shared(this->previous()), "Expecting \"" + image + "\", encountered \"" + token.getImage() + "\"" ); @@ -147,7 +147,7 @@ Token Parser::consume(TokenType type) { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(this->previous()), + std::make_shared(this->previous()), "Expecting token type, encountered end-of-code." ); @@ -157,7 +157,7 @@ Token Parser::consume(TokenType type) { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(this->current()), + std::make_shared(this->current()), "Expecting " + tokenTypeToString(type) + ", encountered " + tokenTypeToString(token.getType()) ); @@ -166,13 +166,13 @@ Token Parser::consume(TokenType type) { return token; } -const std::vector>& Parser::getGlobalStatements() const { +const std::vector>& Parser::getGlobalStatements() const { return this->globalStatements; } -std::unique_ptr Parser::exprArray() { +std::shared_ptr Parser::exprArray() { Token address = this->consume("["); - std::vector> expressions; + std::vector> expressions; while(!this->isNext("]", TokenType::OPERATOR)) { if(!expressions.empty()) @@ -182,89 +182,89 @@ std::unique_ptr Parser::exprArray() { } this->consume("]"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expressions) ); } -std::unique_ptr Parser::exprBlock() { +std::shared_ptr Parser::exprBlock() { Token address = this->consume("{"); - std::vector> body; + std::vector> body; while(!this->isNext("}", TokenType::OPERATOR)) body.emplace_back(this->expression()); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(body) ); } -std::unique_ptr Parser::exprCatchHandle() { +std::shared_ptr Parser::exprCatchHandle() { Token address = this->consume("catch"); - std::unique_ptr catchExpr = this->expression(); + std::shared_ptr catchExpr = this->expression(); this->consume("handle"); Token handler = this->consume(TokenType::IDENTIFIER); - std::unique_ptr handleExpr = this->expression(); + std::shared_ptr handleExpr = this->expression(); - std::unique_ptr finalExpr = nullptr; + std::shared_ptr finalExpr = nullptr; if(this->isNext("then", TokenType::KEYWORD)) { this->consume("then"); finalExpr = this->expression(); } - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(catchExpr), std::move(handleExpr), - std::make_unique(handler), + std::make_shared(handler), std::move(finalExpr) ); } -std::unique_ptr Parser::exprFunctionDecl() { +std::shared_ptr Parser::exprFunctionDecl() { Token address = this->consume("func"); this->consume("("); - std::vector> parameters; + std::vector> parameters; while(!this->isNext(")", TokenType::OPERATOR)) { if(!parameters.empty()) this->consume(","); parameters.emplace_back( - std::make_unique( + std::make_shared( this->consume(TokenType::IDENTIFIER) ) ); } this->consume(")"); - std::unique_ptr body = this->expression(); - return std::make_unique( - std::make_unique(address), + std::shared_ptr body = this->expression(); + return std::make_shared( + std::make_shared(address), std::move(parameters), std::move(body) ); } -std::unique_ptr Parser::exprLoop() { +std::shared_ptr Parser::exprLoop() { Token address = this->consume("loop"); this->consume("("); - std::unique_ptr initial = this->expression(); + std::shared_ptr initial = this->expression(); this->consume(";"); - std::unique_ptr condition = this->expression(); + std::shared_ptr condition = this->expression(); this->consume(";"); - std::unique_ptr postexpr = this->expression(); + std::shared_ptr postexpr = this->expression(); this->consume(")"); - std::unique_ptr body = this->expression(); - return std::make_unique( - std::make_unique(address), + std::shared_ptr body = this->expression(); + return std::make_shared( + std::make_shared(address), std::move(initial), std::move(condition), std::move(postexpr), @@ -272,61 +272,61 @@ std::unique_ptr Parser::exprLoop() { ); } -std::unique_ptr Parser::exprIf() { +std::shared_ptr Parser::exprIf() { Token address = this->consume("if"); this->consume("("); - std::unique_ptr condition = this->expression(); + std::shared_ptr condition = this->expression(); this->consume(")"); - std::unique_ptr thenExpr = this->expression(); - std::unique_ptr elseExpr = nullptr; + std::shared_ptr thenExpr = this->expression(); + std::shared_ptr elseExpr = nullptr; if(this->isNext("else", TokenType::KEYWORD)) { this->consume("else"); elseExpr = this->expression(); } - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(condition), std::move(thenExpr), std::move(elseExpr) ); } -std::unique_ptr Parser::exprLiteral() { - std::unique_ptr expr = nullptr; +std::shared_ptr Parser::exprLiteral() { + std::shared_ptr expr = nullptr; if(this->isNext("true", TokenType::KEYWORD)) - expr = std::make_unique( - std::make_unique(this->consume("true")), + expr = std::make_shared( + std::make_shared(this->consume("true")), true ); else if(this->isNext("false", TokenType::KEYWORD)) - expr = std::make_unique( - std::make_unique(this->consume("false")), + expr = std::make_shared( + std::make_shared(this->consume("false")), false ); else if(this->isNext("maybe", TokenType::KEYWORD)) - expr = std::make_unique( - std::make_unique(this->consume("maybe")) + expr = std::make_shared( + std::make_shared(this->consume("maybe")) ); else if(this->isNext("nil", TokenType::KEYWORD)) - expr = std::make_unique( - std::make_unique(this->consume("nil")) + expr = std::make_shared( + std::make_shared(this->consume("nil")) ); else if(this->peek().getType() == TokenType::STRING) { Token stringToken = this->consume(TokenType::STRING); - expr = std::make_unique( - std::make_unique(stringToken), + expr = std::make_shared( + std::make_shared(stringToken), stringToken.getImage() ); } else if(this->peek().getType() == TokenType::DIGIT) { Token digitToken = this->consume(TokenType::DIGIT); - expr = std::make_unique( - std::make_unique(digitToken), + expr = std::make_shared( + std::make_shared(digitToken), ZhivoUtil::Convert::translateDigit(digitToken.getImage()) ); } @@ -334,8 +334,8 @@ std::unique_ptr Parser::exprLiteral() { Token regexToken = this->consume(TokenType::REGEX); std::string regExpression(regexToken.getImage()); - expr = std::make_unique( - std::make_unique(regexToken), + expr = std::make_shared( + std::make_shared(regexToken), regExpression ); } @@ -350,17 +350,17 @@ std::unique_ptr Parser::exprLiteral() { ); } - expr = std::make_unique( - std::make_unique(var) + expr = std::make_shared( + std::make_shared(var) ); while(this->isNext("[", TokenType::OPERATOR)) { Token address = this->consume("["); - std::unique_ptr indexExpr = this->expression(); + std::shared_ptr indexExpr = this->expression(); this->consume("]"); - expr = std::make_unique( - std::make_unique(address), + expr = std::make_shared( + std::make_shared(address), std::move(expr), std::move(indexExpr) ); @@ -374,7 +374,7 @@ std::unique_ptr Parser::exprLiteral() { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(address), + std::make_shared(address), "Expecting expression, encountered " + address.getImage() ); @@ -383,34 +383,34 @@ std::unique_ptr Parser::exprLiteral() { return expr; } -std::unique_ptr Parser::exprRandom() { +std::shared_ptr Parser::exprRandom() { Token address = this->consume("random"); - std::unique_ptr thenExpr = this->expression(); - std::unique_ptr elseExpr = nullptr; + std::shared_ptr thenExpr = this->expression(); + std::shared_ptr elseExpr = nullptr; if(this->isNext("else", TokenType::KEYWORD)) { this->consume("else"); elseExpr = this->expression(); } - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(thenExpr), std::move(elseExpr) ); } -std::unique_ptr Parser::exprParallel() { +std::shared_ptr Parser::exprParallel() { Token address = this->consume("parallel"); - std::unique_ptr expression = this->expression(); + std::shared_ptr expression = this->expression(); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expression) ); } -std::unique_ptr Parser::exprRender() { +std::shared_ptr Parser::exprRender() { Token address = this->consume("render"); bool newLine = false, errorStream = false; @@ -424,58 +424,58 @@ std::unique_ptr Parser::exprRender() { errorStream = true; } - std::unique_ptr expression = this->expression(); - return std::make_unique( - std::make_unique(address), + std::shared_ptr expression = this->expression(); + return std::make_shared( + std::make_shared(address), newLine, errorStream, std::move(expression) ); } -std::unique_ptr Parser::exprType() { +std::shared_ptr Parser::exprType() { Token address = this->consume("type"); - std::unique_ptr expression = this->expression(); + std::shared_ptr expression = this->expression(); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expression) ); } -std::unique_ptr Parser::exprUnless() { +std::shared_ptr Parser::exprUnless() { Token address = this->consume("unless"); this->consume("("); - std::unique_ptr condition = this->expression(); + std::shared_ptr condition = this->expression(); this->consume(")"); - std::unique_ptr thenExpr = this->expression(); - std::unique_ptr elseExpr = nullptr; + std::shared_ptr thenExpr = this->expression(); + std::shared_ptr elseExpr = nullptr; if(this->isNext("else", TokenType::KEYWORD)) { this->consume("else"); elseExpr = this->expression(); } - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(condition), std::move(thenExpr), std::move(elseExpr) ); } -std::unique_ptr Parser::exprWhen() { +std::shared_ptr Parser::exprWhen() { Token address = this->consume("when"); this->consume("("); - std::unique_ptr expression = this->expression(); + std::shared_ptr expression = this->expression(); this->consume(")"); this->consume("{"); - std::vector, std::unique_ptr>> cases; - std::unique_ptr defaultCase = nullptr; + std::vector, std::shared_ptr>> cases; + std::shared_ptr defaultCase = nullptr; while(!this->isNext("}", TokenType::OPERATOR)) { if(!cases.empty()) @@ -485,10 +485,10 @@ std::unique_ptr Parser::exprWhen() { this->consume("if"); this->consume("("); - std::unique_ptr caseExpr = this->expression(); + std::shared_ptr caseExpr = this->expression(); this->consume(")"); - std::unique_ptr thenBlock = this->expression(); + std::shared_ptr thenBlock = this->expression(); cases.emplace_back(std::make_pair( std::move(caseExpr), std::move(thenBlock) @@ -500,7 +500,7 @@ std::unique_ptr Parser::exprWhen() { # pragma warning(disable : 5272) #endif throw ParserException( - std::make_unique(address), + std::make_shared(address), "Cannot have more than one (1) else for when expression." ); @@ -510,30 +510,30 @@ std::unique_ptr Parser::exprWhen() { } this->consume("}"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expression), std::move(cases), std::move(defaultCase) ); } -std::unique_ptr Parser::exprWhile() { +std::shared_ptr Parser::exprWhile() { Token address = this->consume("while"); this->consume("("); - std::unique_ptr condition = this->expression(); + std::shared_ptr condition = this->expression(); this->consume(")"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(condition), this->expression() ); } -std::unique_ptr Parser::exprPrimary() { - std::unique_ptr expression = nullptr; +std::shared_ptr Parser::exprPrimary() { + std::shared_ptr expression = nullptr; if(this->isNext("+", TokenType::OPERATOR) || this->isNext("-", TokenType::OPERATOR) || @@ -541,34 +541,34 @@ std::unique_ptr Parser::exprPrimary() { this->isNext("!", TokenType::OPERATOR) ) { Token address = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(std::string(address.getImage())), this->expression() ); } else if(this->isNext("(", TokenType::OPERATOR)) { Token address = this->consume("("); - std::unique_ptr innerExpr = this->expression(); + std::shared_ptr innerExpr = this->expression(); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(innerExpr) ); this->consume(")"); } else if(this->isNext("{", TokenType::OPERATOR)) { Token address = this->consume(TokenType::OPERATOR); - std::vector> statements; + std::vector> statements; while(!this->isNext("}", TokenType::OPERATOR)) { - std::unique_ptr stmt = this->statement(); + std::shared_ptr stmt = this->statement(); statements.emplace_back(std::move(stmt)); } this->consume("}"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(statements) ); } @@ -609,17 +609,17 @@ std::unique_ptr Parser::exprPrimary() { ); } - expression = std::make_unique( - std::make_unique(var) + expression = std::make_shared( + std::make_shared(var) ); while(this->isNext("[", TokenType::OPERATOR)) { Token address = this->consume("["); - std::unique_ptr indexExpr = this->expression(); + std::shared_ptr indexExpr = this->expression(); this->consume("]"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), std::move(indexExpr) ); @@ -631,7 +631,7 @@ std::unique_ptr Parser::exprPrimary() { this->isNext("[", TokenType::OPERATOR)) { while(this->isNext("(", TokenType::OPERATOR)) { Token address = this->consume("("); - std::vector> arguments; + std::vector> arguments; while(!this->isNext(")", TokenType::OPERATOR)) { if(!arguments.empty()) @@ -643,8 +643,8 @@ std::unique_ptr Parser::exprPrimary() { } this->consume(")"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), std::move(arguments) ); @@ -652,11 +652,11 @@ std::unique_ptr Parser::exprPrimary() { while(this->isNext("[", TokenType::OPERATOR)) { Token address = this->consume("["); - std::unique_ptr indexExpr = this->expression(); + std::shared_ptr indexExpr = this->expression(); this->consume("]"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), std::move(indexExpr) ); @@ -666,13 +666,13 @@ std::unique_ptr Parser::exprPrimary() { return expression; } -std::unique_ptr Parser::exprLogicOr() { - std::unique_ptr expression = this->exprLogicAnd(); +std::shared_ptr Parser::exprLogicOr() { + std::shared_ptr expression = this->exprLogicAnd(); while(this->isNext("||", TokenType::OPERATOR)) { Token address = this->consume("||"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), "||", std::move(this->exprLogicAnd()) @@ -682,13 +682,13 @@ std::unique_ptr Parser::exprLogicOr() { return expression; } -std::unique_ptr Parser::exprLogicAnd() { - std::unique_ptr expression = this->exprBitwiseOr(); +std::shared_ptr Parser::exprLogicAnd() { + std::shared_ptr expression = this->exprBitwiseOr(); while(this->isNext("&&", TokenType::OPERATOR)) { Token address = this->consume("&&"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), "&&", std::move(this->exprBitwiseOr()) @@ -698,13 +698,13 @@ std::unique_ptr Parser::exprLogicAnd() { return expression; } -std::unique_ptr Parser::exprBitwiseOr() { - std::unique_ptr expression = this->exprBitwiseXor(); +std::shared_ptr Parser::exprBitwiseOr() { + std::shared_ptr expression = this->exprBitwiseXor(); while(this->isNext("|", TokenType::OPERATOR)) { Token address = this->consume("|"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), "|", std::move(this->exprBitwiseXor()) @@ -714,13 +714,13 @@ std::unique_ptr Parser::exprBitwiseOr() { return expression; } -std::unique_ptr Parser::exprBitwiseXor() { - std::unique_ptr expression = this->exprBitwiseAnd(); +std::shared_ptr Parser::exprBitwiseXor() { + std::shared_ptr expression = this->exprBitwiseAnd(); while(this->isNext("^", TokenType::OPERATOR)) { Token address = this->consume("^"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), "^", std::move(this->exprBitwiseAnd()) @@ -730,13 +730,13 @@ std::unique_ptr Parser::exprBitwiseXor() { return expression; } -std::unique_ptr Parser::exprBitwiseAnd() { - std::unique_ptr expression = this->exprNilCoalescing(); +std::shared_ptr Parser::exprBitwiseAnd() { + std::shared_ptr expression = this->exprNilCoalescing(); while(this->isNext("&", TokenType::OPERATOR)) { Token address = this->consume("&"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), "&", std::move(this->exprNilCoalescing()) @@ -746,13 +746,13 @@ std::unique_ptr Parser::exprBitwiseAnd() { return expression; } -std::unique_ptr Parser::exprNilCoalescing() { - std::unique_ptr expression = this->exprEquality(); +std::shared_ptr Parser::exprNilCoalescing() { + std::shared_ptr expression = this->exprEquality(); while(this->isNext("?", TokenType::OPERATOR)) { Token address = this->consume("?"); - expression = std::make_unique( - std::make_unique(address), + expression = std::make_shared( + std::make_shared(address), std::move(expression), std::move(this->exprEquality()) ); @@ -761,8 +761,8 @@ std::unique_ptr Parser::exprNilCoalescing() { return expression; } -std::unique_ptr Parser::exprEquality() { - std::unique_ptr expression = this->exprComparison(); +std::shared_ptr Parser::exprEquality() { + std::shared_ptr expression = this->exprComparison(); while(this->isNext("==", TokenType::OPERATOR) || this->isNext("!=", TokenType::OPERATOR) || @@ -770,8 +770,8 @@ std::unique_ptr Parser::exprEquality() { this->isNext("::", TokenType::OPERATOR) || this->isNext("!:", TokenType::OPERATOR)) { Token op = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(op), + expression = std::make_shared( + std::make_shared(op), std::move(expression), op.getImage(), std::move(this->exprComparison()) @@ -781,8 +781,8 @@ std::unique_ptr Parser::exprEquality() { return expression; } -std::unique_ptr Parser::exprComparison() { - std::unique_ptr expression = this->exprShift(); +std::shared_ptr Parser::exprComparison() { + std::shared_ptr expression = this->exprShift(); while(this->isNext("<", TokenType::OPERATOR) || this->isNext("<=", TokenType::OPERATOR) || @@ -790,8 +790,8 @@ std::unique_ptr Parser::exprComparison() { this->isNext(">=", TokenType::OPERATOR) ) { Token op = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(op), + expression = std::make_shared( + std::make_shared(op), std::move(expression), op.getImage(), std::move(this->exprShift()) @@ -801,14 +801,14 @@ std::unique_ptr Parser::exprComparison() { return expression; } -std::unique_ptr Parser::exprShift() { - std::unique_ptr expression = this->exprTerm(); +std::shared_ptr Parser::exprShift() { + std::shared_ptr expression = this->exprTerm(); while(this->isNext("<<", TokenType::OPERATOR) || this->isNext(">>", TokenType::OPERATOR)) { Token op = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(op), + expression = std::make_shared( + std::make_shared(op), std::move(expression), op.getImage(), std::move(this->exprTerm()) @@ -818,14 +818,14 @@ std::unique_ptr Parser::exprShift() { return expression; } -std::unique_ptr Parser::exprTerm() { - std::unique_ptr expression = this->exprFactor(); +std::shared_ptr Parser::exprTerm() { + std::shared_ptr expression = this->exprFactor(); while(this->isNext("+", TokenType::OPERATOR) || this->isNext("-", TokenType::OPERATOR)) { Token op = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(op), + expression = std::make_shared( + std::make_shared(op), std::move(expression), op.getImage(), std::move(this->exprFactor()) @@ -835,15 +835,15 @@ std::unique_ptr Parser::exprTerm() { return expression; } -std::unique_ptr Parser::exprFactor() { - std::unique_ptr expression = this->exprPrimary(); +std::shared_ptr Parser::exprFactor() { + std::shared_ptr expression = this->exprPrimary(); while(this->isNext("*", TokenType::OPERATOR) || this->isNext("/", TokenType::OPERATOR) || this->isNext("%", TokenType::OPERATOR)) { Token op = this->consume(TokenType::OPERATOR); - expression = std::make_unique( - std::make_unique(op), + expression = std::make_shared( + std::make_shared(op), std::move(expression), op.getImage(), std::move(this->exprPrimary()) @@ -853,7 +853,7 @@ std::unique_ptr Parser::exprFactor() { return expression; } -std::unique_ptr Parser::exprVal() { +std::shared_ptr Parser::exprVal() { std::string nativePath = ""; Token address = this->consume("val"); @@ -864,12 +864,12 @@ std::unique_ptr Parser::exprVal() { this->consume(")"); } - std::map> declarations; + std::map> declarations; while(true) { if(!declarations.empty()) this->consume(","); - std::unique_ptr value; + std::shared_ptr value; Token variable = this->consume(TokenType::IDENTIFIER); while(this->isNext(".", TokenType::OPERATOR)) { this->consume("."); @@ -884,8 +884,8 @@ std::unique_ptr Parser::exprVal() { this->consume("="); value = this->expression(); } - else value = std::make_unique( - std::make_unique(variable) + else value = std::make_shared( + std::make_shared(variable) ); declarations.insert({variable, std::move(value)}); @@ -894,96 +894,96 @@ std::unique_ptr Parser::exprVal() { break; } - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(declarations), nativePath ); } -std::unique_ptr Parser::expression() { +std::shared_ptr Parser::expression() { return this->exprLogicOr(); } -std::unique_ptr Parser::stmtBreak() { +std::shared_ptr Parser::stmtBreak() { Token address = this->consume("break"); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address) + return std::make_shared( + std::make_shared(address) ); } -std::unique_ptr Parser::stmtContinue() { +std::shared_ptr Parser::stmtContinue() { Token address = this->consume("continue"); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address) + return std::make_shared( + std::make_shared(address) ); } -std::unique_ptr Parser::stmtRet() { +std::shared_ptr Parser::stmtRet() { Token address = this->consume("ret"); - std::unique_ptr expression = this->expression(); + std::shared_ptr expression = this->expression(); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expression) ); } -std::unique_ptr Parser::stmtThrow() { +std::shared_ptr Parser::stmtThrow() { Token address = this->consume("throw"); - std::unique_ptr expression = this->expression(); + std::shared_ptr expression = this->expression(); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(expression) ); } -std::unique_ptr Parser::stmtTest() { +std::shared_ptr Parser::stmtTest() { Token address = this->consume("test"); this->consume("("); - std::unique_ptr testName = this->expression(); + std::shared_ptr testName = this->expression(); this->consume(")"); - std::unique_ptr testBody = this->expression(); + std::shared_ptr testBody = this->expression(); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address), + return std::make_shared( + std::make_shared(address), std::move(testName), std::move(testBody) ); } -std::unique_ptr Parser::stmtWait() { +std::shared_ptr Parser::stmtWait() { Token address = this->consume("wait"); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); - return std::make_unique( - std::make_unique(address) + return std::make_shared( + std::make_shared(address) ); } -std::unique_ptr Parser::statement() { +std::shared_ptr Parser::statement() { if(this->isNext("break", TokenType::KEYWORD)) return this->stmtBreak(); else if(this->isNext("continue", TokenType::KEYWORD)) @@ -997,7 +997,7 @@ std::unique_ptr Parser::statement() { else if(this->isNext("wait", TokenType::KEYWORD)) return this->stmtWait(); - std::unique_ptr expr = this->expression(); + std::shared_ptr expr = this->expression(); if(this->isNext(";", TokenType::OPERATOR)) this->consume(";"); diff --git a/src/zhivo/parser/Tokenizer.cpp b/src/zhivo/parser/Tokenizer.cpp index d2966f9..81e1831 100644 --- a/src/zhivo/parser/Tokenizer.cpp +++ b/src/zhivo/parser/Tokenizer.cpp @@ -19,7 +19,7 @@ #include #include -std::unique_ptr Tokenizer::loadFile(const std::string& filePath) { +std::shared_ptr Tokenizer::loadFile(const std::string& filePath) { std::ifstream file(filePath); if(!file.is_open()) throw std::runtime_error("File not found: " + filePath); @@ -27,7 +27,7 @@ std::unique_ptr Tokenizer::loadFile(const std::string& filePath) { std::ostringstream content; content << file.rdbuf(); - return std::make_unique(content.str(), filePath); + return std::make_shared(content.str(), filePath); } void Tokenizer::scan() {