-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
99 lines (84 loc) · 3.12 KB
/
main.cpp
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
#include "lexer/lexer.h"
#include "lexer/lexer_errors.h"
#include "parser/parser.h"
#include "parser/parser_errors.h"
#include "runtime/interpreter.h"
#include "runtime/runtime_errors.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#define PROMPT "> "
#define OUTPUT_PADDING "\t# "
int main(int argc, char *argv[]) {
// Repl mode
if (argc < 2) {
Interpreter interpreter;
while (true) {
std::cout << PROMPT;
std::string input;
std::getline(std::cin, input);
if (input == "quit" || input == "q" || input == ":q" || input == "exit")
return 0;
try {
// Lexing
Lexer lexer{input};
auto tokens = lexer.tokenize();
// Parsing
Parser parser{tokens};
auto statements = parser.parse();
// Interpret
std::optional<Value> result = interpreter.interpret(std::move(statements));
if (result.has_value()) {
std::cout << OUTPUT_PADDING << result->toString() << std::endl;
}
} catch (const LexerException &le) {
std::string_view sv{input.begin(), input.end()};
std::cerr << "Lexer Error: " << le.what(sv) << std::endl;
} catch (const ParserException &pe) {
std::string_view sv{input.begin(), input.end()};
std::cerr << "Parser Error: " << pe.what(sv) << std::endl;
} catch (const RuntimeException &re) {
std::string_view sv{input.begin(), input.end()};
std::cerr << "Runtime Error: " << re.what(sv) << std::endl;
}
}
return 0;
}
// One line mode
std::string src{argv[1]};
Lexer lexer(src);
std::vector<Token> tokens;
try {
tokens = lexer.tokenize();
printf("\nTokens:\n");
for (Token token : tokens) {
printf("%s(%s, off: %d, l: %d)\n",
TokenTypesToString[token.type()].data(),
token.literal().data(), token.sourceOffset(),
token.length());
}
Parser parser{tokens};
auto statements = parser.parse();
Interpreter interpreter(std::move(statements));
auto lastOutput = interpreter.interpret();
if (lastOutput.has_value()) {
lastOutput.value().dispose();
std::cout << lastOutput.value().toString() << std::endl;
}
} catch (const LexerException &le) {
std::string_view sv{src.begin(), src.end()};
std::cerr << "Lexer Error: " << le.what(sv) << std::endl;
exit(le.error_code());
} catch (const ParserException &pe) {
std::string_view sv{src.begin(), src.end()};
std::cerr << "Parser Error: " << pe.what(sv) << std::endl;
exit(pe.error_code());
} catch (const RuntimeException &re) {
std::string_view sv{src.begin(), src.end()};
std::cerr << "Runtime Error: " << re.what(sv) << std::endl;
exit(re.error_code());
}
return 0;
}