Class: Wardite::Revisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/wardite/revisitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops) ⇒ Revisitor

Returns a new instance of Revisitor.



8
9
10
# File 'lib/wardite/revisitor.rb', line 8

def initialize(ops)
  @ops = ops
end

Instance Attribute Details

#opsObject

: Array



4
5
6
# File 'lib/wardite/revisitor.rb', line 4

def ops
  @ops
end

Instance Method Details

#fetch_ops_while_else_or_end(pc_start) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wardite/revisitor.rb', line 36

def fetch_ops_while_else_or_end(pc_start)
  cursor = pc_start
  depth = 0
  loop {
    cursor += 1
    inst = @ops[cursor]
    case inst&.code
    when nil
      raise EvalError, "end op not found"
    when :if, :block, :loop
      depth += 1
    when :else
      if depth == 0
        return cursor
      end
      # do not touch depth
    when :end
      if depth == 0
        return cursor
      else
        depth -= 1
      end
    else
      # nop
    end
  }
  raise "not found corresponding end"
end

#fetch_ops_while_end(pc_start) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/wardite/revisitor.rb', line 68

def fetch_ops_while_end(pc_start)
  cursor = pc_start
  depth = 0
  loop {
    cursor += 1
    inst = @ops[cursor]
    case inst&.code
    when nil
      raise EvalError, "end op not found"
    when :if, :block, :loop
      depth += 1
    when :end
      if depth == 0
        return cursor
      else
        depth -= 1
      end
    else
      # nop
    end
  }
  raise "not found corresponding end"
end

#revisit!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wardite/revisitor.rb', line 13

def revisit!
  @ops.each_with_index do |op, idx|
    case op.code
    when :block
      next_pc = fetch_ops_while_end(idx)
      op.meta[:end_pos] = next_pc
  
    when :loop
      next_pc = fetch_ops_while_end(idx)
      op.meta[:end_pos] = next_pc
  
    when :if
      next_pc = fetch_ops_while_end(idx)
      else_pc = fetch_ops_while_else_or_end(idx)
      op.meta[:end_pos] = next_pc
      op.meta[:else_pos] = else_pc
    end
  end
end