mirror of
https://github.com/samsonjs/compiler.git
synced 2026-04-27 14:57:45 +00:00
[NEW] Expression grouping with parens. (end of part 2)
This commit is contained in:
parent
af95bd9dec
commit
ff2b68a8f2
2 changed files with 26 additions and 5 deletions
3
Makefile
3
Makefile
|
|
@ -1,6 +1,7 @@
|
||||||
build:
|
build:
|
||||||
# ruby test.rb '5-5+3-2-1'
|
# ruby test.rb '5-5+3-2-1'
|
||||||
ruby test.rb '5*3-5*2+3-9/3-1*1-8/2'
|
# ruby test.rb '5*3-5*2+3-9/3-1*1-8/2'
|
||||||
|
ruby test.rb '5*(3-5)*2+2-9/3-8/2-4*(5+5+5)'
|
||||||
nasm -f elf -g -o test.o test.asm
|
nasm -f elf -g -o test.o test.asm
|
||||||
ld -o test test.o
|
ld -o test test.o
|
||||||
# $? indicates success as per unix convention
|
# $? indicates success as per unix convention
|
||||||
|
|
|
||||||
28
compiler.rb
28
compiler.rb
|
|
@ -80,13 +80,19 @@ 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
|
||||||
emit("mov eax, #{get_num}")
|
if @look == '('
|
||||||
|
match('(')
|
||||||
|
expression
|
||||||
|
match(')')
|
||||||
|
else
|
||||||
|
emit("mov eax, #{get_num}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse and translate a single term. Result is in eax.
|
# Parse and translate a single term. Result is in eax.
|
||||||
def term
|
def term
|
||||||
factor # Result in eax.
|
factor # Result in eax.
|
||||||
while ['*', '/'].include?(@look)
|
while mulop?
|
||||||
# Stash the 1st factor on the stack. This is expected by
|
# Stash the 1st factor on the stack. This is expected by
|
||||||
# multiply & divide. Because they leave their results in eax
|
# multiply & divide. Because they leave their results in eax
|
||||||
# associativity works. Each interim result is pushed on the
|
# associativity works. Each interim result is pushed on the
|
||||||
|
|
@ -106,9 +112,15 @@ class Compiler
|
||||||
# Parse and translate a mathematical expression of terms. Result is
|
# Parse and translate a mathematical expression of terms. Result is
|
||||||
# in eax.
|
# in eax.
|
||||||
def expression
|
def expression
|
||||||
term # Result is in eax.
|
if addop?
|
||||||
|
# Clear eax simulating a zero before unary plus and minus
|
||||||
|
# operations.
|
||||||
|
emit("xor eax, eax")
|
||||||
|
else
|
||||||
|
term # Result is in eax.
|
||||||
|
end
|
||||||
|
|
||||||
while ['+', '-'].include?(@look)
|
while addop?
|
||||||
# Stash the 1st term on the stack. This is expected by add &
|
# Stash the 1st term on the stack. This is expected by add &
|
||||||
# subtract. Because they leave their results in eax
|
# subtract. Because they leave their results in eax
|
||||||
# associativity works. Each interim result is pushed on the
|
# associativity works. Each interim result is pushed on the
|
||||||
|
|
@ -163,4 +175,12 @@ class Compiler
|
||||||
def eof?
|
def eof?
|
||||||
@input.eof? && @look.nil?
|
@input.eof? && @look.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def addop?
|
||||||
|
@look == '+' || @look == '-'
|
||||||
|
end
|
||||||
|
|
||||||
|
def mulop?
|
||||||
|
@look == '*' || @look == '/'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue