Class: SorobanRustBackend::CodeGenerator
- Inherits:
-
Object
- Object
- SorobanRustBackend::CodeGenerator
- Defined in:
- lib/code_generator.rb
Instance Method Summary collapse
- #generate ⇒ Object
- #handle_metadata(instruction, metadata) ⇒ Object
- #if_let?(instruction, metadata) ⇒ Boolean
-
#initialize(instructions, base_indentation: 0, user_defined_types: [], function_names: []) ⇒ CodeGenerator
constructor
A new instance of CodeGenerator.
- #match_statement?(instruction, metadata) ⇒ Boolean
- #udt?(instruction) ⇒ Boolean
- #udt_name_fix(udt) ⇒ Object
- #while_loop?(instruction, metadata) ⇒ Boolean
Constructor Details
#initialize(instructions, base_indentation: 0, user_defined_types: [], function_names: []) ⇒ CodeGenerator
Returns a new instance of CodeGenerator.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/code_generator.rb', line 3 def initialize(instructions, base_indentation: 0, user_defined_types: [], function_names: []) @instructions = instructions @base_indentation = base_indentation @user_defined_types = user_defined_types @function_names = function_names silviculturist = SorobanRustBackend::Silviculturist.new(instructions, base_indentation:) silviculturist.make_forrest @forrest = silviculturist.forrest end |
Instance Method Details
#generate ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/code_generator.rb', line 15 def generate return_string = '' @forrest .code_generator_traverse do |y| y = y.compact og_instruction = y[0] instruction = (y[0], y[2]) indentation = og_instruction.instruction == 'goto' ? y[2][:return_scope] + @base_indentation : y[1] return_string << "#{' ' * indentation}#{InstructionHandler.new(instruction, y[2]).generate_rust}\n" if (instruction.instruction == 'exit_with_message' || instruction.instruction == 'return' || instruction.instruction == 'goto') && instruction.scope != 0 jump_back_indent = indentation - 1 return_string << "#{' ' * jump_back_indent}}\n" end end return_string end |
#handle_metadata(instruction, metadata) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/code_generator.rb', line 37 def (instruction, ) if instruction.instruction == 'goto' DTRCore::Instruction.new( 'jump', [[:return_scope]], instruction.assign, instruction.scope, instruction.id ) elsif match_statement?(instruction, ) DTRCore::Instruction.new( 'jump', instruction.inputs + ['ELSE_IF_BRANCH'], instruction.assign, instruction.scope, instruction.id ) elsif if_let?(instruction, ) let_statement = "let #{[:try_assign][:lhs]} = #{[:try_assign][:rhs]}" DTRCore::Instruction.new( 'jump', [let_statement, instruction.inputs[1]], instruction.assign, instruction.scope, instruction.id ) elsif while_loop?(instruction, ) operator_variable = ([:end_of_iteration_check][:lhs]).to_s iterator_variable = ([:end_of_iteration_check][:rhs]).to_s DTRCore::Instruction.new( 'jump', [operator_variable, iterator_variable, instruction.inputs[1], 'WHILE_LOOP'], instruction.assign, instruction.scope, instruction.id ) elsif udt?(instruction) inputs = instruction.inputs udt_found = nil @user_defined_types.each do |udt| fixed_udt_name = udt_name_fix(udt) next unless fixed_udt_name == inputs[1] || (fixed_udt_name == inputs[2] && inputs[0] == '&') udt_found = if inputs.last.instance_of?(DTRCore::UserDefinedType) inputs.last else inputs.push(udt) end break end raise "Unable to instantiate unrecognized UDT: #{inputs[1]}" if udt_found.nil? DTRCore::Instruction.new( 'instantiate_object', inputs, instruction.assign, instruction.scope, instruction.id ) elsif instruction.instruction == 'evaluate' method_name = instruction.inputs[0] method_name = "Self::#{method_name}" if @function_names.include?(method_name) DTRCore::Instruction.new( 'evaluate', [method_name] + instruction.inputs[1..], instruction.assign, instruction.scope, instruction.id ) else instruction end end |
#if_let?(instruction, metadata) ⇒ Boolean
130 131 132 133 134 135 |
# File 'lib/code_generator.rb', line 130 def if_let?(instruction, ) instruction.instruction == 'jump' && [:try_assign] && [:try_assign][:assign] == instruction.inputs[0] && [:parent_scope] == instruction.scope end |
#match_statement?(instruction, metadata) ⇒ Boolean
126 127 128 |
# File 'lib/code_generator.rb', line 126 def match_statement?(instruction, ) [:last_node_was_conditional_jump] && instruction.instruction == 'jump' && [:parent_scope] && [:parent_scope] == instruction.scope end |
#udt?(instruction) ⇒ Boolean
144 145 146 |
# File 'lib/code_generator.rb', line 144 def udt?(instruction) instruction.instruction == 'instantiate_object' && (instruction.inputs[0] == 'UDT' || (instruction.inputs[0] == '&' && instruction.inputs[1] == 'UDT')) end |
#udt_name_fix(udt) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/code_generator.rb', line 118 def udt_name_fix(udt) if udt.name.end_with?('_STRUCT') || udt.name.end_with?('_ENUM') udt.name.split('_')[0..-2].join('_') else udt.name end end |
#while_loop?(instruction, metadata) ⇒ Boolean
137 138 139 140 141 142 |
# File 'lib/code_generator.rb', line 137 def while_loop?(instruction, ) instruction.instruction == 'jump' && [:end_of_iteration_check] && [:end_of_iteration_check][:assign] == instruction.inputs[0] && [:parent_scope] == instruction.scope end |