Always pass the script string around by reference

This commit is contained in:
Deukhoofd 2019-06-13 17:37:23 +02:00
parent 10a2535c96
commit 601c4a3f89
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 16 additions and 28 deletions

View File

@ -1,7 +1,3 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP #ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP #define PORYGONLANG_BOUNDEXPRESSION_HPP
@ -37,7 +33,7 @@ class BoundExpression{
const unsigned int _length; const unsigned int _length;
const shared_ptr<ScriptType> _type; const shared_ptr<ScriptType> _type;
public: public:
BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type) BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type)
: _start(start), : _start(start),
_length(length), _length(length),
_type(std::move(type)) _type(std::move(type))
@ -47,7 +43,7 @@ public:
virtual ~BoundExpression() = default; virtual ~BoundExpression() = default;
virtual const BoundExpressionKind GetKind() const = 0; virtual const BoundExpressionKind GetKind() const = 0;
virtual const std::shared_ptr<ScriptType> GetType() const{ virtual const std::shared_ptr<ScriptType>& GetType() const{
return _type; return _type;
}; };

View File

@ -1,4 +1,3 @@
#include <memory>
#include <utility> #include <utility>
#include <memory> #include <memory>

View File

@ -5,15 +5,9 @@
#include "Lexer.hpp" #include "Lexer.hpp"
Lexer::Lexer(string* scriptString, class Script* script) { Lexer::Lexer(const string& scriptString, class Script* script)
this -> _scriptString = scriptString; : _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);
this->_scriptSize = scriptString.size(); this->_scriptSize = scriptString.size();
this -> ScriptData = script; this -> ScriptData = script;
this -> _position = 0; this -> _position = 0;
@ -38,7 +32,7 @@ vector<IToken*> Lexer::Lex() {
char Lexer::Peek(){ char Lexer::Peek(){
if (Lexer::_position >= this -> _scriptSize) if (Lexer::_position >= this -> _scriptSize)
return '\0'; return '\0';
return this -> _scriptString->at(Lexer::_position); return this -> _scriptString.at(Lexer::_position);
} }
char Lexer::Next(){ 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())){ switch (HashedString::ConstHash(s.c_str())){
case HashedString::ConstHash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3); case HashedString::ConstHash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3);
case HashedString::ConstHash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5); 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); 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; stringstream stream;
for (int i = 0; i < s.size(); i++){ for (int i = 0; i < s.size(); i++){
c = s[i]; c = s[i];

View File

@ -8,7 +8,7 @@
using namespace std; using namespace std;
class Lexer { class Lexer {
string* _scriptString; const string& _scriptString;
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
public: public:
#endif #endif
@ -24,7 +24,6 @@ public:
Script* ScriptData; Script* ScriptData;
vector<IToken*> Lex(); vector<IToken*> Lex();
explicit Lexer(string* scriptString, class Script* script);
explicit Lexer(const string& scriptString, class Script* script); explicit Lexer(const string& scriptString, class Script* script);
}; };

View File

@ -7,9 +7,9 @@
#include "Parser/Parser.hpp" #include "Parser/Parser.hpp"
#include "Binder/Binder.hpp" #include "Binder/Binder.hpp"
Script* Script::Create(string script) { Script* Script::Create(const string& script) {
auto s = new Script(); auto s = new Script();
s -> Parse(std::move(script)); s -> Parse(script);
return s; return s;
} }
@ -32,8 +32,8 @@ Script::~Script() {
delete this->_scriptVariables; delete this->_scriptVariables;
} }
void Script::Parse(string script) { void Script::Parse(const string& script) {
auto lexer = Lexer(&script, this); auto lexer = Lexer(script, this);
auto lexResult = lexer.Lex(); auto lexResult = lexer.Lex();
auto parser = Parser(lexResult, this); auto parser = Parser(lexResult, this);
auto parseResult = parser.Parse(); auto parseResult = parser.Parse();

View File

@ -23,9 +23,9 @@ class Script {
shared_ptr<ScriptType> _returnType; shared_ptr<ScriptType> _returnType;
explicit Script(); explicit Script();
void Parse(string script); void Parse(const string& script);
public: public:
static Script* Create(string script); static Script* Create(const string& script);
DiagnosticsHolder* Diagnostics; DiagnosticsHolder* Diagnostics;
~Script(); ~Script();

View File

@ -18,7 +18,7 @@ public:
5381; 5381;
} }
const int GetHash(){ const int GetHash() const{
return _hash; return _hash;
} }