Class: WLang::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/wlang/compiler.rb,
lib/wlang/compiler/filter.rb,
lib/wlang/compiler/autospacing.rb,
lib/wlang/compiler/to_ruby_code.rb,
lib/wlang/compiler/static_merger.rb,
lib/wlang/compiler/dialect_enforcer.rb,
lib/wlang/compiler/proc_call_removal.rb,
lib/wlang/compiler/strconcat_flattener.rb,
lib/wlang/compiler/to_ruby_abstraction.rb

Overview

Provides the wlang compiler which works on the following AST abstractions:

  • :template, the root element [:template, [:fn, …]]

  • :fn, a concrete function [:fn, code]

  • :wlang, a high-order function [:wlang, symbols, [:fn, ..], [:fn, …], …]

  • :strconcat, concatenation [:strconcat, […], […], […]]

  • :modulo, modulation [:modulo, dialect, [:fn, …]]

  • :static, constant text [:static, “…”]

Defined Under Namespace

Classes: Autospacing, DialectEnforcer, Filter, ProcCallRemoval, StaticMerger, StrconcatFlattener, ToRubyAbstraction, ToRubyCode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dialect) ⇒ Compiler

Returns a new instance of Compiler.



25
26
27
# File 'lib/wlang/compiler.rb', line 25

def initialize(dialect)
  @dialect = dialect
end

Instance Attribute Details

#dialectObject (readonly)

Returns the value of attribute dialect.



23
24
25
# File 'lib/wlang/compiler.rb', line 23

def dialect
  @dialect
end

Instance Method Details

#engine(generate_code = true) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/wlang/compiler.rb', line 60

def engine(generate_code = true)
  parser.tap{|c|
    c.use ProcCallRemoval
    c.use ToRubyAbstraction
    c.use ToRubyCode if generate_code
  }.new
end

#optionsObject



29
30
31
# File 'lib/wlang/compiler.rb', line 29

def options
  dialect.options
end

#parser(engine = Class.new(Temple::Engine)) ⇒ Object



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

def parser(engine = Class.new(Temple::Engine))
  engine.tap{|c|
    c.use Parser
    c.use DialectEnforcer, :dialect => @dialect
    c.use Autospacing if options[:autospacing]
    c.use StrconcatFlattener
    c.use StaticMerger
  }
end

#to_ast(source) ⇒ Object



46
47
48
# File 'lib/wlang/compiler.rb', line 46

def to_ast(source)
  parser.new.call(source)
end

#to_ruby_code(source) ⇒ Object



42
43
44
# File 'lib/wlang/compiler.rb', line 42

def to_ruby_code(source)
  engine.call(source)
end

#to_ruby_proc(source) ⇒ Object



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

def to_ruby_proc(source)
  source = eval(to_ruby_code(source), TOPLEVEL_BINDING)
  if String===source
    Proc.new{|d,buf| buf << source}
  else
    source
  end
end