dtr_to_rust 0.5.0 → 0.8.0

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: b758e2693dba6f08986542c9a49c6d15f285529cbb172d3a29bb171ec95536b1
4
- data.tar.gz: 3d4ef34c8dfd29ff2124fe8c3b0c8ac194872729bbd9443b633a0e5e973aafb6
3
+ metadata.gz: 2bf21d724df71755ed0769ec333c9c8024b476a8554c7ec3d6823f31973806aa
4
+ data.tar.gz: 1c365dd23e89bb03df80904a86f5e5bdda329a8ea03bb886cff43eeaffa0c2b2
5
5
  SHA512:
6
- metadata.gz: 40cd10cc460cae31a4a596a28e10d9ede4af898effbc4cd14037394ed6d3930cb47420c88f6716c253c3f33b732ed2474cb99f6af5a1d0048d3c71bfea80f9c5
7
- data.tar.gz: b4dda97391b69c4eefa43916514d8185564ba98b2d86b461b8e45b2f94e2e578cc360de5c84a5768df99494c295a9c708db0500a4db9582d5ccacb80fc5d967c
6
+ metadata.gz: '09494797507a355af589c1d6cd9f6973b3d82a51ab66ec7b17ccbed0aabfccb1b991f8b844ce46fc1e64ec1bf48cbbd88637c0225d0ed8285067e6c5718ea5a8'
7
+ data.tar.gz: a6c5e8a6ad5c2c14ff41b3216ef317ca647d379ac0a5a53fb3b055c48c02d2d4f430c387fc406028777a79d5f7aab32feb0a0562166779ac3fd5db50b4d2fa28
data/lib/generator.rb CHANGED
@@ -107,8 +107,12 @@ module DTRToRust
107
107
 
108
108
  return_string = "\n#{is_helper ? '' : ' '}pub fn #{function.name}(#{generate_function_args(function)}) "
109
109
  return_string += generate_function_output(function)
110
- return_string += " {\n#{generate_instructions_for_blocks(instruction_blocks, function_names,
111
- is_helper)}"
110
+ return_string += " {\n"
111
+ if function.output
112
+ return_string += " let Thing_to_return: #{Common::TypeTranslator.translate_type(function.output)};\n"
113
+ end
114
+ return_string += "#{generate_instructions_for_blocks(instruction_blocks, function_names,
115
+ is_helper)}"
112
116
 
113
117
  if @last_scope.positive?
114
118
  while @last_scope.positive?
@@ -5,7 +5,7 @@ module DTRToRust
5
5
  # This class handles the assign instruction.
6
6
  class Assign < Handler
7
7
  def handle
8
- if @instruction.assign.include?('.')
8
+ if @instruction.assign.include?('.') || @instruction.assign == 'Thing_to_return'
9
9
  form_rust_string("#{@instruction.assign} = #{@instruction.inputs[0]};")
10
10
  else
11
11
  form_rust_string("let mut #{@instruction.assign} = #{@instruction.inputs[0]};")
@@ -14,13 +14,19 @@ module DTRToRust
14
14
  def handle_keyword_method_invocation
15
15
  case @instruction.inputs[0]
16
16
  when 'equal_to'
17
- handle_equal_to
17
+ handle_binary('==')
18
18
  when '!'
19
19
  handle_unary_negation
20
20
  when 'less_than'
21
- handle_less_than
21
+ handle_binary('<')
22
22
  when 'less_than_or_equal_to'
23
- handle_less_than_or_equal_to
23
+ handle_binary('<=')
24
+ when 'greater_than'
25
+ handle_binary('>')
26
+ when 'greater_than_or_equal_to'
27
+ handle_binary('>=')
28
+ when 'not_equal_to'
29
+ handle_binary('!=')
24
30
  else
25
31
  handle_non_keyword_method_invocation
26
32
  end
@@ -35,44 +41,22 @@ module DTRToRust
35
41
  # TODO: make this less hacky evaluated_method_name.end_with?('set')
36
42
  body_rust = "#{invocation_name(evaluated_method_name)}(#{inputs_to_rust_string(inputs,
37
43
  append_ref_to_num?, try_append_ref_to_var?)});"
38
- "#{assignment.nil? ? '' : assignment_rust}#{body_rust}"
39
- end
40
-
41
- def handle_equal_to
42
- inputs = @instruction.inputs[1..]
43
- @instruction.inputs[0]
44
- assignment = @instruction.assign
45
-
46
- assignment_rust = "let #{assignment} = "
47
- lhs = Common::ReferenceAppender.call(inputs[0])
48
- rhs = Common::ReferenceAppender.call(inputs[1])
49
- body_rust = "#{lhs} == #{rhs};"
50
-
51
- "#{assignment.nil? ? '' : assignment_rust}#{body_rust}"
52
- end
53
-
54
- def handle_less_than
55
- inputs = @instruction.inputs[1..]
56
- @instruction.inputs[0]
57
- assignment = @instruction.assign
58
-
59
- assignment_rust = "let #{assignment} = "
60
- lhs = Common::ReferenceAppender.call(inputs[0])
61
- rhs = Common::ReferenceAppender.call(inputs[1])
62
- body_rust = "#{lhs} < #{rhs};"
63
-
64
- "#{assignment.nil? ? '' : assignment_rust}#{body_rust}"
44
+ "#{if assignment.nil?
45
+ ''
46
+ else
47
+ assignment == 'Thing_to_return' ? assignment + ' = ' : assignment_rust
48
+ end}#{body_rust}"
65
49
  end
66
50
 
67
- def handle_less_than_or_equal_to
51
+ def handle_binary(operation)
68
52
  inputs = @instruction.inputs[1..]
69
53
  @instruction.inputs[0]
70
54
  assignment = @instruction.assign
71
55
 
72
56
  assignment_rust = "let #{assignment} = "
73
- lhs = Common::ReferenceAppender.call(inputs[0])
74
- rhs = Common::ReferenceAppender.call(inputs[1])
75
- body_rust = "#{lhs} <= #{rhs};"
57
+ lhs = inputs[0]
58
+ rhs = inputs[1]
59
+ body_rust = "#{lhs} #{operation} #{rhs};"
76
60
 
77
61
  "#{assignment.nil? ? '' : assignment_rust}#{body_rust}"
78
62
  end
@@ -94,7 +78,11 @@ module DTRToRust
94
78
 
95
79
  def try_append_ref_to_var?
96
80
  # SO HACKY - Refs are hard man
97
- !(@instruction.inputs[0].end_with?('unwrap_or') || @instruction.inputs[0].end_with?('publish'))
81
+ !(@instruction.inputs[0].end_with?('unwrap_or') ||
82
+ @instruction.inputs[0].end_with?('publish') ||
83
+ @instruction.inputs[0].end_with?('Err') ||
84
+ @instruction.inputs[0].end_with?('Ok')
85
+ )
98
86
  end
99
87
 
100
88
  def invocation_name(evaluated_method_name)
@@ -37,6 +37,17 @@ module DTRToRust
37
37
  @user_defined_type.attributes.map do |x|
38
38
  if x[:value]
39
39
  " #{x[:name]} = #{x[:value]},"
40
+ elsif x[:type] && x[:type].start_with?('(') && x[:type].end_with?(')')
41
+ inner_types = x[:type].gsub('(', '').gsub(')', '').split(',').map do |x|
42
+ Common::TypeTranslator.translate_type(x)
43
+ end
44
+ if inner_types.size == 0 || x[:type] == '()'
45
+ " #{x[:name]},"
46
+ else
47
+ " #{x[:name]}(#{inner_types.join(', ')}),"
48
+ end
49
+ elsif x[:type] && x[:type].match(/\d+/)
50
+ " #{x[:name]} = #{x[:type]},"
40
51
  elsif x[:type] && x[:type] != '()'
41
52
  " #{x[:name]}(#{Common::TypeTranslator.translate_type(x[:type])}),"
42
53
  else
@@ -47,7 +58,7 @@ module DTRToRust
47
58
 
48
59
  def derives
49
60
  base = if error? && enum?
50
- "#[contracterror]\n#[derive(Clone, Debug, Eq, PartialEq)]\n"
61
+ "#[contracterror]\n#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]\n"
51
62
  else
52
63
  "#[contracttype]\n#[derive(Clone, Debug, Eq, PartialEq)]\n"
53
64
  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.5.0
4
+ version: 0.8.0
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-23 00:00:00.000000000 Z
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rust to DTR translator (Digicus Textual Representation).
14
14
  email: