tealrb 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e87d9c19e3fb930116e5a3a3d598140987feb20a0571b2f2fd70f1beb752b3b
4
- data.tar.gz: 67b114fc5e032b9b9f733fb033bc619d209a4b1da4438606064f88306cd0fa2b
3
+ metadata.gz: 94e31d86149bd0b29c7f7a08f2cabd94866246808315a3f70e8ac8fc35ea4e8e
4
+ data.tar.gz: e63533e3a902192d6425594109ea5a5e72b92eeea5f45956dc73ad093a05ceee
5
5
  SHA512:
6
- metadata.gz: 61974eeb0c5454eb434ac0e36308c01ddc4ac51d7dfb8745a7b991e837e208589fbf9d014b47d2b66b876391052b2616ec2e26cffa1f92c0ab2e74db23fd01d4
7
- data.tar.gz: 5f35f5248e16f6a684d3a37ab9d4a76f97f32ee24bcee43c1da7dc3082c044da1037da42a1495b3b8a1a3843d46a681f4a905b0d283ace884131b5815c60fe12
6
+ metadata.gz: 4a5c009da5923b564565b8c78f6e62bd75e17bcac9c19d2ae883d7a789a02ddbefc2e28732b0a278d1b8c868a0728f6ecd81d5cb29eaffc8e2b4a3f808c7acd0
7
+ data.tar.gz: f9310e249ad33ed5199628483916d7e4299da7dbbf76b294cf8fc22d9c6223a58f63135d7ef24d985c3773d3b89a17acffb59bcf9d1cd89c2984be234afdf4c4
@@ -69,6 +69,7 @@ module TEALrb
69
69
  # sets the `#pragma version`, defines teal methods, and defines subroutines
70
70
  def initialize
71
71
  @teal = TEAL.new ["#pragma version #{self.class.version}"]
72
+ @scratch = Scratch.new(@teal)
72
73
 
73
74
  self.class.subroutines.each_key do |name|
74
75
  define_singleton_method(name) do |*_args|
@@ -92,18 +93,8 @@ module TEALrb
92
93
  def define_teal_method(name, definition)
93
94
  @teal.set_as_current
94
95
 
95
- new_source = rewrite(definition.source, method_rewriter: true)
96
-
97
- pre_string = StringIO.new
98
-
99
- definition.parameters.reverse.each_with_index do |param, i|
100
- param_name = param.last
101
- pre_string.puts "store #{200 + i}"
102
- pre_string.puts "comment('#{param_name}', inline: true)"
103
- pre_string.puts "#{param_name} = -> { load #{200 + i}; comment('#{param_name}', inline: true) }"
104
- end
96
+ new_source = generate_method_source(name, definition)
105
97
 
106
- new_source = "#{pre_string.string}#{new_source}"
107
98
  define_singleton_method(name) do |*_args|
108
99
  eval_tealrb(new_source, debug_context: "teal method: #{name}")
109
100
  end
@@ -130,18 +121,9 @@ module TEALrb
130
121
  comment_content = "#{name}(#{comment_params})"
131
122
  comment(comment_content, inline: true)
132
123
 
133
- new_source = rewrite(definition.source, method_rewriter: true)
134
-
135
- pre_string = StringIO.new
136
-
137
- definition.parameters.reverse.each_with_index do |param, i|
138
- param_name = param.last
139
- pre_string.puts "store #{200 + i}"
140
- pre_string.puts "comment('#{param_name}', inline: true)"
141
- pre_string.puts "#{param_name} = -> { load #{200 + i}; comment('#{param_name}', inline: true) }"
142
- end
124
+ new_source = generate_method_source(name, definition)
125
+ new_source = "#{new_source}retsub"
143
126
 
144
- new_source = "#{pre_string.string}#{new_source}retsub"
145
127
  eval_tealrb(new_source, debug_context: "subroutine: #{name}")
146
128
 
147
129
  nil
@@ -189,6 +171,29 @@ module TEALrb
189
171
 
190
172
  private
191
173
 
174
+ def generate_method_source(name, definition)
175
+ new_source = rewrite(definition.source, method_rewriter: true)
176
+
177
+ pre_string = StringIO.new
178
+
179
+ scratch_names = []
180
+ definition.parameters.reverse.each_with_index do |param, _i|
181
+ param_name = param.last
182
+ scratch_name = [name, param_name].map(&:to_s).join(': ')
183
+ scratch_names << scratch_name
184
+
185
+ pre_string.puts "@scratch.store('#{scratch_name}')"
186
+ pre_string.puts "#{param_name} = -> { @scratch['#{scratch_name}'] }"
187
+ end
188
+
189
+ post_string = StringIO.new
190
+ scratch_names.each do |n|
191
+ post_string.puts "@scratch.delete '#{n}'"
192
+ end
193
+
194
+ "#{pre_string.string}#{new_source}#{post_string.string}"
195
+ end
196
+
192
197
  def rewrite_with_rewriter(string, rewriter)
193
198
  process_source = RuboCop::ProcessedSource.new(string, RUBY_VERSION[/\d\.\d/].to_f)
194
199
  rewriter.new.rewrite(process_source)
@@ -6,7 +6,7 @@ module TEALrb
6
6
  attr_accessor :id
7
7
  end
8
8
 
9
- @id = {}
9
+ @id = {}.compare_by_identity
10
10
 
11
11
  def initialize(teal, _cond, &blk)
12
12
  self.class.id[teal] ||= 0
@@ -15,7 +15,6 @@ module TEALrb
15
15
  @id = self.class.id[@teal]
16
16
  @end_label = "if#{@id}_end:"
17
17
 
18
- @id = self.class.id[@teal]
19
18
  self.class.id[@teal] += 1
20
19
 
21
20
  @teal << "bz if#{@id}_else0"
@@ -664,31 +664,40 @@ module TEALrb
664
664
  end
665
665
  end
666
666
 
667
- class TxnArrayIndex
667
+ class TxnaField
668
668
  include Opcodes
669
- include TxnFields
670
669
 
671
- def initialize(index)
672
- @index = index
670
+ def initialize(field, index = nil)
671
+ @field = field
672
+ @teal = TEALrb::TEAL.current[Thread.current]
673
+ txna(field, index) if index
673
674
  end
674
675
 
675
- def opcode(field)
676
+ def [](index)
676
677
  @teal = TEALrb::TEAL.current[Thread.current]
677
- txna field, @index
678
+ txna @field, index
678
679
  end
679
680
  end
680
681
 
681
682
  module Txna
682
- extend Opcodes
683
- extend TxnFields
683
+ def self.application_args(index = nil)
684
+ TxnaField.new('ApplicationArgs', index)
685
+ end
684
686
 
685
- def self.opcode(field, index)
686
- @teal = TEALrb::TEAL.current[Thread.current]
687
- txna field, index
687
+ def self.accounts(index = nil)
688
+ TxnaField.new('Accounts', index)
688
689
  end
689
690
 
690
- def self.[](index)
691
- TxnArrayIndex.new(index)
691
+ def self.assets(index = nil)
692
+ TxnaField.new('Assets', index)
693
+ end
694
+
695
+ def self.applications(index = nil)
696
+ TxnaField.new('Applications', index)
697
+ end
698
+
699
+ def self.logs(index = nil)
700
+ TxnaField.new('Logs', index)
692
701
  end
693
702
  end
694
703
 
@@ -712,7 +721,7 @@ module TEALrb
712
721
 
713
722
  module AppArgs
714
723
  def self.[](index)
715
- Txna.application_args index
724
+ Txna.application_args[index]
716
725
  end
717
726
  end
718
727
  end
@@ -66,7 +66,8 @@ 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'].include? node.source
69
+ insert_after(node.loc.name, '.call') unless ['@teal', '@teal_methods', '@subroutines',
70
+ '@scratch'].include? node.source
70
71
  super
71
72
  end
72
73
  end
@@ -116,6 +117,7 @@ module TEALrb
116
117
 
117
118
  def on_ivar(node)
118
119
  @skips = 2 if node.source == '@teal_methods'
120
+ @skips = 1 if node.source == '@scratch'
119
121
  end
120
122
 
121
123
  def on_send(node)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TEALrb
4
+ class Scratch
5
+ def initialize(teal)
6
+ @teal = teal
7
+ @open_slots = (0..256).to_a
8
+ @named_slots = {}
9
+ end
10
+
11
+ def [](key)
12
+ @teal << "load #{@named_slots[key]} // #{key}"
13
+ end
14
+
15
+ def []=(key, _value)
16
+ store(key)
17
+ end
18
+
19
+ def store(key)
20
+ @teal << "store #{@named_slots[key] ||= @open_slots.shift} // #{key}"
21
+ end
22
+
23
+ def delete(key)
24
+ @open_slots << @named_slots.delete(key)
25
+ end
26
+
27
+ def reserve(slot)
28
+ name = @named_slots.key(slot)
29
+ raise ArgumentError, "Attempted to reserve a slot (#{slot}) that corresponds to a named slot (#{name})" if name
30
+
31
+ @open_slots.delete slot
32
+ end
33
+
34
+ def unreserve(slot)
35
+ @open_slots << slot
36
+ end
37
+ end
38
+ end
data/lib/tealrb.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'tealrb/opcodes'
9
9
  require_relative 'tealrb/opcode_modules'
10
10
  require_relative 'tealrb/rewriters'
11
11
  require_relative 'tealrb/if_block'
12
+ require_relative 'tealrb/scratch'
12
13
  require_relative 'tealrb/contract'
13
14
  require_relative 'tealrb/cmd_line/teal2tealrb'
14
15
 
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.3.0
4
+ version: 0.5.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-15 00:00:00.000000000 Z
11
+ date: 2022-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source
@@ -152,6 +152,7 @@ files:
152
152
  - lib/tealrb/opcodes.rb
153
153
  - lib/tealrb/patches.rb
154
154
  - lib/tealrb/rewriters.rb
155
+ - lib/tealrb/scratch.rb
155
156
  homepage: https://github.com/joe-p/tealrb
156
157
  licenses:
157
158
  - MIT