Skip to content

Commit

Permalink
ruby 1.9 compatible case statements
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Jan 21, 2013
1 parent 88a7ca9 commit e77ac48
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
6 changes: 4 additions & 2 deletions asm/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,10 @@ def movzx(dest, src)
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
Expand Down
6 changes: 4 additions & 2 deletions asm/machofile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,10 @@ def calculate_offsets

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
Expand Down
12 changes: 8 additions & 4 deletions build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def assemble(filename, binformat='elf')
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
Expand All @@ -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
Expand Down
57 changes: 38 additions & 19 deletions compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def term
while op?(:mul, @look)
asm.push(EAX)
case @look
when '*': multiply
when '/': divide
when '*'
multiply
when '/'
divide
end
end
end
Expand All @@ -170,8 +172,10 @@ def arithmetic_expression
while op_char?(@look, :add)
asm.push(EAX)
case @look
when '+': add
when '-': subtract
when '+'
add
when '-'
subtract
end
end
end
Expand Down Expand Up @@ -230,9 +234,12 @@ def bit_expression
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
Expand Down Expand Up @@ -357,12 +364,18 @@ def relation
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
Expand Down Expand Up @@ -921,12 +934,18 @@ def hook(callback, *methods)

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)
Expand Down

0 comments on commit e77ac48

Please sign in to comment.