-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif_else_then.cpp
116 lines (97 loc) · 3.02 KB
/
if_else_then.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
#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 top value is a boolean
* Get top 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
*/
void if_then_else(std::stack<std::string> stringStack) {
// Get top stack value
// ***************int boolean = intStack.top();
int boolean = 0;
//Check that top 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();
//If true, run all if commands and skip then commands
if (boolean) {
//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);
//If else clause is next, skip all its instructions
if (current == "else") {
while (current != "then" || stringStack.size() != 0) {
current = stringStack.top();
stringStack.pop();
}
}
//If false, run all else commands, if found
} else {
//Skip all if instructions
while (current != "else" || current != "then" || stringStack.size() != 0) {
current = stringStack.top();
stringStack.pop();
}
//If else clause is next, run all its instructions
std::stack<std::string> if_string_stack;
if (current == "else") {
while (current != "then" || stringStack.size() != 0) {
else_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);
}
}
//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);
}
int main(){
std::stack<std::string> stringStack;
stringStack.push("dup");
stringStack.push("else");
std::string current = stringStack.top();
stringStack.pop();
std::cout << current << std::endl;
current = stringStack.top();
stringStack.pop();
std::cout << current << std::endl;
current = stringStack.top();
stringStack.pop();
std::cout << current << std::endl;
return 0;
}