tealrb 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -66,8 +66,7 @@ module TEALrb
66
66
  end
67
67
 
68
68
  def on_ivar(node)
69
- insert_after(node.loc.name, '.call') unless ['@teal', '@teal_methods', '@subroutines',
70
- '@scratch'].include? node.source
69
+ insert_after(node.loc.name, '.call') unless ['@teal_methods', '@subroutines', '@scratch'].include? node.source
71
70
  super
72
71
  end
73
72
  end
@@ -83,7 +82,7 @@ module TEALrb
83
82
 
84
83
  class OpRewriter < Rewriter
85
84
  def initialize
86
- @skips = 0
85
+ @skips = []
87
86
  super
88
87
  end
89
88
 
@@ -110,44 +109,62 @@ module TEALrb
110
109
 
111
110
  OPCODE_METHODS = TEALrb::Opcodes.instance_methods.freeze
112
111
 
113
- def on_const(node)
114
- @skips = 1 if %w[Txna Gtxn AppArgs].include? node.loc.name.source
115
- super
116
- end
117
-
118
- def on_ivar(node)
119
- @skips = 2 if node.source == '@teal_methods'
120
- @skips = 1 if node.source == '@scratch'
121
- end
122
-
123
112
  def on_send(node)
124
- meth_name = node.loc.selector.source.to_sym
113
+ meth_name = node.children[1]
125
114
 
126
115
  if OPCODE_METHODS.include? meth_name
127
116
  if meth_name[/(byte|int)cblock/]
128
- @skips = node.children.size - 2
117
+ @skips += node.children[2..]
129
118
  else
130
119
  params = TEALrb::Opcodes.instance_method(meth_name).parameters
131
- @skips = params.count { |param| param[0] == :req }
120
+ req_params = params.count { |param| param[0] == :req }
121
+ @skips += node.children[2..(1 + req_params.size)] unless req_params.zero?
132
122
  end
133
- elsif %i[comment placeholder].include? meth_name
134
- @skips = 1
123
+ elsif %i[comment placeholder rb].include?(meth_name) ||
124
+ (%i[[] []=].include?(meth_name) &&
125
+ (
126
+ %i[@scratch @teal_methods Gtxn
127
+ AppArgs].include?(node.children[0].children.last) ||
128
+ node.children[0].children.first&.children&.last == :Txna
129
+ ))
130
+
131
+ @skips << node.children[2]
132
+ elsif node.children.first&.children&.last == :@scratch && meth_name[/=$/]
133
+ nil
134
+ elsif %i[@scratch Gtxn].include? node.children.first&.children&.last
135
+ @skips << node.children.last
135
136
  end
137
+
136
138
  super
137
139
  end
138
140
 
139
141
  def on_int(node)
140
- wrap(node.source_range, 'int(', ')') if (@skips -= 1).negative?
142
+ if @skips.include? node
143
+ @skips.delete(node)
144
+ else
145
+ wrap(node.source_range, 'int(', ')')
146
+ end
147
+
141
148
  super
142
149
  end
143
150
 
144
151
  def on_str(node)
145
- wrap(node.source_range, 'byte(', ')') if (@skips -= 1).negative?
152
+ if @skips.include? node
153
+ @skips.delete(node)
154
+ else
155
+ wrap(node.source_range, 'byte(', ')')
156
+ end
157
+
146
158
  super
147
159
  end
148
160
 
149
161
  def on_sym(node)
150
- wrap(node.source_range, 'label(', ')') if node.source_range.source[/^:/] && (@skips -= 1).negative?
162
+ if @skips.include? node
163
+ @skips.delete(node)
164
+ elsif node.source_range.source[/^:/]
165
+ wrap(node.source_range, 'label(', ')')
166
+ end
167
+
151
168
  super
152
169
  end
153
170
  end
@@ -156,7 +173,7 @@ module TEALrb
156
173
  def on_if(node)
157
174
  case node.loc.keyword.source
158
175
  when 'if'
159
- replace(node.loc.keyword, 'IfBlock.new(@teal, ')
176
+ replace(node.loc.keyword, 'IfBlock.new(')
160
177
  when 'elsif'
161
178
  replace(node.loc.keyword, 'end.elsif(')
162
179
  end
@@ -168,5 +185,30 @@ module TEALrb
168
185
  super
169
186
  end
170
187
  end
188
+
189
+ class WhileRewriter < Rewriter
190
+ class << self
191
+ attr_accessor :while_count
192
+ end
193
+
194
+ def initialize(*args)
195
+ self.class.while_count = {}
196
+ self.class.while_count[Thread.current] ||= 0
197
+ super
198
+ end
199
+
200
+ def while_count
201
+ self.class.while_count[Thread.current]
202
+ end
203
+
204
+ def on_while(node)
205
+ cond_node = node.children.first
206
+ replace(node.loc.keyword, ":while#{while_count}\n#{cond_node.source}\nbz :end_while#{while_count}")
207
+ replace(node.loc.begin, '') if node.loc.begin
208
+ replace(node.loc.end, "b :while#{while_count}\n:end_while#{while_count}")
209
+ replace(cond_node.loc.expression, '')
210
+ super
211
+ end
212
+ end
171
213
  end
172
214
  end
@@ -2,14 +2,13 @@
2
2
 
3
3
  module TEALrb
4
4
  class Scratch
5
- def initialize(teal)
6
- @teal = teal
5
+ def initialize
7
6
  @open_slots = (0..256).to_a
8
7
  @named_slots = {}
9
8
  end
10
9
 
11
10
  def [](key)
12
- @teal << "load #{@named_slots[key]} // #{key}"
11
+ TEAL.instance << "load #{@named_slots[key]} // #{key}"
13
12
  end
14
13
 
15
14
  def []=(key, _value)
@@ -17,7 +16,7 @@ module TEALrb
17
16
  end
18
17
 
19
18
  def store(key)
20
- @teal << "store #{@named_slots[key] ||= @open_slots.shift} // #{key}"
19
+ TEAL.instance << "store #{@named_slots[key] ||= @open_slots.shift} // #{key}"
21
20
  end
22
21
 
23
22
  def delete(key)
@@ -34,5 +33,21 @@ module TEALrb
34
33
  def unreserve(slot)
35
34
  @open_slots << slot
36
35
  end
36
+
37
+ def respond_to_missing?
38
+ !!(meth[/=$/] || @named_slots.key?(meth.to_s))
39
+ end
40
+
41
+ def method_missing(meth, *args, &block)
42
+ super unless block.nil?
43
+
44
+ if meth[/=$/]
45
+ store(meth.to_s.chomp('='))
46
+ elsif @named_slots.key?(meth.to_s)
47
+ self[meth.to_s]
48
+ else
49
+ super
50
+ end
51
+ end
37
52
  end
38
53
  end
data/lib/tealrb.rb CHANGED
@@ -16,13 +16,18 @@ require_relative 'tealrb/cmd_line/teal2tealrb'
16
16
  module TEALrb
17
17
  class TEAL < Array
18
18
  class << self
19
- attr_accessor :current
20
- end
19
+ attr_writer :instance
20
+
21
+ def instance
22
+ raise 'TEALrb does not support multi-threading' if Thread.current != Thread.main
21
23
 
22
- @current = {}
24
+ @instance
25
+ end
26
+ end
23
27
 
24
- def set_as_current
25
- self.class.current[Thread.current] = self
28
+ def initialize(teal_array)
29
+ self.class.instance = self
30
+ super
26
31
  end
27
32
 
28
33
  def teal
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tealrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Polny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-22 00:00:00.000000000 Z
11
+ date: 2022-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source