-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (55 loc) · 1.86 KB
/
main.cpp
File metadata and controls
65 lines (55 loc) · 1.86 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include "DuelScriptScanner.h"
#include "Parser.h"
#include "AstNodes.h"
#include "AstPrinter.h"
#include "Resolver.h"
bool hadError = false;
std::string readFile(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "Could not open file: " << path << std::endl;
exit(74);
}
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
int main(int argc, char* argv[]) {
std::string sourceFile = "test.duelscript";
if (argc == 2) {
sourceFile = argv[1];
}
std::string sourceCode = readFile(sourceFile);
// --- 1. Scanning ---
std::cout << "--- 1. Scanning DuelScript file ---" << std::endl;
Scanner scanner(sourceCode);
std::vector<Token> tokens = scanner.scanTokens();
std::cout << "--- Scanning Complete (" << tokens.size() << " tokens) ---" << std::endl;
// --- 2. Parsing ---
std::cout << "\n--- 2. Parsing Tokens into AST ---" << std::endl;
Parser parser(tokens);
std::vector<std::unique_ptr<Stmt>> statements = parser.parse();
if (hadError) {
std::cout << "--- Parsing Failed (see errors above) ---" << std::endl;
return 65;
}
std::cout << "--- Parsing Complete ---" << std::endl;
// --- 3. Semantic Analysis ---
std::cout << "\n--- 3. Semantic Analysis (Resolver) ---" << std::endl;
Resolver resolver;
resolver.resolve(statements);
if (resolver.hadError) {
std::cout << "--- Semantic Analysis Failed! Fix the errors above. ---" << std::endl;
return 65;
}
std::cout << "--- Semantic Analysis Passed! ---" << std::endl;
// --- 4. Printing ---
std::cout << "\n--- 4. Abstract Syntax Tree (AST) ---" << std::endl;
AstPrinter printer;
printer.print(statements);
return 0;
}