-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
84 lines (66 loc) · 2.13 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
#define TEST 0
#define CMD_COMPILER 1
#include <iostream>
#include <fstream>
#if TEST
#include "Scope.hpp"
#include "Var.hpp"
#include "Expr.hpp"
#include "Instr.hpp"
#include "Func.hpp"
#else
#if CMD_COMPILER
#include <sstream>
#endif
#include "Parser.hpp"
#endif
int main(int argc, const char* argv[]) {
#if TEST
Func* func = new Func("_alpha_main", new Scope(nullptr));
func->getScope()->addVar("a", new Var(new NumExpr(42)));
func->getScope()->addVar("b", new Var(new NumExpr(23)));
// Scope* nested = new Scope(func->getScope());
// nested->addVar("foo", new Var(new NumExpr(128)));
// nested->prepare();
// nested->eval(std::cout);
// delete nested;
// std::cout << "----" << std::endl;
func->getScope()->addVar("c", new Var(new AddOp(new VarExpr(func->getScope()->getVar("b")), new MulOp(new VarExpr(func->getScope()->getVar("a")), new NumExpr(3)))));
func->getScope()->addVar("b", new Var(new NumExpr(1337)));
std::string label = "LC0";
std::string str = "b + (a * 3) = ";
auto pr1 = new PrintInstr(new VarExpr(func->getScope()->getVar("b")));
func->getScope()->addInstr(pr1);
auto pr2 = new PrintInstr(new StringExpr(label));
pr2->addExpr(new VarExpr(func->getScope()->getVar("c")));
func->getScope()->addInstr(pr2);
func->eval(std::cout);
std::cout << label << ':' << std::endl;
std::cout << "\t.ascii\t\"" << str << "\\0\"" << std::endl;
delete func;
#elif CMD_COMPILER
if (argc >= 2) {
std::string filename(argv[1]);
if (filename.length() > 5) {
std::stringstream buf(filename);
std::string basename;
std::getline(buf, basename, '.');
std::ofstream output(basename + ".s");
Parser p;
Env* env = p.parse(filename);
if (env)
env->eval(output);
} else {
std::cerr << "Invalid input file" << std::endl;
}
} else {
std::cerr << "No input file" << std::endl;
}
#else
std::ofstream output("test.s");
Parser p;
Env* env = p.parse("test.alpha");
if (env)
env->eval(output);
#endif
}