-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
146 lines (127 loc) · 3.56 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
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
#include <iostream>
#include "parse/lex.hpp"
#include "parse/parse.hpp"
#include "compile/compile.hpp"
#include "compile/bytecode.hpp"
#include "util.hpp"
#include "vm/vm.hpp"
#include "vm/bc/read_bc.hpp"
#include "compile/tree_to_source.hpp"
// scl <cmd> <in> options
void print_help_msg(){
std::cout <<"scl <command> [flags]\n"
<<"\nUsage:\n"
<<"scl build <entry file> # Generate bytecode binary\n"
<<"scl minify <entry file> # Minify the input code\n"
<<"scl debug <entry file> # Generate bytecode text\n"
<<"scl exec <bytecode file> # Execute bytecode\n"
<<"scl eval <entry file> # Build and Exect programs\n";
#ifndef SCL_DEBUG_MODE
std::cout <<"\nRecompile with debugging enabled for verbose output\n";
#else
std::cout <<"\nYou are using the debug version of SCL\n";
#endif
}
// The main virtual machine instance
VM* interpreter;
int main(int argc, char** argv) {
// Not enough arguments
if (argc < 3) {
print_help_msg();
return 1;
}
const char* cmd = argv[1];
const char* arg = argv[2];
if (!arg) {
print_help_msg();
return 1;
}
bool exec, out_bc, out_bct, out_minified, eval;
exec = out_bc = out_bct = out_minified = eval = false;
if (strcmp(cmd, "build") == 0)
out_bc = true;
else if (strcmp(cmd, "minify") == 0)
out_minified = true;
else if (strcmp(cmd, "debug") == 0)
out_bct = true;
else if (strcmp(cmd, "exec") == 0)
exec = true;
else if (strcmp(cmd, "eval") == 0)
eval = true;
else {
std::cerr <<"unknown command \"" <<cmd <<"\"\n";
print_help_msg();
}
if (exec) {
std::ifstream bc_src = std::ifstream(arg);
#ifdef SCL_DEBUG
std::cout <<"reading lit header... ";
#endif
std::vector<Literal>&& lits = read_lit_header(bc_src);
#ifdef SCL_DEBUG
std::cout <<"done\n";
#endif
std::vector<std::string> args(argc);
for (int i = 0; i < argc; i++)
args.emplace_back(argv[i]);
interpreter = new VM{lits, args, bc_src};
interpreter->run();
return 0; // never gets called... (hopefully)
}
Program p;
try {
p = Program(arg);
} catch (std::vector<SyntaxError>& es) {
for (auto &e: es)
std::cout << "Syntax Error: " << e.msg << std::endl
<< util::show_line_pos(arg, e.token.pos) << std::endl;
}
std::vector<Command> bytecode;
std::vector<SemanticError> errs = p.compile(bytecode);
if (!errs.empty()) {
bool fatal = false;
auto fis = std::ifstream(arg); // reuse istream
for (auto& e : errs)
if (e.is_warn) {
std::cout <<util::term_eff_red <<"Compiler Warning: " <<util::term_eff_reset <<e.msg <<std::endl
<< util::show_line_pos(fis, e.pos, e.file.string());
} else {
std::cout <<util::term_eff_red <<"Compiler Error: " <<util::term_eff_reset <<e.msg <<std::endl
<<util::show_line_pos(fis, e.pos, e.file.string()) <<std::endl;
fatal = true;
}
if (fatal)
return 1;
}
if (out_bc) {
std::ofstream out{"o.bin"};
for (const char c : compile_bin(bytecode))
out << c;
std::cout <<"compiled to o.bin\n";
}
if (out_bct)
std::cout <<compile_text(bytecode) <<std::endl;
if (out_minified) {
std::cout <<tree_to_source(p.main) <<std::endl;
}
if (eval) {
// Compile file
std::stringstream bin{"o.bin"};
for (const char c : compile_bin(bytecode))
bin << c;
// Run file
#ifdef SCL_DEBUG
std::cout <<"reading lit header... ";
#endif
std::vector<Literal>&& lits = read_lit_header(bin);
#ifdef SCL_DEBUG
std::cout <<"done\n";
#endif
std::vector<std::string> args(argc);
for (int i = 0; i < argc; i++)
args.emplace_back(argv[i]);
interpreter = new VM{lits, args, bin};
interpreter->run();
return 0; // never gets called... (hopefully)
}
}