Class: Goling::Linguified

Inherits:
Object
  • Object
show all
Defined in:
lib/goling/linguified.rb,
lib/goling/translators/javascript.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, bind) ⇒ Linguified

Returns a new instance of Linguified.



12
13
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
# 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

Instance Attribute Details

#procObject

Returns the value of attribute proc.



10
11
12
# File 'lib/goling/linguified.rb', line 10

def proc
  @proc
end

#sentenceObject

Returns the value of attribute sentence.



10
11
12
# File 'lib/goling/linguified.rb', line 10

def sentence
  @sentence
end

Class Method Details

.cacheObject



105
106
107
# File 'lib/goling/linguified.rb', line 105

def self.cache
  @@cache ||= {}
end

.trampoline(code) ⇒ Object



101
102
103
# File 'lib/goling/linguified.rb', line 101

def self.trampoline code
  @@me.register_code code
end

Instance Method Details

#dentObject



493
494
495
496
# File 'lib/goling/translators/javascript.rb', line 493

def dent
  @indenture ||= ''
  @indenture = @indenture[2..-1]
end

#find_rule(str) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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

#indentObject



479
480
481
482
# File 'lib/goling/translators/javascript.rb', line 479

def indent
  @indenture ||= ''
  @indenture += '  '
end

#indentureObject



483
484
485
486
# File 'lib/goling/translators/javascript.rb', line 483

def indenture
  @indenture ||= ''
  @indenture
end

#indenture=(str) ⇒ Object



487
488
489
# File 'lib/goling/translators/javascript.rb', line 487

def indenture= str
  @indenture = str
end

#new_lineObject



490
491
492
# File 'lib/goling/translators/javascript.rb', line 490

def new_line
  "\n" + indenture
end

#register_code(code) ⇒ Object



97
98
99
# File 'lib/goling/linguified.rb', line 97

def register_code code
  @proc = code
end

#runObject



89
90
91
92
93
94
95
# File 'lib/goling/linguified.rb', line 89

def run
  begin
    @proc.call
  rescue Exception => e
    $stderr.puts e
  end
end

#to_js(sexy = @sexy) ⇒ Object



498
499
500
# File 'lib/goling/translators/javascript.rb', line 498

def to_js sexy = @sexy
  Ruby2Js.new.process(sexy)
end

#to_ruby(additional = nil) ⇒ Object



83
84
85
86
87
# 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_sexpObject



79
80
81
# File 'lib/goling/linguified.rb', line 79

def to_sexp
  @sexy
end