mirror of
https://github.com/samsonjs/compiler.git
synced 2026-04-27 14:57:45 +00:00
[CHANGED] subtraction is now implemented it terms of ADD.
This commit is contained in:
parent
e7a6162e7b
commit
785f229eec
1 changed files with 6 additions and 7 deletions
13
compiler.rb
13
compiler.rb
|
|
@ -150,14 +150,13 @@ class Compiler
|
||||||
|
|
||||||
# Parse and translate a single factor. Result is in eax.
|
# Parse and translate a single factor. Result is in eax.
|
||||||
def factor
|
def factor
|
||||||
case
|
if @look == '('
|
||||||
when @look == '('
|
|
||||||
match('(')
|
match('(')
|
||||||
expression
|
expression
|
||||||
match(')')
|
match(')')
|
||||||
when alpha?(@look)
|
elsif alpha?(@look)
|
||||||
identifier
|
identifier
|
||||||
when digit?(@look)
|
elsif digit?(@look)
|
||||||
x86_mov(:eax, get_num)
|
x86_mov(:eax, get_num)
|
||||||
else
|
else
|
||||||
expected("a number, identifier, or an expression wrapped in parens")
|
expected("a number, identifier, or an expression wrapped in parens")
|
||||||
|
|
@ -250,9 +249,9 @@ class Compiler
|
||||||
# left in eax. The 1st term (a) is expected on the stack.
|
# left in eax. The 1st term (a) is expected on the stack.
|
||||||
def subtract
|
def subtract
|
||||||
match('-')
|
match('-')
|
||||||
term # Result is in eax.
|
term # Result, b, is in eax.
|
||||||
x86_sub(:eax, '[esp]') # Subtract a from b (this is backwards).
|
x86_neg(:eax) # Fake the subtraction. a - b == a + -b
|
||||||
x86_neg(:eax) # Fix things up. -(b-a) == a-b
|
x86_add(:eax, '[esp]') # Add a and -b.
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse an addition operator and the 2nd term (b). The result is
|
# Parse an addition operator and the 2nd term (b). The result is
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue