From 898f03cc3b8a7261104730518a6cf1b6eae9f6f5 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 27 May 2017 00:52:28 -0700 Subject: 3881 - allow students to turn sandboxes into recipes Thanks Juan Crispin Hernandez for the suggestion. --- 065duplex_list.mu | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to '065duplex_list.mu') diff --git a/065duplex_list.mu b/065duplex_list.mu index dea42fbc..4b16a870 100644 --- a/065duplex_list.mu +++ b/065duplex_list.mu @@ -573,3 +573,79 @@ def dump-from x:&:duplex-list:_elem [ } $print 10/newline, [---], 10/newline ] + +scenario stash-duplex-list [ + local-scope + list:&:duplex-list:num <- push 1, 0 + list <- push 2, list + list <- push 3, list + run [ + stash [list:], list + ] + trace-should-contain [ + app: list: 3 <-> 2 <-> 1 + ] +] + +def to-text in:&:duplex-list:_elem -> result:text [ + local-scope + load-ingredients + buf:&:buffer:char <- new-buffer 80 + buf <- to-buffer in, buf + result <- buffer-to-array buf +] + +# variant of 'to-text' which stops printing after a few elements (and so is robust to cycles) +def to-text-line in:&:duplex-list:_elem -> result:text [ + local-scope + load-ingredients + buf:&:buffer:char <- new-buffer 80 + buf <- to-buffer in, buf, 6 # max elements to display + result <- buffer-to-array buf +] + +def to-buffer in:&:duplex-list:_elem, buf:&:buffer:char -> buf:&:buffer:char [ + local-scope + load-ingredients + { + break-if in + buf <- append buf, [[]] + return + } + # append in.value to buf + val:_elem <- get *in, value:offset + buf <- append buf, val + # now prepare next + next:&:duplex-list:_elem <- next in + nextn:num <- copy next + return-unless next + buf <- append buf, [ <-> ] + # and recurse + remaining:num, optional-ingredient-found?:bool <- next-ingredient + { + break-if optional-ingredient-found? + # unlimited recursion + buf <- to-buffer next, buf + return + } + { + break-unless remaining + # limited recursion + remaining <- subtract remaining, 1 + buf <- to-buffer next, buf, remaining + return + } + # past recursion depth; insert ellipses and stop + append buf, [...] +] + +scenario stash-empty-duplex-list [ + local-scope + x:&:duplex-list:num <- copy 0 + run [ + stash x + ] + trace-should-contain [ + app: [] + ] +] -- cgit 1.4.1-2-gfad0