`if` in evaluator.
Chris Pressey
1 year, 8 months ago
137 | 137 |
|
138 | 138 |
* import statements
|
139 | 139 |
* let_star statements
|
|
140 |
* `if`
|
140 | 141 |
* cond
|
141 | 142 |
* more thorough mangling
|
142 | 143 |
* compile to file named on command line
|
|
158 | 159 |
|
159 | 160 |
### Forms
|
160 | 161 |
|
161 | |
* `if`
|
162 | |
* `=`
|
163 | |
* `map`
|
|
162 |
* `=` -- this only works on numbers
|
|
163 |
* `map` -- this is in Scheme
|
164 | 164 |
* `fold`
|
165 | |
* `error`
|
166 | |
* `assert`
|
|
165 |
* `error` -- this is in Chicken
|
|
166 |
* `assert` -- this is in Chicken too
|
167 | 167 |
* Comparison operators on strings -- what does Scheme do for this?
|
84 | 84 |
else
|
85 | 85 |
local result = eval_expr(test, env)
|
86 | 86 |
if is_a(result, Boolean) and result.value() then
|
87 | |
return eval_expr(b.tail().head(), env)
|
|
87 |
return eval_expr(b.tail().head(), env)
|
88 | 88 |
end
|
89 | 89 |
end
|
90 | 90 |
branch = branch.tail()
|
91 | 91 |
end
|
92 | 92 |
error("No else in cond")
|
|
93 |
elseif head.text() == "if" then
|
|
94 |
local sexp = ast.tail()
|
|
95 |
local result = eval_expr(sexp.head(), env)
|
|
96 |
if not is_a(result, Boolean) or result.value() then
|
|
97 |
return eval_expr(sexp.tail().head(), env)
|
|
98 |
else
|
|
99 |
return eval_expr(sexp.tail().tail().head(), env)
|
|
100 |
end
|
93 | 101 |
elseif head.text() == "lambda" then
|
94 | 102 |
local formals = ast.tail().head()
|
95 | 103 |
local body = ast.tail().tail().head()
|
166 | 166 |
|
167 | 167 |
Decision-making
|
168 | 168 |
---------------
|
|
169 |
|
|
170 |
`if`.
|
|
171 |
|
|
172 |
(if (equal? (quote a) (quote a)) (quote true) (quote false))
|
|
173 |
===> true
|
|
174 |
|
|
175 |
(if (equal? (quote a) (quote b)) (quote true) (quote false))
|
|
176 |
===> false
|
|
177 |
|
|
178 |
All non-booleans are considered true. This at least coincides with
|
|
179 |
what Chicken tells me.
|
|
180 |
|
|
181 |
(if (list 1) (quote true) (quote false))
|
|
182 |
===> true
|
|
183 |
|
|
184 |
(if (list) (quote true) (quote false))
|
|
185 |
===> true
|
|
186 |
|
|
187 |
(if 1 (quote true) (quote false))
|
|
188 |
===> true
|
|
189 |
|
|
190 |
(if 0 (quote true) (quote false))
|
|
191 |
===> true
|
|
192 |
|
|
193 |
(if "hey" (quote true) (quote false))
|
|
194 |
===> true
|
|
195 |
|
|
196 |
(if "" (quote true) (quote false))
|
|
197 |
===> true
|
169 | 198 |
|
170 | 199 |
`cond` works.
|
171 | 200 |
|