-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.cpp
More file actions
150 lines (142 loc) · 3.69 KB
/
vm.cpp
File metadata and controls
150 lines (142 loc) · 3.69 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
#include "vm.h"
VM::InterpretResult VM::interpret(std::istream& input) {
Compiler compiler;
if (!compiler.compile(input, &this->bytecode))
return VM::InterpretResult::COMPILE_ERROR;
return run();
}
Value& VM::peek(int distance) {
return st.at(st.size() - 1 - distance);
}
Value VM::pop() {
Value val = peek(0);
st.pop_back();
return val;
}
uint8_t VM::readByte() {
return bytecode.codeAt(ip++);
}
Value VM::readConstant() {
size_t add = readByte();
return bytecode.valueAt(add);
}
void VM::runtimeError(std::string message, uint8_t idx) {
// resetStack();
std::cerr << message << " at line " << std::to_string(bytecode.lineAt(idx));
}
bool VM::isFalsey(Value& val) {
switch (val.getType()) {
case Value::NUL:
return true;
case Value::BOOL:
return !std::get<bool>(val.getVal());
/*case Value::STRING:
return std::get<std::string>(val.getVal()) == "";*/
case Value::NUM:
return std::get<double>(val.getVal()) == 0;
default:
runtimeError("Expected a boolean expression", ip);
std::exit(InterpretResult::COMPILE_ERROR);
}
}
VM::InterpretResult VM::binaryOp(uint8_t op) {
if (peek(0).getType() != Value::NUM) {
runtimeError("Expected a number after '-'", op);
return RUNTIME_ERROR;
}
double num1 = std::get<double>(pop().getVal());
if (peek(0).getType() != Value::NUM) {
runtimeError("Expected a number after '-'", op);
return RUNTIME_ERROR;
}
double num2 = std::get<double>(pop().getVal());
switch (op) {
case Opcode::ADD:
st.push_back(Value(num2 + num1));
break;
case Opcode::SUBTRACT:
st.push_back(Value(num2 - num1));
break;
case Opcode::MULTIPLY:
st.push_back(Value(num2 * num1));
break;
case Opcode::DIVIDE:
st.push_back(Value(num2 / num1));
break;
case Opcode::GREATER:
st.push_back(Value(num2 > num1));
break;
case Opcode::LESSER:
st.push_back(Value(num2 < num1));
break;
}
return VM::OK;
}
VM::InterpretResult VM::run() {
while (true) {
#ifdef DEBUG_TRACE_EXECUTION
std::cout << "stack => ";
for (auto it = st.begin(); it != st.end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
bytecode.disassembleInstruction(ip);
#endif
switch (uint8_t instruction = readByte()) {
case Opcode::RETURN:
std::cout << pop() << std::endl;
return InterpretResult::OK;
case Opcode::OP_CONSTANT: st.push_back(readConstant()); break;
case Opcode::TRUE: st.push_back(Value(true)); break;
case Opcode::FALSE: st.push_back(Value(false)); break;
case Opcode::NUL: st.push_back(Value()); break;
case Opcode::NEGATE:
{
if (peek(0).getType() != Value::NUM) {
runtimeError("Expected a number after '-'", instruction);
return RUNTIME_ERROR;
}
st.push_back(Value(-std::get<double>(pop().getVal())));
break;
}
case Opcode::ADD:
if (peek(1).getType() == Value::STRING &&
peek(0).getType() == Value::STRING) {
std::string s1 = std::get<std::string>(pop().getVal());
std::string s2 = std::get<std::string>(pop().getVal());
st.push_back(Value(s2 + s1));
}
else if (peek(1).getType() == Value::NUM &&
peek(0).getType() == Value::NUM) {
double n1 = std::get<double>(pop().getVal());
double n2 = std::get<double>(pop().getVal());
st.push_back(Value(n1 + n2));
}
else {
runtimeError("Operands must be value of string", instruction);
return RUNTIME_ERROR;
}
break;
case Opcode::SUBTRACT:
case Opcode::MULTIPLY:
case Opcode::DIVIDE:
case Opcode::GREATER:
case Opcode::LESSER:
if (binaryOp(instruction) == RUNTIME_ERROR)
return RUNTIME_ERROR;
break;
case Opcode::NOT:
{
Value val = pop();
st.push_back(Value(isFalsey(val)));
break;
}
case Opcode::EQUAL:
{
Value v1 = pop();
Value v2 = pop();
st.push_back(Value(v1.getVal() == v2.getVal()));
break;
}
}
}
}