-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (96 loc) · 2.88 KB
/
main.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
#include <iostream>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <array>
#include <queue>
#include "utils.hpp"
#include "words.hpp"
#include "new_word.hpp"
using namespace std;
#define DATA_STACK_SIZE 256
#define MEMORY_SIZE 65536
int main()
{
int *memory = new int[MEMORY_SIZE];
int mem_end = MEMORY_SIZE - 1;
string input;
vector<string> tokens;
while (1)
{
getline(cin, input);
if (input.compare("exit") == 0)
{
break;
}
else
{
tokens = tokenize(input);
for (string &t : tokens)
{
program.push(t);
}
while (program_counter <= program.size())
{
string word = queueNth(program, program_counter);
if (check_number(word))
{
if (executing)
{
data_stack.push(stoi(word));
}
program_counter++;
}
else if (isWord(word))
{
if (executing || word.compare("ELSE") == 0 || word.compare("THEN") == 0 || word.compare(";") == 0)
{
execute_word(word, data_stack, return_stack, memory, mem_end);
}
else
{
program_counter++;
}
}
else if (isVariable(word, variables))
{
if (executing)
{
data_stack.push(findVariable(word, variables)->getAddress());
}
program_counter++;
}
else if (userWord(word, user_words))
{
for (NewWord &nw : user_words)
{
if (nw.getName().compare(word) == 0)
{
// insert definition of user word into progam queue and move
// program counter to star executing it
deleteLast(program);
string wrds = nw.getWords();
vector<string> def = tokenize(wrds);
for(string &s : def){
program.push(s);
}
break;
}
}
}
else
{
cout << "Unknown word" << endl;
program_counter++;
}
}
}
}
// print full program that was executed
while (!program.empty()) {
std::cout << program.front() << " ";
program.pop();
}
delete memory;
return 0;
}