-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
75 lines (69 loc) · 1.52 KB
/
interpreter.cpp
File metadata and controls
75 lines (69 loc) · 1.52 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
#include <interpreter.hpp>
#include <iostream>
#include <queue>
using namespace std;
// Interpreter functions for all Statement
Interpreter::Interpreter(vector<unique_ptr<Statement>> &statements)
{
source = &number;
for (auto &t : statements)
{
t->accept(*this);
}
}
void Interpreter::visit(Statement &target)
{
}
// set source to target value
void Interpreter::visit(SetStack &target)
{
source = target.stack.get();
}
// sets the value of Number stack and point source to number stack
void Interpreter::visit(SetNumber &target)
{
number.value = target.number;
source = &number;
}
// loops till the source have zero at top
void Interpreter::visit(ZeroBlock &target)
{
while (source->read() != 0)
{
for (auto &t : target.statements)
{
t->accept(*this);
}
}
}
// loops till the source is empty
void Interpreter::visit(EmptyBlock &target)
{
while (!source->isEmpty())
{
for (auto &t : target.statements)
{
t->accept(*this);
}
}
}
// operates from source to target
void Interpreter::visit(Operator &target)
{
queue<memseg> q;
// operates on source and keeps the element in queue
for (auto t : target.operations)
{
q.push(source->read());
if (t == Operation::Move)
source->pop();
}
// empties the queue to target
while (!q.empty())
{
target.target->push(q.front());
q.pop();
}
// set the source to target
source = target.target.get();
}