dtr_core 0.1.0 → 0.1.1

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: d7262ad471060f31d350056263e7c7e7fef3a37451aa6e62dd8ec7fc6bc14ed6
4
+ data.tar.gz: 72ab429eba23bbe05fe544a940c14b169460812abac111f789631c218dff9f9c
5
5
  SHA512:
6
- metadata.gz: a2de4799a22ec515c6a23ee27e09735f6cd7043832dc3ab79b108ea68c802ddc0ae721ca78c72384712796302a2df55aee0b529c12439430b1ceaf948ee6ad79
7
- data.tar.gz: 0b379f38905d1d28ea6d4dc8044622b7598383d1ee3bf627fa70f9c6d25d019506b8ab269bc99561c71bd6548afa969e3ec18621af19372bf4f441c2677b2fcd
6
+ metadata.gz: 4aa2e73de48f3fa830191fd343672aae42a2772508ec14a8934e9bcfe184d32eb6be6ae7b0f6aa10bd5374865b529fb79592aa61f03fb3ea0900dd369c6c846b
7
+ data.tar.gz: af2b76f508014aadc00445fc9e942a4036579fdd7c2812f6b6b614e7d8539f0ea8a511b29e221fb87d4cfbb10b3551c07739596cd8fea35c71df84fec991ac93
@@ -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,12 @@ 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
+
14
20
  def ==(other)
15
21
  name == other.name &&
16
22
  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
@@ -13,105 +13,51 @@ module DTRCore
13
13
  @content = File.read(file_path)
14
14
  end
15
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)
22
- end
23
-
24
- private
25
-
26
16
  attr_reader :content
27
17
  attr_accessor :sections
28
18
 
29
- def parse_contract_name_section
30
- contract_name_section = first_match_for_content(/\[Contract\]:\s*(.+)/)
19
+ def name_section
20
+ return @name_section if @name_section
21
+
22
+ name_section = first_match_for_content(/\[Contract\]:\s*(.+)/)
31
23
 
32
- raise 'Missing contract name.' if contract_name_section.nil?
24
+ raise 'Missing contract name.' if name_section.nil?
33
25
 
34
- contract_name_section
26
+ @name_section ||= name_section
35
27
  end
36
28
 
37
- def parse_state_section
29
+ def state_section
30
+ return @state_definitions if @state_definitions
31
+
38
32
  state_section = first_match_for_content(/\[State\]:\s*((?:\s*\*\s*\[.+?\]\n(?:\s* \* .+\n?)*)*)/)
39
33
 
40
34
  return nil if state_section.nil?
41
35
 
42
36
  state_definitions = state_section
43
37
  .split(/\n\s*\*\s*\[/).map { |x| "[#{x.strip}" }
44
- .map { |definition| state_definition_to_state_object(definition) }
38
+ .map { |definition| DTRCore::State.from_definition(definition) }
45
39
 
46
40
  raise 'Empty state section.' if state_definitions.empty?
47
41
 
48
- state_definitions
42
+ @state_section ||= state_definitions
49
43
  end
50
44
 
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!
45
+ def function_section
46
+ return @function_definitions if @function_definitions
62
47
 
63
- DTRCore::State.new(name, type, initial_value)
64
- end
65
-
66
- def parse_function_section
67
48
  function_section = first_match_for_content(/\[Functions\]:(?<all>.*):\[Functions\]/m)
68
49
 
69
50
  return nil if function_section.nil?
70
51
 
71
- function_definitions = parse_parse_function_section(function_section)
72
-
73
- raise 'Empty function section.' if function_definitions.empty?
74
-
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?
52
+ function_definitions = function_section.split('-()').map do |x|
53
+ DTRCore::Function.from_definition(x.strip.to_s)
54
+ end
96
55
 
97
- split_strip_select(inputs).map { |x| { name: x.split(':')[0].strip, type_name: x.split(':')[1].strip } }
98
- end
56
+ function_definitions.reject! { |x| x.name.nil? }
99
57
 
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
58
+ raise 'Empty function section.' if function_definitions.empty?
111
59
 
112
- def parse_function_instruction_input(definition)
113
- definition[/\s*input:\s*\((?<all>[^\)]+)\)/, 1]
114
- &.split(',')&.map(&:strip)
60
+ @function_section ||= function_definitions
115
61
  end
116
62
  end
117
63
  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.1
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-11 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: []