From 0f9a65dc0d10d93319eacf8ccff75a068e5f97a8 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 26 Oct 2020 22:35:20 -0700 Subject: 7120 - tile: array of lines from file Requires a quick hacky change to Mu compiler. --- apps/tile/environment.mu | 6 +++++- apps/tile/rpn.mu | 46 ++++++++++++++++++++++++++++++++++++++++++++++ apps/tile/value-stack.mu | 26 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) (limited to 'apps/tile') 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 + } +} -- cgit 1.4.1-2-gfad0 '#n75'>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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469