-
Notifications
You must be signed in to change notification settings - Fork 0
/
deflangVM.cpp
76 lines (59 loc) · 1.89 KB
/
deflangVM.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
/*
* File: deflangVM.cpp
* Author: border2
*
* Created on 10 August 2014, 16:22
*/
#include "deflangVM.h"
deflangVM::deflangVM() {
addNativeFunction("ADD", *new funcAdd());
addNativeFunction("ADD", *new funcSubtract());
}
void deflangVM::loadInProgram(std::string program) {
deflangVM::programBus.clear();
istringstream f(program);
string s;
while (getline(f, s, '\n')) {
vector<string> programParts;
istringstream g(s);
string ss;
while (getline(g, ss, ' ')) {
programParts.push_back(ss);
}
deflangVM::programBus.push_back(programParts);
}
}
void deflangVM::exacute() {
bool doNextCycle = true;
unsigned int progPos = 0;
while(doNextCycle) {
std::vector<std::string> memBus;
memBus= deflangVM::programBus[progPos];
if(memBus.size() != 0) {
int commandPos = 0;
while(commandPos < memBus.size()) {
if(deflangVM::nativeFunctionMap.find(memBus[commandPos]) != deflangVM::nativeFunctionMap.end()) {
deflangVM::nativeFunctionMap[memBus[commandPos]].callFunction(&memBus,&commandPos,this);
} else {
commandPos++;
}
}
}
printMemBus(memBus);
progPos++;
doNextCycle = progPos < deflangVM::programBus.size();
}
}
void deflangVM::printMemBus(std::vector<std::string> memBus) {
if(memBus.size() != 0) {
int commandPos = 0;
while(commandPos < memBus.size()) {
std::cout << memBus[commandPos] << " ";
commandPos++;
}
}
std::cout << endl;
}
void deflangVM::addNativeFunction(std::string name, nativeFunction function) {
deflangVM::nativeFunctionMap[name] = function;
}