-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_if.cpp
202 lines (158 loc) · 5.76 KB
/
test_if.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <string>
#include <stack>
#include <queue>
/* ****If/Then/Else Breakdown****
*
* Condition First
* If
* Instructions If True
* Else
* Instructions If False
* Then
* Instruction That Are Ran Regardless
*
* Example:
* : is-it-zero? 0 = if ." Yes!" else ." No!" then ;
*/
/* ****Tasks****
* Handle stack underflows for the int stack
* Verify front value is a boolean
* Get front value and use to determine what will be run
* If true, run all if code
* If false, run all else code
* Run all Then code regardless
*/
/* ****Assumptions****
* Int Stack is global
* Int Stack will handle underflow
* Function is called after "if" is popped and recognized
*/
std::stack<int> intStack;
void if_then_else(std::stack<std::string> stringStack) {
std::cout << "Better make it here" << std::endl;
// Get front stack value and remove it from the int stack
int boolean = intStack.top();
intStack.pop();
//Check that front stack value is a boolean
if (boolean != 0 && boolean != -1) {
std::cout << "Top Stack Value is not a boolean" << std::endl;
return;
}
std::string current = stringStack.top();
stringStack.pop();
std::cout << "Worked like its supposed to (get first value and store in current)" << std::endl;
//If true, run all if commands and skip then commands
if (boolean) {
std::cout << "Apparently True" << std::endl;
//Run all instructions in if clause
std::stack<std::string> if_string_stack;
while (current != "else" && current != "then" && stringStack.size() != 0) {
if_string_stack.push(current);
current = stringStack.top();
stringStack.pop();
}
/*std::queue<std::string> if_string_queue = stack_to_queue(if_string_stack);
/////////////token_seperator(if_string_stack, if_string_queue);
std::string if_instructions = "";
while(!if_string_queue.empty()) {
if_instructions += " " + if_string_queue.front();
if_string_queue.pop();
}
std::cout << "Executing if instructions: " << if_instructions << std::endl;*/
std::string if_instructions = "";
while(!if_string_stack.empty()) {
if_instructions += " " + if_string_stack.top();
if_string_stack.pop();
}
std::cout << "Executing if instructions: " << if_instructions << std::endl;
//If else clause is next, skip all its instructions
if (current == "else") {
std::cout << "Found else" << std::endl;
current = stringStack.top();
stringStack.pop();
while (current != "then" && stringStack.size() != 0) {
current = stringStack.top();
stringStack.pop();
}
}
//If false, run all else commands, if found
} else {
std::cout << "Apparently False" << std::endl;
//Skip all if instructions
while (current != "else" && current != "then" && stringStack.size() != 0) {
current = stringStack.top();
stringStack.pop();
std::cout << "Got a token from if block: " << current << std::endl;
}
//If else clause is next, run all its instructions
if (current == "else") {
current = stringStack.top();
stringStack.pop();
std::stack<std::string> else_string_stack;
while (current != "then" && stringStack.size() != 0) {
else_string_stack.push(current);
current = stringStack.top();
stringStack.pop();
std::cout << "Got a token from else block: " << current << std::endl;
}
/*std::queue<std::string> else_string_queue = stack_to_queue(else_string_stack);
////////////token_seperator(else_string_stack, else_string_queue);
std::string else_instructions = "";
while(!else_string_queue.empty()) {
else_instructions += " " + else_string_queue.front();
else_string_queue.pop();
}
std::cout << "Executing else instructions: " << else_instructions << std::endl;*/
std::string else_instructions = "";
while(!else_string_stack.empty()){
else_instructions += " " + else_string_stack.top();
else_string_stack.pop();
}
std::cout << "Executing else instructions: " << else_instructions << std::endl;
}
}
//Once then is reached or no instructions are left, return the string stack to token_seperator
/*std::queue<std::string> stringQueue = stack_to_queue(stringStack);
////////////////////token_seperator(stringStack, stringQueue);
std::string rest_instructions = "";
while(!stringQueue.empty()) {
rest_instructions += " " + stringQueue.front();
stringQueue.pop();
}
std::cout << "Executing rest of instructions: " << rest_instructions << std::endl;*/
//Remove then if it remains
if(current == "then") {
stringStack.pop();
}
std::string rest_instructions = "";
while(!stringStack.empty()){
rest_instructions += " " + stringStack.top();
stringStack.pop();
}
std::cout << "Executing rest instructions: " << rest_instructions << std::endl;
}
int main() {
intStack.push(-1); //pushes false onto the stack
std::stack<std::string> stringStack;
/* stringStack.push("4");
stringStack.push("2dup");
stringStack.push("then");
stringStack.push("over");
stringStack.push("else");
stringStack.push("dup");
stringStack.push("3");
stringStack.push("if");
*/
stringStack.push("4");
stringStack.push("2dup");
stringStack.push("then");
stringStack.push("over");
stringStack.push("else");
stringStack.push("5");
stringStack.push("3");
stringStack.push("if");
std::cout << "Obviously making it here" << std::endl;
if_then_else(stringStack);
return 0;
}