From bd0410c9935174e48590c3eaa98e83464c7560a7 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 22 Apr 2021 09:03:39 -0700 Subject: 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. --- shell/evaluate.mu | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- shell/global.mu | 1 + 2 files changed, 62 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 } # diff --git a/shell/global.mu b/shell/global.mu index 649964b7..eab66bbe 100644 --- a/shell/global.mu +++ b/shell/global.mu @@ -1314,6 +1314,7 @@ fn apply-print _args-ah: (addr handle cell), out: (addr handle cell), trace: (ad var stream-storage: (stream byte 0x100) var stream/edi: (addr stream byte) <- address stream-storage print-cell second-ah, stream, trace + set-cursor-position screen, 0, 0 draw-stream-wrapping-right-then-down-from-cursor-over-full-screen screen, stream, 7/fg, 0/bg # return what was printed copy-object second-ah, out -- cgit 1.4.1-2-gfad0