Class: Alf::Compiler::Plan

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler = Compiler.new) ⇒ Plan

Returns a new instance of Plan


6
7
8
9
10
11
12
13
# File 'lib/alf/compiler/plan.rb', line 6

def initialize(compiler = Compiler.new)
  @parser        = Lang::Parser::Lispy.new
  @compilers     = {}
  @compiled      = {}
  @usage_count   = Hash.new{|h,k| h[k] = 0 }
  @main_compiler = compiler
  join(compiler)
end

Instance Attribute Details

#main_compilerObject (readonly)

Returns the value of attribute main_compiler


14
15
16
# File 'lib/alf/compiler/plan.rb', line 14

def main_compiler
  @main_compiler
end

#parserObject (readonly)

Returns the value of attribute parser


14
15
16
# File 'lib/alf/compiler/plan.rb', line 14

def parser
  @parser
end

Instance Method Details

#__compile(expr = nil, compiled = nil, &bl) ⇒ Object

Real compilation, post-reuse


41
42
43
44
45
46
47
48
49
# File 'lib/alf/compiler/plan.rb', line 41

def __compile(expr = nil, compiled = nil, &bl)
  expr      ||= bl.call(parser)
  compiled  ||= children(expr).map{|op| _compile(op) }
  compiler    = compiler(compiled)
  usage_count = @usage_count[expr]
  compiler.compile(self, expr, compiled, usage_count)
rescue NotSupportedError
  main_compiler.compile(self, expr, compiled, usage_count)
end

#_compile(expr) ⇒ Object

Post-visit, pre-reuse


32
33
34
35
36
37
38
# File 'lib/alf/compiler/plan.rb', line 32

def _compile(expr)
  compiled = @compiled[expr]
  return @compiled[expr] = __compile(expr) unless compiled
  return __compile(expr) unless compiler = compiled.compiler
  return __compile(expr) unless compiler.supports_reuse?
  compiler.reuse(self, compiled)
end

#compile(sexpr) ⇒ Object

Pre-visit


26
27
28
29
# File 'lib/alf/compiler/plan.rb', line 26

def compile(sexpr)
  visit(sexpr)
  _compile(sexpr)
end

#join(compiler) ⇒ Object


16
17
18
19
# File 'lib/alf/compiler/plan.rb', line 16

def join(compiler)
  @compilers[compiler] ||= compiler.join(self)
  compiler
end

#options(compiler) ⇒ Object


21
22
23
# File 'lib/alf/compiler/plan.rb', line 21

def options(compiler)
  @compilers[compiler]
end

#recompile(*compiled, &bl) ⇒ Object


51
52
53
# File 'lib/alf/compiler/plan.rb', line 51

def recompile(*compiled, &bl)
  __compile(nil, compiled, &bl)
end

#visit(expr) ⇒ Object


55
56
57
58
# File 'lib/alf/compiler/plan.rb', line 55

def visit(expr)
  @usage_count[expr] += 1
  children(expr).each{|op| visit(op) }
end