-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.cpp
139 lines (132 loc) · 6.75 KB
/
Interpreter.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
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
#include "Interpreter.hpp"
#include <vector>
#include "Block.hpp"
#include "Var_Block.hpp"
static Func_Block* fakeMap(Visible_Block_type type);
/**
* @brief dataStorage::interpretAll
* interpret all the frontend blocks back to the backend
* construct all the functions to prepare for runWindow
*
* loop(all functions in func_pool):
* find "start" block in every function first,
* follow its next block to construct the whole function
*/
void dataStorage::interpretAll()
{
std::map<std::string, WriteBackend::Block*>::iterator funcPoolIter;
for(funcPoolIter = func_pool.begin();
funcPoolIter != func_pool.end();
++funcPoolIter)
{
WriteBackend::Func_Block* processFunc = static_cast<WriteBackend::Func_Block*>(funcPoolIter->second);
std::map<std::string, Visible_Block*>* processVBPool = correspond_pool[processFunc];
Visible_Block* processVB = (*processVBPool)["start"];
bodyType_e nextBodyType;
while(processVB->getNextBlock() != "notExist")
{
processVB = (*processVBPool)[processVB->getNextBlock()];
switch (processVB->getBlockType()) //total 19(start, user-var no need)
{
case Visible_Block_type::IF:
nextBodyType = bodyType_e::IF;
processFunc->setNextBody(nextBodyType, &var_pool[processVB->getOperandSource1()->name],
static_cast<Func_Block*>(*processVB->getSubFunction1()), static_cast<Func_Block*>(*processVB->getSubFunction2()));
for(int i = 0; i < processVB->getUserFuncOperandsNumT(); i++)
{
processFunc->setNextBodyParam(i, &var_pool[processVB->getUserFuncITHOperNameT(i)], 0);
}
for(int i = 0; i < processVB->getUserFuncOperandsNumF(); i++)
{
processFunc->setNextBodyParam(i, &var_pool[processVB->getUserFuncITHOperNameF(i)], 1);
}
break;
case Visible_Block_type::WHILE:
nextBodyType = bodyType_e::WHILE;
processFunc->setNextBody(nextBodyType, &var_pool[processVB->getOperandSource1()->name],
static_cast<Func_Block*>(*processVB->getSubFunction1()),nullptr);
for(int i = 0; i < processVB->getUserFuncOperandsNumT(); i++)
{
processFunc->setNextBodyParam(i, &var_pool[processVB->getUserFuncITHOperNameT(i)]);
}
break;
case Visible_Block_type::USER_FUNC:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, static_cast<Func_Block*>(func_pool[processVB->getRealFuncName()]), nullptr);
for(int i = 0; i < processVB->getUserFuncOperandsNum(); i++)
{
processFunc->setNextBodyParam(i, &var_pool[processVB->getUserFuncITHOperName(i)]);
}
break;
case Visible_Block_type::BIGGERTHAN:
case Visible_Block_type::SMALLERTHAN:
case Visible_Block_type::EQUALCOMPARE:
case Visible_Block_type::PLUS:
case Visible_Block_type::MINUS:
case Visible_Block_type::DIVISION:
case Visible_Block_type::MULTIPLY:
case Visible_Block_type::AND:
case Visible_Block_type::OR:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, fakeMap(processVB->getBlockType()),nullptr);
processFunc->setNextBodyParam(0, &var_pool[processVB->getDestination()->name]);
processFunc->setNextBodyParam(1, &var_pool[processVB->getOperandSource1()->name]);
processFunc->setNextBodyParam(2, &var_pool[processVB->getOperandSource2()->name]);
break;
case Visible_Block_type::NOT:
case Visible_Block_type::ASSIGNMENT:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, fakeMap(processVB->getBlockType()),nullptr);
processFunc->setNextBodyParam(0, &var_pool[processVB->getDestination()->name]);
processFunc->setNextBodyParam(1, &var_pool[processVB->getOperandSource1()->name]);
break;
case Visible_Block_type::TAKEINDGET:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, fakeMap(processVB->getBlockType()),nullptr);
processFunc->setNextBodyParam(0, &var_pool[processVB->getDestination()->name]);
processFunc->setNextBodyParam(1, &var_pool[processVB->getOperandSource1()->name]);
processFunc->setNextBodyParam(2, &var_pool[processVB->getIndexFakeName()]);
break;
case Visible_Block_type::TAKEINDSET:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, fakeMap(processVB->getBlockType()), nullptr);
processFunc->setNextBodyParam(0, &var_pool[processVB->getDestination()->name]);
processFunc->setNextBodyParam(1, &var_pool[processVB->getIndexFakeName()]);
processFunc->setNextBodyParam(2, &var_pool[processVB->getOperandSource1()->name]);
break;
case Visible_Block_type::PRINT:
nextBodyType = bodyType_e::FUNCCALL;
processFunc->setNextBody(nextBodyType, nullptr, fakeMap(processVB->getBlockType()),nullptr);
processFunc->setNextBodyParam(0,&var_pool[processVB->getOperandSource1()->name]);
break;
}
}
}
}
/**
* @brief fakeMap
* @param type
*
* give a illusion of map from Visible_Block_type to BuiltInOperators
* for easier call use later on
*/
static Func_Block* fakeMap(Visible_Block_type type)
{
switch (type) {
case Visible_Block_type::PRINT: return &printBlock;
case Visible_Block_type::BIGGERTHAN: return &biggerThanBlock;
case Visible_Block_type::SMALLERTHAN: return &smallerThanBlock;
case Visible_Block_type::EQUALCOMPARE: return &equalBlock;
case Visible_Block_type::PLUS: return &plusBlock;
case Visible_Block_type::MINUS: return &minusBlock;
case Visible_Block_type::DIVISION: return &divisionBlock;
case Visible_Block_type::MULTIPLY: return &multiplicationBlock;
case Visible_Block_type::TAKEINDGET: return &subsBlock_get;
case Visible_Block_type::TAKEINDSET: return &subsBlock_set;
case Visible_Block_type::ASSIGNMENT: return &assignmentBlock;
case Visible_Block_type::AND: return &andBlock;
case Visible_Block_type::NOT: return ¬Block;
case Visible_Block_type::OR: return &orBlock;
default: return nullptr; // will not arrive here
}
}