Class: Mlc::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/mlc/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Compiler

Returns a new instance of Compiler.



13
14
15
# File 'lib/mlc/compiler.rb', line 13

def initialize(code)
  @tree = RubyParser.new.parse(code)
end

Instance Method Details

#ensure_block(sexp) ⇒ Object



26
27
28
29
30
31
# File 'lib/mlc/compiler.rb', line 26

def ensure_block(sexp)
  unless sexp[0] == :block
    sexp = Sexp.new :block, sexp
  end
  parse_block(sexp)
end

#parse(sexp) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mlc/compiler.rb', line 33

def parse(sexp)
  if sexp.nil?
    nil
  else
    send(('parse_' + sexp[0].to_s).to_sym, sexp)
  end
end

#parse!Object



17
18
19
20
21
22
23
24
# File 'lib/mlc/compiler.rb', line 17

def parse!
  puts (tree = @tree).inspect
  snip = Mlc::Abstract::Snippet.new
  if tree
    snip << ensure_block(tree)
  end
  snip
end

#parse_block(sexp) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mlc/compiler.rb', line 41

def parse_block(sexp)
  block = Abstract::Block.new
  sexp[1..sexp.length].each do |el|
    block << parse(el)
  end
  block
end

#parse_call(sexp) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/mlc/compiler.rb', line 49

def parse_call(sexp)
  call = Abstract::Call.new
  call.object = parse(sexp[1]) || Abstract::Raw.new('self')
  call.name = sexp[2]
  sexp[3..sexp.length].each do |arg|
    call.args << parse(arg)
  end
  call
end

#parse_const(sexp) ⇒ Object



67
68
69
# File 'lib/mlc/compiler.rb', line 67

def parse_const(sexp)
  Abstract::Const.new(sexp[1].to_s)
end

#parse_lasgn(sexp) ⇒ Object



71
72
73
# File 'lib/mlc/compiler.rb', line 71

def parse_lasgn(sexp)
  Abstract::LAsgn.new(sexp[1], parse(sexp[2]))
end

#parse_lit(sexp) ⇒ Object



63
64
65
# File 'lib/mlc/compiler.rb', line 63

def parse_lit(sexp)
  Abstract::Literal.new(sexp[1])
end

#parse_lvar(sexp) ⇒ Object



75
76
77
# File 'lib/mlc/compiler.rb', line 75

def parse_lvar(sexp)
  Abstract::LVar.new(sexp[1])
end

#parse_str(sexp) ⇒ Object



59
60
61
# File 'lib/mlc/compiler.rb', line 59

def parse_str(sexp)
  Abstract::Literal.new(sexp[1])
end