main.cpp (3695B)
1 #include <iostream> 2 #include <sstream> 3 #include <fstream> 4 5 #include "lexer.hpp" 6 #include "parser.hpp" 7 8 std::string tokenTypeToString(TokenType type) { 9 switch (type) { 10 case TokenType::PLUS: return "PLUS"; 11 case TokenType::MINUS: return "MINUS"; 12 case TokenType::MULTIPLY: return "MULTIPLY"; 13 case TokenType::DIVIDE: return "DIVIDE"; 14 case TokenType::ASSIGN: return "ASSIGN"; 15 case TokenType::SEMICOLON: return "SEMICOLON"; 16 case TokenType::INVALID: return "INVALID"; 17 case TokenType::END_OF_FILE: return "EOF"; 18 case TokenType::NUMBER: return "NUMBER"; 19 case TokenType::IDENTIFIER: return "IDENTIFIER"; 20 case TokenType::INT: return "INT"; 21 default: return "UNKNOWN"; 22 } 23 } 24 25 void printAST(ASTNode* node, int indent) { 26 std::string indentation(indent * 2, ' '); 27 28 if (auto* program = dynamic_cast<Program*>(node)) { 29 std::cout << indentation << "Program\n"; 30 for (const auto& decl : program->declarations) { 31 printAST(decl.get(), indent + 1); 32 } 33 } 34 35 else if (auto* decl = dynamic_cast<Declaration*>(node)) { 36 std::cout << indentation << "Declaration\n"; 37 std::cout << indentation << " Type: " << decl->type << "\n"; 38 std::cout << indentation << " Name: " << decl->var_name << "\n"; 39 40 if (decl->value) { 41 std::cout << indentation << " Value:\n"; 42 printAST(decl->value.get(), indent + 2); 43 } 44 } 45 46 else if (auto* binop = dynamic_cast<BinaryOp*>(node)) { 47 std::cout << indentation << "BinaryOp: " << binop->value << "\n"; 48 printAST(binop->left.get(), indent + 1); 49 printAST(binop->right.get(), indent + 1); 50 } 51 52 else if (auto* id = dynamic_cast<Identifier*>(node)) { 53 std::cout << indentation << "Identifier: " << id->name << "\n"; 54 } 55 56 else if (auto* num = dynamic_cast<NumberLiteral*>(node)) { 57 std::cout << indentation << "NumberLiteral: " << num->value << "\n"; 58 } 59 60 else if (auto* str = dynamic_cast<StringLiteral*>(node)) { 61 std::cout << indentation << "StringLiteral: " << str->value << "\n"; 62 } 63 64 else if (auto* assign = dynamic_cast<Assignment*>(node)) { 65 std::cout << indentation << "Assignment: " << assign->variable_name << "\n"; 66 printAST(assign->value.get(), indent + 1); 67 } 68 } 69 70 int main() { 71 std::ifstream file("test.bl"); 72 std::ostringstream buffer; 73 std::string line; 74 75 if (file) { 76 while (std::getline(file, line)) { 77 buffer << line << '\n'; 78 79 } 80 file.close(); 81 82 } else { 83 std::cerr << "Error opening file!" << std::endl; 84 return 1; 85 86 } 87 88 std::string file_contents = buffer.str(); 89 90 std::cout << "File contents:\n" << file_contents << std::endl; 91 92 Lexer lexer(file_contents); 93 auto tokens = lexer.tokenise(); 94 95 for (const auto& token : tokens) { 96 if (token.type == TokenType::END_OF_FILE) { 97 std::cout << "EOF" << std::endl; 98 99 } else { 100 std::cout << tokenTypeToString(token.type) 101 << " '" << token.value << "'" 102 << " at line " << token.line 103 << ", column " << token.column 104 << std::endl; 105 106 } 107 } 108 109 std::cout << "\n=== PARSING ===\n"; 110 Parser parser(tokens); 111 auto ast = parser.parse(); 112 113 std::cout << "Parse successful!" << std::endl; 114 std::cout << "Number of declarations: " << ast->declarations.size() << std::endl; 115 116 std::cout << "\n=== AST ===\n"; 117 printAST(ast.get(), 0); 118 119 }