-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
95 lines (85 loc) · 2.09 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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <Neon/Lexer.hpp>
#include <Neon/AstPrinter.hpp>
#include <Neon/Parser.hpp>
#include <Neon/Interpreter.hpp>
void run(const std::string &code, Ne::Interpreter& interpreter)
{
Ne::Lexer lexer{code};
std::vector<Ne::Token> tokens = lexer.scanTokens();
// std::cout << "number of tokens : " << tokens.size() << std::endl;
// for(auto& token : tokens){
// std::cout << token.toString() << std::endl;
// }
Ne::Parser parser(tokens);
auto statements = parser.parse();
// std::cout << Ne::toString(statements) << std::endl;
interpreter.evaluateStmts(statements);
// std::cout << interpreter.stringify(obj) << std::endl;
}
std::string readFile(std::string path)
{
std::ifstream file{ path };
if ( file )
{
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
else
{
throw std::runtime_error("file not found!");
}
}
void runFile(const std::string &path)
{
Ne::Interpreter interpreter;
run(readFile(path), interpreter);
}
void runPrompt()
{
Ne::Interpreter interpreter;
for (;;)
{
std::cout << "> ";
std::string line;
std::getline(std::cin, line);
if (line == "exit" || line == "quit")
break;
// std::cout << "line : " << line << std::endl;
run(line, interpreter);
}
}
int main(int argc, char *argv[])
{
if (argc > 2)
{
std::cout << "too many arguments";
return 10;
}
else if (argc == 2)
{
runFile(argv[1]);
}
else
{
runPrompt();
}
// auto expr = Ne::createBinaryEV(
// Ne::createUnaryEV(
// Ne::Token(Ne::TokenType::MINUS, "-", 1),
// Ne::createLiteralEV("123")
// ),
// Ne::Token(Ne::TokenType::STAR, "*",1),
// Ne::createGroupingEV(
// Ne::createLiteralEV("47.65")
// )
// );
// std::cout << Ne::toString(expr) << std::endl;
return 0;
}