ruby 1.9 compatible case statements

This commit is contained in:
Sami Samhuri 2013-01-20 22:50:51 -08:00
parent 88a7ca99e9
commit e77ac481d8
4 changed files with 54 additions and 27 deletions

View file

@ -637,8 +637,10 @@ module Assembler
if register?(dest) if register?(dest)
opcode = case opcode = case
when rm?(src, :byte): 0xb6 # movzx Gv, Eb when rm?(src, :byte)
when rm?(src, :word): 0xb7 # movzx Gv, Ew 0xb6 # movzx Gv, Eb
when rm?(src, :word)
0xb7 # movzx Gv, Ew
else else
raise "unsupported MOVZX instruction, dest=#{dest.inspect} << src=#{src.inspect} >>" raise "unsupported MOVZX instruction, dest=#{dest.inspect} << src=#{src.inspect} >>"
end end

View file

@ -358,8 +358,10 @@ module Assembler
def segname_based_on_filetype(segname) def segname_based_on_filetype(segname)
case @header[:filetype] case @header[:filetype]
when MH_OBJECT: '' when MH_OBJECT
when MH_EXECUTE: segname ''
when MH_EXECUTE
segname
else else
raise "unsupported MachO file type: #{@header.inspect}" raise "unsupported MachO file type: #{@header.inspect}"
end end

View file

@ -76,8 +76,10 @@ end
def link(filename, outdir, platform='linux') def link(filename, outdir, platform='linux')
f = base(filename) f = base(filename)
cmd, args = *case platform cmd, args = *case platform
when 'darwin': ['gcc', '-arch i386'] when 'darwin'
when 'linux': ['ld', ''] ['gcc', '-arch i386']
when 'linux'
['ld', '']
else else
raise "unsupported platform: #{platform}" raise "unsupported platform: #{platform}"
end end
@ -90,8 +92,10 @@ def build(filename, outdir, platform='linux', binformat='elf')
objfile = File.join(outdir, base(filename) + '.o') objfile = File.join(outdir, base(filename) + '.o')
symtab, objwriter_class = symtab, objwriter_class =
case binformat case binformat
when 'elf': [Assembler::ELFSymtab.new, Assembler::ELFFile] when 'elf'
when 'macho': [Assembler::MachOSymtab.new, Assembler::MachOFile] [Assembler::ELFSymtab.new, Assembler::ELFFile]
when 'macho'
[Assembler::MachOSymtab.new, Assembler::MachOFile]
else else
raise "unsupported binary format: #{binformat}" raise "unsupported binary format: #{binformat}"
end end

View file

@ -156,8 +156,10 @@ class Compiler
while op?(:mul, @look) while op?(:mul, @look)
asm.push(EAX) asm.push(EAX)
case @look case @look
when '*': multiply when '*'
when '/': divide multiply
when '/'
divide
end end
end end
end end
@ -170,8 +172,10 @@ class Compiler
while op_char?(@look, :add) while op_char?(@look, :add)
asm.push(EAX) asm.push(EAX)
case @look case @look
when '+': add when '+'
when '-': subtract add
when '-'
subtract
end end
end end
end end
@ -230,9 +234,12 @@ class Compiler
while op?(:bit, @look) while op?(:bit, @look)
scan scan
case @value case @value
when '|': bitor_expression when '|'
when '^': bitxor_expression bitor_expression
when '&': bitand_expression when '^'
bitxor_expression
when '&'
bitand_expression
else else
backtrack backtrack
return return
@ -357,12 +364,18 @@ class Compiler
scan scan
asm.push(EAX) asm.push(EAX)
case @value case @value
when '==': eq_relation when '=='
when '!=': neq_relation eq_relation
when '>': gt_relation when '!='
when '>=': ge_relation neq_relation
when '<': lt_relation when '>'
when '<=': le_relation gt_relation
when '>='
ge_relation
when '<'
lt_relation
when '<='
le_relation
end end
end end
end end
@ -921,12 +934,18 @@ class Compiler
def print_token def print_token
print(case @token print(case @token
when :keyword: '[kw] ' when :keyword
when :number: '[nu] ' '[kw] '
when :identifier: '[id] ' when :number
when :op: '[op] ' '[nu] '
when :boolean: '[bo] ' when :identifier
when :newline: '' '[id] '
when :op
'[op] '
when :boolean
'[bo] '
when :newline
''
else else
raise "print doesn't know about #{@token}: #{@value}" raise "print doesn't know about #{@token}: #{@value}"
end) end)