From 10c45765696d48d03cccb7b60ec650c44a890551 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 14 Feb 2010 19:02:15 -0800 Subject: [PATCH] rearranged a few things --- compiler.rb | 66 ++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/compiler.rb b/compiler.rb index d85d279..e3f1c69 100644 --- a/compiler.rb +++ b/compiler.rb @@ -33,7 +33,26 @@ class Compiler 'else' => 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 def initialize(input, asm) @@ -237,6 +256,13 @@ class Compiler # boolean expressions # ####################### + def op(name) + asm.push(EAX) + expected(name) unless match_word(name) + yield + asm.add(ESP, 4) + end + def boolean_expression boolean_term while @look == '|' @@ -673,25 +699,6 @@ class Compiler @input.eof? && @look.nil? 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) OpChars[kind].include?(char) end @@ -708,7 +715,7 @@ class Compiler @input.readbyte.chr end end - + # Report error and halt def abort(msg) raise ParseError, msg @@ -744,8 +751,9 @@ class Compiler end # XXX disabled! ... should treat true/false as constants + # once again we need a token of lookahead def boolean?(char) - char == 't' || char == 'f' + #char == 't' || char == 'f' false end @@ -776,7 +784,7 @@ class Compiler # Match literal input. def match(char) expected(char, :got => @look) unless @look == char - # puts "[ch] #{indent}#{char}" +# puts "[ch] #{indent}#{char}" get_char skip_whitespace end @@ -862,14 +870,6 @@ class Compiler asm.add(ESP, 4) end - def op(name) - asm.push(EAX) - get_op - expected(name) unless match_word(name) - yield - asm.add(ESP, 4) - end - class <