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+ }
0 commit comments