diff --git a/src/Zhivo.cpp b/src/Zhivo.cpp index 2ef03c6..f7c38ff 100644 --- a/src/Zhivo.cpp +++ b/src/Zhivo.cpp @@ -16,313 +16,31 @@ * along with Zhivo. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - #include - -#include #include -#include -#include #include -#include #include -#include -#include auto interpreter() -> int { - SymbolTable globalSymbols; - - auto nil = std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - ); - - // Define the token for the function - auto myFunc = std::make_unique( - "myFunc", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - ); - - // Create and visit a variable declaration - auto varDecl1Tok = std::make_unique("x", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto varDecl = std::make_unique( - std::move(std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - )), - std::move(std::make_unique(std::move(varDecl1Tok))), - "=", - std::make_unique( - std::make_unique(*nil), - 3.1416 - ) - ); - varDecl->visit(globalSymbols); - - // Create and visit a variable access - auto varAcces1Tok = std::make_unique("x", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto varAccess = std::make_unique(std::move(varAcces1Tok)); - - // Create and visit a function declaration - auto funcBodyStmts = std::vector>(); - funcBodyStmts.push_back( - std::make_unique( - std::make_unique(*nil), - std::make_unique( - std::move(std::make_unique("x", "test.jnt", 1, 1, TokenType::IDENTIFIER)) - ) - ) - ); - - auto funcBody = std::make_unique( - std::make_unique(*nil), - std::move(funcBodyStmts) - ); - auto funcDecl = std::make_unique( - std::move(myFunc), - std::vector>{}, - std::move(funcBody) - ); - - // Store the function declaration directly - auto varDecl2Tok = std::make_unique("myFunc", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto myFuncDecl = std::make_unique( - std::move(std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - )), - std::move(std::make_unique(std::move(varDecl2Tok))), - "=", - std::move(funcDecl) - ); - myFuncDecl->visit(globalSymbols); - - { - // Create and visit a function call - auto funcCallArgs = std::vector>{}; - auto funcCall = std::make_unique( - std::make_unique(*nil), - "myFunc", - std::move(funcCallArgs) - ); - auto funcCallPrint = std::make_unique( - std::make_unique(*nil), - std::move(funcCall) - ); - funcCallPrint->visit(globalSymbols); - std::cout << std::endl; - } - - { - // Create and visit a function call - auto funcCallArgs = std::vector>{}; - auto funcCall = std::make_unique( - std::make_unique(*nil), - "myFunc", - std::move(funcCallArgs) - ); - auto funcCallPrint = std::make_unique( - std::make_unique(*nil), - std::move(funcCall) - ); - funcCallPrint->visit(globalSymbols); - std::cout << std::endl; - } - - // Create and visit string and number variables - auto varDecl3Tok = std::make_unique("message", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto strDecl = std::make_unique( - std::move(std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - )), - std::move(std::make_unique(std::move(varDecl3Tok))), - "=", - std::make_unique( - std::make_unique(*nil), - "The number is: " - ) - ); - strDecl->visit(globalSymbols); - - auto varDecl4Tok = std::make_unique("number", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto numDecl = std::make_unique( - std::move(std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - )), - std::move(std::make_unique(std::move(varDecl4Tok))), - "=", - std::make_unique( - std::make_unique(*nil), - 42 - ) - ); - numDecl->visit(globalSymbols); - - // Create and visit a binary expression - auto varAcces2Tok = std::make_unique("message", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto strAccess = std::make_unique( - std::move(varAcces2Tok) - ); - auto varAcces3Tok = std::make_unique("number", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto numAccess = std::make_unique( - std::move(varAcces3Tok) - ); - - auto addExpr = std::make_unique( - std::make_unique(*nil), - std::move(strAccess), - "+", - std::move(numAccess) - ); - - std::vector> elements; - elements.push_back(std::make_unique(std::make_unique(*nil), std::move("Hello"))); - elements.push_back(std::make_unique(std::make_unique(*nil), std::move("Kamusta"))); - elements.push_back(std::make_unique(std::make_unique(*nil), std::move("Marhaba"))); - - auto arrayExpr = std::make_unique( - std::make_unique(*nil), - std::move(elements) - ); - - auto varDecl_2Tok = std::make_unique("array", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto varDecl2 = std::make_unique( - std::move(std::make_unique( - "", - "test.jnt", - 1, - 1, - TokenType::IDENTIFIER - )), - std::move(std::make_unique(std::move(varDecl_2Tok))), - "=", - std::move(arrayExpr) - ); - varDecl2->visit(globalSymbols); + SymbolTable symbols; + DynamicObject result; - // Create ArrayAccessExpression for use in both assignments and printing - auto arrayAccessVar1 = std::make_unique( - std::make_unique(*nil), - std::make_unique( - std::make_unique("array", "test.jnt", 1, 1, TokenType::IDENTIFIER) - ), - std::make_unique(std::make_unique(*nil), 2.0) - ); - - auto arrayAccessVar2 = std::make_unique( - std::make_unique(*nil), - std::make_unique( - std::make_unique("array", "test.jnt", 1, 1, TokenType::IDENTIFIER) - ), - std::make_unique(std::make_unique(*nil), 2.0) - ); - - // Create BinaryExpression to set value of array at index 2 - auto setValExpr = std::make_unique( - std::make_unique(*nil), - std::move(arrayAccessVar1), - "=", - std::make_unique(std::make_unique(*nil), 3.14) - ); - setValExpr->visit(globalSymbols); - - // Print the array element at index 2 - auto varDecl_3Tok = std::make_unique("array", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto print1Stmt = std::make_unique( - std::make_unique(*nil), - std::move(std::make_unique(std::move(varDecl_3Tok))) - ); - print1Stmt->visit(globalSymbols); - std::cout << std::endl; - - // Print the function declaration - auto varAcces4Tok = std::make_unique("myFunc", "test.jnt", 1, 1, TokenType::IDENTIFIER); - auto funcVarAccess = std::make_unique( - std::move(varAcces4Tok) - ); - auto printStmt = std::make_unique( - std::make_unique(*nil), - std::move(addExpr) - ); - - printStmt->visit(globalSymbols); - std::cout << std::endl; - - // Tokenizer example try { - std::string sourceCode = R"( - int main() { - std::string s = "Hello, World!"; - int x = 0xabcdef; - - if(x >= 10) - x = x + 1; - } - )"; - - auto tokenizer = std::make_unique(sourceCode, "example.cpp"); - tokenizer->scan(); - - const auto& tokens = tokenizer->getTokens(); - for(const auto& token : tokens) - std::cout << "Token: " << token.toString() << std::endl; - } - catch (const LexicalAnalysisException& e) { - std::cerr << "Lexical Analysis Error: " << e.what() << "\n"; - return 1; - } - - return 0; -} - -decltype(interpreter()) main() { - try { - SymbolTable symbols; Parser parser = Parser::fromFile("test.zhv"); parser.parse(); - for(const auto& statement : parser.getGlobalStatements()) { - DynamicObject object = statement->visit(symbols); - std::cout << "Result: " << object.toString() << std::endl; - } - - return interpreter(); + for(const auto& statement : parser.getGlobalStatements()) + result = statement->visit(symbols); + return 0; } - catch (const std::exception& exc) { + catch(const std::exception& exc) { std::cerr << "Caught exception: " << exc.what() << std::endl; } - return 0; -} \ No newline at end of file + return 1; +} + +decltype(interpreter()) main() { + return interpreter(); +}