littlebigcomputer/compiler/examples/calculator_1.parva
2024-05-17 18:37:51 +02:00

96 lines
1.5 KiB
Text

# target parva_0_1
li sp, stack
fn tokenize(input) {
let current_token_ptr = input - 1;
let temp = -1;
push(temp); // mark the beginning of the token stack
loop {
current_token_ptr += 1;
let current_token = rw(current_token_ptr);
if current_token == 0 {
goto(tokenize_end);
}
if current_token == '0' {
goto(tokenize_symbol);
}
if current_token == '9' {
goto(tokenize_symbol);
}
let number_start_addr = sp
current_token_ptr
let token_value = current_token - '0';
lw x3, x2
blt x3, x4, tokenize__number_gather_end
bgt x3, x5, tokenize__number_gather_end
b tokenize__number_gather
tokenize__number_gather_end:
li x0, 0 # x0 = sum
li x7, 1 # x7 = power of 10
.align 2
tokenize__number_sum:
pop x3
mulu x3, x3, x7
add x0, x0, x3
mului x7, x7, 10
bne x6, sp, tokenize__number_sum
li x3, 1 # token type = 1 = number
push x3
push x0
b tokenize__loop
tokenize__symbol:
xori x3, x3, 42 # todo, set to appropriate value for hashing symbols
lw x3, tokenize__symbol_hashtable(x3)
beqz x3, error_invalid_input
push x3
b tokenize__loop
}
tokenize__shunting_yard:
pop x3
bnez x3, 3
}
b end
tokenize__output_queue:
.repeat 0 40
tokenize__symbol_hashtable:
.data 0
.data 1
.repeat 0 30 # todo
error_invalid_input:
li a0, error_message_invalid_input
li a1, 1
call print
b end
# function
print:
# todo
ret
end:
wfi
input_string:
.string_encoding terminal
.string "125+23*8"
.string "\0"
error_message_invalid_input:
.string_encoding ascii # todo, use correct encoding
.string "Invalid input\0"
stack: