dtr_to_rust 0.0.8 → 0.1.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: 75a4155b6f5e591a764deb02cca02bd05cb781481ae4d5602f2b784219d90837
4
- data.tar.gz: 111292133a70c1c1b81b402d263837b271655445262cc62c26ebbead2bdfb398
3
+ metadata.gz: c0da890c9d6e5d3255e3e68e3682c44aa96dc9183300fcda4fd9213804e00177
4
+ data.tar.gz: f2e4076c01ae9df3ea11d6673638565b319ee5912d175a5ccbc961e1dc539727
5
5
  SHA512:
6
- metadata.gz: 3dd217b90fd5aa4480c8932ddc95abf57630837aaf0f870248911233e23c093216c131eb18f431e5e65aaa48558c2fb051d480009048fb98bbd1aee7cb916d63
7
- data.tar.gz: 12a427b6a3f01d2afd76886168a8078d8cf9c7c090830f0e97250e9df71ae923e65b04f3bd43988c53027d147c560398764ea7a9d9882f37686e2874f902a1f9
6
+ metadata.gz: 1afffffff9bbc3b1508bc51486b1c9baecc379c509d553b45a8083788544a8460f3ac9aff5b3c4bd1620fe8c957ff77f6696543c91b0d059e9d376bced1705a9
7
+ data.tar.gz: 71b9a961cd662205319e5604f563574ae770ed15baec2b28d3b3d35c69bdb09271d44ad1cafe0f63da58c4ec293af0684c25ad6480b4f9c58d96e058c070e289
@@ -0,0 +1,63 @@
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 number?(@input)
17
+ number_return(@input)
18
+ elsif string?(@input)
19
+ string_return(@input)
20
+ else
21
+ variable_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) || input&.match?(/^\s*\d+\.?\d*\s*$/)
48
+ end
49
+
50
+ def number_return(_input)
51
+ { value: contains_decimal?(@input.to_s) ? @input.to_f : @input.to_i, type: 'number', needs_reference: false }
52
+ end
53
+
54
+ def contains_decimal?(str)
55
+ # Define a regular expression pattern for a decimal number
56
+ decimal_pattern = /\d+\.\d+/
57
+
58
+ # Check if the string matches the pattern
59
+ !!(str =~ decimal_pattern)
60
+ end
61
+ end
62
+ end
63
+ 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,14 +5,34 @@ 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(', ')});"
10
- else
11
- "let mut #{@instruction[:assign]} = #{@instruction[:inputs][0]}(#{@instruction[:inputs][1..].join(', ')});"
12
- end
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
13
17
 
14
18
  form_rust_string(rust_string, @instruction[:scope])
15
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
16
36
  end
17
37
  end
18
38
  end
@@ -5,7 +5,23 @@ module DTRToRust
5
5
  # This class is responsible for generating Rust code for the LogString instruction.
6
6
  class LogString < Handler
7
7
  def handle
8
- form_rust_string("log!(#{@instruction[:inputs].join(',')});", @instruction[:scope])
8
+ form_rust_string("log!(#{inputs_to_rust_string(@instruction[:inputs])});", @instruction[:scope])
9
+ end
10
+
11
+ private
12
+
13
+ def inputs_to_rust_string(inputs)
14
+ inputs.map { |input| ref_appender(input) }.join(', ')
15
+ end
16
+
17
+ def ref_appender(input)
18
+ decorated_input = Common::InputInterpreter.interpret(input)
19
+
20
+ if decorated_input[:needs_reference]
21
+ "&#{decorated_input[:value]}"
22
+ else
23
+ decorated_input[:value]
24
+ end
9
25
  end
10
26
  end
11
27
  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.1.0
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