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