dtr_to_rust 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 71e0a411f0334b28f5a9c2cdb1c37edf9d5d0809ec1e58ab788a8f415f826051
4
+ data.tar.gz: 9b5e033c957a78ab03662b32ec4f8db4806cb4842571437d9eed4ff353a816cf
5
+ SHA512:
6
+ metadata.gz: b0ee5eae92ae5a2ac7ce5942c058a2acfdfd33ae1d73e2f7464172f586b7cf6cf8b9eadaa38e31f87c79dd7f3a853d652257cbe4f374ef422b76d88c8a5b879e
7
+ data.tar.gz: 4580e5e730eb5045319de5206335f56021a2c8f294428ed2e86236c9686c31e5962540d1876f3ae8bd79762e96ff764759c5ed1a86f763137e15a3af9a5a0112
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is the main module for the DTR to Rust gem.
4
+ module DTRToRust
5
+ autoload :Generator, 'generator'
6
+ autoload :InstructionHandler, 'instruction_handler'
7
+ end
data/lib/generator.rb ADDED
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dtr_core'
4
+
5
+ module DTRToRust
6
+ # Generates Rust code from a DTR contract
7
+ class Generator
8
+ def initialize(file_path)
9
+ @file_path = file_path
10
+ @dtr_contract = ::DTRCore::Parser.parse(file_path)
11
+ end
12
+
13
+ def generate
14
+ @content = ''
15
+
16
+ generate_contract_header
17
+ generate_contract_name
18
+ generate_state
19
+ generate_functions
20
+
21
+ @content
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :dtr_contract
27
+
28
+ def generate_contract_header
29
+ # TODO: don't hardcode imports
30
+ @content += "#![no_std]\nuse soroban_sdk::{contract, contractimpl, symbol_short, vec, Env, Symbol, Vec};\n\n"
31
+ end
32
+
33
+ def generate_contract_name
34
+ @content += "#[contract]\npub struct #{dtr_contract.name};\n\n"
35
+ end
36
+
37
+ def generate_state
38
+ return if dtr_contract.state.nil?
39
+
40
+ @content += 'pub struct State {'
41
+ dtr_contract.state.each do |state|
42
+ @content += " pub #{state.name}: #{state.type},"
43
+ end
44
+ @content += "}\n\n"
45
+ end
46
+
47
+ def generate_functions
48
+ @content += "#[contractimpl]\nimpl #{dtr_contract.name} {#{generate_functions_each(dtr_contract.functions)}}\n"
49
+ end
50
+
51
+ def generate_functions_each(functions)
52
+ functions.map do |function|
53
+ "\n pub fn #{function.name}(#{generate_function_args(function)}) " \
54
+ "-> #{function.output} {\n#{generate_instructions_each(function.instructions)}\n }\n"
55
+ end.join("\n")
56
+ end
57
+
58
+ def generate_function_args(function)
59
+ all_inputs = [{ name: 'env', type_name: 'Env' }] + function.inputs
60
+
61
+ all_inputs.map { |x| "#{x[:name]}: #{x[:type_name]}" }.join(', ')
62
+ end
63
+
64
+ def generate_instructions_each(instructions)
65
+ instructions.map do |instruction|
66
+ generate_instruction(instruction)
67
+ end.join("\n")
68
+ end
69
+
70
+ def generate_instruction(instruction)
71
+ handler = InstructionHandler.new(instruction)
72
+ handler.generate_rust
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DTRToRust
4
+ # This class is responsible for generating Rust code for a single instruction.
5
+ class InstructionHandler
6
+ def initialize(instruction)
7
+ @instruction = instruction
8
+ end
9
+
10
+ def generate_rust
11
+ case @instruction[:instruction]
12
+ when 'AddSymbols'
13
+ handle_add_symbols
14
+ when 'Return'
15
+ handle_return
16
+ else
17
+ raise "Unknown instruction type: #{@instruction[:instruction]}"
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :instruction
24
+
25
+ def handle_add_symbols
26
+ " let #{instruction[:assign]} " \
27
+ '= vec![&env, ' \
28
+ "symbol_short!(#{instruction[:inputs][0]}), #{instruction[:inputs][1]}];"
29
+ end
30
+
31
+ def handle_return
32
+ " #{instruction[:inputs][0]}"
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dtr_to_rust
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Durst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Rust to DTR translator (Digicus Textual Representation).
14
+ email:
15
+ - me@robdurst.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dtr_to_rust.rb
21
+ - lib/generator.rb
22
+ - lib/instruction_handler.rb
23
+ homepage: https://spaced-out-thoughts-dev-foundation.github.io/digicus/
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ rubygems_mfa_required: 'true'
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.2.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.4.10
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Rust to DTR translator (Digicus Textual Representation).
47
+ test_files: []