dtr_to_rust 0.12.1 → 0.13.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: b0c45750a14c1fb946e49b0c0e5b084db7ed6189db1f93e7268ebc5100927831
4
- data.tar.gz: 1ad7efddb1fbc6002cd24e67d73b8653512515e31f78b88b8492d5aa7e1b109c
3
+ metadata.gz: 159dd48a238334709063b4cf8412eb41ac4d2365b1ba19a1e44a769db1105852
4
+ data.tar.gz: ebea69c27f3325cbe6886fbe9f03e3926ef89f3c4a79b796dc78e3e1b6f54fc8
5
5
  SHA512:
6
- metadata.gz: cceae4b47a0a759fe9ab25b820f8e77bd687550a9c0e0ab6259c251f5484069f9804c12f25cc654c6048498f502a317e3835f20a912f5deba1d9f9f09d86bd15
7
- data.tar.gz: ccc003beca43b656890b4360da90eee866b87aa3711448a9f20b3613c2f86df6e179eb4ec9364645bcfaf82fcba61da2b8fa8d1da70d24868c70d9f849f23254
6
+ metadata.gz: 80b1596a4070c89e64b3f52f95557cb7cd41b5d75e999b83952ee30bb35c439fe216e90742f1ac31604252453f0e051ba66a747d5cafc4974bee39f061c7a980
7
+ data.tar.gz: e00c32786d303609e6846b0314557f3fefc55bf149ea52830908649135ed10da88051798b62b8f0d6ede80c7d69a1ee59ee27435898d17ae47d8deabf8358f22
@@ -1,8 +1,10 @@
1
1
  module SorobanRustBackend
2
2
  class CodeGenerator
3
- def initialize(instructions, base_indentation: 0)
3
+ def initialize(instructions, base_indentation: 0, user_defined_types: [], function_names: [])
4
4
  @instructions = instructions
5
5
  @base_indentation = base_indentation
6
+ @user_defined_types = user_defined_types
7
+ @function_names = function_names
6
8
 
7
9
  silviculturist = SorobanRustBackend::Silviculturist.new(instructions, base_indentation:)
8
10
  silviculturist.make_forrest
@@ -22,6 +24,11 @@ module SorobanRustBackend
22
24
  indentation = og_instruction.instruction == 'goto' ? y[2][:return_scope] + @base_indentation : y[1]
23
25
 
24
26
  return_string << "#{' ' * indentation}#{InstructionHandler.new(instruction, y[2]).generate_rust}\n"
27
+
28
+ if instruction.instruction == 'exit_with_message' && instruction.scope != 0
29
+ jump_back_indent = indentation - 1
30
+ return_string << "#{' ' * jump_back_indent}}\n"
31
+ end
25
32
  end
26
33
 
27
34
  return_string
@@ -54,8 +61,8 @@ module SorobanRustBackend
54
61
  instruction.id
55
62
  )
56
63
  elsif while_loop?(instruction, metadata)
57
- operator_variable = "#{metadata[:end_of_iteration_check][:lhs]}"
58
- iterator_variable = "#{metadata[:end_of_iteration_check][:rhs]}"
64
+ operator_variable = (metadata[:end_of_iteration_check][:lhs]).to_s
65
+ iterator_variable = (metadata[:end_of_iteration_check][:rhs]).to_s
59
66
 
60
67
  DTRCore::Instruction.new(
61
68
  'jump',
@@ -64,11 +71,58 @@ module SorobanRustBackend
64
71
  instruction.scope,
65
72
  instruction.id
66
73
  )
74
+ elsif udt?(instruction)
75
+ inputs = instruction.inputs
76
+ udt_found = nil
77
+
78
+ @user_defined_types.each do |udt|
79
+ fixed_udt_name = udt_name_fix(udt)
80
+
81
+ next unless fixed_udt_name == inputs[1] || (fixed_udt_name == inputs[2] && inputs[0] == '&')
82
+
83
+ udt_found = if inputs.last.instance_of?(DTRCore::UserDefinedType)
84
+ inputs.last
85
+ else
86
+
87
+ inputs.push(udt)
88
+ end
89
+ break
90
+ end
91
+
92
+ raise "Unable to instantiate unrecognized UDT: #{inputs[1]}" if udt_found.nil?
93
+
94
+ DTRCore::Instruction.new(
95
+ 'instantiate_object',
96
+ inputs,
97
+ instruction.assign,
98
+ instruction.scope,
99
+ instruction.id
100
+ )
101
+ elsif instruction.instruction == 'evaluate'
102
+ method_name = instruction.inputs[0]
103
+
104
+ method_name = "Self::#{method_name}" if @function_names.include?(method_name)
105
+
106
+ DTRCore::Instruction.new(
107
+ 'evaluate',
108
+ [method_name] + instruction.inputs[1..],
109
+ instruction.assign,
110
+ instruction.scope,
111
+ instruction.id
112
+ )
67
113
  else
68
114
  instruction
69
115
  end
70
116
  end
71
117
 
118
+ def udt_name_fix(udt)
119
+ if udt.name.end_with?('_STRUCT') || udt.name.end_with?('_ENUM')
120
+ udt.name.split('_')[0..-2].join('_')
121
+ else
122
+ udt.name
123
+ end
124
+ end
125
+
72
126
  def match_statement?(instruction, metadata)
73
127
  metadata[:last_node_was_conditional_jump] && instruction.instruction == 'jump' && metadata[:parent_scope] && metadata[:parent_scope] == instruction.scope
74
128
  end
@@ -86,5 +140,9 @@ module SorobanRustBackend
86
140
  metadata[:end_of_iteration_check][:assign] == instruction.inputs[0] &&
87
141
  metadata[:parent_scope] == instruction.scope
88
142
  end
143
+
144
+ def udt?(instruction)
145
+ instruction.instruction == 'instantiate_object' && (instruction.inputs[0] == 'UDT' || (instruction.inputs[0] == '&' && instruction.inputs[1] == 'UDT'))
146
+ end
89
147
  end
90
148
  end
@@ -0,0 +1,72 @@
1
+ module SorobanRustBackend
2
+ module Common
3
+ # This class is responsible for interpreting the input string.
4
+ class InputInterpreter
5
+ def initialize(input)
6
+ @input = input
7
+ end
8
+
9
+ def self.interpret(input)
10
+ new(input).interpret
11
+ end
12
+
13
+ def interpret
14
+ if number?(@input)
15
+ number_return(@input)
16
+ elsif string?(@input)
17
+ string_return(@input)
18
+ elsif boolean?(@input)
19
+ boolean_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: !@input.start_with?('&') }
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
+
62
+ ## Boolean ##
63
+ def boolean?(input)
64
+ input.is_a?(TrueClass) || input.is_a?(FalseClass) || input&.match?(/true|false/)
65
+ end
66
+
67
+ def boolean_return(_input)
68
+ { value: @input, type: 'boolean', needs_reference: false }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Common
5
3
  # TypeTranslator translates DTR types to Rust types
@@ -21,10 +21,8 @@ module SorobanRustBackend
21
21
 
22
22
  @content += NonTranslatables::Handler.generate(dtr_contract.non_translatables) if dtr_contract.non_translatables
23
23
 
24
- if dtr_contract.user_defined_types
25
- dtr_contract.user_defined_types.each do |user_defined_type|
26
- @content += SorobanRustBackend::UserDefinedTypesHandler.generate(user_defined_type)
27
- end
24
+ dtr_contract.user_defined_types&.each do |user_defined_type|
25
+ @content += SorobanRustBackend::UserDefinedTypesHandler.generate(user_defined_type)
28
26
  end
29
27
  @content += ContractState::Handler.generate(dtr_contract.state) if dtr_contract.state
30
28
  generate_contract_name
@@ -46,10 +44,9 @@ module SorobanRustBackend
46
44
  end
47
45
 
48
46
  def generate_functions_each(functions, is_helper)
49
- # function_names = functions&.map(&:name)
50
-
47
+ function_names = functions.map(&:name)
51
48
  functions&.map do |function|
52
- FunctionHandler.generate(function, is_helper)
49
+ FunctionHandler.generate(function, is_helper, dtr_contract.user_defined_types, function_names)
53
50
  end&.join("\n")
54
51
  end
55
52
 
@@ -21,8 +21,9 @@ module SorobanRustBackend
21
21
  @state.each do |state_value|
22
22
  if state_value.type == 'Symbol'
23
23
  content += "const #{state_value.name}: Symbol = symbol_short!(#{state_value.initial_value});\n"
24
+ # TODO: This is a hack. We need to find a better way to handle this and prove its correctness.
24
25
  elsif state_value.type == 'String'
25
- content += "const #{state_value.name}: String = String::from_str(#{state_value.initial_value});\n"
26
+ content += "const #{state_value.name}: Symbol = symbol_short!(#{state_value.initial_value});\n"
26
27
  else
27
28
  content += "const #{state_value.name}: #{Common::TypeTranslator.translate_type(state_value.type)} = #{state_value.initial_value};\n"
28
29
  end
@@ -1,27 +1,27 @@
1
1
  module SorobanRustBackend
2
2
  class FunctionHandler
3
- def initialize(function, is_helper)
3
+ def initialize(function, is_helper, user_defined_types, function_names)
4
4
  @function = function
5
5
  @is_helper = is_helper
6
+ @user_defined_types = user_defined_types
7
+ @function_names = function_names
6
8
  end
7
9
 
8
- def self.generate(function, is_helper)
9
- new(function, is_helper).generate
10
+ def self.generate(function, is_helper, user_defined_types, function_names)
11
+ new(function, is_helper, user_defined_types, function_names).generate
10
12
  end
11
13
 
12
14
  def generate
13
- content = ''
14
-
15
15
  content = "\n#{@is_helper ? '' : ' '}pub fn #{@function.name}(#{generate_function_args}) "
16
16
  content += generate_function_output
17
17
 
18
18
  content += " {\n"
19
19
  if @function.output
20
- content += "#{@is_helper ? ' ' : ' '}let Thing_to_return: #{Common::TypeTranslator.translate_type(@function.output)};\n"
20
+ content += "#{@is_helper ? ' ' : ' '}let mut Thing_to_return: #{Common::TypeTranslator.translate_type(@function.output)};\n"
21
21
  end
22
22
  content += generate_instructions_for_blocks(@function.instructions)
23
23
 
24
- content += "\n#{@is_helper ? '' : ' '}}\n"
24
+ content += "#{@is_helper ? '' : ' '}}\n"
25
25
 
26
26
  content
27
27
  end
@@ -41,7 +41,8 @@ module SorobanRustBackend
41
41
  end
42
42
 
43
43
  def generate_instructions_for_blocks(instructions)
44
- CodeGenerator.new(instructions, base_indentation: @is_helper ? 1 : 2).generate
44
+ CodeGenerator.new(instructions, base_indentation: @is_helper ? 1 : 2,
45
+ user_defined_types: @user_defined_types, function_names: @function_names).generate
45
46
  end
46
47
  end
47
48
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the and instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the assign instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
@@ -11,7 +9,7 @@ module SorobanRustBackend
11
9
  end
12
10
 
13
11
  def handle
14
- if @metadata[:symbol_table].include?(@instruction.assign) || @instruction.assign.include?('.')
12
+ if @metadata[:symbol_table].include?(@instruction.assign) || @instruction.assign.include?('.') || @instruction.assign == 'Thing_to_return'
15
13
  "#{@instruction.assign} = #{@instruction.inputs[0]} #{@operation} #{@instruction.inputs[1]};"
16
14
  else
17
15
  "let mut #{@instruction.assign} = #{@instruction.inputs[0]} #{@operation} #{@instruction.inputs[1]};"
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
@@ -1,9 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
6
- class Divide < Handler
4
+ class Divide < BinaryInstruction
7
5
  def initialize(instruction, metadata)
8
6
  super('/', instruction, metadata)
9
7
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  class EndOfIterationCheck < Handler
@@ -1,10 +1,34 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the Evaluate instruction.
6
4
  class Evaluate < Handler
7
5
  def handle
6
+ @ref_preface = false
7
+
8
+ if @instruction.inputs[0] == '&'
9
+ @ref_preface = true
10
+ @instruction.inputs.shift
11
+ end
12
+
13
+ adjusted_inputs = []
14
+ last_was_ref = false
15
+ @instruction.inputs.each do |input|
16
+ if input == '&'
17
+ last_was_ref = true
18
+ else
19
+ adjusted_inputs << (last_was_ref ? "&#{input}" : input)
20
+ last_was_ref = false
21
+ end
22
+ end
23
+
24
+ @instruction = DTRCore::Instruction.new(
25
+ @instruction.instruction,
26
+ adjusted_inputs,
27
+ @instruction.assign,
28
+ @instruction.scope,
29
+ @instruction.id
30
+ )
31
+
8
32
  handle_keyword_method_invocation
9
33
  end
10
34
 
@@ -44,8 +68,8 @@ module SorobanRustBackend
44
68
 
45
69
  assignment_rust = "let mut #{assignment} = "
46
70
  # TODO: make this less hacky evaluated_method_name.end_with?('set')
47
- body_rust = "#{invocation_name(evaluated_method_name)}(#{inputs_to_rust_string(inputs,
48
- append_ref_to_num?, try_append_ref_to_var?)});"
71
+ body_rust = "#{@ref_preface ? '&' : ''}#{invocation_name(evaluated_method_name)}(#{inputs_to_rust_string(inputs,
72
+ append_ref_to_num?, try_append_ref_to_var?)});"
49
73
  "#{if assignment.nil?
50
74
  ''
51
75
  else
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the exit_with_message instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the Field instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the AddAndAssign instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  class Increment < Handler
@@ -1,19 +1,32 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the LogString instruction.
6
4
  class InstantiateObject < Handler
7
5
  def handle
6
+ @ref_preface = false
7
+ if @instruction.inputs[0] == '&'
8
+ @ref_preface = true
9
+ @instruction.inputs.shift
10
+ end
11
+
8
12
  case @instruction.inputs[0]
9
13
  when 'List'
10
14
  handle_list
11
15
  when 'UDT'
12
16
  handle_udt
17
+
13
18
  when 'Tuple'
14
- "let mut #{@instruction.assign} = (#{normalish_inputs});"
19
+ if @instruction.assign.include?('.') || @instruction.assign == 'Thing_to_return'
20
+ "#{@instruction.assign} = #{@ref_preface ? '&' : ''}(#{normalish_inputs});"
21
+ else
22
+ "let mut #{@instruction.assign} = #{@ref_preface ? '&' : ''}(#{normalish_inputs});"
23
+ end
15
24
  when 'Range'
16
- "let mut #{@instruction.assign} = #{range_inputs};"
25
+ if @instruction.assign.include?('.') || @instruction.assign == 'Thing_to_return'
26
+ "#{@instruction.assign} = #{@ref_preface ? '&' : ''}#{range_inputs};"
27
+ else
28
+ "let mut #{@instruction.assign} = #{@ref_preface ? '&' : ''}#{range_inputs};"
29
+ end
17
30
  else
18
31
  raise "Unknown object type: #{@instruction.inputs[0]}"
19
32
  end
@@ -22,7 +35,11 @@ module SorobanRustBackend
22
35
  private
23
36
 
24
37
  def handle_list
25
- "let mut #{@instruction.assign} = vec![#{normalish_inputs}];"
38
+ if @instruction.assign.include?('.') || @instruction.assign == 'Thing_to_return'
39
+ "#{@instruction.assign} = #{@ref_preface ? '&' : ''}vec![#{normalish_inputs}];"
40
+ else
41
+ "let mut #{@instruction.assign} = #{@ref_preface ? '&' : ''}vec![#{normalish_inputs}];"
42
+ end
26
43
  end
27
44
 
28
45
  def range_inputs
@@ -46,18 +63,16 @@ module SorobanRustBackend
46
63
  end
47
64
 
48
65
  def handle_udt
49
- # udt_found = @user_defined_types.filter { |udt| udt_name_fix(udt) == @instruction.inputs[1] }
50
-
51
- # assignment = "let mut #{@instruction.assign} = "
52
- # udt = "#{@instruction.inputs[1]}{"
53
- # inputs = inputs_to_rust_string(@instruction.inputs[2..], udt_found[0].attributes.map { |x| x[:name] })
54
- # end_ = '};'
55
- # form_rust_string("#{assignment}#{udt}#{inputs}#{end_}")
56
-
57
- "let mut #{@instruction.assign} = #{@instruction.inputs[1]}{#{inputs_to_rust_string(@instruction.inputs[2..],
58
- @instruction.inputs[1])}}"
59
-
60
- # raise 'Not implemented'
66
+ udt_found = @instruction.inputs[@instruction.inputs.size - 1]
67
+
68
+ inputs_sans_udt = @instruction.inputs[..-2][2..]
69
+ assignment = "let mut #{@instruction.assign} = "
70
+ udt = "#{@ref_preface ? '&' : ''}#{@instruction.inputs[1]}{"
71
+ inputs = inputs_to_rust_string(inputs_sans_udt, udt_found.attributes.map do |x|
72
+ x[:name]
73
+ end)
74
+ end_ = '};'
75
+ "#{assignment}#{udt}#{inputs}#{end_}"
61
76
  end
62
77
 
63
78
  def inputs_to_rust_string(inputs, udt_type_names)
@@ -70,16 +85,15 @@ module SorobanRustBackend
70
85
  end
71
86
 
72
87
  def foobar(input)
73
- input
74
- # decorated_input = Common::InputInterpreter.interpret(input)
88
+ decorated_input = Common::InputInterpreter.interpret(input)
75
89
 
76
- # if decorated_input[:type] == 'string'
77
- # "symbol_short!(#{input})"
78
- # elsif decorated_input[:needs_reference] && input == 'env'
79
- # "&#{input}"
80
- # else
81
- # input
82
- # end
90
+ if decorated_input[:type] == 'string'
91
+ "String::from_str(&env, #{input})"
92
+ elsif decorated_input[:needs_reference] && input == 'env'
93
+ "&#{input}"
94
+ else
95
+ input
96
+ end
83
97
  end
84
98
 
85
99
  def handle_input(input, udt_type_name)
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the jump instruction.
@@ -17,7 +15,7 @@ module SorobanRustBackend
17
15
  elsif @instruction.inputs.size == 4 && last_instruction_is_while_loop
18
16
  handle_while_loop
19
17
  else
20
- raise 'Invalid jump instruction. Received too many inputs: ' + @instruction.inputs.size.to_s
18
+ raise "Invalid jump instruction. Received too many inputs: #{@instruction.inputs.size}"
21
19
  end
22
20
  end
23
21
 
@@ -1,9 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
6
- class Multiply < Handler
4
+ class Multiply < BinaryInstruction
7
5
  def initialize(instruction, metadata)
8
6
  super('&', instruction, metadata)
9
7
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the or instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the LogString instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class is responsible for generating Rust code for the Return instruction.
@@ -1,9 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the add instruction.
6
- class Subtract < Handler
4
+ class Subtract < BinaryInstruction
7
5
  def initialize(instruction, metadata)
8
6
  super('-', instruction, metadata)
9
7
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  module Instruction
5
3
  # This class handles the goto instruction.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  # This class is responsible for generating Rust code for a single instruction.
5
3
  class InstructionHandler
data/lib/lcpbt_forrest.rb CHANGED
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  class LCPBT_Forrest
5
3
  attr_accessor :trees
@@ -54,30 +52,5 @@ module SorobanRustBackend
54
52
  def all_paths_to(instruction_id)
55
53
  @trees.map { |x| x.all_paths_to(instruction_id) }.flatten(1).compact
56
54
  end
57
-
58
- def walk_it
59
- @trees.each do |x|
60
- puts "Walking tree #{x.tree_id}"
61
-
62
- stack = []
63
- symbol_table = {}
64
-
65
- stack << x
66
-
67
- while stack.any?
68
- current = stack.pop
69
-
70
- if current.value.assign
71
- symbol_table[current.value.assign] = {
72
- value: current.value.id
73
- }
74
- end
75
-
76
- stack << current.left_child if current.left_child
77
-
78
- stack << current.right_child if current.right_child
79
- end
80
- end
81
- end
82
55
  end
83
56
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'securerandom'
4
2
 
5
3
  module SorobanRustBackend
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  class Silviculturist
5
3
  attr_accessor :forrest
@@ -127,9 +125,14 @@ module SorobanRustBackend
127
125
 
128
126
  return plant_trees(scope, rest_of_instructions, from_id: cur_instruction.id, indentation:, metadata:)
129
127
  else
130
- last_symbol_table = metadata[:symbol_table]
131
- last_symbol_table[cur_instruction.assign] = {} unless last_symbol_table.include?(cur_instruction.assign)
132
- last_symbol_table[cur_instruction.assign][cur_instruction.scope] = cur_instruction
128
+ last_symbol_table = metadata[:symbol_table].clone
129
+
130
+ if cur_instruction.assign
131
+ assignment = cur_instruction.assign
132
+ assignment = assignment.split('|||').first if assignment.include?('|||')
133
+ last_symbol_table[assignment] = {} unless last_symbol_table.include?(assignment)
134
+ last_symbol_table[assignment][cur_instruction.scope] = cur_instruction
135
+ end
133
136
 
134
137
  metadata = {
135
138
  last_node_was_conditional_jump: false,
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  # This is the main module for the DTR to Rust gem.
4
2
  module SorobanRustBackend
5
3
  autoload :LCPBT_Forrest, 'lcpbt_forrest'
@@ -39,6 +37,7 @@ module SorobanRustBackend
39
37
 
40
38
  module Common
41
39
  autoload :TypeTranslator, './lib/common/type_translator'
40
+ autoload :InputInterpreter, './lib/common/input_interpreter'
42
41
  end
43
42
 
44
43
  module NonTranslatables
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module SorobanRustBackend
4
2
  class UserDefinedTypesHandler
5
3
  def initialize(user_defined_type)
@@ -41,16 +39,16 @@ module SorobanRustBackend
41
39
  Common::TypeTranslator.translate_type(x)
42
40
  end
43
41
  if inner_types.empty? || x[:type] == '()'
44
- " #{x[:name]}: (),"
42
+ " #{x[:name]},"
45
43
  else
46
- " #{x[:name]}: (#{inner_types.join(', ')}),"
44
+ " #{x[:name]}(#{inner_types.join(', ')}),"
47
45
  end
48
46
  elsif x[:type]&.match(/\d+/)
49
47
  " #{x[:name]} = #{x[:type]},"
50
48
  elsif x[:type] && x[:type] != '()'
51
- " #{x[:name]}: (#{Common::TypeTranslator.translate_type(x[:type])}),"
49
+ " #{x[:name]}(#{Common::TypeTranslator.translate_type(x[:type])}),"
52
50
  else
53
- " #{x[:name]}: (),"
51
+ " #{x[:name]},"
54
52
  end
55
53
  end.join("\n")
56
54
  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.12.1
4
+ version: 0.13.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-07-15 00:00:00.000000000 Z
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rust to DTR translator (Digicus Textual Representation).
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/code_generator.rb
21
+ - lib/common/input_interpreter.rb
21
22
  - lib/common/type_translator.rb
22
23
  - lib/contract_handler.rb
23
24
  - lib/contract_state/handler.rb