-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (142 loc) · 6.3 KB
/
main.cpp
File metadata and controls
163 lines (142 loc) · 6.3 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "interpreter/binary_operations.hpp"
#include "interpreter/code_flow.hpp"
#include "interpreter/expression.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/values.hpp"
#include <iostream>
#include <memory>
#include <ostream>
void testSeq() {
std::unique_ptr<Expression> seq = std::make_unique<Seq>(
std::make_unique<Seq>(
std::make_unique<Unit>(),
std::make_unique<Unit>()
),
std::make_unique<Num>(-10)
);
auto interpreter = std::make_unique<Interpreter>(std::move(seq));
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
}
void testIf() {
std::unique_ptr<Expression> ifTrue = std::make_unique<If>(
std::make_unique<Gt>(
std::make_unique<Num>(5),
std::make_unique<Num>(2)
),
std::make_unique<Num>(-10),
std::make_unique<Bool>(true)
);
std::unique_ptr<Expression> ifFalse = std::make_unique<If>(
std::make_unique<Lt>(
std::make_unique<Num>(5),
std::make_unique<Num>(2)
),
std::make_unique<Num>(-10),
std::make_unique<Bool>(true)
);
auto interpreter = std::make_unique<Interpreter>(std::move(ifTrue));
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(ifFalse));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
}
void testBinops() {
std::unique_ptr<Expression> n1 = std::make_unique<Num>(8);
std::unique_ptr<Expression> n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> sum = std::make_unique<Sum>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> sub = std::make_unique<Sub>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> mul = std::make_unique<Mul>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> div = std::make_unique<Div>(std::move(n1), std::move(n2));
std::unique_ptr<Expression> b1 = std::make_unique<Bool>(true);
std::unique_ptr<Expression> b2 = std::make_unique<Bool>(false);
std::unique_ptr<Expression> and_ = std::make_unique<And>(std::move(b1), std::move(b2));
b1 = std::make_unique<Bool>(true);
b2 = std::make_unique<Bool>(false);
std::unique_ptr<Expression> or_ = std::make_unique<Or>(std::move(b1), std::move(b2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> lt = std::make_unique<Lt>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> gt = std::make_unique<Gt>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> eq = std::make_unique<Eq>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> neq = std::make_unique<Neq>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> sum1 = std::make_unique<Sum>(std::move(n1), std::move(n2));
n1 = std::make_unique<Num>(8);
n2 = std::make_unique<Num>(-2);
std::unique_ptr<Expression> sub1 = std::make_unique<Sub>(std::move(n1), std::move(n2));
std::unique_ptr<Expression> sum2 = std::make_unique<Sum>(std::move(sum1), std::move(sub1));
auto interpreter = std::make_unique<Interpreter>(std::move(sum));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(sub));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(mul));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(div));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(and_));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(or_));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(lt));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(gt));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(eq));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(neq));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(sum2));
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
}
void testValues() {
std::unique_ptr<Expression> num = std::make_unique<Num>(2);
std::unique_ptr<Expression> boo = std::make_unique<Bool>(true);
std::unique_ptr<Expression> uni = std::make_unique<Unit>();
std::unique_ptr<Expression> ref = std::make_unique<Ref>(1);
auto interpreter = std::make_unique<Interpreter>(std::move(num));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(boo));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(uni));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
interpreter->setCode(std::move(ref));
interpreter->evaluate();
std::cout << interpreter->getCode()->toString() << std::endl;
}
int main() {
testIf();
testSeq();
return 0;
}