changeset 13:c92633098f1d

simulator now properly parses map and accepts input until A is sent to it over stdin
author Mike Pavone <pavone@retrodev.com>
date Sat, 14 Jul 2012 05:15:00 -0700
parents 6ef6dc8ab95e
children 26cfb964fe81 8d93102a48bd
files Makefile src/sim.tp
diffstat 2 files changed, 38 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Jul 14 03:07:02 2012 -0700
+++ b/Makefile	Sat Jul 14 05:15:00 2012 -0700
@@ -9,7 +9,7 @@
 all : $(BINS) $(OBJS)
 
 $(OUTDIR)/% : $(OBJDIR)/%.tp.c
-	gcc -I$(TPDIR) -o $@ $< $(TPDIR)/runtime/object.c 
+	gcc -ggdb -I$(TPDIR) -o $@ $< $(TPDIR)/runtime/object.c 
 
 $(OBJDIR)/%.tp.c : $(SRCDIR)/%.tp
 	d8 $(TPC) -- -basedir $(TPDIR)/ $< > $@
--- a/src/sim.tp	Sat Jul 14 03:07:02 2012 -0700
+++ b/src/sim.tp	Sat Jul 14 05:15:00 2012 -0700
@@ -22,7 +22,7 @@
 	}
 
 	makeCellTypes <- {
-		allstr <- #[]
+		allid <- #[]
 		allobj <- #[]
 		new <- :idstr {
 			ret <- #{
@@ -30,21 +30,21 @@
 				isrobot <- { false }
 			}
 			allobj append: ret
-			allstr append: idstr
+			allid append: (ret id)
 			ret
 		}
 		#{
-			find <- :idstr {
-				if: idstr = "R" { robot: } else: {
+			find <- :id {
+				if: id = ("R" byte: 0) { robot: } else: {
 					index <- 0
 					while: { 
-						if: index < (allstr length) { 
-							(allstr get: index) != idstr
+						if: index < (allid length) { 
+							(allid get: index) != id
 						} else: {false}
 					} do: {
 						index <- index + 1
 					}
-					if: index < (allstr length) {
+					if: index < (allid length) {
 						allobj get: index
 					} else: {
 						empty
@@ -64,6 +64,9 @@
 					id <- ("R"  byte: 0) 
 					isrobot <- { true }
 					heldBreath <- 0
+					move <- :cmd {
+						cmd
+					}
 				}
 			}
 		}
@@ -86,9 +89,13 @@
 		new <- :in_grid in_width in_height { 
 			nextGrid <- #[]
 			robot <- false
+			endreached <- false
 			foreach: in_grid :index el{
 				nextGrid append: el
-				if: (el isrobot) { robot <- el } else: { true }
+				if: (el isrobot) { 
+					print: "found robot\n"
+					robot <- el 
+				} else: { true }
 			}
 			#{
 				grid <- in_grid
@@ -106,12 +113,12 @@
 				waterproof <- 10
 				collected <- 0
 				moves <- 0
-				ended <- false
+				ended <- {endreached}
 				doUpdate <- {
 					true
 				}
 				advance <- :roboCmd {
-					ended <- roboCmd = "A"
+					endreached <- roboCmd = "A"
 					robot move: roboCmd
 					moves <- moves + 1
 					doUpdate:
@@ -123,28 +130,36 @@
 		fromStr <- :str {
 
 			strLen <- str byte_length:
-			maxRow <- 0
-			curRow <- 0
+			maxCol <- 0
+			col <- 0
+			rows <- 0
+			nl <- (cellTypes newline) id
+			blank <- cellTypes empty
 			eachbyte: str :index element {
-				if: element = ((cellTypes newline) id) {
-					maxRow <- if: curRow > maxRow {curRow} else: {maxRow}					
-					curRow <- 0
+				if: element = nl {
+					maxCol <- if: col > maxCol {col} else: {maxCol}					
+					col <- 0
+					rows <- rows + 1
 				} else: {
-					curRow <- curRow + 1
+					col <- col + 1
 				}
 			}
 			
+			grid <- #[]
 			eachbyte: str :index element {
-				if: element = ((cellTypes newline) id) {
+				if: element = nl {
 					// add spaces
-					curRow <- 0
+					while: { col < maxCol } do: {
+						grid append: blank
+						col <- col + 1
+					}
+					col <- 0
 				} else: {
-					curRow = curRow + 1
+					grid append: (cellTypes find: element)
+					col = col + 1
 				}
 			}
-			grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) (" " byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] 
-			fresh <- new: grid 3 3
-			fresh
+			new: grid maxCol rows
 		}
 	}