about summary refs log tree commit diff stats
path: root/apps/tile
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-26 22:35:20 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-26 23:45:40 -0700
commit0f9a65dc0d10d93319eacf8ccff75a068e5f97a8 (patch)
treea34427229e12d9cbea59d71d4a94825ee24f4c85 /apps/tile
parentf3d1929033856ac12db84435c317c5a288f898b3 (diff)
downloadmu-0f9a65dc0d10d93319eacf8ccff75a068e5f97a8.tar.gz
7120 - tile: array of lines from file
Requires a quick hacky change to Mu compiler.
Diffstat (limited to 'apps/tile')
-rw-r--r--apps/tile/environment.mu6
-rw-r--r--apps/tile/rpn.mu46
-rw-r--r--apps/tile/value-stack.mu26
3 files changed, 77 insertions, 1 deletions
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
index 230fc1be..971e8d9f 100644
--- a/apps/tile/environment.mu
+++ b/apps/tile/environment.mu
@@ -790,6 +790,10 @@ fn bound-function? w: (addr word), functions-ah: (addr handle function) -> resul
   subresult <- word-equal? w, "slurp"
   compare subresult, 0  # false
   break-if-!=
+  # if w == "lines" return true
+  subresult <- word-equal? w, "lines"
+  compare subresult, 0  # false
+  break-if-!=
   # if w == "dup" return true
   subresult <- word-equal? w, "dup"
   compare subresult, 0  # false
@@ -1558,7 +1562,7 @@ fn clear-canvas _env: (addr environment) {
   move-cursor screen, 2, start-col
   print-string screen, "+ - * len"
   move-cursor screen, 3, start-col
-  print-string screen, "open read slurp"
+  print-string screen, "open read slurp lines"
   move-cursor screen, 4, start-col
   print-string screen, "dup swap"
   # currently defined functions
diff --git a/apps/tile/rpn.mu b/apps/tile/rpn.mu
index 7b65b71c..2529e5ca 100644
--- a/apps/tile/rpn.mu
+++ b/apps/tile/rpn.mu
@@ -206,6 +206,52 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         copy-handle empty, target-file-ah
         break $evaluate:process-word
       }
+      {
+        var is-lines?/eax: boolean <- stream-data-equal? curr-stream, "lines"
+        compare is-lines?, 0
+        break-if-=
+        # pop target-val from out
+        var out2/esi: (addr value-stack) <- copy out
+        var top-addr/ecx: (addr int) <- get out2, top
+        compare *top-addr, 0
+        break-if-<=
+        var data-ah/eax: (addr handle array value) <- get out2, data
+        var data/eax: (addr array value) <- lookup *data-ah
+        var top/edx: int <- copy *top-addr
+        top <- decrement
+        var dest-offset/edx: (offset value) <- compute-offset data, top
+        var target-val/edx: (addr value) <- index data, dest-offset
+        # check target-val is a file
+        var target-type-addr/eax: (addr int) <- get target-val, type
+        compare *target-type-addr, 3  # file
+        break-if-!=
+        # read all lines from file and save as an array of strings in target-val
+        # read target-val as a filename and save the handle in target-val
+        var file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
+        var file/eax: (addr buffered-file) <- lookup *file-ah
+        var h: (handle array (handle array byte))
+        var ah/ecx: (addr handle array (handle array byte)) <- address h
+#?         enable-screen-type-mode
+#?         clear-screen 0
+        read-lines file, ah
+#?         {
+#?           var x/eax: (addr array (handle array byte)) <- lookup h
+#?           var len/eax: int <- length x
+#?           var foo/eax: int <- copy len
+#?           print-string 0, "aa: "
+#?           print-int32-hex 0, foo
+#?           print-string 0, "\n"
+#?         }
+        var target/eax: (addr handle array value) <- get target-val, array-data
+        save-lines h, target
+        # save result into target-val
+        var type-addr/eax: (addr int) <- get target-val, type
+        copy-to *type-addr, 2  # array
+        var target-file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
+        var empty: (handle buffered-file)
+        copy-handle empty, target-file-ah
+        break $evaluate:process-word
+      }
       # if curr-stream defines a binding, save top of stack to bindings
       {
         var done?/eax: boolean <- stream-empty? curr-stream
diff --git a/apps/tile/value-stack.mu b/apps/tile/value-stack.mu
index 83b1262a..a6ba4f0f 100644
--- a/apps/tile/value-stack.mu
+++ b/apps/tile/value-stack.mu
@@ -227,3 +227,29 @@ fn array-width _a: (addr array value) -> result/eax: int {
   # we won't add 2 for surrounding brackets since we don't surround arrays in
   # spaces like other value types
 }
+
+fn save-lines in-h: (handle array (handle array byte)), _out-ah: (addr handle array value) {
+  var _in/eax: (addr array (handle array byte)) <- lookup in-h
+  var in/esi: (addr array (handle array byte)) <- copy _in
+  var len/ecx: int <- length in
+  var out-ah/edi: (addr handle array value) <- copy _out-ah
+  populate out-ah, len
+  var out/eax: (addr array value) <- lookup *out-ah
+  # copy in into out
+  var i/ebx: int <- copy 0
+  {
+    compare i, len
+    break-if->=
+#?     print-int32-hex 0, i
+#?     print-string 0, "\n"
+    var src/ecx: (addr handle array byte) <- index in, i
+    var dest-offset/edx: (offset value) <- compute-offset out, i
+    var dest-val/edx: (addr value) <- index out, dest-offset
+    var dest/eax: (addr handle array byte) <- get dest-val, text-data
+    copy-object src, dest
+    var type/edx: (addr int) <- get dest-val, type
+    copy-to *type, 1  # string
+    i <- increment
+    loop
+  }
+}
'alt'>
72c4fb2d ^

22a4c1d3 ^
a11d9c4a ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
a11d9c4a ^
72c4fb2d ^

22a4c1d3 ^
a11d9c4a ^
72c4fb2d ^

22a4c1d3 ^
a11d9c4a ^
72c4fb2d ^

22a4c1d3 ^
a11d9c4a ^
72c4fb2d ^

22a4c1d3 ^
249a5672 ^
39f77cc5 ^

249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^

249a5672 ^
39f77cc5 ^

249a5672 ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
22a4c1d3 ^
72c4fb2d ^

22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^

249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^

72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
e7eeb475 ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
22a4c1d3 ^
72c4fb2d ^

22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^

249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^

72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
e7eeb475 ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
a11d9c4a ^
c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^

c495d2ac ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
22a4c1d3 ^
72c4fb2d ^

22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^

249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^

72c4fb2d ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
e7eeb475 ^
39f77cc5 ^
249a5672 ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
22a4c1d3 ^
72c4fb2d ^
39f77cc5 ^
249a5672 ^
72c4fb2d ^
22a4c1d3 ^

39f77cc5 ^

a11d9c4a ^
22a4c1d3 ^

22b30692 ^
a11d9c4a ^
22b30692 ^




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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309