about summary refs log tree commit diff stats
path: root/html/073list.mu.html
blob: edb8aac8ad3101f5969287723de5b89841b9cf9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!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 - 073list.mu</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="none">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
.muScenario { color: #00af00; }
.muControl { color: #c0a020; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Constant { color: #00a0a0; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.Comment { color: #9090ff; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment"># A list links up multiple objects together to make them easier to manage.</span>
<span class="Comment">#</span>
<span class="Comment"># The objects must be of the same type. If you want to store multiple types in</span>
<span class="Comment"># a single list, use an exclusive-container.</span>

<span class="muData">container</span> list:_elem [
  value:_elem
  next:address:shared:list:_elem
]

<span class="muRecipe">def</span> push x:_elem, in:address:shared:list:_elem<span class="muRecipe"> -&gt; </span>in:address:shared:list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result:address:shared:list:_elem<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(list _elem): type<span class="Delimiter">}</span>
  val:address:_elem<span class="Special"> &lt;- </span>get-address *result, <span class="Constant">value:offset</span>
  *val<span class="Special"> &lt;- </span>copy x
  next:address:address:shared:list:_elem<span class="Special"> &lt;- </span>get-address *result, <span class="Constant">next:offset</span>
  *next<span class="Special"> &lt;- </span>copy in
  <span class="muControl">return</span> result  <span class="Comment"># needed explicitly because we need to replace 'in' with 'result'</span>
]

<span class="muRecipe">def</span> first in:address:shared:list:_elem<span class="muRecipe"> -&gt; </span>result:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
]

<span class="muRecipe">def</span> rest in:address:shared:list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared:list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
]

<span class="muScenario">scenario</span> list-handling [
  run [
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>first <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>rest <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>first <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>rest <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first <span class="Constant">1</span>:address:shared:list:number
    <span class="Constant">1</span>:address:shared:list:number<span class="Special"> &lt;- </span>rest <span class="Constant">1</span>:address:shared:list:number
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># empty to empty, dust to dust..</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
  ]
]

<span class="muRecipe">def</span> to-text in:address:shared:list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared:array:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  buf:address:shared:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">80</span>
  buf<span class="Special"> &lt;- </span>to-buffer in, buf
  result<span class="Special"> &lt;- </span>buffer-to-array buf
]

<span class="Comment"># variant of 'to-text' which stops printing after a few elements (and so is robust to cycles)</span>
<span class="muRecipe">def</span> to-text-line in:address:shared:list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared:array:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  buf:address:shared:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">80</span>
  buf<span class="Special"> &lt;- </span>to-buffer in, buf, <span class="Constant">6</span>  <span class="Comment"># max elements to display</span>
  result<span class="Special"> &lt;- </span>buffer-to-array buf
]

<span class="muRecipe">def</span> to-buffer in:address:shared:list:_elem, buf:address:shared:buffer<span class="muRecipe"> -&gt; </span>buf:address:shared:buffer [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> in
    buf<span class="Special"> &lt;- </span>append buf, <span class="Constant">48/0</span>
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># append in.value to buf</span>
  val:_elem<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
  buf<span class="Special"> &lt;- </span>append buf, val
  <span class="Comment"># now prepare next</span>
  next:address:shared:list:_elem<span class="Special"> &lt;- </span>rest in
  nextn:number<span class="Special"> &lt;- </span>copy next
  <span class="muControl">return-unless</span> next
  space:character<span class="Special"> &lt;- </span>copy <span class="Constant">32/space</span>
  buf<span class="Special"> &lt;- </span>append buf, space:character
  s:address:shared:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[-&gt; ]</span>
  n:number<span class="Special"> &lt;- </span>length *s
  buf<span class="Special"> &lt;- </span>append buf, s
  <span class="Comment"># and recurse</span>
  remaining:number, optional-ingredient-found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> optional-ingredient-found?
    <span class="Comment"># unlimited recursion</span>
    buf<span class="Special"> &lt;- </span>to-buffer next, buf
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> remaining
    <span class="Comment"># limited recursion</span>
    remaining<span class="Special"> &lt;- </span>subtract remaining, <span class="Constant">1</span>
    buf<span class="Special"> &lt;- </span>to-buffer next, buf, remaining
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># past recursion depth; insert ellipses and stop</span>
  s:address:shared:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[...]</span>
  append buf, s
]

<span class="muScenario">scenario</span> stash-on-list-converts-to-text [
  run [
    x:address:shared:list:number<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">0</span>
    x<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, x
    x<span class="Special"> &lt;- </span>push <span class="Constant">6</span>, x
    stash <span class="Constant">[foo foo]</span>, x
  ]
  trace-should-contain [
    app: foo foo <span class="Constant">6</span><span class="muRecipe"> -&gt; </span><span class="Constant">5</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span>
  ]
]

<span class="muScenario">scenario</span> stash-handles-list-with-cycle [
  run [
    x:address:shared:list:number<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">0</span>
    y:address:address:shared:list:number<span class="Special"> &lt;- </span>get-address *x, <span class="Constant">next:offset</span>
    *y<span class="Special"> &lt;- </span>copy x
    stash <span class="Constant">[foo foo]</span>, x
  ]
  trace-should-contain [
    app: foo foo <span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span><span class="Constant">4</span><span class="muRecipe"> -&gt; </span>...
  ]
]
</pre>
</body>
</html>