-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
68 lines (63 loc) · 2.08 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
#include "Interpreter.h"
#include <iostream>
int main()
{
Lexer lexer({
[]{ return std::cin.peek(); },
[]{ return std::cin.get(); },
[]{ return std::cin.peek() == decltype(std::cin)::traits_type::eof(); }
});
Parser parser{lexer};
Interpreter interpreter;
bool debug_parsetree = false;
interpreter.globalEnvironment() = var{{
{"console", {{
{"log", var(
[](auto args){
std::copy(std::begin(args), std::end(args), std::ostream_iterator<var>(std::cout));
std::cout << '\n';
return var{};
})
}
}}},
{"exit", var(
[](auto args)->var{
exit(args.size() >= 1 ? static_cast<int>(args[0].to_double()) : 0);
})
},
{"debug", var(
[&interpreter, &debug_parsetree](auto args)->var{
if(args.size() >= 1){
debug_parsetree = args[0].to_bool();
}else{
std::cout << interpreter;
}
return var{};
})
}
}};
while(true){
std::cout << "Cpp.js> " << std::flush;
try{
Parser::ParseTree translation_unit(Parser::Statement::STM_TranslationUnit);
do{
parser.parse_append(translation_unit);
}while(std::cin.peek() != '\n');
if(debug_parsetree){
std::cout << translation_unit;
}
interpreter.feed(std::move(translation_unit));
}catch(std::invalid_argument& e){
std::cout << '\n' << "ParseError: " << e.what() << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
try{
std::cout << '\n' << interpreter.execute() << '\n';
}catch(Interpreter::unimplemented_error& e){
std::cout << '\n' << "Error: " << e.what() << " is not implemented yet." << '\n';
}
std::cin.clear();
}
}