Class: Prattle::AST::KeywordSend

Inherits:
Node
  • Object
show all
Defined in:
lib/prattle/ast/keyword_send.rb

Defined Under Namespace

Classes: Pair

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Node

#run

Constructor Details

- (KeywordSend) initialize(receiver, name)

Returns a new instance of KeywordSend



10
11
12
13
14
# File 'lib/prattle/ast/keyword_send.rb', line 10

def initialize(receiver, name)
  @receiver = receiver
  @method_name = name
  @arguments = []
end

Instance Attribute Details

- (Object) arguments (readonly)

Returns the value of attribute arguments



16
17
18
# File 'lib/prattle/ast/keyword_send.rb', line 16

def arguments
  @arguments
end

- (Object) method_name (readonly)

Returns the value of attribute method_name



16
17
18
# File 'lib/prattle/ast/keyword_send.rb', line 16

def method_name
  @method_name
end

- (Object) receiver (readonly)

Returns the value of attribute receiver



16
17
18
# File 'lib/prattle/ast/keyword_send.rb', line 16

def receiver
  @receiver
end

Class Method Details

+ (Object) collect(pairs)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/prattle/ast/keyword_send.rb', line 20

def self.collect(pairs)
  name = ""
  args = []

  if pairs.kind_of? Array
    pairs.each do |pair|
      name << "#{pair.name}:"
      args << pair.value
    end
  else
    name << "#{pairs.name}:"
    args << pairs.value
  end

  [name, args]
end

+ (Object) grammar(g)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prattle/ast/keyword_send.rb', line 37

def self.grammar(g)
  g.name_var_pair = g.seq(g.t(:method_name), ":", :sp, g.t(:level3),
                          g.kleene(" ")) do |n,v|
    Pair.new(n,v)
  end

  g.send_args = g.many(:name_var_pair) do |*pairs|
    collect(pairs)
  end

  g.keyword_send = g.seq(:level3, :sig_sp, :send_args) do
                        |v,_,arg|

    s = new(v,arg.first)
    s.arguments.concat arg.last
    s
  end
end

+ (Object) if_cond(g, name, args, if_true)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/prattle/ast/keyword_send.rb', line 56

def self.if_cond(g, name, args, if_true)
  return false unless args[0].kind_of? AST::Block

  done_lbl = g.new_label
  else_lbl = g.new_label

  if if_true
    g.gif else_lbl
  else
    g.git else_lbl
  end

  args[0].body.each_with_index do |e,idx|
    g.pop unless idx == 0
    e.bytecode(g)
  end

  g.goto done_lbl

  else_lbl.set!
  g.push :nil

  done_lbl.set!

  return true
end

+ (Object) if_cond_else(g, name, args, if_true)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/prattle/ast/keyword_send.rb', line 83

def self.if_cond_else(g, name, args, if_true)
  return false unless args[0].kind_of? AST::Block
  return false unless args[1].kind_of? AST::Block

  done_lbl = g.new_label
  else_lbl = g.new_label

  if if_true
    g.gif else_lbl
  else
    g.git else_lbl
  end

  args[0].body.each_with_index do |e,idx|
    g.pop unless idx == 0
    e.bytecode(g)
  end

  g.goto done_lbl

  else_lbl.set!

  args[1].body.each_with_index do |e,idx|
    g.pop unless idx == 0
    e.bytecode(g)
  end

  done_lbl.set!

  return true
end

+ (Object) rule_name



6
7
8
# File 'lib/prattle/ast/keyword_send.rb', line 6

def self.rule_name
  "keyword_send"
end

+ (Object) send_method(g, name, args)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/prattle/ast/keyword_send.rb', line 115

def self.send_method(g, name, args)
  case name
  when "ifTrue:"
    return if if_cond g, name, args, true
  when "ifFalse:"
    return if if_cond g, name, args, false
  when "ifTrue:ifFalse:"
    return if if_cond_else g, name, args, true
  when "ifFalse:ifTrue:"
    return if if_cond_else g, name, args, false
  end

  if name[0] == ?~
    if args.last.kind_of? Block
      block = args.pop
    end

    args.each do |a|
      a.bytecode(g)
    end

    colon = name.index(":")
    new_name = name[1..colon-1]

    if block
      block.bytecode(g)
      g.send_with_block new_name.to_sym, args.size
    else
      g.send new_name.to_sym, args.size
    end

  else
    args.each do |a|
      a.bytecode(g)
    end

    g.send name.to_sym, args.size
  end
end

Instance Method Details

- (Object) bytecode(g)



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/prattle/ast/keyword_send.rb', line 188

def bytecode(g)
  case @method_name
  when "whileTrue:"
    return if loop_cond g, true
  when "whileFalse:"
    return if loop_cond g, false
  end

  @receiver.bytecode(g)

  KeywordSend.send_method g, @method_name, @arguments
end

- (Object) loop_cond(g, if_true)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/prattle/ast/keyword_send.rb', line 155

def loop_cond(g, if_true)
  return false unless @receiver.kind_of? AST::Block
  return false unless @arguments[0].kind_of? AST::Block

  top_lbl  = g.new_label
  done_lbl = g.new_label

  top_lbl.set!

  @receiver.body.each_with_index do |e,idx|
    g.pop unless idx == 0
    e.bytecode(g)
  end

  if if_true
    g.gif done_lbl
  else
    g.git done_lbl
  end

  @arguments[0].body.each_with_index do |e,idx|
    e.bytecode(g)
    g.pop
  end

  g.goto top_lbl

  done_lbl.set!
  g.push :nil

  return true
end