identify some tokens

This commit is contained in:
andromeda
2026-03-18 16:53:35 +01:00
parent de60e52c5a
commit d5c2dde221
2 changed files with 150 additions and 0 deletions

View File

@@ -912,6 +912,71 @@ evaluate_constant:
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
@@ -405,6 +411,7 @@ test_evaluate_constant:
.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
@@ -412,6 +419,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