Skip to content

Commit

Permalink
Merge pull request #1 from Samathingamajig/stop_copying_in_functions
Browse files Browse the repository at this point in the history
Minimized the amount of implicit copying in function headers and varables
  • Loading branch information
Samathingamajig authored Feb 23, 2021
2 parents 3c2b8ac + 7447f47 commit 7b5b393
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 167 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
enc_temp_folder

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down Expand Up @@ -341,4 +342,4 @@ ASALocalRun/
healthchecksdb

# build folder
build
build
2 changes: 1 addition & 1 deletion BarkScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "interpreter/interpreter.h"
#include "context/context.h"

const std::string bsversion = "0.1.4";
const std::string bsversion = "0.1.5";

int main() {
std::cout << "BarkScript version " << bsversion << std::endl;
Expand Down
30 changes: 15 additions & 15 deletions ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Node {
Position positionStart;
Position positionEnd;

std::string virtual to_string() {
std::string virtual to_string() const {
return "Not implemented! " + nodeType;
};

Expand All @@ -55,14 +55,14 @@ struct Node {
};

struct NumberNode : Node {
NumberNode(Token token) {
NumberNode(const Token& token) {
this->nodeType = nodetypes::Number;
this->token = token;
this->positionStart = token.positionStart;
this->positionEnd = token.positionEnd;
}

std::string to_string() override {
std::string to_string() const override {
return token.value;
}

Expand All @@ -72,15 +72,15 @@ struct NumberNode : Node {
};

struct VariableDeclarationNode : Node {
VariableDeclarationNode(Token token, spNode valueNode) {
VariableDeclarationNode(const Token& token, const spNode& valueNode) {
this->nodeType = nodetypes::VariableDeclaration;
this->token = token;
this->valueNode = valueNode;
this->positionStart = token.positionStart;
this->positionEnd = valueNode->positionEnd;
}

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

Expand All @@ -90,15 +90,15 @@ struct VariableDeclarationNode : Node {
};

struct VariableAssignmentNode : Node {
VariableAssignmentNode(Token token, spNode valueNode) {
VariableAssignmentNode(const Token& token, const spNode& valueNode) {
this->nodeType = nodetypes::VariableAssignment;
this->token = token;
this->valueNode = valueNode;
this->positionStart = token.positionStart;
this->positionEnd = valueNode->positionEnd;
}

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

Expand All @@ -108,14 +108,14 @@ struct VariableAssignmentNode : Node {
};

struct VariableRetrievementNode : Node {
VariableRetrievementNode(Token token) {
VariableRetrievementNode(const Token& token) {
this->nodeType = nodetypes::VariableRetrievement;
this->token = token;
this->positionStart = token.positionStart;
this->positionEnd = token.positionEnd;
}

std::string to_string() override {
std::string to_string() const override {
return token.value;
}

Expand All @@ -125,7 +125,7 @@ struct VariableRetrievementNode : Node {
};

struct BinaryOperatorNode : Node {
BinaryOperatorNode(spNode leftNode, Token token, spNode rightNode) {
BinaryOperatorNode(const spNode& leftNode, const Token& token, const spNode& rightNode) {
this->nodeType = nodetypes::BinaryOperator;
this->token = token;
this->leftNode = leftNode;
Expand All @@ -134,7 +134,7 @@ struct BinaryOperatorNode : Node {
this->positionEnd = rightNode->positionEnd;
}

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

Expand All @@ -144,15 +144,15 @@ struct BinaryOperatorNode : Node {
};

struct UnaryOperatorNode : Node {
UnaryOperatorNode(Token token, spNode rightNode) {
UnaryOperatorNode(const Token& token, const spNode& rightNode) {
this->nodeType = nodetypes::UnaryOperator;
this->token = token;
this->rightNode = rightNode;
this->positionStart = token.positionStart;
this->positionEnd = rightNode->positionEnd;
}

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

Expand All @@ -162,12 +162,12 @@ struct UnaryOperatorNode : Node {
};

struct ErrorNode : Node {
ErrorNode(Token token) {
ErrorNode(const Token& token) {
this->nodeType = nodetypes::Error;
this->token = token;
}

std::string to_string() override {
std::string to_string() const override {
return token.value;
}

Expand Down
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
@@ -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
"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
2 changes: 1 addition & 1 deletion context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Context {
Position parentEntryPosition;
spSymbolTable symbolTable;

Context(std::string displayName, spContext parent = nullptr, Position parentEntryPosition = Position()) {
Context(const std::string& displayName, const spContext& parent = nullptr, const Position& parentEntryPosition = Position()) {
this->displayName = displayName;
this->parent = parent;
this->parentEntryPosition = parentEntryPosition;
Expand Down
26 changes: 13 additions & 13 deletions error/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ struct Error {

Error() {}

Error(Position positionStart, Position positionEnd, std::string type, std::string details) {
Error(const Position& positionStart, const Position& positionEnd, const std::string& type, const std::string& details) {
this->positionStart = positionStart;
this->positionEnd = positionEnd;
this->type = type;
this->details = details;
this->isError = true;
}

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

struct IllegalCharError : Error {
IllegalCharError(Position positionStart, Position positionEnd, std::string details) {
IllegalCharError(const Position& positionStart, const Position& positionEnd, const std::string&& details) {
this->positionStart = positionStart;
this->positionEnd = positionEnd;
this->type = errortypes::IllegalCharError;
this->details = details;
this->isError = true;
}

operator spError() {
operator spError() override {
return makeSharedError(*this);
}
};

struct InvalidSyntaxError : Error {
InvalidSyntaxError(Position positionStart, Position positionEnd, std::string details) {
InvalidSyntaxError(const Position& positionStart, const Position& positionEnd, const std::string&& details) {
this->positionStart = positionStart;
this->positionEnd = positionEnd;
this->type = errortypes::InvalidSyntaxError;
this->details = details;
this->isError = true;
}

operator spError() {
operator spError() override {
return makeSharedError(*this);
}
};

struct RuntimeError : Error {
spContext context;

RuntimeError(Position positionStart, Position positionEnd, std::string details, spContext context) {
RuntimeError(const Position& positionStart, const Position& positionEnd, const std::string&& details, const spContext& context) {
this->positionStart = positionStart;
this->positionEnd = positionEnd;
this->type = errortypes::RuntimeError;
this->details = details;
this->context = context;
}

std::string to_string() override {
std::string to_string() const override {
std::string output = generateTraceback();
output += type + ": " + details;
output += "\n\n";
output += strings_with_arrows(positionStart.filetext, positionStart, positionEnd);
return output;
}

std::string generateTraceback() {
std::string generateTraceback() const {
std::string output = "";
Position pos = positionStart;
const Position* pos = &positionStart;
spContext ctx = context;

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

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

operator spError() {
operator spError() override {
return makeSharedError(*this);
}
};
Expand Down
24 changes: 12 additions & 12 deletions interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
#include "../ast/ast.h"
#include "../object/object.h"

bool RuntimeResult::hasError() { return error != nullptr; }
bool RuntimeResult::hasError() const { return error != nullptr; }

spObject RuntimeResult::registerRT(spObject object) {
spObject RuntimeResult::registerRT(const spObject& object) {
return object;
}

spObject RuntimeResult::registerRT(RuntimeResult rt) {
spObject RuntimeResult::registerRT(const RuntimeResult& rt) {
if (rt.hasError()) {
this->error = rt.error;
}
return rt.object;
}

RuntimeResult RuntimeResult::success(spObject object) {
RuntimeResult RuntimeResult::success(const spObject& object) {
this->object = object;
return *this;
}

RuntimeResult RuntimeResult::failure(spError error) {
RuntimeResult RuntimeResult::failure(const spError& error) {
this->error = error;
return *this;
}

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

if (type == nodetypes::Number) {
Expand All @@ -47,14 +47,14 @@ RuntimeResult Interpreter::visit(spNode node, spContext context) {
}
}

RuntimeResult Interpreter::visitNumberNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitNumberNode(const spNode& node, const spContext& context) {
spObject number = Number(node->token.value);
number->setContext(context);
number->setPosition(node->positionStart, node->positionEnd);
return RuntimeResult().success(number);
}

RuntimeResult Interpreter::visitVariableDeclarationNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitVariableDeclarationNode(const spNode& node, const spContext& context) {
RuntimeResult rt;
std::string variableName = node->token.value;
if (context->symbolTable->exists(variableName, false)) {
Expand All @@ -73,7 +73,7 @@ RuntimeResult Interpreter::visitVariableDeclarationNode(spNode node, spContext c
}
}

RuntimeResult Interpreter::visitVariableAssignmentNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitVariableAssignmentNode(const spNode& node, const spContext& context) {
RuntimeResult rt;
std::string variableName = node->token.value;
spObject value = rt.registerRT(visit(node->valueNode, context));
Expand All @@ -88,7 +88,7 @@ RuntimeResult Interpreter::visitVariableAssignmentNode(spNode node, spContext co
}
}

RuntimeResult Interpreter::visitVariableRetrievementNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitVariableRetrievementNode(const spNode& node, const spContext& context) {
RuntimeResult rt;
std::string variableName = node->token.value;
spObject value = context->symbolTable->get(variableName);
Expand All @@ -99,7 +99,7 @@ RuntimeResult Interpreter::visitVariableRetrievementNode(spNode node, spContext
return rt.success(value);
}

RuntimeResult Interpreter::visitBinaryOperatorNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitBinaryOperatorNode(const spNode& node, const spContext& context) {
RuntimeResult rt;
spObject left = rt.registerRT(visit(node->leftNode, context));
if (rt.hasError()) return rt;
Expand Down Expand Up @@ -129,7 +129,7 @@ RuntimeResult Interpreter::visitBinaryOperatorNode(spNode node, spContext contex
return rt.success(result.object);
}

RuntimeResult Interpreter::visitUnaryOperatorNode(spNode node, spContext context) {
RuntimeResult Interpreter::visitUnaryOperatorNode(const spNode& node, const spContext& context) {
RuntimeResult rt;
spObject object = rt.registerRT(visit(node->rightNode, context));
if (rt.hasError()) return rt;
Expand Down
24 changes: 12 additions & 12 deletions interpreter/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ struct RuntimeResult {
spError error = nullptr;
spObject object = nullptr;

bool hasError();
spObject registerRT(spObject object);
spObject registerRT(RuntimeResult rt);
bool hasError() const;
spObject registerRT(const spObject& object);
spObject registerRT(const RuntimeResult& rt);

RuntimeResult success(spObject object);
RuntimeResult failure(spError error);
RuntimeResult success(const spObject& object);
RuntimeResult failure(const spError& error);
};

struct Interpreter {
RuntimeResult visit(spNode node, spContext context);
RuntimeResult visit(const spNode& node, const spContext& context);

RuntimeResult visitNumberNode(spNode node, spContext context);
RuntimeResult visitVariableDeclarationNode(spNode node, spContext context);
RuntimeResult visitVariableAssignmentNode(spNode node, spContext context);
RuntimeResult visitVariableRetrievementNode(spNode node, spContext context);
RuntimeResult visitBinaryOperatorNode(spNode node, spContext context);
RuntimeResult visitUnaryOperatorNode(spNode node, spContext context);
RuntimeResult visitNumberNode(const spNode& node, const spContext& context);
RuntimeResult visitVariableDeclarationNode(const spNode& node, const spContext& context);
RuntimeResult visitVariableAssignmentNode(const spNode& node, const spContext& context);
RuntimeResult visitVariableRetrievementNode(const spNode& node, const spContext& context);
RuntimeResult visitBinaryOperatorNode(const spNode& node, const spContext& context);
RuntimeResult visitUnaryOperatorNode(const spNode& node, const spContext& context);
};

#endif // !INTERPRETER_H
Loading

0 comments on commit 7b5b393

Please sign in to comment.