6534 - arith: add/subtract

This commit is contained in:
Kartik Agaram 2020-06-15 19:37:07 -07:00
parent 2041ce7e59
commit 705dfd8587
1 changed files with 37 additions and 3 deletions

View File

@ -20,19 +20,53 @@ fn main -> exit-status/ebx: int {
}
fn simplify -> result/eax: int, look/esi: byte {
$simplify:body: {
look <- get-char # prime the pump
# first arg
look <- skip-spaces look
{
var is-digit?/eax: boolean <- is-decimal-digit? look
compare is-digit?, 0 # false
break-if-=
{
var is-digit?/eax: boolean <- is-decimal-digit? look
compare is-digit?, 0 # false
break-if-= $simplify:body
}
result, look <- num look
}
# operator
var op/ecx: byte <- copy 0
look <- skip-spaces look
op, look <- operator look
# second arg
var second/edx: int <- copy 0
look <- skip-spaces look
{
var tmp/eax: int <- copy 0
tmp, look <- num look
second <- copy tmp
}
# perform op
{
compare op, 0x2b # '+'
break-if-!=
result <- add second
break $simplify:body
}
{
compare op, 0x2d # '-'
break-if-!=
result <- subtract second
break $simplify:body
}
} # $simplify:body
# trailing spaces
look <- skip-spaces look
}
fn operator _look: byte -> op/ecx: byte, look/esi: byte {
op <- copy _look
look <- get-char
}
fn num _look: byte -> result/eax: int, look/esi: byte {
look <- copy _look # should be a no-op; guaranteed to be a digit
var out/edi: int <- copy 0