/
Boolean.c
187 lines (169 loc) · 3.88 KB
/
Boolean.c
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
#include "Vyion.h"
/* Wrappers for checking truth values */
int IsTrue(VyBoolean** b){
if(b[0]->torf != 0){
return 1;
}else{
return 0;
}
}
int IsFalse(VyBoolean** b){
if(b[0]->torf == 0){
return 1;
}else{
return 0;
}
}
/* Creation of booleans
* Note: since all boolean values are the same, there is no need for more
* than two of them. Therefore, just return the global true or false value. */
VyBoolean** t = NULL;
VyBoolean** f = NULL;
VyBoolean** MakeTrueBool(){
if(t == NULL){
/* Create a true boolean object if one doesn't exist yet */
t = CreateBoolObj();
t[0]->torf = 1;
}
return t;
}
VyBoolean** MakeFalseBool(){
if(f == NULL){
/* Create a false boolean object if one doesn't exist yet */
f = CreateBoolObj();
f[0]->torf = 0;
}
return f;
}
/* Functions for logic operations and, or, xor, and not (non-short-circuiting) */
VyBoolean** BoolAnd(VyBoolean** one, VyBoolean** two){
if(IsTrue(one) && IsTrue(two)){
return t;
}
return f;
}
VyBoolean** BoolOr(VyBoolean** one, VyBoolean** two){
if(IsTrue(one) || IsTrue(two)){
return t;
}
return f;
}
VyBoolean** BoolXor(VyBoolean** one, VyBoolean** two){
if((IsTrue(one) && IsFalse(two)) || (IsFalse(one) && IsFalse(two))){
return t;
}
return f;
}
VyBoolean** BoolNot(VyBoolean** b){
if(IsTrue(b)){
return f;
}else{
return t;
}
}
/* Functions for comparing numbers */
VyBoolean** LessThan(VyNumber** one, VyNumber** two){
/* If they are of the same type, then compare */
if(one[0]->type == INT && two[0]->type == INT){
if(GetInt(one) < GetInt(two)){
return t;
} else{
return f;
}
}
if(one[0]->type == REAL && two[0]->type == REAL){
if(GetDouble(one) < GetDouble(two)){
return t;
} else{
return f;
}
}
/* If the types are different, make sure that one is an int */
if(one[0]->type == REAL){
return LessThan(two, one);
}
/* Guaranteed that type one is int and type two is double */
else{
if((double)(GetInt(one)) < GetDouble(two)){
return t;
}else{
return f;
}
}
}
VyBoolean** GreaterThan(VyNumber** one, VyNumber** two){
/* If they are of the same type, then compare */
if(one[0]->type == INT && two[0]->type == INT){
if(GetInt(one) > GetInt(two)){
return t;
} else{
return f;
}
}
if(one[0]->type == REAL && two[0]->type == REAL){
if(GetDouble(one) > GetDouble(two)){
return t;
} else{
return f;
}
}
/* If the types are different, make sure that one is an int */
if(one[0]->type == REAL){
return GreaterThan(two, one);
}
/* Guaranteed that type one is int and type two is double */
else{
if((double)(GetInt(one)) > GetDouble(two)){
return t;
}else{
return f;
}
}
}
VyBoolean** LessThanOrEqual(VyNumber** one, VyNumber** two){
if(IsTrue(LessThan(one, two)) || IsTrue(Equal(one, two))){
return t;
}
return f;
}
VyBoolean** GreaterThanOrEqual(VyNumber** one, VyNumber** two){
if(IsTrue(GreaterThan(one, two)) || IsTrue(Equal(one, two))){
return t;
}
return f;
}
VyBoolean** Equal(VyNumber** one, VyNumber** two){
/* Because all numbers are in simplest terms, numbers with different types are not equal */
if(one[0]->type != two[0]->type){
return f;
}
/* Now check each type */
if(one[0]->type == INT){
if(GetInt(one) == GetInt(two)){
return t;
}
}
if(one[0]->type == REAL){
if(GetDouble(one) == GetDouble(two)){
return t;
}
}
if(one[0]->type == COMPLEX){
/* Check that both the imaginary and real parts are equal */
return BoolAnd(Equal(GetReal(one), GetReal(two)), Equal(GetImaginary(one), GetImaginary(two)));
}
else {
return f;
}
}
VyBoolean** NotEqual(VyNumber** one, VyNumber** two){
return BoolNot(Equal(one, two));
}
/* Print a boolean value as either true! or false!, which are the names of the variables that represent the booleans */
void PrintBoolean(VyBoolean** b){
if(IsTrue(b)){
printf("true!");
}else{
printf("false!");
}
}