From 601c4a3f89916fb22286aad52a4efd5ebe7ac7ea Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 13 Jun 2019 17:37:23 +0200 Subject: [PATCH] Always pass the script string around by reference --- .../BoundExpressions/BoundExpression.hpp | 8 ++------ src/Evaluator/Evaluator.cpp | 1 - src/Parser/Lexer.cpp | 18 ++++++------------ src/Parser/Lexer.hpp | 3 +-- src/Script.cpp | 8 ++++---- src/Script.hpp | 4 ++-- src/Utilities/HashedString.hpp | 2 +- 7 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/Binder/BoundExpressions/BoundExpression.hpp b/src/Binder/BoundExpressions/BoundExpression.hpp index 5fdb380..01fe5ff 100644 --- a/src/Binder/BoundExpressions/BoundExpression.hpp +++ b/src/Binder/BoundExpressions/BoundExpression.hpp @@ -1,7 +1,3 @@ -#include - -#include - #ifndef PORYGONLANG_BOUNDEXPRESSION_HPP #define PORYGONLANG_BOUNDEXPRESSION_HPP @@ -37,7 +33,7 @@ class BoundExpression{ const unsigned int _length; const shared_ptr _type; public: - BoundExpression(unsigned int start, unsigned int length, shared_ptr type) + BoundExpression(unsigned int start, unsigned int length, shared_ptr type) : _start(start), _length(length), _type(std::move(type)) @@ -47,7 +43,7 @@ public: virtual ~BoundExpression() = default; virtual const BoundExpressionKind GetKind() const = 0; - virtual const std::shared_ptr GetType() const{ + virtual const std::shared_ptr& GetType() const{ return _type; }; diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index 2d6ec45..056f3b7 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -1,4 +1,3 @@ -#include #include #include diff --git a/src/Parser/Lexer.cpp b/src/Parser/Lexer.cpp index 8629944..6c35425 100644 --- a/src/Parser/Lexer.cpp +++ b/src/Parser/Lexer.cpp @@ -5,15 +5,9 @@ #include "Lexer.hpp" -Lexer::Lexer(string* scriptString, class Script* script) { - this -> _scriptString = scriptString; - this->_scriptSize = scriptString->size(); - this -> ScriptData = script; - this -> _position = 0; -} - -Lexer::Lexer(const string& scriptString, class Script *script) { - this -> _scriptString = new string(scriptString); +Lexer::Lexer(const string& scriptString, class Script* script) + : _scriptString(scriptString) +{ this->_scriptSize = scriptString.size(); this -> ScriptData = script; this -> _position = 0; @@ -38,7 +32,7 @@ vector Lexer::Lex() { char Lexer::Peek(){ if (Lexer::_position >= this -> _scriptSize) return '\0'; - return this -> _scriptString->at(Lexer::_position); + return this -> _scriptString.at(Lexer::_position); } char Lexer::Next(){ @@ -200,7 +194,7 @@ IToken * Lexer::LexIdentifierOrKeyword() { } } - string s = this -> _scriptString->substr(start, end - start + 1); + string s = this -> _scriptString.substr(start, end - start + 1); switch (HashedString::ConstHash(s.c_str())){ case HashedString::ConstHash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3); case HashedString::ConstHash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5); @@ -258,7 +252,7 @@ IToken* Lexer::LexString(char c){ return new SimpleToken(TokenKind::BadToken, start, end -start + 1); } - string s = this -> _scriptString->substr(start + 1, end - start); + string s = this -> _scriptString.substr(start + 1, end - start); stringstream stream; for (int i = 0; i < s.size(); i++){ c = s[i]; diff --git a/src/Parser/Lexer.hpp b/src/Parser/Lexer.hpp index c951f8e..a7bd7a2 100644 --- a/src/Parser/Lexer.hpp +++ b/src/Parser/Lexer.hpp @@ -8,7 +8,7 @@ using namespace std; class Lexer { - string* _scriptString; + const string& _scriptString; #ifdef TESTS_BUILD public: #endif @@ -24,7 +24,6 @@ public: Script* ScriptData; vector Lex(); - explicit Lexer(string* scriptString, class Script* script); explicit Lexer(const string& scriptString, class Script* script); }; diff --git a/src/Script.cpp b/src/Script.cpp index 74cadd8..60dd070 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -7,9 +7,9 @@ #include "Parser/Parser.hpp" #include "Binder/Binder.hpp" -Script* Script::Create(string script) { +Script* Script::Create(const string& script) { auto s = new Script(); - s -> Parse(std::move(script)); + s -> Parse(script); return s; } @@ -32,8 +32,8 @@ Script::~Script() { delete this->_scriptVariables; } -void Script::Parse(string script) { - auto lexer = Lexer(&script, this); +void Script::Parse(const string& script) { + auto lexer = Lexer(script, this); auto lexResult = lexer.Lex(); auto parser = Parser(lexResult, this); auto parseResult = parser.Parse(); diff --git a/src/Script.hpp b/src/Script.hpp index 34208dc..d25d804 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -23,9 +23,9 @@ class Script { shared_ptr _returnType; explicit Script(); - void Parse(string script); + void Parse(const string& script); public: - static Script* Create(string script); + static Script* Create(const string& script); DiagnosticsHolder* Diagnostics; ~Script(); diff --git a/src/Utilities/HashedString.hpp b/src/Utilities/HashedString.hpp index 9cc8e2f..2f02855 100644 --- a/src/Utilities/HashedString.hpp +++ b/src/Utilities/HashedString.hpp @@ -18,7 +18,7 @@ public: 5381; } - const int GetHash(){ + const int GetHash() const{ return _hash; }