shell: quote

This commit is contained in:
Kartik K. Agaram 2021-04-06 09:40:13 -07:00
parent 6ef0eabdcf
commit 26a1849895
4 changed files with 76 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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