Compare commits

...

4 Commits

Author SHA1 Message Date
andromeda
ec88bd7381 rearrange test case 2026-03-18 18:41:48 +01:00
andromeda
d5c2dde221 identify some tokens 2026-03-18 16:53:35 +01:00
andromeda
de60e52c5a range check char length 2026-03-18 16:21:34 +01:00
andromeda
17a0a1a406 yeh only chr consts fn 2026-03-18 16:18:19 +01:00
2 changed files with 181 additions and 56 deletions

View File

@@ -732,8 +732,7 @@ tokenise:
; | 0x00 | 0x | hexidecimal |
; | 0x01 | 0q | octal |
; | 0x02 | 0b | binary |
; | 0x03 | '' | ascii char |
; | 0x04 | "" | ascii string |
; | 0x03 | " | char |
; | 0xFF | | unrecognised |
;
; where `p.` is the prefix or otherwise indicator
@@ -763,19 +762,12 @@ evaluate_constant:
cmp dl, '0'
je .numeric
; char case
; chr case
mov rcx, 0x03
push rcx
cmp dl, "'"
je .chr
pop rcx
; str case
mov rcx, 0x04
push rcx
xor ecx, ecx ; rcx = number of times right-rolled
cmp dl, '"'
je .str
je .chr
pop rcx
jmp .unrecognised
@@ -874,26 +866,10 @@ evaluate_constant:
jmp .bin_loop
.chr:
mov al, [rdi]
; bound check byte as printable char
cmp al, 0x20
jl .unrecognised
cmp al, 0x7E
cmp rcx, 4 ; ensure char is only 4 bytes long
jg .unrecognised
dec rsi
inc rdi
mov dl, [rdi]
cmp dl, "'"
jne .unrecognised
jmp .break
.str:
; TODO range check rcx / return string longer as a register
cmp rsi, 1 ; range check
je .str_break
je .chr_break
ror rax, 8
inc rcx
@@ -911,17 +887,17 @@ evaluate_constant:
dec rsi
inc rdi
jmp .str
jmp .chr
.str_break:
.chr_break:
cmp rcx, 1 ; for each [1..rcx]
jle .str_break_for_good
jle .chr_break_for_good
rol rax, 8 ; roll left to make up for the roll right earlier
dec rcx
jmp .str_break
jmp .chr_break
.str_break_for_good:
mov dl, [rdi]
.chr_break_for_good:
mov dl, [rdi] ; make sure the chr is closed
cmp dl, '"'
jne .unrecognised
@@ -932,9 +908,75 @@ evaluate_constant:
ret
.unrecognised:
pop rdx
mov rdx, 0xFF ; unrecognised type
ret
; ------------------------------------------------------------------------------
; identify_register
;
; description:
; takes a register in ascii-encoded text and returns its token ID or
; UNRECOGNISED_TOKEN_ID if not recognised
;
; parameters:
; edi = register to be searched
;
; returned:
; ax = register's token ID or UNRECOGNISED_TOKEN_ID
; ------------------------------------------------------------------------------
identify_register:
xor eax, eax ; tokens.registers + eax -> entry in tokens.registers
.loop:
cmp eax, (tokens.registers_end - tokens.registers)
jge .not_found
cmp edi, [tokens.registers + eax]
je .found
add eax, 6
jmp .loop
.found:
mov ax, [tokens.registers + eax + 4]
ret
.not_found:
mov ax, UNRECOGNISED_TOKEN_ID
ret
; ------------------------------------------------------------------------------
; identify_operator
; TODO combine with identify_register
;
; description:
; takes an operator in ascii-encoded text and returns its token ID or
; UNRECOGNISED_TOKEN_ID if not recognised
;
; parameters:
; edi = operator to be searched
;
; returned:
; ax = operator's token ID or UNRECOGNISED_TOKEN_ID
; ------------------------------------------------------------------------------
identify_operator:
xor eax, eax ; tokens.operators + eax -> entry in tokens.operators
.loop:
cmp eax, (tokens.operators_end - tokens.operators)
jge .not_found
cmp edi, [tokens.operators + eax]
je .found
add eax, 6
jmp .loop
.found:
mov ax, [tokens.operators + eax + 4]
ret
.not_found:
mov ax, UNRECOGNISED_TOKEN_ID
ret
; ------------------------------------------------------------------------------
; utilities
; ------------------------------------------------------------------------------

View File

@@ -34,6 +34,12 @@ run_tests:
call clear_test_arena
call test_evaluate_constant
call clear_test_arena
call test_identify_register
call clear_test_arena
call test_identify_operator
ret
.msg db "running test suite...", 0x0A, 0x00
@@ -50,31 +56,31 @@ test_elemb:
; [0]
mov rdi, 5
mov rsi, test_elemb_5
mov dl, [test_elemb_5]
mov rsi, .case1
mov dl, [.case1]
call elemb
cmp al, 1
jne .fail
; [n - 1]
mov rdi, 5
mov rsi, test_elemb_5
mov dl, [test_elemb_5 + 4]
mov rsi, .case1
mov dl, [.case1 + 4]
call elemb
cmp al, 1
jne .fail
; [1]
mov rdi, 5
mov rsi, test_elemb_5
mov dl, [test_elemb_5 + 1]
mov rsi, .case1
mov dl, [.case1 + 1]
call elemb
cmp al, 1
jne .fail
; not present
mov rdi, 5
mov rsi, test_elemb_5
mov rsi, .case1
mov dl, 0xDA
call elemb
cmp al, 0
@@ -82,7 +88,7 @@ test_elemb:
; 0 length list
mov rdi, 0
mov rsi, test_elemb_0
mov rsi, .case0
mov dl, 0x34
call elemb
cmp al, 0
@@ -96,6 +102,8 @@ test_elemb:
mov rsi, msg_fail
call print
ret
.case0: ; [This Page Intentionally Left Blank]
.case1 db 0x54, 0x00, 0x21, 0x20, 0x34
.msg db "test_elemb...", 0x00
; ------------------------------------------------------------------------------
@@ -370,20 +378,19 @@ test_evaluate_constant:
; char
mov rdi, .case0c
mov rsi, 3
mov rsi, 6
call evaluate_constant
cmp rax, [.case0c_solution]
jne .fail
cmp rdx, 0x03
jne .fail
; str
mov rdi, .case0s
mov rsi, 5
; oversized char
mov rdi, .case1c
mov rsi, 7
call evaluate_constant
cmp rax, [.case0s_solution]
cmp rdx, 0xFF
jne .fail
cmp rdx, 0x04
.pass:
mov rsi, msg_pass
@@ -402,10 +409,11 @@ test_evaluate_constant:
.case2h_solution dq 0x1234567890
.case3h db "0x243F6A8885A308D3"
.case3h_solution dq 0x243F6A8885A308D3
.case0c db "' '"
.case0c_solution dq ' '
.case0s db '"str"'
.case0s_solution dq "str"
.case0c db '"char"'
.case0c_solution dq "char"
.case1c db '"chars"'
.case1c_solution dq "chars"
; " wow my editor really doesn't like highlighting quotes correctly
.case0q db "0q31103755242102"
.case0q_solution dq 0q31103755242102
@@ -413,6 +421,84 @@ test_evaluate_constant:
.case0b db "0b0110011001101001011100100111001101110100001000000011011000110100"
.case0b_solution dq 0b0110011001101001011100100111001101110100001000000011011000110100
; ------------------------------------------------------------------------------
; test_identify_register
;
; description:
; tests identify_register described funtionality
; ------------------------------------------------------------------------------
test_identify_register:
mov rsi, .msg
call print.test
mov edi, "rcx"
call identify_register
cmp ax, 0x0002
jne .fail
mov edi, "RaNd"
call identify_register
cmp ax, UNRECOGNISED_TOKEN_ID
jne .fail
mov edi, ""
call identify_register
cmp ax, UNRECOGNISED_TOKEN_ID
jne .fail
.pass:
mov rsi, msg_pass
call print
ret
.fail:
mov rsi, msg_fail
call print
ret
.msg db "test_identify_register...", 0x00
; ------------------------------------------------------------------------------
; test_identify_operator
;
; description:
; tests identify_operator described funtionality
; ------------------------------------------------------------------------------
test_identify_operator:
mov rsi, .msg
call print.test
mov edi, "xor"
call identify_operator
cmp ax, 0x0053
jne .fail
mov edi, [tokens.operators_end]
call identify_operator
cmp ax, UNRECOGNISED_TOKEN_ID
jne .fail
mov edi, "RaNd"
call identify_operator
cmp ax, UNRECOGNISED_TOKEN_ID
jne .fail
mov edi, ""
call identify_operator
cmp ax, UNRECOGNISED_TOKEN_ID
jne .fail
.pass:
mov rsi, msg_pass
call print
ret
.fail:
mov rsi, msg_fail
call print
ret
.msg db "test_identify_operator...", 0x00
msg_pass:
db 0x0A
times (TEST_LINE_LENGTH + .start - .end) db " ", ; right align
@@ -423,6 +509,3 @@ msg_fail:
times (TEST_LINE_LENGTH + .start - .end) db " ",
.start db "failed."
.end db 0x0A, 0x00
test_elemb_0: ; [This Page Intentionally Left Blank]
test_elemb_5 db 0x54, 0x00, 0x21, 0x20, 0x34