Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 83 additions & 63 deletions interpreter/CodeInterpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,94 @@
#include "CodeInterpreter.h"

#include <iostream>
#include <sstream>

#include "CodeInterpreter.h"
#include "src/GlobalScope.h"
void CodeInterpreter::_bind_methods()
{
ClassDB::bind_method(D_METHOD("registerBuiltInMethod"), &CodeInterpreter::registerBuiltInMethod);
ClassDB::bind_method(D_METHOD("interpret"), &CodeInterpreter::interpret);
ClassDB::bind_method(D_METHOD("getVariable"), &CodeInterpreter::getVariable);
ClassDB::bind_method(D_METHOD("getSequence"), &CodeInterpreter::getSequence);
}

void CodeInterpreter::registerBuiltInMethod(const String& str)
{
const std::string text{str.utf8().get_data()};

void CodeInterpreter::_bind_methods() {
ClassDB::bind_method(D_METHOD("interpret"), &CodeInterpreter::interpret);
ClassDB::bind_method(D_METHOD("getVariable"), &CodeInterpreter::getVariable);
ClassDB::bind_method(D_METHOD("getGlobalScope"), &CodeInterpreter::getGlobalScope);
interpreter.registerBuiltInMethod(text);
}

void CodeInterpreter::interpret(const String& str) {
std::string text{str.utf8().get_data()};
std::shared_ptr<AstNode> result = Interpreter::interpret(text);
String CodeInterpreter::interpret(const String& str)
{
try {
const std::string text{str.utf8().get_data()};

interpreter.symbolTable().init();
interpreter.initParser(text);

interpreter.registerBuiltInMethod("moveNorth()");
interpreter.registerBuiltInMethod("moveSouth()");
interpreter.registerBuiltInMethod("moveEast()");
interpreter.registerBuiltInMethod("moveWest()");
interpreter.registerBuiltInMethod("rotateLeft()");
interpreter.registerBuiltInMethod("rotateRight()");
interpreter.registerBuiltInMethod("push()");

std::shared_ptr<AstNode> tree = interpreter.buildTree(text);

st::SymbolTable& stRef = interpreter.symbolTable();
SymbolTableBuilder stb;
stb.build(tree, stRef);

std::shared_ptr<AstNode> result = interpreter.interpret(tree);

return String();
} catch (const std::exception& e) {
return String(e.what());
}
}

String CodeInterpreter::getVariable(const String& str) {
std::string text{str.utf8().get_data()};
std::stringstream ssOut;

try {
std::variant<int, float, bool> value = GLOBAL_SCOPE.at(text); // TODO: Handle exception

if (std::holds_alternative<int>(value)) {
int intValue = std::get<int>(value);
std::cout << "Int value" << intValue << std::endl;
ssOut << intValue;
} else if (std::holds_alternative<float>(value)) {
float floatValue = std::get<float>(value);
std::cout << "Float value" << floatValue << std::endl;
ssOut << floatValue;
} else if (std::holds_alternative<bool>(value)) {
bool boolValue = std::get<bool>(value);
std::cout << "Bool value" << boolValue << std::endl;
ssOut << boolValue;
} else {
std::cout << "Unknown variant" << std::endl;
ssOut << "Unknown variant";
}

std::string sOut = ssOut.str();
return String(sOut.c_str());


} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
return String("Error");
}

String CodeInterpreter::getVariable(const String& str)
{
const std::string variableName{str.utf8().get_data()};
std::stringstream ssOut;

try {
std::variant<int, float, bool> value = interpreter.getVariableVariant(variableName);

if (std::holds_alternative<int>(value)) {
int intValue = std::get<int>(value);
std::cout << "Int value" << intValue << std::endl;
ssOut << intValue;
} else if (std::holds_alternative<float>(value)) {
float floatValue = std::get<float>(value);
std::cout << "Float value" << floatValue << std::endl;
ssOut << floatValue;
} else if (std::holds_alternative<bool>(value)) {
bool boolValue = std::get<bool>(value);
std::cout << "Bool value" << boolValue << std::endl;
ssOut << boolValue;
} else {
std::cout << "Unknown variant" << std::endl;
ssOut << "Unknown variant";
}

std::string sOut = ssOut.str();
return String(sOut.c_str());

} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
return String("Error");
}
}

Dictionary CodeInterpreter::getGlobalScope() {

Dictionary dict{};

for (auto const& [key, value] : GLOBAL_SCOPE) {
String strKey = String(key.c_str());

if (std::holds_alternative<int>(value)) {
dict[strKey] = std::get<int>(value);
} else if (std::holds_alternative<float>(value)) {
dict[strKey] = std::get<float>(value);
} else if (std::holds_alternative<bool>(value)) {
dict[strKey] = std::get<bool>(value);
} else {
std::cout << "Unknown variant" << std::endl;
}
}

return dict;

}
Array CodeInterpreter::getSequence()
{
Array array;
std::vector<std::string> seq = interpreter.getSequence();
for (const auto& instruction : seq) {
array.append(String(instruction.c_str()));
}

return array;
}
17 changes: 11 additions & 6 deletions interpreter/CodeInterpreter.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#pragma once

#include "src/Interpreter.h"
#include "core/object/ref_counted.h"
#include "core/variant/dictionary.h"
#include "src/Interpreter.h"

class CodeInterpreter : public RefCounted, public Interpreter {
class CodeInterpreter : public RefCounted, public Interpreter
{
GDCLASS(CodeInterpreter, RefCounted);

protected:
static void _bind_methods();

public:
void interpret(const String& str);
String getVariable(const String& str);
Dictionary getGlobalScope();

void registerBuiltInMethod(const String& str);
String interpret(const String& str);
String getVariable(const String& str);
Array getSequence();

private:
Interpreter interpreter;
};
6 changes: 4 additions & 2 deletions interpreter/grammar.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
section : START statementList END

statement : section | assignmentStatement | ifStatement
| variableDeclaration | functionDeclaration | functionCall
| whileLoop | forLoop | empty
| variableDeclaration | functionDeclaration | builtInFunction
| functionCall | whileLoop | forLoop | empty

statementList : statement | statement SEMI statementList

Expand All @@ -20,6 +20,8 @@
variableDeclaration : (TYPE | auto) ID (ASSIGN expression)?

variable : ID

builtInFunction : builtInFunction

functionDeclaration : FUN ID LPAREN RPAREN COLON section

Expand Down
12 changes: 12 additions & 0 deletions interpreter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ int main(int argc, char* argv[])
}

Interpreter interpreter;

interpreter.symbolTable().init();
interpreter.initParser(input);

interpreter.registerBuiltInMethod("moveNorth()");
interpreter.registerBuiltInMethod("moveSouth()");
interpreter.registerBuiltInMethod("moveEast()");
interpreter.registerBuiltInMethod("moveWest()");
interpreter.registerBuiltInMethod("rotateLeft()");
interpreter.registerBuiltInMethod("rotateRight()");
interpreter.registerBuiltInMethod("push()");

std::shared_ptr<AstNode> tree = interpreter.buildTree(input);

st::SymbolTable& stRef = interpreter.symbolTable();
Expand Down
10 changes: 10 additions & 0 deletions interpreter/src/Ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ NodeVariant getVariant(const std::shared_ptr<AstNode>& node)

return NodeVariant(ForLoop(*forAst));
}
case NodeType::BUILT_IN_FUNCTION: {
auto builtInFunction = std::dynamic_pointer_cast<BuiltInFunction>(node);
if (!builtInFunction) {
throw std::runtime_error("Casting to BuiltInFunction failed");
}

return NodeVariant(BuiltInFunction(*builtInFunction));
}
}

throw std::runtime_error("Unknown NodeType");
Expand Down Expand Up @@ -141,6 +149,8 @@ std::string getTypeString(NodeType nt)
return "WHILE";
case NodeType::FOR:
return "FOR";
case NodeType::BUILT_IN_FUNCTION:
return "BUILT_IN_FUNCTION";
}

throw std::runtime_error("Unknown NodeType");
Expand Down
21 changes: 20 additions & 1 deletion interpreter/src/Ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class VariableDeclaration;
class Variable;
class EmptyNode;
class FunDeclaration;
class BuiltInFunction;
class FunCall;
class IfStatement;
class WhileLoop;
Expand All @@ -30,6 +31,7 @@ using NodeVariant = std::variant<Number,
Variable,
EmptyNode,
FunDeclaration,
BuiltInFunction,
FunCall,
IfStatement,
WhileLoop,
Expand All @@ -51,7 +53,8 @@ enum class NodeType : uint8_t
FUN_CALL = 9,
IF = 10,
WHILE = 11,
FOR = 12
FOR = 12,
BUILT_IN_FUNCTION = 13
};

class Token;
Expand Down Expand Up @@ -325,6 +328,22 @@ class ForLoop : public AstNode
std::shared_ptr<AstNode> _body;
};

class BuiltInFunction : public AstNode
{
public:
BuiltInFunction(const std::string& name) :
_name(name)
{
}

NodeType nodeType() const override { return NodeType::BUILT_IN_FUNCTION; }

const std::string& name() const { return _name; }

private:
std::string _name;
};

NodeVariant getVariant(const std::shared_ptr<AstNode>& node);

std::string getTypeString(NodeType nt);
Expand Down
Loading