comparison code/lmc.tp @ 13:451043a65ff7

Add support for while:do. Fix lambda expressions. Fix function call expressions
author Michael Pavone <pavone@retrodev.com>
date Fri, 25 Jul 2014 21:03:36 -0700
parents 66d0858692a9
children 4bc308c03952
comparison
equal deleted inserted replaced
12:1c6d4f2642d0 13:451043a65ff7
15 } 15 }
16 } 16 }
17 missing 17 missing
18 } 18 }
19 label <- "" 19 label <- ""
20 comment <- ""
20 string <- { 21 string <- {
21 (if: label != "" { ";" . label . "\n " } else: { " " } 22 (if: label != "" { ";" . label . "\n " } else: { " " }
22 ) . name . " " . (args join: " ") 23 ) . name . " " . (args join: " ") . (
24 if: comment = "" { "" } else: { " ;" . comment})
23 } 25 }
24 } 26 }
25 } 27 }
26 _nextLabel <- 0 28 _nextLabel <- 0
27 _setLabel <- :inst { 29 _setLabel <- :inst {
149 foreach: ((args value) expressions) :idx expr { 151 foreach: ((args value) expressions) :idx expr {
150 compileExpr: expr syms: syms 152 compileExpr: expr syms: syms
151 } 153 }
152 prog setLabel: elabel 154 prog setLabel: elabel
153 } 155 }
156 _funHandlers set: "while:do" :args syms {
157 top <- prog makeLabel: "loop_top"
158 body <- prog makeLabel: "loop_body"
159 end <- prog makeLabel: "loop_end"
160 cond <- args value
161 prog setLabel: top
162 foreach: (cond expressions) :idx expr {
163 compileExpr: expr syms: syms
164 }
165 prog add: (inst: "TSEL" #[
166 body
167 end
168 ])
169 prog setLabel: body
170 blambda <- (args tail) value
171 foreach: (blambda expressions) :idx expr {
172 compileExpr: expr syms: syms
173 }
174 prog add: (inst: "LDC" #[1])
175 prog add: (inst: "TSEL" #[
176 top
177 top
178 ])
179 prog setLabel: end
180 }
154 _funHandlers set: "isInteger?" :args syms { 181 _funHandlers set: "isInteger?" :args syms {
155 compileExpr: (args value) syms: syms 182 compileExpr: (args value) syms: syms
156 prog add: (inst: "ATOM" #[]) 183 prog add: (inst: "ATOM" #[])
157 } 184 }
158 _funHandlers set: "value" :args syms { 185 _funHandlers set: "value" :args syms {
182 normal <- false 209 normal <- false
183 } else: { 210 } else: {
184 } 211 }
185 } 212 }
186 if: normal { 213 if: normal {
187 compileExpr: tc syms: syms
188 num <- 0 214 num <- 0
189 foreach: (expr args) :idx arg { 215 foreach: (expr args) :idx arg {
190 compileExpr: arg syms: syms 216 compileExpr: arg syms: syms
191 num <- num + 1 217 num <- num + 1
192 } 218 }
219 compileExpr: tc syms: syms
193 prog add: (inst: "AP" #[num]) 220 prog add: (inst: "AP" #[num])
194 } 221 }
195 } 222 }
196 223
197 _exprHandlers set: (ast sym) :expr syms { 224 _exprHandlers set: (ast sym) :expr syms {
268 end 295 end
269 end 296 end
270 ]) 297 ])
271 compileLambda: fname expr syms: syms 298 compileLambda: fname expr syms: syms
272 prog setLabel: end 299 prog setLabel: end
300 prog add: (inst: "LDF" #[fname])
273 } 301 }
274 #{ 302 #{
275 compile <- :code { 303 compile <- :code {
276 res <- parser top: code 304 res <- parser top: code
277 if: res { 305 if: res {