class Goling::Linguified

Attributes

proc[RW]
sentence[RW]

Public Class Methods

cache() click to toggle source
# File lib/goling/linguified.rb, line 105
def self.cache
  @@cache ||= {}
end
new(str,bind) click to toggle source
# File lib/goling/linguified.rb, line 12
def initialize str,bind

  #
  # reduction loop
  #
  @sentence = str.dup
  @bind = bind
  loop do
    rule = find_rule(str)
    
    reduced = Reduction.new(
      :returns  => rule[:result],
      :lang     => rule[:lang],
      :inline   => rule[:inline],
      :location => rule[:proc].source_location[0],
      :line     => rule[:proc].source_location[1],
      :regexp   => rule[:match].inspect,
      :args     => rule[:match].match(str).to_a[1..-1],
      :sexp     => rule[:proc].to_sexp,
    )
    str.gsub!(rule[:match],reduced.to_rexp)
    break if /^{.*}$/ =~ str
  end

  @encoded = str

  @merged_code = []
  if /^{(?<code>.*)}$/ =~ @encoded
    # successfully reduced entire string, compile it
    code = Reduction::parse(code).compile_with_return_to_var(nil)

    # and wrap it up
    @sexy = Sexp.new(:block,
      Sexp.new(:lasgn,:code, Sexp.new(:iter,
        Sexp.new(:call, nil, :lambda, Sexp.new(:arglist)), nil,
          Sexp.new(:block,
            *code
          )
        )
      ),
    )

    @@me = self
    eval to_ruby(
        Sexp.new(:call,
          Sexp.new(:colon2, Sexp.new(:const, :Goling), :Linguified), :trampoline, Sexp.new(:arglist, Sexp.new(:lvar, :code))
        )
      ),bind
    raise "hell" unless @proc      
  else
    raise "hell"
  end
end
trampoline(code) click to toggle source
# File lib/goling/linguified.rb, line 101
def self.trampoline code
  @@me.register_code code
end

Public Instance Methods

dent() click to toggle source
# File lib/goling/translators/javascript.rb, line 493
def dent
  @indenture ||= ''
  @indenture = @indenture[2..-1]
end
find_rule(str) click to toggle source
# File lib/goling/linguified.rb, line 66
def find_rule str
  found = Goling.rules.select do |rule|
    if rule[:match] =~ str
      true
    else
      false
    end
  end
  raise "no step definition for #{str}" if found.size == 0

  found[0]
end
indent() click to toggle source
# File lib/goling/translators/javascript.rb, line 479
def indent
  @indenture ||= ''
  @indenture += '  '
end
indenture() click to toggle source
# File lib/goling/translators/javascript.rb, line 483
def indenture
  @indenture ||= ''
  @indenture
end
indenture=(str) click to toggle source
# File lib/goling/translators/javascript.rb, line 487
def indenture= str
  @indenture = str
end
new_line() click to toggle source
# File lib/goling/translators/javascript.rb, line 490
def new_line
  "\n" + indenture
end
register_code(code) click to toggle source
# File lib/goling/linguified.rb, line 97
def register_code code
  @proc = code
end
run() click to toggle source
# File lib/goling/linguified.rb, line 89
def run
  begin
    @proc.call
  rescue Exception => e
    $stderr.puts e
  end
end
to_js(sexy = @sexy) click to toggle source
# File lib/goling/translators/javascript.rb, line 498
def to_js sexy = @sexy
  Ruby2Js.new.process(sexy)
end
to_ruby(additional=nil) click to toggle source
# File lib/goling/linguified.rb, line 83
def to_ruby additional=nil
  clone = Marshal.load(Marshal.dump(@sexy)) # sexy is not cleanly duplicated
  clone << additional if additional
  Ruby2Ruby.new.process(clone)
end
to_sexp() click to toggle source
# File lib/goling/linguified.rb, line 79
def to_sexp
  @sexy
end