Skip to content

Commit 7b5b393

Browse files
Merge pull request #1 from Samathingamajig/stop_copying_in_functions
Minimized the amount of implicit copying in function headers and varables
2 parents 3c2b8ac + 7447f47 commit 7b5b393

20 files changed

Lines changed: 161 additions & 167 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ bld/
2626
[Bb]in/
2727
[Oo]bj/
2828
[Ll]og/
29+
enc_temp_folder
2930

3031
# Visual Studio 2015/2017 cache/options directory
3132
.vs/
@@ -341,4 +342,4 @@ ASALocalRun/
341342
healthchecksdb
342343

343344
# build folder
344-
build
345+
build

BarkScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "interpreter/interpreter.h"
99
#include "context/context.h"
1010

11-
const std::string bsversion = "0.1.4";
11+
const std::string bsversion = "0.1.5";
1212

1313
int main() {
1414
std::cout << "BarkScript version " << bsversion << std::endl;

ast/ast.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct Node {
3737
Position positionStart;
3838
Position positionEnd;
3939

40-
std::string virtual to_string() {
40+
std::string virtual to_string() const {
4141
return "Not implemented! " + nodeType;
4242
};
4343

@@ -55,14 +55,14 @@ struct Node {
5555
};
5656

5757
struct NumberNode : Node {
58-
NumberNode(Token token) {
58+
NumberNode(const Token& token) {
5959
this->nodeType = nodetypes::Number;
6060
this->token = token;
6161
this->positionStart = token.positionStart;
6262
this->positionEnd = token.positionEnd;
6363
}
6464

65-
std::string to_string() override {
65+
std::string to_string() const override {
6666
return token.value;
6767
}
6868

@@ -72,15 +72,15 @@ struct NumberNode : Node {
7272
};
7373

7474
struct VariableDeclarationNode : Node {
75-
VariableDeclarationNode(Token token, spNode valueNode) {
75+
VariableDeclarationNode(const Token& token, const spNode& valueNode) {
7676
this->nodeType = nodetypes::VariableDeclaration;
7777
this->token = token;
7878
this->valueNode = valueNode;
7979
this->positionStart = token.positionStart;
8080
this->positionEnd = valueNode->positionEnd;
8181
}
8282

83-
std::string to_string() override {
83+
std::string to_string() const override {
8484
return "(LET, identifier:\"" + token.value + "\", EQUAL, " + valueNode->to_string() + ")";
8585
}
8686

@@ -90,15 +90,15 @@ struct VariableDeclarationNode : Node {
9090
};
9191

9292
struct VariableAssignmentNode : Node {
93-
VariableAssignmentNode(Token token, spNode valueNode) {
93+
VariableAssignmentNode(const Token& token, const spNode& valueNode) {
9494
this->nodeType = nodetypes::VariableAssignment;
9595
this->token = token;
9696
this->valueNode = valueNode;
9797
this->positionStart = token.positionStart;
9898
this->positionEnd = valueNode->positionEnd;
9999
}
100100

101-
std::string to_string() override {
101+
std::string to_string() const override {
102102
return "(identifier:\"" + token.value + "\", EQUAL, " + valueNode->to_string() + ")";
103103
}
104104

@@ -108,14 +108,14 @@ struct VariableAssignmentNode : Node {
108108
};
109109

110110
struct VariableRetrievementNode : Node {
111-
VariableRetrievementNode(Token token) {
111+
VariableRetrievementNode(const Token& token) {
112112
this->nodeType = nodetypes::VariableRetrievement;
113113
this->token = token;
114114
this->positionStart = token.positionStart;
115115
this->positionEnd = token.positionEnd;
116116
}
117117

118-
std::string to_string() override {
118+
std::string to_string() const override {
119119
return token.value;
120120
}
121121

@@ -125,7 +125,7 @@ struct VariableRetrievementNode : Node {
125125
};
126126

127127
struct BinaryOperatorNode : Node {
128-
BinaryOperatorNode(spNode leftNode, Token token, spNode rightNode) {
128+
BinaryOperatorNode(const spNode& leftNode, const Token& token, const spNode& rightNode) {
129129
this->nodeType = nodetypes::BinaryOperator;
130130
this->token = token;
131131
this->leftNode = leftNode;
@@ -134,7 +134,7 @@ struct BinaryOperatorNode : Node {
134134
this->positionEnd = rightNode->positionEnd;
135135
}
136136

137-
std::string to_string() override {
137+
std::string to_string() const override {
138138
return "(" + leftNode->to_string() + ", " + token.type + ", " + rightNode->to_string() + ")";
139139
}
140140

@@ -144,15 +144,15 @@ struct BinaryOperatorNode : Node {
144144
};
145145

146146
struct UnaryOperatorNode : Node {
147-
UnaryOperatorNode(Token token, spNode rightNode) {
147+
UnaryOperatorNode(const Token& token, const spNode& rightNode) {
148148
this->nodeType = nodetypes::UnaryOperator;
149149
this->token = token;
150150
this->rightNode = rightNode;
151151
this->positionStart = token.positionStart;
152152
this->positionEnd = rightNode->positionEnd;
153153
}
154154

155-
std::string to_string() override {
155+
std::string to_string() const override {
156156
return "(" + token.type + ", " + rightNode->to_string() + ")";
157157
}
158158

@@ -162,12 +162,12 @@ struct UnaryOperatorNode : Node {
162162
};
163163

164164
struct ErrorNode : Node {
165-
ErrorNode(Token token) {
165+
ErrorNode(const Token& token) {
166166
this->nodeType = nodetypes::Error;
167167
this->token = token;
168168
}
169169

170-
std::string to_string() override {
170+
std::string to_string() const override {
171171
return token.value;
172172
}
173173

build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat" && cl.exe /EHsc BarkScript.cpp lexer/Lexer.cpp parser/Parser.cpp object/Object.cpp interpreter/Interpreter.cpp symboltable/SymbolTable.cpp /link /out:build/BarkScript.exe
1+
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat" && cl.exe /O2 /EHsc BarkScript.cpp lexer/Lexer.cpp parser/Parser.cpp object/Object.cpp interpreter/Interpreter.cpp symboltable/SymbolTable.cpp /link /out:build/BarkScript.exe

context/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Context {
1616
Position parentEntryPosition;
1717
spSymbolTable symbolTable;
1818

19-
Context(std::string displayName, spContext parent = nullptr, Position parentEntryPosition = Position()) {
19+
Context(const std::string& displayName, const spContext& parent = nullptr, const Position& parentEntryPosition = Position()) {
2020
this->displayName = displayName;
2121
this->parent = parent;
2222
this->parentEntryPosition = parentEntryPosition;

error/error.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ struct Error {
3535

3636
Error() {}
3737

38-
Error(Position positionStart, Position positionEnd, std::string type, std::string details) {
38+
Error(const Position& positionStart, const Position& positionEnd, const std::string& type, const std::string& details) {
3939
this->positionStart = positionStart;
4040
this->positionEnd = positionEnd;
4141
this->type = type;
4242
this->details = details;
4343
this->isError = true;
4444
}
4545

46-
std::string virtual to_string() {
46+
std::string virtual to_string() const {
4747
std::string output = type + ": " + details + '\n';
4848
output += "File \"" + positionStart.filename + "\", line " + std::to_string(positionStart.lineNumber + 1);
4949
output += "\n\n";
@@ -59,67 +59,67 @@ struct Error {
5959
};
6060

6161
struct IllegalCharError : Error {
62-
IllegalCharError(Position positionStart, Position positionEnd, std::string details) {
62+
IllegalCharError(const Position& positionStart, const Position& positionEnd, const std::string&& details) {
6363
this->positionStart = positionStart;
6464
this->positionEnd = positionEnd;
6565
this->type = errortypes::IllegalCharError;
6666
this->details = details;
6767
this->isError = true;
6868
}
6969

70-
operator spError() {
70+
operator spError() override {
7171
return makeSharedError(*this);
7272
}
7373
};
7474

7575
struct InvalidSyntaxError : Error {
76-
InvalidSyntaxError(Position positionStart, Position positionEnd, std::string details) {
76+
InvalidSyntaxError(const Position& positionStart, const Position& positionEnd, const std::string&& details) {
7777
this->positionStart = positionStart;
7878
this->positionEnd = positionEnd;
7979
this->type = errortypes::InvalidSyntaxError;
8080
this->details = details;
8181
this->isError = true;
8282
}
8383

84-
operator spError() {
84+
operator spError() override {
8585
return makeSharedError(*this);
8686
}
8787
};
8888

8989
struct RuntimeError : Error {
9090
spContext context;
9191

92-
RuntimeError(Position positionStart, Position positionEnd, std::string details, spContext context) {
92+
RuntimeError(const Position& positionStart, const Position& positionEnd, const std::string&& details, const spContext& context) {
9393
this->positionStart = positionStart;
9494
this->positionEnd = positionEnd;
9595
this->type = errortypes::RuntimeError;
9696
this->details = details;
9797
this->context = context;
9898
}
9999

100-
std::string to_string() override {
100+
std::string to_string() const override {
101101
std::string output = generateTraceback();
102102
output += type + ": " + details;
103103
output += "\n\n";
104104
output += strings_with_arrows(positionStart.filetext, positionStart, positionEnd);
105105
return output;
106106
}
107107

108-
std::string generateTraceback() {
108+
std::string generateTraceback() const {
109109
std::string output = "";
110-
Position pos = positionStart;
110+
const Position* pos = &positionStart;
111111
spContext ctx = context;
112112

113113
while (ctx != nullptr) {
114-
output = " File \"" + pos.filename + "\", line " + std::to_string(pos.lineNumber) + ", in \"" + ctx->displayName + "\"\n" + output;
115-
pos = ctx->parentEntryPosition;
114+
output = " File \"" + pos->filename + "\", line " + std::to_string(pos->lineNumber) + ", in \"" + ctx->displayName + "\"\n" + output;
115+
pos = &ctx->parentEntryPosition;
116116
ctx = ctx->parent;
117117
}
118118

119119
return "Traceback (most recent call last):\n" + output;
120120
}
121121

122-
operator spError() {
122+
operator spError() override {
123123
return makeSharedError(*this);
124124
}
125125
};

interpreter/Interpreter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44
#include "../ast/ast.h"
55
#include "../object/object.h"
66

7-
bool RuntimeResult::hasError() { return error != nullptr; }
7+
bool RuntimeResult::hasError() const { return error != nullptr; }
88

9-
spObject RuntimeResult::registerRT(spObject object) {
9+
spObject RuntimeResult::registerRT(const spObject& object) {
1010
return object;
1111
}
1212

13-
spObject RuntimeResult::registerRT(RuntimeResult rt) {
13+
spObject RuntimeResult::registerRT(const RuntimeResult& rt) {
1414
if (rt.hasError()) {
1515
this->error = rt.error;
1616
}
1717
return rt.object;
1818
}
1919

20-
RuntimeResult RuntimeResult::success(spObject object) {
20+
RuntimeResult RuntimeResult::success(const spObject& object) {
2121
this->object = object;
2222
return *this;
2323
}
2424

25-
RuntimeResult RuntimeResult::failure(spError error) {
25+
RuntimeResult RuntimeResult::failure(const spError& error) {
2626
this->error = error;
2727
return *this;
2828
}
2929

30-
RuntimeResult Interpreter::visit(spNode node, spContext context) {
30+
RuntimeResult Interpreter::visit(const spNode& node, const spContext& context) {
3131
std::string type = node->nodeType;
3232

3333
if (type == nodetypes::Number) {
@@ -47,14 +47,14 @@ RuntimeResult Interpreter::visit(spNode node, spContext context) {
4747
}
4848
}
4949

50-
RuntimeResult Interpreter::visitNumberNode(spNode node, spContext context) {
50+
RuntimeResult Interpreter::visitNumberNode(const spNode& node, const spContext& context) {
5151
spObject number = Number(node->token.value);
5252
number->setContext(context);
5353
number->setPosition(node->positionStart, node->positionEnd);
5454
return RuntimeResult().success(number);
5555
}
5656

57-
RuntimeResult Interpreter::visitVariableDeclarationNode(spNode node, spContext context) {
57+
RuntimeResult Interpreter::visitVariableDeclarationNode(const spNode& node, const spContext& context) {
5858
RuntimeResult rt;
5959
std::string variableName = node->token.value;
6060
if (context->symbolTable->exists(variableName, false)) {
@@ -73,7 +73,7 @@ RuntimeResult Interpreter::visitVariableDeclarationNode(spNode node, spContext c
7373
}
7474
}
7575

76-
RuntimeResult Interpreter::visitVariableAssignmentNode(spNode node, spContext context) {
76+
RuntimeResult Interpreter::visitVariableAssignmentNode(const spNode& node, const spContext& context) {
7777
RuntimeResult rt;
7878
std::string variableName = node->token.value;
7979
spObject value = rt.registerRT(visit(node->valueNode, context));
@@ -88,7 +88,7 @@ RuntimeResult Interpreter::visitVariableAssignmentNode(spNode node, spContext co
8888
}
8989
}
9090

91-
RuntimeResult Interpreter::visitVariableRetrievementNode(spNode node, spContext context) {
91+
RuntimeResult Interpreter::visitVariableRetrievementNode(const spNode& node, const spContext& context) {
9292
RuntimeResult rt;
9393
std::string variableName = node->token.value;
9494
spObject value = context->symbolTable->get(variableName);
@@ -99,7 +99,7 @@ RuntimeResult Interpreter::visitVariableRetrievementNode(spNode node, spContext
9999
return rt.success(value);
100100
}
101101

102-
RuntimeResult Interpreter::visitBinaryOperatorNode(spNode node, spContext context) {
102+
RuntimeResult Interpreter::visitBinaryOperatorNode(const spNode& node, const spContext& context) {
103103
RuntimeResult rt;
104104
spObject left = rt.registerRT(visit(node->leftNode, context));
105105
if (rt.hasError()) return rt;
@@ -129,7 +129,7 @@ RuntimeResult Interpreter::visitBinaryOperatorNode(spNode node, spContext contex
129129
return rt.success(result.object);
130130
}
131131

132-
RuntimeResult Interpreter::visitUnaryOperatorNode(spNode node, spContext context) {
132+
RuntimeResult Interpreter::visitUnaryOperatorNode(const spNode& node, const spContext& context) {
133133
RuntimeResult rt;
134134
spObject object = rt.registerRT(visit(node->rightNode, context));
135135
if (rt.hasError()) return rt;

interpreter/interpreter.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ struct RuntimeResult {
1313
spError error = nullptr;
1414
spObject object = nullptr;
1515

16-
bool hasError();
17-
spObject registerRT(spObject object);
18-
spObject registerRT(RuntimeResult rt);
16+
bool hasError() const;
17+
spObject registerRT(const spObject& object);
18+
spObject registerRT(const RuntimeResult& rt);
1919

20-
RuntimeResult success(spObject object);
21-
RuntimeResult failure(spError error);
20+
RuntimeResult success(const spObject& object);
21+
RuntimeResult failure(const spError& error);
2222
};
2323

2424
struct Interpreter {
25-
RuntimeResult visit(spNode node, spContext context);
25+
RuntimeResult visit(const spNode& node, const spContext& context);
2626

27-
RuntimeResult visitNumberNode(spNode node, spContext context);
28-
RuntimeResult visitVariableDeclarationNode(spNode node, spContext context);
29-
RuntimeResult visitVariableAssignmentNode(spNode node, spContext context);
30-
RuntimeResult visitVariableRetrievementNode(spNode node, spContext context);
31-
RuntimeResult visitBinaryOperatorNode(spNode node, spContext context);
32-
RuntimeResult visitUnaryOperatorNode(spNode node, spContext context);
27+
RuntimeResult visitNumberNode(const spNode& node, const spContext& context);
28+
RuntimeResult visitVariableDeclarationNode(const spNode& node, const spContext& context);
29+
RuntimeResult visitVariableAssignmentNode(const spNode& node, const spContext& context);
30+
RuntimeResult visitVariableRetrievementNode(const spNode& node, const spContext& context);
31+
RuntimeResult visitBinaryOperatorNode(const spNode& node, const spContext& context);
32+
RuntimeResult visitUnaryOperatorNode(const spNode& node, const spContext& context);
3333
};
3434

3535
#endif // !INTERPRETER_H

0 commit comments

Comments
 (0)