dtr_core 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c7633c61c5ed0d1268fb63d2b9245037292c352e850c819e5017fab420c8044
4
- data.tar.gz: 7a0dcefa916e3daecefa505c8556e75fe5047e39836a31fe893affa043745f02
3
+ metadata.gz: 9594c7bac85d086ec888836ba17242023cadaccc4f6d4dc268520035324e682b
4
+ data.tar.gz: a9cb05cd8ac136ebaf433b7f7f6c6d2fc64f60e807588b59f435a056311a2374
5
5
  SHA512:
6
- metadata.gz: a2de4799a22ec515c6a23ee27e09735f6cd7043832dc3ab79b108ea68c802ddc0ae721ca78c72384712796302a2df55aee0b529c12439430b1ceaf948ee6ad79
7
- data.tar.gz: 0b379f38905d1d28ea6d4dc8044622b7598383d1ee3bf627fa70f9c6d25d019506b8ab269bc99561c71bd6548afa969e3ec18621af19372bf4f441c2677b2fcd
6
+ metadata.gz: 189ffb27f3065aaa6d09bb5da75b693abe88236e073792aea4b0f33a609fa9383d187cbb12bf246e5bb470476bb767b4c74f60067f7cfa3e51a24383f742ff46
7
+ data.tar.gz: a261d658929cd4c9208f47ccebafc3d5b9b065fb328a184637fad60c843d6a000ec4cbbf736b57e9a4152be1e2433dd2bfb13b86319a653c7cec25a3d71b122a
@@ -8,11 +8,15 @@ module DTRCore
8
8
  end
9
9
 
10
10
  def split_strip_select(some_list)
11
- some_list&.split("\n")&.map { |x| x&.strip }&.select { |x| x.length.positive? }
11
+ some_list&.split("\n")&.map(&:strip)&.select { |x| x.length.positive? }
12
12
  end
13
13
 
14
14
  def first_match_for_content(patterm)
15
15
  content.match(patterm)&.captures&.first
16
16
  end
17
+
18
+ def clean_name(definition)
19
+ definition.gsub(/[\*\n\[]/, '').strip
20
+ end
17
21
  end
18
22
  end
@@ -11,6 +11,18 @@ module DTRCore
11
11
  @functions = functions
12
12
  end
13
13
 
14
+ def self.from_dtr(filepath)
15
+ parser = DTRCore::Parser.new(filepath)
16
+
17
+ new(parser.name_section, parser.state_section, parser.function_section)
18
+ end
19
+
20
+ def self.from_dtr_raw(content)
21
+ parser = DTRCore::Parser.new('', content:)
22
+
23
+ new(parser.name_section, parser.state_section, parser.function_section)
24
+ end
25
+
14
26
  def ==(other)
15
27
  name == other.name &&
16
28
  state == other.state &&
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dtr_core/common'
4
+
3
5
  module DTRCore
4
6
  # Represents a state in a DTR file.
5
7
  class Function
8
+ include ::DTRCore::Common
9
+
6
10
  attr_reader :name, :inputs, :output, :instructions
7
11
 
8
12
  def initialize(name, inputs, output, instructions)
@@ -12,11 +16,55 @@ module DTRCore
12
16
  @instructions = instructions
13
17
  end
14
18
 
19
+ def sanitize
20
+ @inputs = format_function_inputs(@inputs)
21
+ @instructions = format_function_instruction(@instructions)
22
+ end
23
+
24
+ def self.from_definition(definition)
25
+ name = definition[/\s*\[(?<all>[^\]]+)]/, 1]
26
+ inputs = (definition[/Inputs\s*:\s*{\s*(?<inputs>[^}]+)\s*}/, 1])
27
+ # TODO: check output type
28
+ output = definition[/Output:\s*(.+)/, 1]
29
+ instructions = definition[/Instructions:\s*\$(?<inputs>[^\$]+)\$/, 1]
30
+
31
+ function_object = new(name, inputs, output, instructions)
32
+
33
+ function_object.sanitize
34
+
35
+ function_object
36
+ end
37
+
15
38
  def ==(other)
16
39
  name == other.name &&
17
40
  inputs == other.inputs &&
18
41
  output == other.output &&
19
42
  instructions == other.instructions
20
43
  end
44
+
45
+ private
46
+
47
+ def format_function_inputs(inputs)
48
+ return [] if inputs.nil?
49
+
50
+ split_strip_select(inputs).map { |x| { name: x.split(':')[0].strip, type_name: x.split(':')[1].strip } }
51
+ end
52
+
53
+ def format_function_instruction(instructions)
54
+ split_strip_select(instructions)&.map { |instruction| parse_function_instruction(instruction) }
55
+ end
56
+
57
+ def parse_function_instruction(instruction)
58
+ {
59
+ instruction: instruction[/instruction:\s*(?<all>[^\s,]+)/, 1],
60
+ inputs: parse_function_instruction_input(instruction),
61
+ assign: instruction[/\s*assign:\s*(?<all>[^\s\,]+)/, 1]
62
+ }
63
+ end
64
+
65
+ def parse_function_instruction_input(definition)
66
+ definition[/\s*input:\s*\((?<all>[^\)]+)\)/, 1]
67
+ &.split(',')&.map(&:strip)
68
+ end
21
69
  end
22
70
  end
@@ -7,111 +7,61 @@ module DTRCore
7
7
  class Parser
8
8
  include ::DTRCore::Common
9
9
 
10
- def initialize(file_path)
11
- raise "Unable to find file: #{file_path}." unless File.exist?(file_path)
12
-
13
- @content = File.read(file_path)
14
- end
15
-
16
- def self.parse(file_path)
17
- new(file_path).parse
18
- end
19
-
20
- def parse
21
- DTRCore::Contract.new(parse_contract_name_section, parse_state_section, parse_function_section)
10
+ def initialize(file_path, content: nil)
11
+ if content
12
+ @content = content
13
+ else
14
+ raise "Unable to find file: #{file_path}." unless File.exist?(file_path)
15
+
16
+ @content = File.read(file_path)
17
+ end
22
18
  end
23
19
 
24
- private
25
-
26
20
  attr_reader :content
27
21
  attr_accessor :sections
28
22
 
29
- def parse_contract_name_section
30
- contract_name_section = first_match_for_content(/\[Contract\]:\s*(.+)/)
23
+ def name_section
24
+ return @name_section if @name_section
31
25
 
32
- raise 'Missing contract name.' if contract_name_section.nil?
26
+ name_section = first_match_for_content(/\[Contract\]:\s*(.+)/)
33
27
 
34
- contract_name_section
28
+ raise 'Missing contract name.' if name_section.nil?
29
+
30
+ @name_section ||= name_section
35
31
  end
36
32
 
37
- def parse_state_section
33
+ def state_section
34
+ return @state_definitions if @state_definitions
35
+
38
36
  state_section = first_match_for_content(/\[State\]:\s*((?:\s*\*\s*\[.+?\]\n(?:\s* \* .+\n?)*)*)/)
39
37
 
40
38
  return nil if state_section.nil?
41
39
 
42
40
  state_definitions = state_section
43
41
  .split(/\n\s*\*\s*\[/).map { |x| "[#{x.strip}" }
44
- .map { |definition| state_definition_to_state_object(definition) }
42
+ .map { |definition| DTRCore::State.from_definition(definition) }
45
43
 
46
44
  raise 'Empty state section.' if state_definitions.empty?
47
45
 
48
- state_definitions
46
+ @state_section ||= state_definitions
49
47
  end
50
48
 
51
- def clean_state_definition_name(definition)
52
- definition.gsub(/[\*\n\[]/, '').strip
53
- end
54
-
55
- def state_definition_to_state_object(definition)
56
- name = clean_state_definition_name definition[/^\[([^\]]+)\]/, 1]
57
-
58
- type = definition[/Type:\s*(\w+)/, 1]
59
-
60
- initial_value = DTRCore::TypeValidator.new(type, definition[/Initial Value:\s*(.+)/, 1])
61
- .validate_then_coerce_initial_value!
49
+ def function_section
50
+ return @function_definitions if @function_definitions
62
51
 
63
- DTRCore::State.new(name, type, initial_value)
64
- end
65
-
66
- def parse_function_section
67
52
  function_section = first_match_for_content(/\[Functions\]:(?<all>.*):\[Functions\]/m)
68
53
 
69
54
  return nil if function_section.nil?
70
55
 
71
- function_definitions = parse_parse_function_section(function_section)
56
+ function_definitions = function_section.split('-()').map do |x|
57
+ DTRCore::Function.from_definition(x.strip.to_s)
58
+ end
72
59
 
73
- raise 'Empty function section.' if function_definitions.empty?
60
+ function_definitions.reject! { |x| x.name.nil? }
74
61
 
75
- function_definitions
76
- end
77
-
78
- def parse_parse_function_section(function_section)
79
- function_section.split('-()')
80
- .map { |x| function_definition_to_function_object(x.strip.to_s) }
81
- .reject { |x| x.name.nil? }
82
- end
83
-
84
- def function_definition_to_function_object(definition)
85
- name = definition[/\s*\[(?<all>[^\]]+)]/, 1]
86
- inputs = format_function_inputs(definition[/Inputs\s*:\s*{\s*(?<inputs>[^}]+)\s*}/, 1])
87
- # TODO: check output type
88
- output = definition[/Output:\s*(.+)/, 1]
89
- instructions = format_function_instruction(definition[/Instructions:\s*\$(?<inputs>[^\$]+)\$/, 1])
90
-
91
- DTRCore::Function.new(name, inputs, output, instructions)
92
- end
93
-
94
- def format_function_inputs(inputs)
95
- return [] if inputs.nil?
96
-
97
- split_strip_select(inputs).map { |x| { name: x.split(':')[0].strip, type_name: x.split(':')[1].strip } }
98
- end
99
-
100
- def format_function_instruction(instructions)
101
- split_strip_select(instructions)&.map { |instruction| parse_function_instruction(instruction) }
102
- end
103
-
104
- def parse_function_instruction(instruction)
105
- {
106
- instruction: instruction[/instruction:\s*(?<all>[^\s,]+)/, 1],
107
- inputs: parse_function_instruction_input(instruction),
108
- assign: instruction[/\s*assign:\s*(?<all>[^\s\,]+)/, 1]
109
- }
110
- end
62
+ raise 'Empty function section.' if function_definitions.empty?
111
63
 
112
- def parse_function_instruction_input(definition)
113
- definition[/\s*input:\s*\((?<all>[^\)]+)\)/, 1]
114
- &.split(',')&.map(&:strip)
64
+ @function_section ||= function_definitions
115
65
  end
116
66
  end
117
67
  end
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'dtr_core/common'
4
+
3
5
  module DTRCore
4
6
  # Represents a state in a DTR file.
5
7
  class State
8
+ include ::DTRCore::Common
9
+
6
10
  attr_reader :name, :type, :initial_value
7
11
 
8
12
  def initialize(name, type, initial_value)
@@ -11,6 +15,25 @@ module DTRCore
11
15
  @initial_value = initial_value
12
16
  end
13
17
 
18
+ def sanitize
19
+ @name = clean_name @name
20
+ end
21
+
22
+ def self.from_definition(definition)
23
+ not_yet_cleaned_name = definition[/^\[([^\]]+)\]/, 1]
24
+
25
+ type = definition[/Type:\s*(\w+)/, 1]
26
+
27
+ initial_value = DTRCore::TypeValidator.new(type, definition[/Initial Value:\s*(.+)/, 1])
28
+ .validate_then_coerce_initial_value!
29
+
30
+ state_object = new(not_yet_cleaned_name, type, initial_value)
31
+
32
+ state_object.sanitize
33
+
34
+ state_object
35
+ end
36
+
14
37
  def ==(other)
15
38
  name == other.name &&
16
39
  type == other.type &&
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtr_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
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-04-22 00:00:00.000000000 Z
11
+ date: 2024-05-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A longer description of my_gem
13
+ description: Core smart contract intermediate language (Digicus Textual Representation)
14
+ parser.
14
15
  email:
15
16
  - me@robdurst.com
16
17
  executables: []
@@ -25,7 +26,7 @@ files:
25
26
  - lib/dtr_core/parser.rb
26
27
  - lib/dtr_core/state.rb
27
28
  - lib/dtr_core/type_validator.rb
28
- homepage:
29
+ homepage: https://spaced-out-thoughts-dev-foundation.github.io/digicus/
29
30
  licenses:
30
31
  - MIT
31
32
  metadata:
@@ -48,5 +49,6 @@ requirements: []
48
49
  rubygems_version: 3.4.10
49
50
  signing_key:
50
51
  specification_version: 4
51
- summary: A short summary of my_gem
52
+ summary: Core smart contract intermediate language (Digicus Textual Representation)
53
+ parser.
52
54
  test_files: []