tealrb 0.6.0 → 0.9.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
 
@@ -108,46 +107,77 @@ module TEALrb
108
107
  super
109
108
  end
110
109
 
111
- OPCODE_METHODS = TEALrb::Opcodes.instance_methods.freeze
112
-
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
110
+ OPCODE_METHODS = TEALrb::Opcodes::AllOpcodes.instance_methods.freeze
122
111
 
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
- params = TEALrb::Opcodes.instance_method(meth_name).parameters
131
- @skips = params.count { |param| param[0] == :req }
119
+ params = TEALrb::Opcodes::AllOpcodes.instance_method(meth_name).parameters
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 rb].include? meth_name
134
- @skips = node.children.last.children.size
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 Accounts ApplicationArgs Assets Apps Logs].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
+
168
+ super
169
+ end
170
+ end
171
+
172
+ class InlineIfRewriter < Rewriter
173
+ def on_if(node)
174
+ unless node.loc.respond_to? :else
175
+ conditional = node.children[0].source
176
+ if_blk = node.children[1].source
177
+
178
+ replace(node.loc.expression, "if(#{conditional})\n#{if_blk}\nend")
179
+ end
180
+
151
181
  super
152
182
  end
153
183
  end
@@ -156,7 +186,7 @@ module TEALrb
156
186
  def on_if(node)
157
187
  case node.loc.keyword.source
158
188
  when 'if'
159
- replace(node.loc.keyword, 'IfBlock.new(@teal, ')
189
+ replace(node.loc.keyword, 'IfBlock.new(')
160
190
  when 'elsif'
161
191
  replace(node.loc.keyword, 'end.elsif(')
162
192
  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)
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
@@ -39,13 +44,13 @@ module TEALrb
39
44
 
40
45
  TEALrb::Opcodes::BINARY_OPCODE_METHOD_MAPPING.each do |meth, opcode|
41
46
  define_method(meth) do |other|
42
- ExtendedOpcodes.send(opcode, self, other)
47
+ TEALrb::Opcodes::ExtendedOpcodes.send(opcode, self, other)
43
48
  end
44
49
  end
45
50
 
46
51
  TEALrb::Opcodes::UNARY_OPCODE_METHOD_MAPPING.each do |meth, opcode|
47
52
  define_method(meth) do
48
- ExtendedOpcodes.send(opcode, self)
53
+ TEALrb::Opcodes::ExtendedOpcodes.send(opcode, self)
49
54
  end
50
55
  end
51
56
  end
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.6.0
4
+ version: 0.9.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-27 00:00:00.000000000 Z
11
+ date: 2022-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.27'
33
+ version: '1.36'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.27'
40
+ version: '1.36'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.6.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.21.2
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.21.2
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: yard
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -177,5 +191,5 @@ requirements: []
177
191
  rubygems_version: 3.1.6
178
192
  signing_key:
179
193
  specification_version: 4
180
- summary: A DSL for building Algorand applications
194
+ summary: A DSL for building Algorand smart contracts
181
195
  test_files: []