changeset 8:5941e6b3684c

Read map file in simulator
author Mike Pavone <pavone@retrodev.com>
date Sat, 14 Jul 2012 01:42:05 -0700
parents 86cdb799f950
children 66ea6fdd3fcb
files src/sim.tp
diffstat 1 files changed, 36 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/sim.tp	Fri Jul 13 21:48:03 2012 -0700
+++ b/src/sim.tp	Sat Jul 14 01:42:05 2012 -0700
@@ -27,10 +27,10 @@
 		new <- :in_grid in_width in_height { 
 			next_grid <- #[]
 			foreach: in_grid :index el{
-				dst_grid append: el
+				next_grid append: el
 			}
 			#{
-				grid <- []
+				grid <- in_grid
 				width <- in_width
 				height <- in_height
 				address <- :x y { x + y * width }
@@ -59,7 +59,7 @@
 			index <- 0
 			maxRow <- 0
 			curRow <- 0
-			while: {index < strLen} do {
+			while: {index < strLen} do: {
 				curByte <- str byte: index
 				if: curByte = (cellType newline) {
 					maxRow <- if: curRow > maxRow {curRow} else: {maxRow}
@@ -69,16 +69,7 @@
 			}
 			grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) (" " byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] 
 			fresh <- new: grid 3 3
-		}
-		deepCopy <- :oldState {
-			newState <- new: 
-			#{
-				// grid <- (use array copy thing)
-				width <- oldState width
-				height <- oldState height
-				address <- oldState address
-				// ... better way?
-			}
+			fresh
 		}
 	}
 
@@ -91,21 +82,40 @@
 		}
 	}
 	
-	main <- {
-		
+	readFile <- :path {
+		fd <- os open: path (os O_RDONLY)
+		print: "fd: " . fd . "\n"
+		if: fd < 0 { "" } else: {
+			cur <- ""
+			part <- ""
+			while: { 
+				part <- os read: fd 128
+				print: "read: " . part . "\n"
+				part != ""
+			} do: {
+				cur <- cur . part
+			}
+			os close: fd
+			cur
+		}
+	}
+	
+	getMove <- {
+		os read: 0 1
 	}
 
-/*
-	main <- {
-		testInput <- "derp" 
-		simState <- state fromStr: testInput
-		roboMove <- "W"
-		getMove <- testMoves:
-		while: {playing: simState roboMove} do: {
-			print: "step..."
-			roboMove <- getMove:
-			simState advance:
+	main <- :args {
+		if: (args length) < 2 {
+			print: "usage: sim filename\n"
+		} else: {
+			print: (args get: 1) . "\n"
+			text <- readFile: (args get: 1)
+			simState <- state fromStr: text
+			while: { if: (simState ended) {false} else: {true} } do: {
+				print: "step...\n"
+				simState advance: (getMove: )
+			}
 		}
 	}
-*/
+
 }