-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (58 loc) · 1.46 KB
/
main.cpp
File metadata and controls
67 lines (58 loc) · 1.46 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
#include <string>
#include <utility>
#include "evaluator.h"
#include "error.h"
#include "iostream"
#include "reader.h"
#include "types.h"
#include "printer.h"
#include "env.h"
#include "builtin.h"
#include <cstdlib>
MalType* READ(std::string input){
return Reader::read_str(std::move(input));
}
MalType* EVAL(MalType* input, Env& env) {
return Evaluator::eval(input, &env);
}
std::string PRINT(const MalType* input) {
return Printer::pr_str(input, true);
}
void file_exec(char** argv){
auto path = std::string(argv[1]);
try {
load_file({new MalString(path)}, false);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
std::exit(1);
}
}
void repl(Env& global_env){
while(true){
std::cout << "user> ";
std::string input;
if (!std::getline(std::cin, input)) {
std::cout << std::endl;
break;
}try {
std::cout << PRINT(EVAL(READ(input), global_env)) << std::endl;
}catch(const liscppError& e) {
std::cerr << e.what() << std::endl;
}
}
}
int main(int argc, char** argv){
Env global_env;
std::vector<MalType*> argv_list;
for (int i = 2; i < argc; ++i) {
argv_list.push_back(new MalString(argv[i]));
}
global_env.set("*ARGV*", new MalList(argv_list));
Evaluator::set_env(&global_env);
if (argc >= 2){
file_exec(argv);
} else{
repl(global_env);
}
return 0;
}