about summary refs log tree commit diff stats
path: root/shell/evaluate.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-04-22 09:03:39 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-04-22 09:05:11 -0700
commitbd0410c9935174e48590c3eaa98e83464c7560a7 (patch)
tree619b127340dfc06adb5511462c6747d504e4382b /shell/evaluate.mu
parent4e70cac9984bf1731da052c1509ff16f0783c625 (diff)
downloadmu-bd0410c9935174e48590c3eaa98e83464c7560a7.tar.gz
snapshot
It took me _way_ too long to realize that I'm not checking for errors within
the loop, and that will cause it to manifest as an infinite loop as inner
evaluations fail to run.

Debugging notes, for posterity:
  printing one row of a chessboard pattern over fake screen (chessboard screen 4 0 0 15) gets stuck in an infinite loop halfway through
    debug pattern during infinite loop: VWEX. It's still in the loop but it's not executing the body
    raw (fill_rect screen 16 0 20 4 15) works fine
    same number of calls to fill_rect work fine
    replacing calls to fill_rect with pixel inside chessboard2 works fine
    at the point of the infinite loop it's repeatedly going through the hline loop
      -- BUT it never executes the check of the loop (< lo hi) with lo=20, hi=20. Something is returning 1, but it's not inside <
    stream optimization is not implicated

  simple test case with a single loop
    (
      (globals . (
        (foo . (fn () (screen i n)
                 (while (< i n)
                   (pixel screen 4 4 i)
                   (pixel screen 5 4 i)
                   (pixel screen 6 4 i)
                   (pixel screen 7 4 i)
                   (set i (+ i 1)))))
      ))
      (sandbox . (foo screen 0 100))
    )

  simpler (if you reset cursor position before every print):
    (
      (globals . (
        (foo . (fn () (screen i n)
                 (while (< i n)
                   (print screen i)
                   (set i (+ i 1)))))
      ))
      (sandbox . (foo screen 0 210))
    )

  I now believe it has nothing to do with the check. The check always works.
  Sometimes no body is evaluated. And so the set has no effect.
Diffstat (limited to 'shell/evaluate.mu')
-rw-r--r--shell/evaluate.mu63
1 files changed, 61 insertions, 2 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu
index 79204bd7..0248b3f7 100644
--- a/shell/evaluate.mu
+++ b/shell/evaluate.mu
@@ -4,8 +4,9 @@
 # 'call-number' is just for showing intermediate progress; this is a _slow_ interpreter
 fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
   # stack overflow?   # disable when enabling Really-debug-print
-  check-stack
-  show-stack-state
+#?   check-stack
+#?   show-stack-state
+  debug-print "^", 7/fg, 0/bg
   # errors? skip
   {
     compare trace, 0
@@ -54,6 +55,7 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     trace trace, "eval", stream
   }
   # }}}
+  debug-print "!", 7/fg, 0/bg
   trace-lower trace
   var in-addr/eax: (addr cell) <- lookup *in
   {
@@ -66,6 +68,7 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     trace-higher trace
     return
   }
+  debug-print "@", 7/fg, 0/bg
   var in-type/ecx: (addr int) <- get in-addr, type
   compare *in-type, 1/number
   {
@@ -211,6 +214,12 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     compare set?, 0/false
     break-if-=
     #
+    {
+      var foo/eax: boolean <- debug-print?
+      compare foo, 0/false
+      break-if-=
+      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "set|", 7/fg, 0/bg
+    }
     trace-text trace, "eval", "set"
     trace-text trace, "eval", "evaluating second arg"
     var rest/eax: (addr cell) <- lookup *rest-ah
@@ -308,17 +317,60 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     rest-ah <- get rest, right
     var guard-h: (handle cell)
     var guard-ah/esi: (addr handle cell) <- address guard-h
+    # skip first 201 iterations without debug
+    var i/ebx: int <- copy 0
+    {
+      trace-text trace, "eval", "loop termination check"
+      debug-print "V", 4/fg, 0/bg
+      increment call-number
+      evaluate first-arg-ah, guard-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
+      debug-print "W", 4/fg, 0/bg
+      var guard-a/eax: (addr cell) <- lookup *guard-ah
+      var done?/eax: boolean <- nil? guard-a
+      compare done?, 0/false
+      break-if-!=
+      evaluate-exprs rest-ah, out, env-h, globals, trace, screen-cell, keyboard-cell, call-number
+      i <- increment
+      compare i, 0xc9/201
+      break-if->
+      loop
+    }
+    turn-on-debug-print
+    i <- copy 0
     {
       trace-text trace, "eval", "loop termination check"
+  {
+    compare trace, 0
+    break-if-=
+    var error?/eax: boolean <- has-errors? trace
+    compare error?, 0/false
+    break-if-=
+    return
+  }
+      set-cursor-position 0/screen, 0/x, i
+      i <- increment
+      compare i, 0x20
+      {
+        break-if-<
+        clear-screen 0/screen
+        i <- copy 0
+        set-cursor-position 0/screen, 0/x, i
+      }
       debug-print "V", 4/fg, 0/bg
       increment call-number
+      dump-cell-from-cursor-over-full-screen first-arg-ah
+      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "|", 7/fg, 0/bg
       evaluate first-arg-ah, guard-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
       debug-print "W", 4/fg, 0/bg
+      dump-cell-from-cursor-over-full-screen guard-ah
+      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "|", 7/fg, 0/bg
       var guard-a/eax: (addr cell) <- lookup *guard-ah
       var done?/eax: boolean <- nil? guard-a
       compare done?, 0/false
       break-if-!=
+      dump-cell-from-cursor-over-full-screen rest-ah
       evaluate-exprs rest-ah, out, env-h, globals, trace, screen-cell, keyboard-cell, call-number
+      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "]", 7/fg, 0/bg
       loop
     }
     trace-text trace, "eval", "loop terminated"
@@ -453,6 +505,13 @@ fn evaluate-exprs _exprs-ah: (addr handle cell), out: (addr handle cell), env-h:
       debug-print "E", 7/fg, 0/bg
       increment call-number
       evaluate curr-ah, out, env-h, globals, trace, screen-cell, keyboard-cell, call-number
+      {
+        var foo/eax: boolean <- debug-print?
+        compare foo, 0/false
+        break-if-=
+        dump-cell-from-cursor-over-full-screen out
+        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "|", 7/fg, 0/bg
+      }
       debug-print "X", 7/fg, 0/bg
     }
     #