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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,10 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

.clang-format
.devcontainer
.vscode
cpp.code-workspace
formatted_code
28 changes: 28 additions & 0 deletions interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.0.0)
project(Interpreter VERSION 0.1.0)

find_package(spdlog REQUIRED)
find_package(GTest CONFIG REQUIRED)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++17")

set(SOURCES main.cpp
src/Ast.cpp
src/Interpreter.cpp
src/Lexer.cpp
src/Parser.cpp
src/Token.cpp
)

add_executable(Interpreter ${SOURCES})

target_include_directories(Interpreter PUBLIC ./src)

target_link_libraries(Interpreter
spdlog::spdlog
spdlog
)
74 changes: 74 additions & 0 deletions interpreter/CodeInterpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <sstream>

#include "CodeInterpreter.h"
#include "src/GlobalScope.h"


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);
}

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

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");
}

}

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;

}
17 changes: 17 additions & 0 deletions interpreter/CodeInterpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

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

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();

};
10 changes: 10 additions & 0 deletions interpreter/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SCsub

Import('env')

env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
env.add_source_files(env.modules_sources, "src/*.cpp")
env.add_source_files(env.modules_sources, "tests/*.cpp")
# env.Append(CPPPATH=["./internal:include"])

env.Append( CCFLAGS=["/EHsc"] )
5 changes: 5 additions & 0 deletions interpreter/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def can_build(env, platform):
return True

def configure(env):
pass
38 changes: 38 additions & 0 deletions interpreter/grammar.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


factor : PLUS factor | MINUS factor | INTEGER | FLOATING_NUMBER
| LPAREN expression RPAREN | variable

term : factor ((MUL | DIV) factor)*

expression : term ((PLUS | MINUS) term)*

section : START statementList END

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

statementList : statement | statement SEMI statementList

assignmentStatement : variable ASSIGN expression

variableDeclaration : (TYPE | auto) ID (ASSIGN expression)?

variable : ID

functionDeclaration : FUN ID LPAREN RPAREN COLON section

functionCall : ID LPAREN RPAREN

ifStatement : IF expression COLON SECTION (ELSE COLON section)?

whileLoop : WHILE expression COLON START SECTION

forLoop : FOR LPAREN assignmentStatement SEMICOLON
expression SEMICOLON assignmentStatement RPAREN COLON section

program : section DOT



67 changes: 67 additions & 0 deletions interpreter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "./src/Ast.h"
#include "./src/Interpreter.h"
#include "./src/Lexer.h"
#include "./src/Parser.h"
#include "./src/Token.h"

#include <fstream>
#include <iostream>
#include <sstream>

int main(int argc, char* argv[])
{

if (argc > 1) {
std::string input;

std::string file_path;
std::string other_arg;

std::string arg = argv[1];

if (arg.find("--file=") == 0) {
file_path = arg.substr(7);
} else {
other_arg = arg;
}

if (!file_path.empty()) {
std::cout << "File path: " << file_path << std::endl;

std::ifstream file(file_path);

if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
input = buffer.str();

std::cout << "File content:\n" << input << std::endl;
} else {
std::cerr << "Error: Could not open file " << file_path << std::endl;
}

} else {
std::cout << "No file path provided. Use --file=path_to_file" << std::endl;
if (!arg.empty()) {
std::cout << "Argument:" << std::endl;
std::cout << arg << std::endl;
input = arg;
}
}

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

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

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

stRef.debugPrint();

return 0;
}

return 1;
}
17 changes: 17 additions & 0 deletions interpreter/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "register_types.h"

#include "core/object/class_db.h"
#include "CodeInterpreter.h"

void initialize_interpreter_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
GDREGISTER_CLASS(CodeInterpreter);
}

void uninitialize_interpreter_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}
5 changes: 5 additions & 0 deletions interpreter/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "modules/register_module_types.h"

void initialize_interpreter_module(ModuleInitializationLevel p_level);
void uninitialize_interpreter_module(ModuleInitializationLevel p_level);
/* the word in the middle must be the same as the module folder name */
Loading