- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
Latest commit
84 lines (66 loc) · 2.13 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (66 loc) · 2.13 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#defineTEST0
#defineCMD_COMPILER1
#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
intmain(int argc, constchar* argv[]) {
#if TEST
Func* func = newFunc("_alpha_main", newScope(nullptr));
func->getScope()->addVar("a", newVar(newNumExpr(42)));
func->getScope()->addVar("b", newVar(newNumExpr(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", newVar(newAddOp(newVarExpr(func->getScope()->getVar("b")), newMulOp(newVarExpr(func->getScope()->getVar("a")), newNumExpr(3)))));
func->getScope()->addVar("b", newVar(newNumExpr(1337)));
std::string label = "LC0";
std::string str = "b + (a * 3) = ";
auto pr1 = newPrintInstr(newVarExpr(func->getScope()->getVar("b")));
func->getScope()->addInstr(pr1);
auto pr2 = newPrintInstr(newStringExpr(label));
pr2->addExpr(newVarExpr(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
}