about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-01-29 23:19:22 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-01-29 23:19:22 -0800
commit526e9b70d72c1674d8ccd3dc042f04491188e69a (patch)
tree6ae4c8b385b11f20d63c4127fbc4edaf7cc1fccf
parent8b2ffc8ce73083b85e2a7a8039b2c3bac1bd368f (diff)
downloadmu-526e9b70d72c1674d8ccd3dc042f04491188e69a.tar.gz
682
You can't just extract the array from inside a buffer. Its length isn't
right. Only reason we didn't catch this sooner is I think that arc's
simulated memory is initialized to all nils, which has some
serendipitous properties.

I should initialize memory to random values one of these days and see
what shakes out.
-rw-r--r--color-repl.mu8
-rw-r--r--mu.arc37
2 files changed, 41 insertions, 4 deletions
diff --git a/color-repl.mu b/color-repl.mu
index 69ca7651..c5686484 100644
--- a/color-repl.mu
+++ b/color-repl.mu
@@ -10,7 +10,7 @@
   (default-space:space-address <- new space:literal 30:literal)
   (abort:continuation <- next-input)
   (history:buffer-address <- next-input)  ; buffer of strings
-  (result:buffer-address <- init-buffer 3:literal)  ; string to maybe add to
+  (result:buffer-address <- init-buffer 10:literal)  ; string to maybe add to
   (open-parens:integer <- copy 0:literal)  ; for balancing parens and tracking nesting depth
   ; we can change color when backspacing over parens or comments or strings,
   ; but we need to know that they aren't escaped
@@ -24,7 +24,8 @@
     (c:character <- $wait-for-key-from-host)
     (done?:boolean <- process-key default-space:space-address c:character)
   }
-  (s:string-address <- get result:buffer-address/deref data:offset)
+  ; test: 3<enter> => size of s is 2
+  (s:string-address <- to-array result:buffer-address)
   (reply s:string-address)
 ])
 
@@ -370,6 +371,9 @@
   { begin
     (s:string-address <- read-expression abort:continuation history:buffer-address)
     (break-unless s:string-address)
+;?     (x:integer <- length s:string-address/deref) ;? 1
+;?     (print-primitive-to-host x:integer) ;? 1
+;?     (print-primitive-to-host ((#\newline literal))) ;? 1
     (history:buffer-address <- append history:buffer-address s:string-address)
     (len:integer <- get history:buffer-address/deref length:offset)
     (print-primitive-to-host len:integer)
diff --git a/mu.arc b/mu.arc
index 7d195bb7..5ff72949 100644
--- a/mu.arc
+++ b/mu.arc
@@ -160,6 +160,8 @@
               character-address (obj size 1  address t  elem '(character))
               ; a buffer makes it easy to append to a string/array
               ; todo: make this generic
+              ; data isn't a 'real' array: its length is stored outside it,
+              ; so for example, 'print-string' won't work on it.
               buffer (obj size 2  and-record t  elems '((integer) (string-address))  fields '(length data))
               buffer-address (obj size 1  address t  elem '(buffer))
               ; isolating function calls
@@ -447,6 +449,8 @@
   (cdr operand.0))
 
 (def literal? (operand)
+  (unless (acons ty.operand)
+    (err "no type in operand @operand"))
   (in ty.operand.0 'literal 'offset 'fn))
 
 (def typeinfo (operand)
@@ -2392,6 +2396,35 @@
   (reply result:boolean)
 )
 
+(init-fn to-array  ; from buffer
+  (default-space:space-address <- new space:literal 30:literal)
+  (in:buffer-address <- next-input)
+  (len:integer <- get in:buffer-address/deref length:offset)
+  (s:string-address <- get in:buffer-address/deref data:offset)
+  { begin
+    ; test: ctrl-d -> s is nil -> to-array returns nil -> read-expression returns t -> exit repl
+    (break-if s:string-address)
+    (reply nil:literal)
+  }
+  ; we can't just return s because it is usually the wrong length
+  (result:string-address <- new string:literal len:integer)
+  (i:integer <- copy 0:literal)
+  { begin
+    (done?:boolean <- greater-or-equal i:integer len:integer)
+    (break-if done?:boolean)
+    (src:byte <- index s:string-address/deref i:integer)
+;?     (foo:integer <- character-to-integer src:byte) ;? 1
+;?     (print-primitive-to-host (("a: " literal))) ;? 1
+;?     (print-primitive-to-host foo:integer) ;? 1
+;?     (print-primitive-to-host ((#\newline literal))) ;? 1
+    (dest:byte-address <- index-address result:string-address/deref i:integer)
+    (dest:byte-address/deref <- copy src:byte)
+    (i:integer <- add i:integer 1:literal)
+    (loop)
+  }
+  (reply result:string-address)
+)
+
 (init-fn append
   (default-space:space-address <- new space:literal 30:literal)
   (in:buffer-address <- next-input)
@@ -2546,11 +2579,11 @@
 ;? (new-trace "main") ;? 4
 (awhen (pos "--" argv)
   (map add-code:readfile (cut argv (+ it 1)))
-;?   (= dump-trace* (obj whitelist '("run")))
+;?   (= dump-trace* (obj whitelist '("run"))) ;? 1
 ;?   (= dump-trace* (obj whitelist '("schedule")))
 ;?   (= dump-trace* (obj whitelist '("run" "continuation"))) ;? 1
 ;?   (= dump-trace* (obj whitelist '("cn0" "cn1")))
-;?   (set dump-trace*) ;? 1
+;?   (set dump-trace*) ;? 2
 ;?   (freeze function*)
 ;?   (prn function*!factorial)
   (run 'main)