dtr_to_rust 0.0.7 → 0.0.9

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: 0b601b1a9ea3ca3f47651dbd2cfd90d563244994bdccb0aa7b44ec2882219067
4
- data.tar.gz: a480b70e8c6bfb2421900f5b82db3c2f8c83d1194954a78683b30db78a67a860
3
+ metadata.gz: 9c1b886c52baf81105bc299619682bcca98bc31e80fae633241bd5fabfcc9df4
4
+ data.tar.gz: 59d69ab207c8c293ea91ada01691e9282eafc62274846cb78cba76d3ef5323a8
5
5
  SHA512:
6
- metadata.gz: d2497d871d595c64552926cde73f20dc574e342ba769de4961baef8c595c31d78f2b9080d3c6793b8ecc28aa981d4ced0c00fa9ff7f1ebbc6335765e4983309d
7
- data.tar.gz: 5c2eb5673bff37de5fe02a64456eae6b60a2b89182b75fc4a22489e29c016d0fd9f1e3f1fe8ebb30888efa2f4f89f308da9942276d3e4bc1bccd461cdc167654
6
+ metadata.gz: ee8323788b1676913b11cab4c24076ad0bfe09a7327fd48b7754d4beaa13b2b18b9eb9ba72faa82e1c7bc8405bf633b87652535c3c068e04fc1b6e66cf94444b
7
+ data.tar.gz: 3a6df6377e896aeb61b14ccea0ed7425eab24aa92db8ae37b632224a7688da4db369fda4bc4545bc2d46eaa7a3b68d98e6c64f9807520ed928901767e25b1756
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DTRToRust
4
+ module Common
5
+ # This class is responsible for interpreting the input string.
6
+ class InputInterpreter
7
+ def initialize(input)
8
+ @input = input
9
+ end
10
+
11
+ def self.interpret(input)
12
+ new(input).interpret
13
+ end
14
+
15
+ def interpret
16
+ if variable?(@input)
17
+ variable_return(@input)
18
+ elsif string?(@input)
19
+ string_return(@input)
20
+ elsif number?(@input)
21
+ number_return(@input)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ ## Variable ##
28
+ def variable?(input)
29
+ !string?(input) && !number?(input)
30
+ end
31
+
32
+ def variable_return(_input)
33
+ { value: @input, type: 'variable', needs_reference: true }
34
+ end
35
+
36
+ ## String ##
37
+ def string?(input)
38
+ input.is_a?(String) && (input.match?(/".*"/) || input.match?(/'.*'/))
39
+ end
40
+
41
+ def string_return(_input)
42
+ { value: @input, type: 'string', needs_reference: false }
43
+ end
44
+
45
+ ## Number ##
46
+ def number?(input)
47
+ input.is_a?(Numeric)
48
+ end
49
+
50
+ def number_return(_input)
51
+ { value: @input, type: 'number', needs_reference: false }
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/dtr_to_rust.rb CHANGED
@@ -13,4 +13,9 @@ module DTRToRust
13
13
  autoload :LogString, 'instruction/log_string'
14
14
  autoload :AddAndAssign, 'instruction/add_and_assign'
15
15
  end
16
+
17
+ # This module contains all the classes that handle common logic.
18
+ module Common
19
+ autoload :InputInterpreter, 'common/input_interpreter'
20
+ end
16
21
  end
data/lib/generator.rb CHANGED
@@ -34,7 +34,7 @@ module DTRToRust
34
34
 
35
35
  def generate_contract_header
36
36
  # TODO: don't hardcode imports
37
- @content += "#![no_std]\nuse soroban_sdk::{contract, contractimpl, symbol_short, vec, Env, Symbol, Vec};\n\n"
37
+ @content += "#![no_std]\nuse soroban_sdk::{contract, contractimpl, symbol_short, vec, Env, Symbol, Vec, log};\n\n"
38
38
  end
39
39
 
40
40
  def generate_contract_name
@@ -5,10 +5,38 @@ module DTRToRust
5
5
  # This class is responsible for generating Rust code for the Evaluate instruction.
6
6
  class Evaluate < Handler
7
7
  def handle
8
- rust_string = "#{@instruction[:assign]} = #{@instruction[:inputs][0]}(#{@instruction[:inputs][1..].join(', ')});"
8
+ inputs = @instruction[:inputs][1..]
9
+ evaluated_method_name = @instruction[:inputs][0]
10
+ assignment = @instruction[:assign]
11
+
12
+ rust_string = if assignment.nil?
13
+ "#{evaluated_method_name}(#{inputs_to_rust_string(inputs)});"
14
+ else
15
+ "let mut #{assignment} = #{evaluated_method_name}(#{inputs_to_rust_string(inputs)});"
16
+ end
9
17
 
10
18
  form_rust_string(rust_string, @instruction[:scope])
11
19
  end
20
+
21
+ private
22
+
23
+ def inputs_to_rust_string(inputs)
24
+ inputs.map { |input| ref_appender(input) }.join(', ')
25
+ end
26
+
27
+ def ref_appender(input)
28
+ decorated_input = Common::InputInterpreter.interpret(input)
29
+
30
+ if decorated_input[:needs_reference]
31
+ "&#{decorated_input[:value]}"
32
+ else
33
+ decorated_input[:value]
34
+ end
35
+ end
36
+
37
+ def variable?(input)
38
+ !input.contains?('"') && !input.contains?("'") && !input.match?(/$\d+/)
39
+ end
12
40
  end
13
41
  end
14
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtr_to_rust
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Durst
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-02 00:00:00.000000000 Z
11
+ date: 2024-06-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rust to DTR translator (Digicus Textual Representation).
14
14
  email:
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/common/input_interpreter.rb
20
21
  - lib/dtr_to_rust.rb
21
22
  - lib/generator.rb
22
23
  - lib/instruction/add_and_assign.rb