dtr_to_rust 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75a4155b6f5e591a764deb02cca02bd05cb781481ae4d5602f2b784219d90837
4
- data.tar.gz: 111292133a70c1c1b81b402d263837b271655445262cc62c26ebbead2bdfb398
3
+ metadata.gz: 9c1b886c52baf81105bc299619682bcca98bc31e80fae633241bd5fabfcc9df4
4
+ data.tar.gz: 59d69ab207c8c293ea91ada01691e9282eafc62274846cb78cba76d3ef5323a8
5
5
  SHA512:
6
- metadata.gz: 3dd217b90fd5aa4480c8932ddc95abf57630837aaf0f870248911233e23c093216c131eb18f431e5e65aaa48558c2fb051d480009048fb98bbd1aee7cb916d63
7
- data.tar.gz: 12a427b6a3f01d2afd76886168a8078d8cf9c7c090830f0e97250e9df71ae923e65b04f3bd43988c53027d147c560398764ea7a9d9882f37686e2874f902a1f9
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
@@ -5,13 +5,37 @@ 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 = if @instruction[:assign].nil?
9
- "#{@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
17
+
18
+ form_rust_string(rust_string, @instruction[:scope])
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]}"
10
32
  else
11
- "let mut #{@instruction[:assign]} = #{@instruction[:inputs][0]}(#{@instruction[:inputs][1..].join(', ')});"
33
+ decorated_input[:value]
12
34
  end
35
+ end
13
36
 
14
- form_rust_string(rust_string, @instruction[:scope])
37
+ def variable?(input)
38
+ !input.contains?('"') && !input.contains?("'") && !input.match?(/$\d+/)
15
39
  end
16
40
  end
17
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtr_to_rust
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Durst
@@ -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