-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
160 lines (138 loc) · 4.32 KB
/
main.cpp
File metadata and controls
160 lines (138 loc) · 4.32 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @file main.cpp
* @brief Main entry point for the ManaScript interpreter
*/
#include "lexer.hpp"
#include "parser.hpp"
#include "ast.hpp"
#include "transpiler.hpp"
#include "error.hpp"
#include "token.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <memory>
#include <vector>
#include <filesystem>
namespace mana {
void printUsage() {
std::cout << "ManaScript Interpreter v0.1.0\n"
<< "Usage:\n"
<< " manascript [options] [file]\n\n"
<< "Options:\n"
<< " -h, --help Show this help message\n"
<< " -v, --version Show version information\n"
<< " -i, --interactive Start interactive mode\n"
<< " -t, --tokenize Show tokenized output\n\n"
<< "Examples:\n"
<< " manascript script.ms Run a script file\n"
<< " manascript -i Start interactive mode\n"
<< " manascript -t script.ms Show tokenized output\n";
}
void printVersion() {
std::cout << "ManaScript Interpreter v0.1.0\n"
<< "Copyright (c) 2024\n";
}
void printTokens(const std::vector<Token>& tokens) {
std::cout << "\nTokenized output:\n";
std::cout << "----------------\n";
for (const auto& token : tokens) {
std::cout << "Line " << token.line << ", Col " << token.column << ": ";
std::cout << "Type: " << static_cast<int>(token.type) << ", ";
std::cout << "Lexeme: '" << token.lexeme << "'\n";
}
std::cout << "----------------\n";
}
void runInteractiveMode() {
std::cout << "ManaScript Interactive Mode\n"
<< "Type 'exit' or 'quit' to exit\n"
<< "Type 'help' for help\n\n";
std::string line;
while (true) {
std::cout << "> ";
std::getline(std::cin, line);
if (line == "exit" || line == "quit") {
break;
}
if (line == "help") {
std::cout << "Available commands:\n"
<< " exit, quit Exit the interpreter\n"
<< " help Show this help message\n"
<< " clear Clear the screen\n";
continue;
}
if (line == "clear") {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
continue;
}
if (line.empty()) {
continue;
}
try {
Lexer lexer(line);
auto tokens = lexer.scanTokens();
printTokens(tokens);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
}
void runFile(const std::string& filename, bool showTokens) {
try {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file '" << filename << "'\n";
return;
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
Lexer lexer(content, filename);
auto tokens = lexer.scanTokens();
if (showTokens) {
printTokens(tokens);
} else {
// TODO: Add parser and interpreter here
std::cout << "Running script: " << filename << "\n";
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
} // namespace mana
int main(int argc, char* argv[]) {
if (argc < 2) {
mana::printUsage();
return 1;
}
std::string arg = argv[1];
if (arg == "-h" || arg == "--help") {
mana::printUsage();
return 0;
}
if (arg == "-v" || arg == "--version") {
mana::printVersion();
return 0;
}
if (arg == "-i" || arg == "--interactive") {
mana::runInteractiveMode();
return 0;
}
bool showTokens = false;
if (arg == "-t" || arg == "--tokenize") {
showTokens = true;
if (argc < 3) {
std::cerr << "Error: No input file specified\n";
return 1;
}
mana::runFile(argv[2], showTokens);
return 0;
}
// If no special flags, treat as a file
mana::runFile(arg, showTokens);
return 0;
}// Adding main.cpp from manu-r12