Skip to content

Commit 87ed8e4

Browse files
authored
Merge pull request #4 from Digit16/wkubski/interpreter
Wkubski/interpreter
2 parents fb7d958 + 55d2499 commit 87ed8e4

File tree

14 files changed

+326
-184
lines changed

14 files changed

+326
-184
lines changed

interpreter/CodeInterpreter.cpp

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,94 @@
1+
#include "CodeInterpreter.h"
2+
13
#include <iostream>
24
#include <sstream>
35

4-
#include "CodeInterpreter.h"
5-
#include "src/GlobalScope.h"
6+
void CodeInterpreter::_bind_methods()
7+
{
8+
ClassDB::bind_method(D_METHOD("registerBuiltInMethod"), &CodeInterpreter::registerBuiltInMethod);
9+
ClassDB::bind_method(D_METHOD("interpret"), &CodeInterpreter::interpret);
10+
ClassDB::bind_method(D_METHOD("getVariable"), &CodeInterpreter::getVariable);
11+
ClassDB::bind_method(D_METHOD("getSequence"), &CodeInterpreter::getSequence);
12+
}
613

14+
void CodeInterpreter::registerBuiltInMethod(const String& str)
15+
{
16+
const std::string text{str.utf8().get_data()};
717

8-
void CodeInterpreter::_bind_methods() {
9-
ClassDB::bind_method(D_METHOD("interpret"), &CodeInterpreter::interpret);
10-
ClassDB::bind_method(D_METHOD("getVariable"), &CodeInterpreter::getVariable);
11-
ClassDB::bind_method(D_METHOD("getGlobalScope"), &CodeInterpreter::getGlobalScope);
18+
interpreter.registerBuiltInMethod(text);
1219
}
1320

14-
void CodeInterpreter::interpret(const String& str) {
15-
std::string text{str.utf8().get_data()};
16-
std::shared_ptr<AstNode> result = Interpreter::interpret(text);
21+
String CodeInterpreter::interpret(const String& str)
22+
{
23+
try {
24+
const std::string text{str.utf8().get_data()};
25+
26+
interpreter.symbolTable().init();
27+
interpreter.initParser(text);
28+
29+
interpreter.registerBuiltInMethod("moveNorth()");
30+
interpreter.registerBuiltInMethod("moveSouth()");
31+
interpreter.registerBuiltInMethod("moveEast()");
32+
interpreter.registerBuiltInMethod("moveWest()");
33+
interpreter.registerBuiltInMethod("rotateLeft()");
34+
interpreter.registerBuiltInMethod("rotateRight()");
35+
interpreter.registerBuiltInMethod("push()");
36+
37+
std::shared_ptr<AstNode> tree = interpreter.buildTree(text);
38+
39+
st::SymbolTable& stRef = interpreter.symbolTable();
40+
SymbolTableBuilder stb;
41+
stb.build(tree, stRef);
42+
43+
std::shared_ptr<AstNode> result = interpreter.interpret(tree);
44+
45+
return String();
46+
} catch (const std::exception& e) {
47+
return String(e.what());
48+
}
1749
}
1850

19-
String CodeInterpreter::getVariable(const String& str) {
20-
std::string text{str.utf8().get_data()};
21-
std::stringstream ssOut;
22-
23-
try {
24-
std::variant<int, float, bool> value = GLOBAL_SCOPE.at(text); // TODO: Handle exception
25-
26-
if (std::holds_alternative<int>(value)) {
27-
int intValue = std::get<int>(value);
28-
std::cout << "Int value" << intValue << std::endl;
29-
ssOut << intValue;
30-
} else if (std::holds_alternative<float>(value)) {
31-
float floatValue = std::get<float>(value);
32-
std::cout << "Float value" << floatValue << std::endl;
33-
ssOut << floatValue;
34-
} else if (std::holds_alternative<bool>(value)) {
35-
bool boolValue = std::get<bool>(value);
36-
std::cout << "Bool value" << boolValue << std::endl;
37-
ssOut << boolValue;
38-
} else {
39-
std::cout << "Unknown variant" << std::endl;
40-
ssOut << "Unknown variant";
41-
}
42-
43-
std::string sOut = ssOut.str();
44-
return String(sOut.c_str());
45-
46-
47-
} catch (const std::exception& e) {
48-
std::cout << e.what() << std::endl;
49-
return String("Error");
50-
}
51-
51+
String CodeInterpreter::getVariable(const String& str)
52+
{
53+
const std::string variableName{str.utf8().get_data()};
54+
std::stringstream ssOut;
55+
56+
try {
57+
std::variant<int, float, bool> value = interpreter.getVariableVariant(variableName);
58+
59+
if (std::holds_alternative<int>(value)) {
60+
int intValue = std::get<int>(value);
61+
std::cout << "Int value" << intValue << std::endl;
62+
ssOut << intValue;
63+
} else if (std::holds_alternative<float>(value)) {
64+
float floatValue = std::get<float>(value);
65+
std::cout << "Float value" << floatValue << std::endl;
66+
ssOut << floatValue;
67+
} else if (std::holds_alternative<bool>(value)) {
68+
bool boolValue = std::get<bool>(value);
69+
std::cout << "Bool value" << boolValue << std::endl;
70+
ssOut << boolValue;
71+
} else {
72+
std::cout << "Unknown variant" << std::endl;
73+
ssOut << "Unknown variant";
74+
}
75+
76+
std::string sOut = ssOut.str();
77+
return String(sOut.c_str());
78+
79+
} catch (const std::exception& e) {
80+
std::cout << e.what() << std::endl;
81+
return String("Error");
82+
}
5283
}
5384

54-
Dictionary CodeInterpreter::getGlobalScope() {
55-
56-
Dictionary dict{};
57-
58-
for (auto const& [key, value] : GLOBAL_SCOPE) {
59-
String strKey = String(key.c_str());
60-
61-
if (std::holds_alternative<int>(value)) {
62-
dict[strKey] = std::get<int>(value);
63-
} else if (std::holds_alternative<float>(value)) {
64-
dict[strKey] = std::get<float>(value);
65-
} else if (std::holds_alternative<bool>(value)) {
66-
dict[strKey] = std::get<bool>(value);
67-
} else {
68-
std::cout << "Unknown variant" << std::endl;
69-
}
70-
}
71-
72-
return dict;
73-
74-
}
85+
Array CodeInterpreter::getSequence()
86+
{
87+
Array array;
88+
std::vector<std::string> seq = interpreter.getSequence();
89+
for (const auto& instruction : seq) {
90+
array.append(String(instruction.c_str()));
91+
}
92+
93+
return array;
94+
}

interpreter/CodeInterpreter.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#pragma once
22

3-
#include "src/Interpreter.h"
43
#include "core/object/ref_counted.h"
54
#include "core/variant/dictionary.h"
5+
#include "src/Interpreter.h"
66

7-
class CodeInterpreter : public RefCounted, public Interpreter {
7+
class CodeInterpreter : public RefCounted, public Interpreter
8+
{
89
GDCLASS(CodeInterpreter, RefCounted);
910

1011
protected:
1112
static void _bind_methods();
13+
1214
public:
13-
void interpret(const String& str);
14-
String getVariable(const String& str);
15-
Dictionary getGlobalScope();
16-
15+
void registerBuiltInMethod(const String& str);
16+
String interpret(const String& str);
17+
String getVariable(const String& str);
18+
Array getSequence();
19+
20+
private:
21+
Interpreter interpreter;
1722
};

interpreter/grammar.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
section : START statementList END
1111

1212
statement : section | assignmentStatement | ifStatement
13-
| variableDeclaration | functionDeclaration | functionCall
14-
| whileLoop | forLoop | empty
13+
| variableDeclaration | functionDeclaration | builtInFunction
14+
| functionCall | whileLoop | forLoop | empty
1515

1616
statementList : statement | statement SEMI statementList
1717

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

2222
variable : ID
23+
24+
builtInFunction : builtInFunction
2325

2426
functionDeclaration : FUN ID LPAREN RPAREN COLON section
2527

interpreter/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ int main(int argc, char* argv[])
5050
}
5151

5252
Interpreter interpreter;
53+
54+
interpreter.symbolTable().init();
55+
interpreter.initParser(input);
56+
57+
interpreter.registerBuiltInMethod("moveNorth()");
58+
interpreter.registerBuiltInMethod("moveSouth()");
59+
interpreter.registerBuiltInMethod("moveEast()");
60+
interpreter.registerBuiltInMethod("moveWest()");
61+
interpreter.registerBuiltInMethod("rotateLeft()");
62+
interpreter.registerBuiltInMethod("rotateRight()");
63+
interpreter.registerBuiltInMethod("push()");
64+
5365
std::shared_ptr<AstNode> tree = interpreter.buildTree(input);
5466

5567
st::SymbolTable& stRef = interpreter.symbolTable();

interpreter/src/Ast.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ NodeVariant getVariant(const std::shared_ptr<AstNode>& node)
107107

108108
return NodeVariant(ForLoop(*forAst));
109109
}
110+
case NodeType::BUILT_IN_FUNCTION: {
111+
auto builtInFunction = std::dynamic_pointer_cast<BuiltInFunction>(node);
112+
if (!builtInFunction) {
113+
throw std::runtime_error("Casting to BuiltInFunction failed");
114+
}
115+
116+
return NodeVariant(BuiltInFunction(*builtInFunction));
117+
}
110118
}
111119

112120
throw std::runtime_error("Unknown NodeType");
@@ -141,6 +149,8 @@ std::string getTypeString(NodeType nt)
141149
return "WHILE";
142150
case NodeType::FOR:
143151
return "FOR";
152+
case NodeType::BUILT_IN_FUNCTION:
153+
return "BUILT_IN_FUNCTION";
144154
}
145155

146156
throw std::runtime_error("Unknown NodeType");

interpreter/src/Ast.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class VariableDeclaration;
1616
class Variable;
1717
class EmptyNode;
1818
class FunDeclaration;
19+
class BuiltInFunction;
1920
class FunCall;
2021
class IfStatement;
2122
class WhileLoop;
@@ -30,6 +31,7 @@ using NodeVariant = std::variant<Number,
3031
Variable,
3132
EmptyNode,
3233
FunDeclaration,
34+
BuiltInFunction,
3335
FunCall,
3436
IfStatement,
3537
WhileLoop,
@@ -51,7 +53,8 @@ enum class NodeType : uint8_t
5153
FUN_CALL = 9,
5254
IF = 10,
5355
WHILE = 11,
54-
FOR = 12
56+
FOR = 12,
57+
BUILT_IN_FUNCTION = 13
5558
};
5659

5760
class Token;
@@ -325,6 +328,22 @@ class ForLoop : public AstNode
325328
std::shared_ptr<AstNode> _body;
326329
};
327330

331+
class BuiltInFunction : public AstNode
332+
{
333+
public:
334+
BuiltInFunction(const std::string& name) :
335+
_name(name)
336+
{
337+
}
338+
339+
NodeType nodeType() const override { return NodeType::BUILT_IN_FUNCTION; }
340+
341+
const std::string& name() const { return _name; }
342+
343+
private:
344+
std::string _name;
345+
};
346+
328347
NodeVariant getVariant(const std::shared_ptr<AstNode>& node);
329348

330349
std::string getTypeString(NodeType nt);

0 commit comments

Comments
 (0)