diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-04-06 09:40:13 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-04-06 09:40:13 -0700 |
commit | 26a184989549e700ffb84f18222197813f317cc4 (patch) | |
tree | 9018654bc70a9ed211cf9be11bd6f05f8a2434d3 /shell | |
parent | 6ef0eabdcff3d02b3f0610311f61aa7d0bf79f7e (diff) | |
download | mu-26a184989549e700ffb84f18222197813f317cc4.tar.gz |
shell: quote
Diffstat (limited to 'shell')
-rw-r--r-- | shell/evaluate.mu | 28 | ||||
-rw-r--r-- | shell/parse.mu | 14 | ||||
-rw-r--r-- | shell/sandbox.mu | 20 | ||||
-rw-r--r-- | shell/tokenize.mu | 14 |
4 files changed, 76 insertions, 0 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu index bf95cde5..4066db20 100644 --- a/shell/evaluate.mu +++ b/shell/evaluate.mu @@ -2,6 +2,12 @@ # we never modify `in` or `env` fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace) { var in/esi: (addr handle cell) <- copy _in +#? dump-cell in +#? { +#? var foo/eax: byte <- read-key 0/keyboard +#? compare foo, 0 +#? loop-if-= +#? } # trace "evaluate " in " in environment " env {{{ { var stream-storage: (stream byte 0x40) @@ -60,6 +66,28 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel trace-higher trace return } + # builtins with "special" evaluation rules + $evaluate:quote: { + # trees starting with single quote create literals + var expr/esi: (addr cell) <- copy in-addr + # if its first elem is not "'", break + var first-ah/ecx: (addr handle cell) <- get in-addr, left + var rest-ah/edx: (addr handle cell) <- get in-addr, right + var first/eax: (addr cell) <- lookup *first-ah + var first-type/ecx: (addr int) <- get first, type + compare *first-type, 2/symbol + break-if-!= + var sym-data-ah/eax: (addr handle stream byte) <- get first, text-data + var sym-data/eax: (addr stream byte) <- lookup *sym-data-ah + var quote?/eax: boolean <- stream-data-equal? sym-data, "'" + compare quote?, 0/false + break-if-= + # + trace-text trace, "eval", "quote" + copy-object rest-ah, out + trace-higher trace + return + } trace-text trace, "eval", "function call" trace-text trace, "eval", "evaluating list elements" var evaluated-list-storage: (handle cell) diff --git a/shell/parse.mu b/shell/parse.mu index eea5b13f..9fd555b3 100644 --- a/shell/parse.mu +++ b/shell/parse.mu @@ -37,6 +37,20 @@ fn parse-sexpression tokens: (addr stream cell), _out: (addr handle cell), trace } read-from-stream tokens, curr-token $parse-sexpression:type-check: { + # single quote -> parse as list with a special car + var quote-token?/eax: boolean <- quote-token? curr-token + compare quote-token?, 0/false + { + break-if-= + var out/edi: (addr handle cell) <- copy _out + allocate-pair out + var out-addr/eax: (addr cell) <- lookup *out + var left-ah/ecx: (addr handle cell) <- get out-addr, left + new-symbol left-ah, "'" + var right-ah/ecx: (addr handle cell) <- get out-addr, right + var result/eax: boolean <- parse-sexpression tokens, right-ah, trace + return result + } # not bracket -> parse atom var bracket-token?/eax: boolean <- bracket-token? curr-token compare bracket-token?, 0/false diff --git a/shell/sandbox.mu b/shell/sandbox.mu index 255098da..2b612a13 100644 --- a/shell/sandbox.mu +++ b/shell/sandbox.mu @@ -239,6 +239,26 @@ fn test-run-with-spaces { check-screen-row screen, 3/y, "=> 1 ", "F - test-run-with-spaces/3" } +fn test-run-quote { + var sandbox-storage: sandbox + var sandbox/esi: (addr sandbox) <- address sandbox-storage + initialize-sandbox sandbox + # type "'a" + edit-sandbox sandbox, 0x27/quote, 0/no-globals, 0/no-screen, 0/no-keyboard, 0/no-disk + edit-sandbox sandbox, 0x61/a, 0/no-globals, 0/no-screen, 0/no-keyboard, 0/no-disk + # eval + edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-screen, 0/no-keyboard, 0/no-disk + # setup: screen + var screen-on-stack: screen + var screen/edi: (addr screen) <- address screen-on-stack + initialize-screen screen, 0x80/width, 0x10/height + # + render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height + check-screen-row screen, 0/y, "'a ", "F - test-run-quote/0" + check-screen-row screen, 1/y, "... ", "F - test-run-quote/1" + check-screen-row screen, 2/y, "=> a ", "F - test-run-quote/2" +} + fn test-run-error-invalid-integer { var sandbox-storage: sandbox var sandbox/esi: (addr sandbox) <- address sandbox-storage diff --git a/shell/tokenize.mu b/shell/tokenize.mu index 8391ae7e..d40ca2bb 100644 --- a/shell/tokenize.mu +++ b/shell/tokenize.mu @@ -540,6 +540,20 @@ fn bracket-token? _in: (addr cell) -> _/eax: boolean { return result } +fn quote-token? _in: (addr cell) -> _/eax: boolean { + var in/eax: (addr cell) <- copy _in + var in-data-ah/eax: (addr handle stream byte) <- get in, text-data + var in-data/eax: (addr stream byte) <- lookup *in-data-ah + rewind-stream in-data + var g/eax: grapheme <- read-grapheme in-data + compare g, 0x27/single-quote + { + break-if-!= + return 1/true + } + return 0/false +} + fn open-paren-token? _in: (addr cell) -> _/eax: boolean { var in/eax: (addr cell) <- copy _in var in-data-ah/eax: (addr handle stream byte) <- get in, text-data |