diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-04-06 07:51:56 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-04-06 07:51:56 -0700 |
commit | b9656ea881a8c845b009de90538188656be71b17 (patch) | |
tree | 1d4438baa47d490f759e432299dbbe5e72995815 /shell | |
parent | 0db683ffdbf09ef6830857c6bedc887d654de78f (diff) | |
download | mu-b9656ea881a8c845b009de90538188656be71b17.tar.gz |
shell: look up globals
Diffstat (limited to 'shell')
-rw-r--r-- | shell/evaluate.mu | 43 | ||||
-rw-r--r-- | shell/global.mu | 41 |
2 files changed, 53 insertions, 31 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu index 00fb36f9..bf95cde5 100644 --- a/shell/evaluate.mu +++ b/shell/evaluate.mu @@ -40,7 +40,7 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel { break-if-!= trace-text trace, "eval", "symbol" - lookup-symbol in-addr, out, env-h, trace + lookup-symbol in-addr, out, env-h, globals, trace trace-higher trace return } @@ -258,7 +258,7 @@ fn push-bindings _params-ah: (addr handle cell), _args-ah: (addr handle cell), o trace-higher trace } -fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell), trace: (addr trace) { +fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace) { # trace sym { var stream-storage: (stream byte 0x40) @@ -291,7 +291,7 @@ fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell) var env-nil?/eax: boolean <- nil? env compare env-nil?, 0/false break-if-= - lookup-symbol-in-hardcoded-globals sym, out, trace + lookup-symbol-in-globals sym, out, globals, trace trace-higher trace # trace "=> " out " (global)" {{{ { @@ -363,7 +363,7 @@ fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell) var env-tail-storage: (handle cell) var env-tail-ah/eax: (addr handle cell) <- address env-tail-storage cdr env, env-tail-ah, trace - lookup-symbol sym, out, *env-tail-ah, trace + lookup-symbol sym, out, *env-tail-ah, globals, trace trace-higher trace # trace "=> " out " (recurse)" {{{ { @@ -380,28 +380,6 @@ fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell) # }}} } -fn lookup-symbol-in-hardcoded-globals _sym: (addr cell), out: (addr handle cell), trace: (addr trace) { - var sym/eax: (addr cell) <- copy _sym - var sym-data-ah/eax: (addr handle stream byte) <- get sym, text-data - var _sym-data/eax: (addr stream byte) <- lookup *sym-data-ah - var sym-data/esi: (addr stream byte) <- copy _sym-data - { - var add?/eax: boolean <- stream-data-equal? sym-data, "+" - compare add?, 0/false - break-if-= - new-primitive-function out, 1/add - trace-text trace, "eval", "global +" - return - } - # otherwise error "unbound symbol: ", sym - var stream-storage: (stream byte 0x40) - var stream/ecx: (addr stream byte) <- address stream-storage - write stream, "unbound symbol: " - rewind-stream sym-data - write-stream stream, sym-data - trace trace, "error", stream -} - fn test-lookup-symbol-in-env { # tmp = (a . 3) var val-storage: (handle cell) @@ -423,7 +401,7 @@ fn test-lookup-symbol-in-env { var tmp-ah/edx: (addr handle cell) <- address tmp-storage new-symbol tmp-ah, "a" var in/eax: (addr cell) <- lookup *tmp-ah - lookup-symbol in, tmp-ah, *env-ah, 0/no-trace + lookup-symbol in, tmp-ah, *env-ah, 0/no-globals, 0/no-trace var result/eax: (addr cell) <- lookup *tmp-ah var result-type/edx: (addr int) <- get result, type check-ints-equal *result-type, 1/number, "F - test-lookup-symbol-in-env/0" @@ -432,7 +410,10 @@ fn test-lookup-symbol-in-env { check-ints-equal result-value, 3, "F - test-lookup-symbol-in-env/1" } -fn test-lookup-symbol-in-hardcoded-globals { +fn test-lookup-symbol-in-globals { + var globals-storage: global-table + var globals/edi: (addr global-table) <- address globals-storage + initialize-globals globals # env = nil var nil-storage: (handle cell) var nil-ah/ecx: (addr handle cell) <- address nil-storage @@ -442,12 +423,12 @@ fn test-lookup-symbol-in-hardcoded-globals { var tmp-ah/ebx: (addr handle cell) <- address tmp-storage new-symbol tmp-ah, "+" var in/eax: (addr cell) <- lookup *tmp-ah - lookup-symbol in, tmp-ah, *nil-ah, 0/no-trace + lookup-symbol in, tmp-ah, *nil-ah, globals, 0/no-trace var result/eax: (addr cell) <- lookup *tmp-ah var result-type/edx: (addr int) <- get result, type - check-ints-equal *result-type, 4/primitive-function, "F - test-lookup-symbol-in-hardcoded-globals/0" + check-ints-equal *result-type, 4/primitive-function, "F - test-lookup-symbol-in-globals/0" var result-value/eax: (addr int) <- get result, index-data - check-ints-equal *result-value, 1/add, "F - test-lookup-symbol-in-hardcoded-globals/1" + check-ints-equal *result-value, 1/add, "F - test-lookup-symbol-in-globals/1" } fn car _in: (addr cell), out: (addr handle cell), trace: (addr trace) { diff --git a/shell/global.mu b/shell/global.mu index 5aa1e22c..f0e00ae0 100644 --- a/shell/global.mu +++ b/shell/global.mu @@ -33,6 +33,47 @@ fn append-primitive _self: (addr global-table), name: (addr array byte) { new-primitive-function curr-value-ah, curr-index } +fn lookup-symbol-in-globals _sym: (addr cell), out: (addr handle cell), _globals: (addr global-table), trace: (addr trace) { + var sym/eax: (addr cell) <- copy _sym + var sym-data-ah/eax: (addr handle stream byte) <- get sym, text-data + var _sym-data/eax: (addr stream byte) <- lookup *sym-data-ah + var sym-data/edx: (addr stream byte) <- copy _sym-data + var globals/esi: (addr global-table) <- copy _globals + { + compare globals, 0 + break-if-= + var global-data-ah/eax: (addr handle array global) <- get globals, data + var global-data/eax: (addr array global) <- lookup *global-data-ah + var final-index/ecx: (addr int) <- get globals, final-index + var curr-index/ecx: int <- copy *final-index + { + compare curr-index, 0 + break-if-< + var curr-offset/ebx: (offset global) <- compute-offset global-data, curr-index + var curr/ebx: (addr global) <- index global-data, curr-offset + var curr-name-ah/eax: (addr handle array byte) <- get curr, name + var curr-name/eax: (addr array byte) <- lookup *curr-name-ah + var found?/eax: boolean <- stream-data-equal? sym-data, curr-name + { + compare found?, 0/false + break-if-= + var curr-value/eax: (addr handle cell) <- get curr, value + copy-object curr-value, out + return + } + curr-index <- decrement + loop + } + } + # otherwise error "unbound symbol: ", sym + var stream-storage: (stream byte 0x40) + var stream/ecx: (addr stream byte) <- address stream-storage + write stream, "unbound symbol: " + rewind-stream sym-data + write-stream stream, sym-data + trace trace, "error", stream +} + # a little strange; goes from value to name and selects primitive based on name fn apply-primitive _f: (addr cell), args-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), _globals: (addr global-table), trace: (addr trace) { var f/esi: (addr cell) <- copy _f |