changeset 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 1c6d4f2642d0
children ce68c8a607ee
files code/lmc.tp
diffstat 1 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/code/lmc.tp	Fri Jul 25 19:19:07 2014 -0700
+++ b/code/lmc.tp	Fri Jul 25 21:03:36 2014 -0700
@@ -17,9 +17,11 @@
 				missing
 			}
 			label <- ""
+			comment <- ""
 			string <- {
 				(if: label != "" { ";" . label . "\n  " } else: { "  " }
-				) . name . " " . (args join: " ")
+				) . name . " " . (args join: " ") . (
+				if: comment = "" { "" } else: { " ;" . comment})
 			}
 		}
 	}
@@ -151,6 +153,31 @@
 		}
 		prog setLabel: elabel
 	}
+	_funHandlers set: "while:do" :args syms {
+		top <- prog makeLabel: "loop_top"
+		body <- prog makeLabel: "loop_body"
+		end <- prog makeLabel: "loop_end"
+		cond <- args value
+		prog setLabel: top
+		foreach: (cond expressions) :idx expr {
+			compileExpr: expr syms: syms
+		}
+		prog add: (inst: "TSEL" #[
+			body
+			end
+		])
+		prog setLabel: body
+		blambda <- (args tail) value
+		foreach: (blambda expressions) :idx expr {
+			compileExpr: expr syms: syms
+		}
+		prog add: (inst: "LDC" #[1])
+		prog add: (inst: "TSEL" #[
+			top
+			top
+		])
+		prog setLabel: end
+	}
 	_funHandlers set: "isInteger?" :args syms {
 		compileExpr: (args value) syms: syms
 		prog add: (inst: "ATOM" #[])
@@ -184,12 +211,12 @@
 			}
 		}
 		if: normal {
-			compileExpr: tc syms: syms
 			num <- 0
 			foreach: (expr args) :idx arg {
 				compileExpr: arg syms: syms
 				num <- num + 1
 			}
+			compileExpr: tc syms: syms
 			prog add: (inst: "AP" #[num])
 		}
 	}
@@ -270,6 +297,7 @@
 		])
 		compileLambda: fname expr syms: syms
 		prog setLabel: end
+		prog add: (inst: "LDF" #[fname])
 	}
 	#{
 		compile <- :code {