Class: SyntaxTree::YARV::Bf

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/bf.rb

Overview

Parses the given source code into a syntax tree, compiles that syntax tree into YARV bytecode.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Bf

Returns a new instance of Bf.



10
11
12
# File 'lib/syntax_tree/yarv/bf.rb', line 10

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/syntax_tree/yarv/bf.rb', line 8

def source
  @source
end

Instance Method Details

#compileObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/syntax_tree/yarv/bf.rb', line 14

def compile
  # Set up the top-level instruction sequence that will be returned.
  iseq = InstructionSequence.new("<compiled>", "<compiled>", 1, :top)

  # Set up the $tape global variable that will hold our state.
  iseq.duphash({ 0 => 0 })
  iseq.setglobal(:$tape)
  iseq.getglobal(:$tape)
  iseq.putobject(0)
  iseq.send(YARV.calldata(:default=, 1))

  # Set up the $cursor global variable that will hold the current position
  # in the tape.
  iseq.putobject(0)
  iseq.setglobal(:$cursor)

  stack = []
  source
    .each_char
    .chunk do |char|
      # For each character, we're going to assign a type to it. This
      # allows a couple of optimizations to be made by combining multiple
      # instructions into single instructions, e.g., +++ becomes a single
      # change_by(3) instruction.
      case char
      when "+", "-"
        :change
      when ">", "<"
        :shift
      when "."
        :output
      when ","
        :input
      when "[", "]"
        :loop
      else
        :ignored
      end
    end
    .each do |type, chunk|
      # For each chunk, we're going to emit the appropriate instruction.
      case type
      when :change
        change_by(iseq, chunk.count("+") - chunk.count("-"))
      when :shift
        shift_by(iseq, chunk.count(">") - chunk.count("<"))
      when :output
        chunk.length.times { output_char(iseq) }
      when :input
        chunk.length.times { input_char(iseq) }
      when :loop
        chunk.each do |char|
          case char
          when "["
            stack << loop_start(iseq)
          when "]"
            loop_end(iseq, *stack.pop)
          end
        end
      end
    end

  iseq.leave
  iseq.compile!
  iseq
end