Class: Strling::Core::Compiler

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

Overview

Compiler class for transforming AST to IR

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(node) ⇒ IROp

Compile an AST node tree to IR

Parameters:

  • node (Node)

    The root AST node to compile

Returns:

  • (IROp)

    The compiled IR root node



26
27
28
# File 'lib/strling/core/compiler.rb', line 26

def self.compile(node)
  new.compile_node(node)
end

Instance Method Details

#compile_node(node) ⇒ IROp

Compile a single AST node to its IR equivalent

Parameters:

  • node (Node)

    The AST node to compile

Returns:

  • (IROp)

    The corresponding IR node



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
# File 'lib/strling/core/compiler.rb', line 34

def compile_node(node)
  case node
  when Alt
    IRAlt.new(node.branches.map { |b| compile_node(b) })
  when Seq
    IRSeq.new(node.parts.map { |p| compile_node(p) })
  when Lit
    IRLit.new(node.value)
  when Dot
    IRDot.new
  when Anchor
    IRAnchor.new(node.at)
  when CharClass
    IRCharClass.new(
      node.negated,
      node.items.map { |item| compile_class_item(item) }
    )
  when Quant
    IRQuant.new(
      compile_node(node.child),
      node.min,
      node.max,
      node.mode
    )
  when Group
    IRGroup.new(
      node.capturing,
      compile_node(node.body),
      name: node.name,
      atomic: node.atomic
    )
  when Backref
    IRBackref.new(by_index: node.by_index, by_name: node.by_name)
  when Look
    IRLook.new(node.dir, node.neg, compile_node(node.body))
  else
    raise "Unknown AST node type: #{node.class}"
  end
end