mirror of
https://github.com/samsonjs/compiler.git
synced 2026-04-27 14:57:45 +00:00
add bit manipulation ops | (OR) & (AND) and ^ (XOR)
This commit is contained in:
parent
d2e7c987d8
commit
18d3bab844
1 changed files with 30 additions and 13 deletions
43
compiler.rb
43
compiler.rb
|
|
@ -135,7 +135,7 @@ class Compiler
|
||||||
|
|
||||||
# Parse and translate a general expression of terms. Result is
|
# Parse and translate a general expression of terms. Result is
|
||||||
# in eax.
|
# in eax.
|
||||||
def expression
|
def arithmetic_expression
|
||||||
term # Result is in eax.
|
term # Result is in eax.
|
||||||
|
|
||||||
while op_char?(@look, :add)
|
while op_char?(@look, :add)
|
||||||
|
|
@ -196,23 +196,40 @@ class Compiler
|
||||||
# bit expressions #
|
# bit expressions #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
def bit_expr(op, token)
|
def bit_expression
|
||||||
match(token)
|
arithmetic_expression
|
||||||
if block_given? then yield else term end
|
# XXX need a token of lookahead
|
||||||
|
while op?(:bit, @look)
|
||||||
|
scan
|
||||||
|
case @value
|
||||||
|
when '|': bitor_expression
|
||||||
|
when '^': bitxor_expression
|
||||||
|
when '&': bitand_expression
|
||||||
|
else
|
||||||
|
puts ">> token: #@token"
|
||||||
|
puts ">> value: #@value"
|
||||||
|
raise 'not actually a bit op!'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def bit_op(op, token)
|
||||||
|
asm.push(EAX)
|
||||||
|
arithmetic_expression
|
||||||
asm.pop(EBX)
|
asm.pop(EBX)
|
||||||
asm.send(op, EAX, EBX)
|
asm.send(op, EAX, EBX)
|
||||||
end
|
end
|
||||||
|
|
||||||
def bitor_expr
|
def bitor_expression
|
||||||
bit_expr(:or, '|')
|
bit_op(:or, '|')
|
||||||
end
|
end
|
||||||
|
|
||||||
def xor_expr
|
def bitxor_expression
|
||||||
bit_expr(:xor, '^')
|
bit_op(:xor, '^')
|
||||||
end
|
end
|
||||||
|
|
||||||
def bitand_expr
|
def bitand_expression
|
||||||
bit_expr(:and, '&') { signed_factor }
|
bit_op(:and, '&')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -277,7 +294,7 @@ class Compiler
|
||||||
end
|
end
|
||||||
|
|
||||||
def relation
|
def relation
|
||||||
expression
|
bit_expression
|
||||||
if op_char?(@look, :rel)
|
if op_char?(@look, :rel)
|
||||||
scan
|
scan
|
||||||
asm.push(EAX)
|
asm.push(EAX)
|
||||||
|
|
@ -299,7 +316,7 @@ class Compiler
|
||||||
# to effectively return false. If b - a is non-zero then a != b,
|
# to effectively return false. If b - a is non-zero then a != b,
|
||||||
# and make_boolean will leave -1 (true) for us in eax.
|
# and make_boolean will leave -1 (true) for us in eax.
|
||||||
def neq_relation
|
def neq_relation
|
||||||
expression
|
bit_expression
|
||||||
asm.pop(EBX)
|
asm.pop(EBX)
|
||||||
asm.sub(EAX, EBX)
|
asm.sub(EAX, EBX)
|
||||||
make_boolean
|
make_boolean
|
||||||
|
|
@ -327,7 +344,7 @@ class Compiler
|
||||||
# true (-1) if the difference was below zero and false (0)
|
# true (-1) if the difference was below zero and false (0)
|
||||||
# otherwise (using JL, jump if less than).
|
# otherwise (using JL, jump if less than).
|
||||||
def cmp_relation(a, b, options={})
|
def cmp_relation(a, b, options={})
|
||||||
expression
|
bit_expression
|
||||||
asm.pop(EBX)
|
asm.pop(EBX)
|
||||||
|
|
||||||
# Invert the sense of the test?
|
# Invert the sense of the test?
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue