rust_to_dtr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/dtr_to_rust.rb +7 -0
- data/lib/generator.rb +75 -0
- data/lib/instruction_handler.rb +35 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7686a28d85393840d3574afabb964a5ff7a4c0fba3c6abaf519bd28cecf01a6
|
4
|
+
data.tar.gz: 5108937d4d2cb804c08a2489fd41a93f91717c0a3767810cb6d19521247232bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac1974fba9b3e51a0b92dba612b597212572df18fdab52d6d510a05a93bf38477973208c8b7bb371478db6811de721532ed97a101645d98cdc3f5dd1f40e2187
|
7
|
+
data.tar.gz: aa2030af04af08f64df45bf1d604692017c3e20dae57698a060e1fb17f905f515f34cf125dad9cc078e6b30fc6f83b45c8d32b6271e219c99c6f76019b92326c
|
data/lib/dtr_to_rust.rb
ADDED
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: rust_to_dtr
|
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: []
|