git @ Cat's Eye Technologies Decoy / 9834bd3
`if` in evaluator. Chris Pressey 1 year, 8 months ago
3 changed file(s) with 43 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
137137
138138 * import statements
139139 * let_star statements
140 * `if`
140141 * cond
141142 * more thorough mangling
142143 * compile to file named on command line
158159
159160 ### Forms
160161
161 * `if`
162 * `=`
163 * `map`
162 * `=` -- this only works on numbers
163 * `map` -- this is in Scheme
164164 * `fold`
165 * `error`
166 * `assert`
165 * `error` -- this is in Chicken
166 * `assert` -- this is in Chicken too
167167 * Comparison operators on strings -- what does Scheme do for this?
8484 else
8585 local result = eval_expr(test, env)
8686 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)
8888 end
8989 end
9090 branch = branch.tail()
9191 end
9292 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
93101 elseif head.text() == "lambda" then
94102 local formals = ast.tail().head()
95103 local body = ast.tail().tail().head()
166166
167167 Decision-making
168168 ---------------
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
169198
170199 `cond` works.
171200