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 +4 -4
- data/lib/code_generator.rb +61 -3
- data/lib/common/input_interpreter.rb +72 -0
- data/lib/common/type_translator.rb +0 -2
- data/lib/contract_handler.rb +4 -7
- data/lib/contract_state/handler.rb +2 -1
- data/lib/function_handler.rb +9 -8
- data/lib/instruction/add.rb +0 -2
- data/lib/instruction/and.rb +0 -2
- data/lib/instruction/assign.rb +0 -2
- data/lib/instruction/binary_instruction.rb +1 -3
- data/lib/instruction/break.rb +0 -2
- data/lib/instruction/divide.rb +1 -3
- data/lib/instruction/end_of_iteration_check.rb +0 -2
- data/lib/instruction/evaluate.rb +28 -4
- data/lib/instruction/exit_with_message.rb +0 -2
- data/lib/instruction/field.rb +0 -2
- data/lib/instruction/handler.rb +0 -2
- data/lib/instruction/increment.rb +0 -2
- data/lib/instruction/instantiate_object.rb +40 -26
- data/lib/instruction/jump.rb +1 -3
- data/lib/instruction/multiply.rb +1 -3
- data/lib/instruction/or.rb +0 -2
- data/lib/instruction/print.rb +0 -2
- data/lib/instruction/return.rb +0 -2
- data/lib/instruction/subtract.rb +1 -3
- data/lib/instruction/try_assign.rb +0 -2
- data/lib/instruction_handler.rb +0 -2
- data/lib/lcpbt_forrest.rb +0 -27
- data/lib/left_child_preferential_binary_tree.rb +0 -2
- data/lib/silviculturist.rb +8 -5
- data/lib/soroban_rust_backend.rb +1 -2
- data/lib/user_defined_types_handler.rb +4 -6
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 159dd48a238334709063b4cf8412eb41ac4d2365b1ba19a1e44a769db1105852
|
4
|
+
data.tar.gz: ebea69c27f3325cbe6886fbe9f03e3926ef89f3c4a79b796dc78e3e1b6f54fc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80b1596a4070c89e64b3f52f95557cb7cd41b5d75e999b83952ee30bb35c439fe216e90742f1ac31604252453f0e051ba66a747d5cafc4974bee39f061c7a980
|
7
|
+
data.tar.gz: e00c32786d303609e6846b0314557f3fefc55bf149ea52830908649135ed10da88051798b62b8f0d6ede80c7d69a1ee59ee27435898d17ae47d8deabf8358f22
|
data/lib/code_generator.rb
CHANGED
@@ -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 =
|
58
|
-
iterator_variable =
|
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
|
data/lib/contract_handler.rb
CHANGED
@@ -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
|
-
|
25
|
-
|
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
|
-
|
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}:
|
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
|
data/lib/function_handler.rb
CHANGED
@@ -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 += "
|
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
|
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
|
data/lib/instruction/add.rb
CHANGED
data/lib/instruction/and.rb
CHANGED
data/lib/instruction/assign.rb
CHANGED
@@ -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]};"
|
data/lib/instruction/break.rb
CHANGED
data/lib/instruction/divide.rb
CHANGED
@@ -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 <
|
4
|
+
class Divide < BinaryInstruction
|
7
5
|
def initialize(instruction, metadata)
|
8
6
|
super('/', instruction, metadata)
|
9
7
|
end
|
data/lib/instruction/evaluate.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/instruction/field.rb
CHANGED
data/lib/instruction/handler.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
#
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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)
|
data/lib/instruction/jump.rb
CHANGED
@@ -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
|
18
|
+
raise "Invalid jump instruction. Received too many inputs: #{@instruction.inputs.size}"
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
data/lib/instruction/multiply.rb
CHANGED
@@ -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 <
|
4
|
+
class Multiply < BinaryInstruction
|
7
5
|
def initialize(instruction, metadata)
|
8
6
|
super('&', instruction, metadata)
|
9
7
|
end
|
data/lib/instruction/or.rb
CHANGED
data/lib/instruction/print.rb
CHANGED
data/lib/instruction/return.rb
CHANGED
data/lib/instruction/subtract.rb
CHANGED
@@ -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 <
|
4
|
+
class Subtract < BinaryInstruction
|
7
5
|
def initialize(instruction, metadata)
|
8
6
|
super('-', instruction, metadata)
|
9
7
|
end
|
data/lib/instruction_handler.rb
CHANGED
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
|
data/lib/silviculturist.rb
CHANGED
@@ -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
|
-
|
132
|
-
|
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,
|
data/lib/soroban_rust_backend.rb
CHANGED
@@ -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]}
|
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]}
|
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.
|
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-
|
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
|