<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 071print.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muRecipe { color: #ff8700; }
.Delimiter { color: #a04060; }
.muScenario { color: #00af00; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.CommentedCode { color: #6c6c6c; }
.muControl { color: #c0a020; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Wrappers around print primitives that take a 'screen' object and are thus</span>
<span class="Comment"># easier to test.</span>
container screen [
num-rows:number
num-columns:number
cursor-row:number
cursor-column:number
data:address:array:screen-cell
]
container screen-cell [
contents:character
color:number
]
<span class="muRecipe">recipe</span> new-fake-screen [
<span class="Constant">local-scope</span>
result:address:screen<span class="Special"> <- </span>new screen:type
width:address:number<span class="Special"> <- </span>get-address *result, num-columns:offset
*width<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
height:address:number<span class="Special"> <- </span>get-address *result, num-rows:offset
*height<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
row:address:number<span class="Special"> <- </span>get-address *result, cursor-row:offset
*row<span class="Special"> <- </span>copy <span class="Constant">0</span>
column:address:number<span class="Special"> <- </span>get-address *result, cursor-column:offset
*column<span class="Special"> <- </span>copy <span class="Constant">0</span>
bufsize:number<span class="Special"> <- </span>multiply *width, *height
buf:address:address:array:screen-cell<span class="Special"> <- </span>get-address *result, data:offset
*buf<span class="Special"> <- </span>new screen-cell:type, bufsize
clear-screen result
<span class="muControl">reply</span> result
]
<span class="muRecipe">recipe</span> clear-screen [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
<span class="Comment"># clear fake screen</span>
buf:address:array:screen-cell<span class="Special"> <- </span>get *sc, data:offset
max:number<span class="Special"> <- </span>length *buf
i:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal i, max
<span class="muControl">break-if</span> done?
curr:address:screen-cell<span class="Special"> <- </span>index-address *buf, i
curr-content:address:character<span class="Special"> <- </span>get-address *curr, contents:offset
*curr-content<span class="Special"> <- </span>copy <span class="Constant">[ ]</span>
curr-color:address:character<span class="Special"> <- </span>get-address *curr, color:offset
*curr-color<span class="Special"> <- </span>copy <span class="Constant">7/white</span>
i<span class="Special"> <- </span>add i, <span class="Constant">1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="Comment"># reset cursor</span>
x:address:number<span class="Special"> <- </span>get-address *sc, cursor-row:offset
*x<span class="Special"> <- </span>copy <span class="Constant">0</span>
x<span class="Special"> <- </span>get-address *sc, cursor-column:offset
*x<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
clear-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> fake-screen-is-empty? [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="muControl">reply-unless</span> sc, <span class="Constant">1/true</span>
buf:address:array:screen-cell<span class="Special"> <- </span>get *sc, data:offset
i:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
len:number<span class="Special"> <- </span>length *buf
<span class="Delimiter">{</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal i, len
<span class="muControl">break-if</span> done?
curr:screen-cell<span class="Special"> <- </span>index *buf, i
curr-contents:character<span class="Special"> <- </span>get curr, contents:offset
i<span class="Special"> <- </span>add i, <span class="Constant">1</span>
<span class="muControl">loop-unless</span> curr-contents
<span class="Comment"># not 0</span>
<span class="muControl">reply</span> <span class="Constant">0/false</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> <span class="Constant">1/true</span>
]
<span class="muRecipe">recipe</span> print-character [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
c:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
color:number, color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color<span class="Special"> <- </span>copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:number, bg-color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color<span class="Special"> <- </span>copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">#? trace [app], [print character] #? 1</span>
<span class="Delimiter">{</span>
<span class="Comment"># if x exists</span>
<span class="Comment"># (handle special cases exactly like in the real screen)</span>
<span class="muControl">break-unless</span> sc
width:number<span class="Special"> <- </span>get *sc, num-columns:offset
height:number<span class="Special"> <- </span>get *sc, num-rows:offset
<span class="Comment"># if cursor is out of bounds, silently exit</span>
row:address:number<span class="Special"> <- </span>get-address *sc, cursor-row:offset
legal?:boolean<span class="Special"> <- </span>greater-or-equal *row, <span class="Constant">0</span>
<span class="muControl">reply-unless</span> legal?, sc
legal?<span class="Special"> <- </span>lesser-than *row, height
<span class="muControl">reply-unless</span> legal?, sc
column:address:number<span class="Special"> <- </span>get-address *sc, cursor-column:offset
legal?<span class="Special"> <- </span>greater-or-equal *column, <span class="Constant">0</span>
<span class="muControl">reply-unless</span> legal?, sc
legal?<span class="Special"> <- </span>lesser-than *column, width
<span class="muControl">reply-unless</span> legal?, sc
<span class="Comment"># special-case: newline</span>
<span class="Delimiter">{</span>
newline?:boolean<span class="Special"> <- </span>equal c, <span class="Constant">10/newline</span>
<span class="CommentedCode">#? $print c, [ ], newline?, 10/newline</span>
<span class="muControl">break-unless</span> newline?
<span class="Delimiter">{</span>
<span class="Comment"># unless cursor is already at bottom</span>
bottom:number<span class="Special"> <- </span>subtract height, <span class="Constant">1</span>
at-bottom?:boolean<span class="Special"> <- </span>greater-or-equal *row, bottom
<span class="muControl">break-if</span> at-bottom?
<span class="Comment"># move it to the next row</span>
*column<span class="Special"> <- </span>copy <span class="Constant">0</span>
*row<span class="Special"> <- </span>add *row, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># save character in fake screen</span>
index:number<span class="Special"> <- </span>multiply *row, width
index<span class="Special"> <- </span>add index, *column
buf:address:array:screen-cell<span class="Special"> <- </span>get *sc, data:offset
len:number<span class="Special"> <- </span>length *buf
<span class="Comment"># special-case: backspace</span>
<span class="Delimiter">{</span>
backspace?:boolean<span class="Special"> <- </span>equal c, <span class="Constant">8</span>
<span class="muControl">break-unless</span> backspace?
<span class="Delimiter">{</span>
<span class="Comment"># unless cursor is already at left margin</span>
at-left?:boolean<span class="Special"> <- </span>lesser-or-equal *column, <span class="Constant">0</span>
<span class="muControl">break-if</span> at-left?
<span class="Comment"># clear previous location</span>
*column<span class="Special"> <- </span>subtract *column, <span class="Constant">1</span>
index<span class="Special"> <- </span>subtract index, <span class="Constant">1</span>
cursor:address:screen-cell<span class="Special"> <- </span>index-address *buf, index
cursor-contents:address:character<span class="Special"> <- </span>get-address *cursor, contents:offset
*cursor-contents<span class="Special"> <- </span>copy <span class="Constant">32/space</span>
cursor-color:address:number<span class="Special"> <- </span>get-address *cursor, color:offset
*cursor-color<span class="Special"> <- </span>copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">#? $print [saving character ], c, [ to fake screen ], cursor, 10/newline</span>
cursor:address:screen-cell<span class="Special"> <- </span>index-address *buf, index
cursor-contents:address:character<span class="Special"> <- </span>get-address *cursor, contents:offset
*cursor-contents<span class="Special"> <- </span>copy c
cursor-color:address:number<span class="Special"> <- </span>get-address *cursor, color:offset
*cursor-color<span class="Special"> <- </span>copy color
<span class="Comment"># increment column unless it's already all the way to the right</span>
<span class="Delimiter">{</span>
right:number<span class="Special"> <- </span>subtract width, <span class="Constant">1</span>
at-right?:boolean<span class="Special"> <- </span>greater-or-equal *column, right
<span class="muControl">break-if</span> at-right?
*column<span class="Special"> <- </span>add *column, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
print-character-to-display c, color, bg-color
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muScenario">scenario</span> print-character-at-top-left [
run [
<span class="CommentedCode">#? $start-tracing #? 3</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">2</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">3</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">2</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-character-color [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97/a</span>, <span class="Constant">1/red</span>
<span class="Constant">2</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">3</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">2</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># red</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-backspace-character [
run [
<span class="CommentedCode">#? $start-tracing #? 3</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">8</span> <span class="Comment"># backspace</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
<span class="Constant">3</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">4</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">3</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># cursor column</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">32</span> <span class="Comment"># space, not 'a'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-extra-backspace-character [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">8</span> <span class="Comment"># backspace</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">8</span> <span class="Comment"># backspace</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
<span class="Constant">3</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">4</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">3</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># cursor column</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">32</span> <span class="Comment"># space, not 'a'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-at-right-margin [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">2/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">98</span> <span class="Comment"># 'b'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">99</span> <span class="Comment"># 'c'</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
<span class="Constant">3</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">4</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">3</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># cursor column</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">4</span> <span class="Comment"># width*height</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">99</span> <span class="Comment"># 'c' over 'b'</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-newline-character [
run [
<span class="CommentedCode">#? $start-tracing #? 3</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-row:offset
<span class="Constant">3</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
<span class="Constant">4</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">5</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">4</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># cursor row</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># cursor column</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muScenario">scenario</span> print-newline-at-bottom-line [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-row:offset
<span class="Constant">3</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># cursor row</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># cursor column</span>
]
]
<span class="muScenario">scenario</span> print-at-bottom-right [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">2/width</span>, <span class="Constant">2/height</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">98</span> <span class="Comment"># 'b'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">99</span> <span class="Comment"># 'c'</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">10/newline</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">100</span> <span class="Comment"># 'd'</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-row:offset
<span class="Constant">3</span>:number<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, cursor-column:offset
<span class="Constant">4</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">5</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">4</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># cursor row</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Comment"># cursor column</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">4</span> <span class="Comment"># width*height</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">10</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">11</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">12</span><span class="Special"> <- </span><span class="Constant">100</span> <span class="Comment"># 'd' over 'b' and 'c' and newline</span>
<span class="Constant">13</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">14</span><span class="Special"> <- </span><span class="Constant">0</span>
]
]
<span class="muRecipe">recipe</span> clear-line [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, clear line in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
width:number<span class="Special"> <- </span>get *sc, num-columns:offset
column:address:number<span class="Special"> <- </span>get-address *sc, cursor-column:offset
original-column:number<span class="Special"> <- </span>copy *column
<span class="Comment"># space over the entire line</span>
<span class="CommentedCode">#? $start-tracing #? 1</span>
<span class="Delimiter">{</span>
<span class="CommentedCode">#? $print *column, 10/newline</span>
right:number<span class="Special"> <- </span>subtract width, <span class="Constant">1</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal *column, right
<span class="muControl">break-if</span> done?
print-character sc, <span class="Constant">[ ]</span> <span class="Comment"># implicitly updates 'column'</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="Comment"># now back to where the cursor was</span>
*column<span class="Special"> <- </span>copy original-column
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
clear-line-on-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-position [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, lookup cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
row:number<span class="Special"> <- </span>get *sc, cursor-row:offset
column:number<span class="Special"> <- </span>get *sc, cursor-column:offset
<span class="muControl">reply</span> row, column, sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
row, column<span class="Special"> <- </span>cursor-position-on-display
<span class="muControl">reply</span> row, column, sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> move-cursor [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
new-row:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
new-column:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
row:address:number<span class="Special"> <- </span>get-address *sc, cursor-row:offset
*row<span class="Special"> <- </span>copy new-row
column:address:number<span class="Special"> <- </span>get-address *sc, cursor-column:offset
*column<span class="Special"> <- </span>copy new-column
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
move-cursor-on-display new-row, new-column
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muScenario">scenario</span> clear-line-erases-printed-characters [
run [
<span class="CommentedCode">#? $start-tracing #? 4</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Comment"># print a character</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-character <span class="Constant">1</span>:address:screen, <span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Comment"># move cursor to start of line</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>move-cursor <span class="Constant">1</span>:address:screen, <span class="Constant">0/row</span>, <span class="Constant">0/column</span>
<span class="Comment"># clear line</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>clear-line <span class="Constant">1</span>:address:screen
<span class="Constant">2</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">3</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">2</span>:address:array:screen-cell
]
<span class="Comment"># screen should be blank</span>
memory-should-contain [
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">7</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">7</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">7</span>
<span class="Constant">10</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">11</span><span class="Special"> <- </span><span class="Constant">7</span>
<span class="Constant">12</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">13</span><span class="Special"> <- </span><span class="Constant">7</span>
<span class="Constant">14</span><span class="Special"> <- </span><span class="Constant">0</span>
<span class="Constant">15</span><span class="Special"> <- </span><span class="Constant">7</span>
]
]
<span class="muRecipe">recipe</span> cursor-down [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
<span class="Delimiter">{</span>
<span class="Comment"># if row < height-1</span>
height:number<span class="Special"> <- </span>get *sc, num-rows:offset
row:address:number<span class="Special"> <- </span>get-address *sc, cursor-row:offset
max:number<span class="Special"> <- </span>subtract height, <span class="Constant">1</span>
at-bottom?:boolean<span class="Special"> <- </span>greater-or-equal *row, max
<span class="muControl">break-if</span> at-bottom?
<span class="Comment"># row = row+1</span>
*row<span class="Special"> <- </span>add *row, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
move-cursor-down-on-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-up [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
<span class="Delimiter">{</span>
<span class="Comment"># if row > 0</span>
row:address:number<span class="Special"> <- </span>get-address *sc, cursor-row:offset
at-top?:boolean<span class="Special"> <- </span>lesser-or-equal *row, <span class="Constant">0</span>
<span class="muControl">break-if</span> at-top?
<span class="Comment"># row = row-1</span>
*row<span class="Special"> <- </span>subtract *row, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
move-cursor-up-on-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-right [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
<span class="Delimiter">{</span>
<span class="Comment"># if column < width-1</span>
width:number<span class="Special"> <- </span>get *sc, num-columns:offset
column:address:number<span class="Special"> <- </span>get-address *sc, cursor-column:offset
max:number<span class="Special"> <- </span>subtract width, <span class="Constant">1</span>
at-bottom?:boolean<span class="Special"> <- </span>greater-or-equal *column, max
<span class="muControl">break-if</span> at-bottom?
<span class="Comment"># column = column+1</span>
*column<span class="Special"> <- </span>add *column, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
move-cursor-right-on-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-left [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
<span class="Delimiter">{</span>
<span class="Comment"># if column > 0</span>
column:address:number<span class="Special"> <- </span>get-address *sc, cursor-column:offset
at-top?:boolean<span class="Special"> <- </span>lesser-or-equal *column, <span class="Constant">0</span>
<span class="muControl">break-if</span> at-top?
<span class="Comment"># column = column-1</span>
*column<span class="Special"> <- </span>subtract *column, <span class="Constant">1</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
move-cursor-left-on-display
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-to-start-of-line [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
row:number, _, sc<span class="Special"> <- </span>cursor-position sc
column:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
sc<span class="Special"> <- </span>move-cursor sc, row, column
<span class="muControl">reply</span> sc/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> cursor-to-next-line [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
screen<span class="Special"> <- </span>cursor-down screen
screen<span class="Special"> <- </span>cursor-to-start-of-line screen
<span class="muControl">reply</span> screen/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muRecipe">recipe</span> screen-width [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
width:number<span class="Special"> <- </span>get *sc, num-columns:offset
<span class="muControl">reply</span> width
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
width:number<span class="Special"> <- </span>display-width
<span class="muControl">reply</span> width
]
<span class="muRecipe">recipe</span> screen-height [
<span class="Constant">local-scope</span>
sc:address:screen<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists, move cursor in fake screen</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> sc
height:number<span class="Special"> <- </span>get *sc, num-rows:offset
<span class="muControl">reply</span> height
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
height:number<span class="Special"> <- </span>display-height
<span class="muControl">reply</span> height
]
<span class="muRecipe">recipe</span> hide-cursor [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists (not real display), do nothing</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="muControl">reply</span> screen
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
hide-cursor-on-display
<span class="muControl">reply</span> screen
]
<span class="muRecipe">recipe</span> show-cursor [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists (not real display), do nothing</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="muControl">reply</span> screen
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
show-cursor-on-display
<span class="muControl">reply</span> screen
]
<span class="muRecipe">recipe</span> hide-screen [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists (not real display), do nothing</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="muControl">reply</span> screen
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
hide-display
<span class="muControl">reply</span> screen
]
<span class="muRecipe">recipe</span> show-screen [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Comment"># if x exists (not real display), do nothing</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="muControl">reply</span> screen
<span class="Delimiter">}</span>
<span class="Comment"># otherwise, real screen</span>
show-display
<span class="muControl">reply</span> screen
]
<span class="muRecipe">recipe</span> print-string [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
s:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
color:number, color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color<span class="Special"> <- </span>copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:number, bg-color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color<span class="Special"> <- </span>copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
len:number<span class="Special"> <- </span>length *s
i:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal i, len
<span class="muControl">break-if</span> done?
c:character<span class="Special"> <- </span>index *s, i
print-character screen, c, color, bg-color
i<span class="Special"> <- </span>add i, <span class="Constant">1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="muControl">reply</span> screen/same-as-ingredient:<span class="Constant">0</span>
]
<span class="muScenario">scenario</span> print-string-stops-at-right-margin [
run [
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Constant">2</span>:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abcd]</span>
<span class="Constant">1</span>:address:screen<span class="Special"> <- </span>print-string <span class="Constant">1</span>:address:screen, <span class="Constant">2</span>:address:array:character
<span class="Constant">3</span>:address:array:screen-cell<span class="Special"> <- </span>get *<span class="Constant">1</span>:address:screen, data:offset
<span class="Constant">4</span>:array:screen-cell<span class="Special"> <- </span>copy *<span class="Constant">3</span>:address:array:screen-cell
]
memory-should-contain [
<span class="Constant">4</span><span class="Special"> <- </span><span class="Constant">6</span> <span class="Comment"># width*height</span>
<span class="Constant">5</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span>
<span class="Constant">6</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">7</span><span class="Special"> <- </span><span class="Constant">98</span> <span class="Comment"># 'b'</span>
<span class="Constant">8</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">9</span><span class="Special"> <- </span><span class="Constant">100</span> <span class="Comment"># 'd' overwrites 'c'</span>
<span class="Constant">10</span><span class="Special"> <- </span><span class="Constant">7</span> <span class="Comment"># white</span>
<span class="Constant">11</span><span class="Special"> <- </span><span class="Constant">0</span> <span class="Comment"># unused</span>
]
]
<span class="muRecipe">recipe</span> print-integer [
<span class="Constant">local-scope</span>
screen:address<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
n:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
color:number, color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color<span class="Special"> <- </span>copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:number, bg-color-found?:boolean<span class="Special"> <- </span><span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color<span class="Special"> <- </span>copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
<span class="Comment"># todo: other bases besides decimal</span>
s:address:array:character<span class="Special"> <- </span>integer-to-decimal-string n
print-string screen, s, color, bg-color
<span class="muControl">reply</span> screen/same-as-ingredient:<span class="Constant">0</span>
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->