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)
opcode = case
when rm?(src, :byte): 0xb6 # movzx Gv, Eb
when rm?(src, :word): 0xb7 # movzx Gv, Ew
when rm?(src, :byte)
0xb6 # movzx Gv, Eb
when rm?(src, :word)
0xb7 # movzx Gv, Ew
else
raise "unsupported MOVZX instruction, dest=#{dest.inspect} << src=#{src.inspect} >>"
end

View file

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

View file

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

View file

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