https://github.com/akkartik/mu/blob/main/405screen.mu
1
2
3
4
5
6
7
8
9 type screen {
10 num-rows: int
11 num-cols: int
12 data: (handle array screen-cell)
13 top-index: int
14 cursor-row: int
15 cursor-col: int
16 cursor-hide?: boolean
17 curr-attributes: screen-cell
18 }
19
20 type screen-cell {
21 data: grapheme
22 color: int
23 background-color: int
24 bold?: boolean
25 underline?: boolean
26 reverse?: boolean
27 blink?: boolean
28 }
29
30 fn initialize-screen screen: (addr screen), nrows: int, ncols: int {
31 var screen-addr/esi: (addr screen) <- copy screen
32 var tmp/eax: int <- copy 0
33 var dest/edi: (addr int) <- copy 0
34
35 dest <- get screen-addr, num-rows
36 tmp <- copy nrows
37 copy-to *dest, tmp
38
39 dest <- get screen-addr, num-cols
40 tmp <- copy ncols
41 copy-to *dest, tmp
42
43 {
44 var data-addr/edi: (addr handle array screen-cell) <- get screen-addr, data
45 tmp <- multiply nrows
46 populate data-addr, tmp
47 }
48
49 dest <- get screen-addr, cursor-row
50 copy-to *dest, 1
51
52 dest <- get screen-addr, cursor-col
53 copy-to *dest, 1
54
55 var tmp2/eax: (addr screen-cell) <- get screen-addr, curr-attributes
56 dest <- get tmp2, background-color
57 copy-to *dest, 7
58 }
59
60 fn screen-size screen: (addr screen) -> _/eax: int, _/ecx: int {
61 var nrows/eax: int <- copy 0
62 var ncols/ecx: int <- copy 0
63 compare screen, 0
64 {
65 break-if-!=
66 nrows, ncols <- real-screen-size
67 return nrows, ncols
68 }
69
70 var screen-addr/esi: (addr screen) <- copy screen
71 var tmp/edx: (addr int) <- get screen-addr, num-rows
72 nrows <- copy *tmp
73 tmp <- get screen-addr, num-cols
74 ncols <- copy *tmp
75 return nrows, ncols
76 }
77
78 fn clear-screen screen: (addr screen) {
79 compare screen, 0
80 {
81 break-if-!=
82 clear-real-screen
83 return
84 }
85
86 var space/edi: grapheme <- copy 0x20
87 move-cursor screen, 1, 1
88 var screen-addr/esi: (addr screen) <- copy screen
89 var i/eax: int <- copy 1
90 var nrows/ecx: (addr int) <- get screen-addr, num-rows
91 {
92 compare i, *nrows
93 break-if->
94 var j/edx: int <- copy 1
95 var ncols/ebx: (addr int) <- get screen-addr, num-cols
96 {
97 compare j, *ncols
98 break-if->
99 print-grapheme screen, space
100 j <- increment
101 loop
102 }
103 i <- increment
104 loop
105 }
106 move-cursor screen, 1, 1
107 }
108
109 fn move-cursor screen: (addr screen), row: int, column: int {
110 compare screen, 0
111 {
112 break-if-!=
113 move-cursor-on-real-screen row, column
114 return
115 }
116
117 var screen-addr/esi: (addr screen) <- copy screen
118
119 {
120 compare row, 0
121 break-if->=
122 return
123 }
124
125 {
126 compare row, 0
127 break-if-!=
128 copy-to row, 1
129 }
130
131 {
132 var nrows-addr/eax: (addr int) <- get screen-addr, num-rows
133 var nrows/eax: int <- copy *nrows-addr
134 compare row, nrows
135 break-if-<=
136 copy-to row, nrows
137 }
138
139 {
140 compare column, 0
141 break-if->=
142 return
143 }
144
145 {
146 compare column, 0
147 break-if-!=
148 copy-to column, 1
149 }
150
151 {
152 var ncols-addr/eax: (addr int) <- get screen-addr, num-cols
153 var ncols/eax: int <- copy *ncols-addr
154 compare column, ncols
155 break-if-<=
156 copy-to column, ncols
157 increment column
158 }
159
160 var dest/edi: (addr int) <- get screen-addr, cursor-row
161 var src/eax: int <- copy row
162 copy-to *dest, src
163
164 dest <- get screen-addr, cursor-col
165 src <- copy column
166 copy-to *dest, src
167 }
168
169 fn print-string screen: (addr screen), s: (addr array byte) {
170 compare screen, 0
171 {
172 break-if-!=
173 print-string-to-real-screen s
174 return
175 }
176
177 var s2: (stream byte 0x100)
178 var s2-addr/esi: (addr stream byte) <- address s2
179 write s2-addr, s
180 var screen-addr/edi: (addr screen) <- copy screen
181 {
182 var done?/eax: boolean <- stream-empty? s2-addr
183 compare done?, 0
184 break-if-!=
185 var g/eax: grapheme <- read-grapheme s2-addr
186 print-grapheme screen, g
187 loop
188 }
189 }
190
191 fn print-array-of-ints-in-decimal screen: (addr screen), _a: (addr array int) {
192 var a/esi: (addr array int) <- copy _a
193 var max/ecx: int <- length a
194 var i/eax: int <- copy 0
195 {
196 compare i, max
197 break-if->=
198 {
199 compare i, 0
200 break-if-=
201 print-string screen, " "
202 }
203 var x/ecx: (addr int) <- index a, i
204 print-int32-decimal screen, *x
205 i <- increment
206 loop
207 }
208 }
209
210 fn print-grapheme screen: (addr screen), c: grapheme {
211 compare screen, 0
212 {
213 break-if-!=
214 print-grapheme-to-real-screen c
215 return
216 }
217
218 var screen-addr/esi: (addr screen) <- copy screen
219 var cursor-col-addr/edx: (addr int) <- get screen-addr, cursor-col
220
221
222 {
223
224 var num-cols-addr/ecx: (addr int) <- get screen-addr, num-cols
225 var num-cols/ecx: int <- copy *num-cols-addr
226 compare *cursor-col-addr, num-cols
227 break-if-<=
228 copy-to *cursor-col-addr, 1
229 var cursor-row-addr/ebx: (addr int) <- get screen-addr, cursor-row
230 increment *cursor-row-addr
231
232 var num-rows-addr/eax: (addr int) <- get screen-addr, num-rows
233 var num-rows/eax: int <- copy *num-rows-addr
234 compare *cursor-row-addr, num-rows
235 break-if-<=
236 copy-to *cursor-row-addr, num-rows
237
238 $print-grapheme:perform-scroll: {
239 var top-index-addr/ebx: (addr int) <- get screen-addr, top-index
240 var data-ah/eax: (addr handle array screen-cell) <- get screen-addr, data
241 var data/eax: (addr array screen-cell) <- lookup *data-ah
242 var max-index/edi: int <- length data
243 compare *top-index-addr, max-index
244 {
245 break-if->=
246 add-to *top-index-addr, num-cols
247 break $print-grapheme:perform-scroll
248 }
249 {
250 break-if-<
251 copy-to *top-index-addr, 0
252 }
253 }
254 }
255 var idx/ecx: int <- current-screen-cell-index screen-addr
256
257
258
259 var data-ah/eax: (addr handle<!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 - factorial.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #aaaaaa; background-color: #080808; }
a { color:#eeeeee; text-decoration: none; }
a:hover { text-decoration: underline; }
* { font-size: 12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
.Special { color: #c00000; }
.Conceal { color: #4e4e4e; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Comment a { color:#0000ee; text-decoration:underline; }
.Constant { color: #00a0a0; }
.LineNr { color: #444444; }
.muControl { color: #c0a020; }
-->
</style>
<script type='text/javascript'>
<!--
/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
var lineNum;
lineNum = window.location.hash;
lineNum = lineNum.substr(1); /* strip off '#' */
if (lineNum.indexOf('L') == -1) {
lineNum = 'L'+lineNum;
}
lineElem = document.getElementById(lineNum);
/* Always jump to new location even if the line was hidden inside a fold, or
* we corrected the raw number to a line ID.
*/
if (lineElem) {
lineElem.scrollIntoView(true);
}
return true;
}
if ('onhashchange' in window) {
window.onhashchange = JumpToLine;
}
-->
</script>
</head>
<body onload='JumpToLine();'>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># example program: compute the factorial of 5</span>
<span id="L2" class="LineNr"> 2 </span>
<span id="L3" class="LineNr"> 3 </span><span class="muRecipe">def</span> <a href='factorial.mu.html#L3'>main</a> [
<span id="L4" class="LineNr"> 4 </span> <span class="Constant">local-scope</span>
<span id="L5" class="LineNr"> 5 </span> x:num <span class="Special"><-</span> <a href='factorial.mu.html#L10'>factorial</a><span class="Constant"> 5</span>
<span id="L6" class="LineNr"> 6 </span> $print <span class="Constant">[result: ]</span>, x, <span class="Constant">[ </span>
<span id="L7" class="LineNr"> 7 </span><span class="Constant">]</span>
<span id="L8" class="LineNr"> 8 </span>]
<span id="L9" class="LineNr"> 9 </span>
<span id="L10" class="LineNr">10 </span><span class="muRecipe">def</span> <a href='factorial.mu.html#L10'>factorial</a> n:num<span class="muRecipe"> -> </span>result:num [
<span id="L11" class="LineNr">11 </span> <span class="Constant">local-scope</span>
<span id="L12" class="LineNr">12 </span> <span class="Constant">load-ingredients</span>
<span id="L13" class="LineNr">13 </span> <span class="Delimiter">{</span>
<span id="L14" class="LineNr">14 </span> <span class="Conceal">¦</span> <span class="Comment"># if n=0 return 1</span>
<span id="L15" class="LineNr">15 </span> <span class="Conceal">¦</span> zero?:bool <span class="Special"><-</span> equal n,<span class="Constant"> 0</span>
<span id="L16" class="LineNr">16 </span> <span class="Conceal">¦</span> <span class="muControl">break-unless</span> zero?
<span id="L17" class="LineNr">17 </span> <span class="Conceal">¦</span> <span class="muControl">return</span><span class="Constant"> 1</span>
<span id="L18" class="LineNr">18 </span> <span class="Delimiter">}</span>
<span id="L19" class="LineNr">19 </span> <span class="Comment"># return n * factorial(n-1)</span>
<span id="L20" class="LineNr">20 </span> x:num <span class="Special"><-</span> subtract n,<span class="Constant"> 1</span>
<span id="L21" class="LineNr">21 </span> subresult:num <span class="Special"><-</span> <a href='factorial.mu.html#L10'>factorial</a> x
<span id="L22" class="LineNr">22 </span> result <span class="Special"><-</span> multiply subresult, n
<span id="L23" class="LineNr">23 </span>]
<span id="L24" class="LineNr">24 </span>
<span id="L25" class="LineNr">25 </span><span class="Comment"># unit test</span>
<span id="L26" class="LineNr">26 </span><span class="muScenario">scenario</span> factorial-test [
<span id="L27" class="LineNr">27 </span> run [
<span id="L28" class="LineNr">28 </span> <span class="Conceal">¦</span> 1:num <span class="Special"><-</span> <a href='factorial.mu.html#L10'>factorial</a><span class="Constant"> 5</span>
<span id="L29" class="LineNr">29 </span> ]
<span id="L30" class="LineNr">30 </span> memory-should-contain [
<span id="L31" class="LineNr">31 </span> <span class="Conceal">¦</span><span class="Constant"> 1</span> <span class="Special"><-</span><span class="Constant"> 120</span>
<span id="L32" class="LineNr">32 </span> ]
<span id="L33" class="LineNr">33 </span>]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->