mirror of
https://github.com/samsonjs/compiler.git
synced 2026-04-27 14:57:45 +00:00
rearranged a few things
This commit is contained in:
parent
18d3bab844
commit
10c4576569
1 changed files with 33 additions and 33 deletions
62
compiler.rb
62
compiler.rb
|
|
@ -34,6 +34,25 @@ class Compiler
|
||||||
'end' => nil
|
'end' => nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Grouped by precedence.
|
||||||
|
Ops = {
|
||||||
|
:add => %w[+ -],
|
||||||
|
:mul => %w[* /],
|
||||||
|
:rel => %w[== != < > <= >=],
|
||||||
|
:or => %w[||],
|
||||||
|
:and => %w[&&],
|
||||||
|
:bit => %w[| ^ &],
|
||||||
|
:unary => %w[- +]
|
||||||
|
}
|
||||||
|
# Op chars are chars that can begin an op, so OpChars needs to be a
|
||||||
|
# map of kinds of operators to a list of valid prefix chars.
|
||||||
|
OpChars = Ops.inject({}) { |hash, kv|
|
||||||
|
key, val = *kv
|
||||||
|
hash[key] = val.map {|op| op[0, 1]} # slice off first char for each op
|
||||||
|
hash
|
||||||
|
# Include :all for a very general test.
|
||||||
|
}.merge(:all => Ops.values.flatten.map{|op| op[0, 1]}.sort.uniq)
|
||||||
|
|
||||||
attr_reader :asm
|
attr_reader :asm
|
||||||
|
|
||||||
def initialize(input, asm)
|
def initialize(input, asm)
|
||||||
|
|
@ -237,6 +256,13 @@ class Compiler
|
||||||
# boolean expressions #
|
# boolean expressions #
|
||||||
#######################
|
#######################
|
||||||
|
|
||||||
|
def op(name)
|
||||||
|
asm.push(EAX)
|
||||||
|
expected(name) unless match_word(name)
|
||||||
|
yield
|
||||||
|
asm.add(ESP, 4)
|
||||||
|
end
|
||||||
|
|
||||||
def boolean_expression
|
def boolean_expression
|
||||||
boolean_term
|
boolean_term
|
||||||
while @look == '|'
|
while @look == '|'
|
||||||
|
|
@ -673,25 +699,6 @@ class Compiler
|
||||||
@input.eof? && @look.nil?
|
@input.eof? && @look.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
Ops = {
|
|
||||||
:add => %w[+ -],
|
|
||||||
:mul => %w[* /],
|
|
||||||
:rel => %w[== != < > <= >=],
|
|
||||||
:or => %w[||],
|
|
||||||
:and => %w[&&],
|
|
||||||
:bitor => %w[| ^],
|
|
||||||
:bitand => %w[&],
|
|
||||||
:unary => %w[- +]
|
|
||||||
}
|
|
||||||
# Op chars are chars that can begin an op, so OpChars needs to be a
|
|
||||||
# map of kinds of operators to a list of valid prefix chars.
|
|
||||||
OpChars = Ops.inject({}) { |hash, kv|
|
|
||||||
key, val = *kv
|
|
||||||
hash[key] = val.map {|op| op[0, 1]} # slice off first char for each op
|
|
||||||
hash
|
|
||||||
# Include :all for a very general test.
|
|
||||||
}.merge(:all => Ops.values.flatten.map{|op| op[0, 1]}.sort.uniq)
|
|
||||||
|
|
||||||
def op_char?(char, kind=:all)
|
def op_char?(char, kind=:all)
|
||||||
OpChars[kind].include?(char)
|
OpChars[kind].include?(char)
|
||||||
end
|
end
|
||||||
|
|
@ -744,8 +751,9 @@ class Compiler
|
||||||
end
|
end
|
||||||
|
|
||||||
# XXX disabled! ... should treat true/false as constants
|
# XXX disabled! ... should treat true/false as constants
|
||||||
|
# once again we need a token of lookahead
|
||||||
def boolean?(char)
|
def boolean?(char)
|
||||||
char == 't' || char == 'f'
|
#char == 't' || char == 'f'
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -776,7 +784,7 @@ class Compiler
|
||||||
# Match literal input.
|
# Match literal input.
|
||||||
def match(char)
|
def match(char)
|
||||||
expected(char, :got => @look) unless @look == char
|
expected(char, :got => @look) unless @look == char
|
||||||
# puts "[ch] #{indent}#{char}"
|
# puts "[ch] #{indent}#{char}"
|
||||||
get_char
|
get_char
|
||||||
skip_whitespace
|
skip_whitespace
|
||||||
end
|
end
|
||||||
|
|
@ -862,14 +870,6 @@ class Compiler
|
||||||
asm.add(ESP, 4)
|
asm.add(ESP, 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
def op(name)
|
|
||||||
asm.push(EAX)
|
|
||||||
get_op
|
|
||||||
expected(name) unless match_word(name)
|
|
||||||
yield
|
|
||||||
asm.add(ESP, 4)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
class <<self
|
class <<self
|
||||||
def hook(callback, *methods)
|
def hook(callback, *methods)
|
||||||
|
|
@ -918,7 +918,7 @@ class Compiler
|
||||||
@label_stack.pop
|
@label_stack.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
# hook(:print_token,
|
# hook(:print_token,
|
||||||
# :get_name, :get_newline, :get_number, :get_op, :get_boolean)
|
# :get_name, :get_newline, :get_number, :get_op, :get_boolean)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue