dtr_core 0.5.6 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dtr_core/common.rb +8 -6
- data/lib/dtr_core/contract.rb +21 -11
- data/lib/dtr_core/function.rb +13 -14
- data/lib/dtr_core/instruction.rb +32 -0
- data/lib/dtr_core/instruction_validator.rb +85 -0
- data/lib/dtr_core/number.rb +6 -15
- data/lib/dtr_core/parser.rb +27 -9
- data/lib/dtr_core/supported_attributes.rb +10 -22
- data/lib/dtr_core/type_validator.rb +52 -14
- data/lib/dtr_core/user_defined_type.rb +1 -1
- data/lib/dtr_core.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daf7d4ee7b08b952fd8fe687e0dd6edbfa09bd576d35cb1b50d24528a411dc2b
|
4
|
+
data.tar.gz: d65462fe604c303f6be6317170582ee0e98bc2ccc459cdde0b2dbd8cf5024e23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 014bd2182a685a04e2acf4349055431102fd48a96f4a0a8928603279af66f4059175d8fe7ed8f9c39d93c8e0388e5e41ca0417ab0539b693e67621f59bab0c78
|
7
|
+
data.tar.gz: a650d2cb77f65292991eabe585d771a0a797b26cab9c30f5b46cb34137365499edb02c80235388240d982c9c1825bdd0fa2d78207b67e77aa49c19b0a1d92144
|
data/lib/dtr_core/common.rb
CHANGED
@@ -3,16 +3,18 @@
|
|
3
3
|
module DTRCore
|
4
4
|
# Common methods used by the DTRCore module.
|
5
5
|
module Common
|
6
|
-
def strip_and_remove_quotes(str)
|
7
|
-
str.strip.gsub(/['"]/, '')
|
8
|
-
end
|
9
|
-
|
10
6
|
def split_strip_select(some_list)
|
11
7
|
some_list&.split("\n")&.map(&:strip)&.select { |x| x.length.positive? }
|
12
8
|
end
|
13
9
|
|
14
|
-
def
|
15
|
-
content.match(pattern)&.captures
|
10
|
+
def capture_section(pattern)
|
11
|
+
captures = content.match(pattern)&.captures
|
12
|
+
|
13
|
+
if content.scan(pattern).length > 1
|
14
|
+
raise 'Multiple captures found for a section.'
|
15
|
+
elsif captures
|
16
|
+
captures&.first
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
18
20
|
def clean_name(definition)
|
data/lib/dtr_core/contract.rb
CHANGED
@@ -3,31 +3,34 @@
|
|
3
3
|
module DTRCore
|
4
4
|
# Represents a contract in a DTR file.
|
5
5
|
class Contract
|
6
|
-
attr_reader :
|
6
|
+
attr_reader :helpers, :interface, :name, :state, :user_defined_types
|
7
7
|
|
8
|
-
def initialize(name, state,
|
8
|
+
def initialize(name, state, interface, user_defined_types, helpers)
|
9
9
|
@name = name
|
10
10
|
@state = state
|
11
|
-
@
|
11
|
+
@interface = interface
|
12
12
|
@user_defined_types = user_defined_types
|
13
|
+
@helpers = helpers
|
13
14
|
end
|
14
15
|
|
15
16
|
def self.from_dtr(filepath)
|
16
17
|
parser = DTRCore::Parser.new(filepath)
|
17
18
|
|
18
|
-
new(parser.name_section, parser.state_section, parser.
|
19
|
+
new(parser.name_section, parser.state_section, parser.interface_section, parser.user_defined_types_section,
|
20
|
+
parser.helpers_section)
|
19
21
|
end
|
20
22
|
|
21
23
|
def self.from_dtr_raw(content)
|
22
24
|
parser = DTRCore::Parser.new('', content:)
|
23
25
|
|
24
|
-
new(parser.name_section, parser.state_section, parser.
|
26
|
+
new(parser.name_section, parser.state_section, parser.interface_section, parser.user_defined_types_section,
|
27
|
+
parser.helpers_section)
|
25
28
|
end
|
26
29
|
|
27
30
|
def ==(other)
|
28
31
|
name == other.name &&
|
29
32
|
state == other.state &&
|
30
|
-
|
33
|
+
interface == other.interface &&
|
31
34
|
user_defined_types == other.user_defined_types
|
32
35
|
end
|
33
36
|
|
@@ -36,8 +39,9 @@ module DTRCore
|
|
36
39
|
|
37
40
|
return_string += name_to_s
|
38
41
|
return_string += "#{state_to_s}\n"
|
39
|
-
return_string +=
|
42
|
+
return_string += interface_to_s
|
40
43
|
return_string += user_defined_types_to_s
|
44
|
+
return_string += helpers_to_s
|
41
45
|
|
42
46
|
return_string
|
43
47
|
end
|
@@ -51,19 +55,25 @@ module DTRCore
|
|
51
55
|
def state_to_s
|
52
56
|
return '' if @state.nil?
|
53
57
|
|
54
|
-
"[State]:\n#{@state&.map(&:to_s)&.join("\n")}\n"
|
58
|
+
"[State]:\n#{@state&.map(&:to_s)&.join("\n")}\n:[State]\n"
|
55
59
|
end
|
56
60
|
|
57
|
-
def
|
61
|
+
def interface_to_s
|
58
62
|
return '' if @state.nil?
|
59
63
|
|
60
|
-
"[
|
64
|
+
"[Interface]:\n#{@interface&.map(&:to_s)&.join("\n")}\n:[Interface]\n"
|
61
65
|
end
|
62
66
|
|
63
67
|
def user_defined_types_to_s
|
64
68
|
return '' if @user_defined_types.nil?
|
65
69
|
|
66
|
-
"[
|
70
|
+
"[User Defined Types]:\n#{@user_defined_types&.map(&:to_s)&.join("\n")}\n:[User Defined Types]\n"
|
71
|
+
end
|
72
|
+
|
73
|
+
def helpers_to_s
|
74
|
+
return '' if @helpers.nil?
|
75
|
+
|
76
|
+
"[Helpers]:\n#{@helpers&.map(&:to_s)&.join("\n")}\n:[Helpers]\n"
|
67
77
|
end
|
68
78
|
end
|
69
79
|
end
|
data/lib/dtr_core/function.rb
CHANGED
@@ -65,7 +65,8 @@ module DTRCore
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def output_to_s
|
68
|
-
return
|
68
|
+
return '' if output.nil?
|
69
|
+
|
69
70
|
" * Output: #{output}\n"
|
70
71
|
end
|
71
72
|
|
@@ -75,19 +76,13 @@ module DTRCore
|
|
75
76
|
return_string += " * Instructions:\n"
|
76
77
|
return_string += " $\n"
|
77
78
|
@instructions.each do |x|
|
78
|
-
return_string += " #{
|
79
|
+
return_string += " #{x}\n"
|
79
80
|
end
|
80
81
|
return_string += " $\n"
|
81
82
|
|
82
83
|
return_string
|
83
84
|
end
|
84
85
|
|
85
|
-
def single_instruction_to_s(ins)
|
86
|
-
"{ instruction: #{ins[:instruction]}," \
|
87
|
-
"input: (#{ins[:inputs]&.join(', ')}), " \
|
88
|
-
"assign: #{ins[:assign]}, scope: #{ins[:scope]} }\n"
|
89
|
-
end
|
90
|
-
|
91
86
|
def format_function_inputs(inputs)
|
92
87
|
return [] if inputs.nil?
|
93
88
|
|
@@ -99,12 +94,16 @@ module DTRCore
|
|
99
94
|
end
|
100
95
|
|
101
96
|
def parse_function_instruction(instruction)
|
102
|
-
|
103
|
-
instruction
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
97
|
+
instruction = DTRCore::Instruction.new(
|
98
|
+
instruction[/instruction:\s*(?<all>[^\s,]+)/, 1],
|
99
|
+
parse_function_instruction_input(instruction),
|
100
|
+
instruction[/\s*assign:\s*(?<all>[^\s\,]+)/, 1],
|
101
|
+
instruction[/\s*scope:\s*(?<all>[^\s\,]+)/, 1].to_i || 0
|
102
|
+
)
|
103
|
+
|
104
|
+
raise "Invalid instruction: #{instruction}" unless instruction.valid?
|
105
|
+
|
106
|
+
instruction
|
108
107
|
end
|
109
108
|
|
110
109
|
def parse_function_instruction_input(definition)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DTRCore
|
4
|
+
# Instruction class
|
5
|
+
class Instruction
|
6
|
+
attr_reader :instruction, :inputs, :assign, :scope
|
7
|
+
|
8
|
+
def initialize(instruction, inputs, assign, scope)
|
9
|
+
@instruction = instruction
|
10
|
+
@inputs = inputs
|
11
|
+
@assign = assign
|
12
|
+
@scope = scope
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
instruction == other.instruction &&
|
17
|
+
inputs == other.inputs &&
|
18
|
+
assign == other.assign &&
|
19
|
+
scope == other.scope
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
"{ instruction: #{instruction}," \
|
24
|
+
"input: (#{inputs&.join(', ')}), " \
|
25
|
+
"assign: #{assign}, scope: #{scope} }"
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
DTRCore::InstructionValidator.new(self).valid?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dtr_core/common'
|
4
|
+
|
5
|
+
module DTRCore
|
6
|
+
# Instruction validator for DTR types.
|
7
|
+
class InstructionValidator
|
8
|
+
include ::DTRCore::Common
|
9
|
+
|
10
|
+
def initialize(instruction)
|
11
|
+
@instruction = instruction
|
12
|
+
|
13
|
+
validate_input!
|
14
|
+
end
|
15
|
+
|
16
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
17
|
+
# rubocop:disable Metrics/MethodLength
|
18
|
+
def valid?
|
19
|
+
return false unless scope_valid?
|
20
|
+
|
21
|
+
case @instruction.instruction
|
22
|
+
when 'assign', 'evaluate', 'log_string'
|
23
|
+
validate_basic_operation!
|
24
|
+
when 'exit_with_message', 'return'
|
25
|
+
validate_terminating_operation!
|
26
|
+
when 'and', 'or'
|
27
|
+
validate_logical_operation!
|
28
|
+
when 'conditional_goto', 'conditional_jump', 'end_of_iteration_check', 'label', 'unconditional_goto', 'unconditional_jump'
|
29
|
+
validate_unconditional_jump_operation!
|
30
|
+
when 'contract_address'
|
31
|
+
validate_smart_contract_specific_operation!
|
32
|
+
when 'create_dictionary', 'create_list', 'create_range', 'create_tuple',
|
33
|
+
'field', 'initialize_udt'
|
34
|
+
validate_object_operation!
|
35
|
+
when 'add', 'subtract', 'multiply', 'divide',
|
36
|
+
'add_and_assign', 'subtract_and_assign', 'multiply_and_assign', 'divide_and_assign'
|
37
|
+
validate_binary_operation!
|
38
|
+
else
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
# rubocop:enable Metrics/MethodLength
|
43
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def validate_input!
|
48
|
+
raise 'Missing Instruction.' if @instruction.nil?
|
49
|
+
raise 'Instruction name missing.' if @instruction.instruction.nil?
|
50
|
+
raise 'Instruction missing scope.' if @instruction.scope.nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
def scope_valid?
|
54
|
+
@instruction.scope >= 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def validate_basic_operation!
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate_terminating_operation!
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
65
|
+
def validate_logical_operation!
|
66
|
+
@instruction.inputs&.length == 2
|
67
|
+
end
|
68
|
+
|
69
|
+
def validate_unconditional_jump_operation!
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def validate_smart_contract_specific_operation!
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
77
|
+
def validate_object_operation!
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
def validate_binary_operation!
|
82
|
+
@instruction.inputs&.length == 2
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/dtr_core/number.rb
CHANGED
@@ -2,22 +2,13 @@
|
|
2
2
|
|
3
3
|
module DTRCore
|
4
4
|
module Number
|
5
|
-
|
6
|
-
|
5
|
+
MIN_Integer = -2**31
|
6
|
+
MAX_Integer = (2**31) - 1
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
MIN_BigInteger = -2**255
|
9
|
+
MAX_BigInteger = (2**255) - 1
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
MIN_I32 = -2**31
|
15
|
-
MAX_I32 = (2**31) - 1
|
16
|
-
|
17
|
-
MIN_I64 = -2**63
|
18
|
-
MAX_I64 = (2**63) - 1
|
19
|
-
|
20
|
-
MIN_I256 = -2**255
|
21
|
-
MAX_I256 = (2**255) - 1
|
11
|
+
MIN_Float = -2**31
|
12
|
+
MAX_Float = (2**31) - 1
|
22
13
|
end
|
23
14
|
end
|
data/lib/dtr_core/parser.rb
CHANGED
@@ -23,7 +23,7 @@ module DTRCore
|
|
23
23
|
def name_section
|
24
24
|
return @name_section if @name_section
|
25
25
|
|
26
|
-
name_section =
|
26
|
+
name_section = capture_section(/\[Contract\]:\s*(.+)/)
|
27
27
|
|
28
28
|
raise 'Missing contract name.' if name_section.nil?
|
29
29
|
|
@@ -33,7 +33,7 @@ module DTRCore
|
|
33
33
|
def state_section
|
34
34
|
return @state_definitions if @state_definitions
|
35
35
|
|
36
|
-
state_section =
|
36
|
+
state_section = capture_section(/\[State\]:\s*((?:\s*\*\s*\[.+?\]\n(?:\s*\*.+\n?)*)*)\s*:\[State\]/)
|
37
37
|
|
38
38
|
return nil if state_section.nil?
|
39
39
|
|
@@ -46,29 +46,29 @@ module DTRCore
|
|
46
46
|
@state_section ||= state_definitions
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
49
|
+
def interface_section
|
50
50
|
return @function_definitions if @function_definitions
|
51
51
|
|
52
|
-
|
52
|
+
interface_section = capture_section(/\[Interface\]:(?<all>.*):\[Interface\]/m)
|
53
53
|
|
54
|
-
return nil if
|
54
|
+
return nil if interface_section.nil?
|
55
55
|
|
56
|
-
function_definitions =
|
56
|
+
function_definitions = interface_section.split('-()').map do |x|
|
57
57
|
DTRCore::Function.from_definition(x.strip.to_s)
|
58
58
|
end
|
59
59
|
|
60
60
|
function_definitions.reject! { |x| x.name.nil? }
|
61
61
|
|
62
|
-
raise 'Empty
|
62
|
+
raise 'Empty interface section.' if function_definitions.empty?
|
63
63
|
|
64
|
-
@
|
64
|
+
@interface_section ||= function_definitions
|
65
65
|
end
|
66
66
|
|
67
67
|
def user_defined_types_section
|
68
68
|
return @user_defined_types if @user_defined_types
|
69
69
|
|
70
70
|
user_defined_types_regex = /\[User Defined Types\]:([\s\S]*?)\s*:\[User Defined Types\]/
|
71
|
-
user_defined_types_section_parsed_out =
|
71
|
+
user_defined_types_section_parsed_out = capture_section(user_defined_types_regex)
|
72
72
|
|
73
73
|
return nil if user_defined_types_section_parsed_out.nil?
|
74
74
|
|
@@ -79,5 +79,23 @@ module DTRCore
|
|
79
79
|
|
80
80
|
@user_defined_types_section ||= user_defined_types
|
81
81
|
end
|
82
|
+
|
83
|
+
def helpers_section
|
84
|
+
return @function_definitions if @function_definitions
|
85
|
+
|
86
|
+
helpers_section = capture_section(/\[Helpers\]:(?<all>.*)\s*:\[Helpers\]/m)
|
87
|
+
|
88
|
+
return nil if helpers_section.nil?
|
89
|
+
|
90
|
+
function_definitions = helpers_section.split('-()').map do |x|
|
91
|
+
DTRCore::Function.from_definition(x.strip.to_s)
|
92
|
+
end
|
93
|
+
|
94
|
+
function_definitions.reject! { |x| x.name.nil? }
|
95
|
+
|
96
|
+
raise 'Empty helpers section.' if function_definitions.empty?
|
97
|
+
|
98
|
+
@helpers_section ||= function_definitions
|
99
|
+
end
|
82
100
|
end
|
83
101
|
end
|
@@ -29,30 +29,18 @@ module DTRCore
|
|
29
29
|
## Instructions ##
|
30
30
|
INSTRUCTIONS = [
|
31
31
|
# basic operations
|
32
|
-
{ name: 'return', description: '
|
32
|
+
{ name: 'return', description: 'return a value from a function.', category: INSTRUCTION_CATEGORY_BASIC },
|
33
33
|
{ name: 'assign', description: 'Assign a value to a variable.', category: INSTRUCTION_CATEGORY_BASIC },
|
34
34
|
{ name: 'panic', description: 'Exit, quickly, and loudly.', category: INSTRUCTION_CATEGORY_BASIC },
|
35
|
-
# state operations
|
36
|
-
{ name: 'fetch_state', description: 'Fetch a value from the state.', category: INSTRUCTION_CATEGORY_STATE },
|
37
35
|
{ name: 'save_state', description: 'Save a value to the state.', category: INSTRUCTION_CATEGORY_STATE },
|
38
36
|
# untyped operations
|
39
37
|
{ name: 'add', description: 'Add two things of unknown types together.', category: INSTRUCTION_CATEGORY_UNTYPED },
|
40
|
-
{ name: 'add_and_assign',
|
41
|
-
description: 'Add two things of unknown types together and then assign to the first one.',
|
42
|
-
category: INSTRUCTION_CATEGORY_UNTYPED },
|
43
38
|
{ name: 'subtract', description: 'Subtract two things of unknown types together.',
|
44
39
|
category: INSTRUCTION_CATEGORY_UNTYPED },
|
45
40
|
{ name: 'divide', description: 'Divide two things of unknown types together.',
|
46
41
|
category: INSTRUCTION_CATEGORY_UNTYPED },
|
47
42
|
{ name: 'multiply', description: 'Multiply two things of unknown types together.',
|
48
43
|
category: INSTRUCTION_CATEGORY_UNTYPED },
|
49
|
-
# numeric operations
|
50
|
-
{ name: 'add_numbers', description: 'Add two numbers.', category: INSTRUCTION_CATEGORY_NUMERIC },
|
51
|
-
{ name: 'subtract_numbers', description: 'Subtract two numbers.', category: INSTRUCTION_CATEGORY_NUMERIC },
|
52
|
-
{ name: 'multiply_numbers', description: 'Multiply two numbers.', category: INSTRUCTION_CATEGORY_NUMERIC },
|
53
|
-
{ name: 'divide_numbers', description: 'Divide two numbers.', category: INSTRUCTION_CATEGORY_NUMERIC },
|
54
|
-
# string operations
|
55
|
-
{ name: 'add_strings', description: 'Concatenate two strings.', category: INSTRUCTION_CATEGORY_STRING },
|
56
44
|
# environment operations
|
57
45
|
{ name: 'contract_address', description: 'Get the contract address.',
|
58
46
|
category: INSTRUCTION_CATEGORY_ENVIRONMENT },
|
@@ -63,9 +51,9 @@ module DTRCore
|
|
63
51
|
{ name: 'field', description: 'Reference an object field.', category: INSTRUCTION_CATEGORY_OBJECTS },
|
64
52
|
{ name: 'initialize_udt', description: 'Instantiate UDT object.', category: INSTRUCTION_CATEGORY_OBJECTS },
|
65
53
|
# conditional operations
|
66
|
-
{ name: '
|
54
|
+
{ name: 'conditional_unconditional_jump', description: 'unconditional_jump to a label if first input is true.',
|
67
55
|
category: INSTRUCTION_CATEGORY_CONDITIONAL },
|
68
|
-
{ name: '
|
56
|
+
{ name: 'unconditional_unconditional_jump', description: 'unconditional_jump to a no matter what.',
|
69
57
|
category: INSTRUCTION_CATEGORY_CONDITIONAL },
|
70
58
|
# logical operations
|
71
59
|
{ name: 'and', description: 'Logical AND.', category: INSTRUCTION_CATEGORY_LOGICAL },
|
@@ -78,21 +66,21 @@ module DTRCore
|
|
78
66
|
'address',
|
79
67
|
'boolean',
|
80
68
|
# string types
|
81
|
-
'
|
69
|
+
'String',
|
82
70
|
# collection types
|
83
71
|
'array',
|
84
72
|
'map',
|
85
73
|
# numeric types
|
86
74
|
## signed
|
87
|
-
'
|
75
|
+
'Integer',
|
88
76
|
'i64',
|
89
|
-
'
|
90
|
-
'
|
77
|
+
'BigInteger',
|
78
|
+
'BigInteger',
|
91
79
|
## unsigned
|
92
|
-
'
|
93
|
-
'
|
80
|
+
'Integer',
|
81
|
+
'Integer',
|
94
82
|
'u128',
|
95
|
-
'
|
83
|
+
'BigInteger'
|
96
84
|
].freeze
|
97
85
|
end
|
98
86
|
end
|
@@ -13,31 +13,69 @@ module DTRCore
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def validate_then_coerce_initial_value!
|
16
|
+
validate_input!
|
17
|
+
|
18
|
+
return validate_integer! if %w[Integer BigInteger Float].include?(@type_name)
|
19
|
+
return validate_string! if ['String'].include?(@type_name)
|
20
|
+
return validate_address! if ['Address'].include?(@type_name)
|
21
|
+
|
22
|
+
raise 'Missing Invalid Type Name.'
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def validate_input!
|
16
28
|
raise 'Missing Type Name.' if @type_name.nil?
|
17
29
|
raise 'Missing Initial Value.' if @initial_value.nil?
|
30
|
+
end
|
18
31
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
strip_and_remove_quotes(@initial_value)
|
26
|
-
else
|
27
|
-
raise 'Missing Invalid Type Name.'
|
28
|
-
end
|
32
|
+
# TODO: implement deeper validation for Address type
|
33
|
+
# TODO: confirm this works for non-Stellar addresses
|
34
|
+
def validate_address!
|
35
|
+
raise "Invalid initial value for Address: #{@initial_value}. Wrong type." unless @initial_value.length == 56
|
36
|
+
|
37
|
+
@initial_value
|
29
38
|
end
|
30
39
|
|
31
|
-
|
40
|
+
def validate_string!
|
41
|
+
unless @initial_value.is_a?(String) && @initial_value.match(/".*"/)
|
42
|
+
raise "Invalid initial value for String: #{@initial_value}. Wrong type."
|
43
|
+
end
|
32
44
|
|
33
|
-
|
34
|
-
|
45
|
+
@initial_value.strip
|
46
|
+
end
|
47
|
+
|
48
|
+
def validate_integer!
|
49
|
+
unless @initial_value =~ (/^[\-\.\d]\d*(\.?\d*)*/)
|
50
|
+
raise "Invalid initial value for #{@type_name}: #{@initial_value}. Wrong type."
|
51
|
+
end
|
35
52
|
|
36
53
|
raise "Invalid initial value for type #{@type_name}. Out of range." unless @initial_value.to_i.between?(
|
37
54
|
DTRCore::Number.const_get(:"MIN_#{@type_name}"), DTRCore::Number.const_get(:"MAX_#{@type_name}")
|
38
55
|
)
|
39
56
|
|
40
|
-
|
57
|
+
handle_each_numeric_type!
|
58
|
+
end
|
59
|
+
|
60
|
+
def handle_each_numeric_type!
|
61
|
+
case @type_name
|
62
|
+
when 'Integer', 'BigInteger'
|
63
|
+
raise "Invalid initial value for #{@type_name}: #{@initial_value}. Wrong type." unless non_float_integer?
|
64
|
+
|
65
|
+
@initial_value.to_i
|
66
|
+
when 'Float'
|
67
|
+
raise "Invalid initial value for #{@type_name}: #{@initial_value}. Wrong type." unless floaty_float?
|
68
|
+
|
69
|
+
@initial_value.to_f
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def non_float_integer?
|
74
|
+
@initial_value =~ (/^[\-\.\d]\d*(\.?\d*)*/) && !@initial_value.include?('.')
|
75
|
+
end
|
76
|
+
|
77
|
+
def floaty_float?
|
78
|
+
@initial_value =~ (/^[\-\.\d]\d*(\.?\d*)*/) && @initial_value.include?('.')
|
41
79
|
end
|
42
80
|
end
|
43
81
|
end
|
data/lib/dtr_core.rb
CHANGED
@@ -9,5 +9,7 @@ module DTRCore
|
|
9
9
|
autoload :State, 'dtr_core/state'
|
10
10
|
autoload :SupportedAttributes, 'dtr_core/supported_attributes'
|
11
11
|
autoload :TypeValidator, 'dtr_core/type_validator'
|
12
|
+
autoload :InstructionValidator, 'dtr_core/instruction_validator'
|
13
|
+
autoload :Instruction, 'dtr_core/instruction'
|
12
14
|
autoload :UserDefinedType, 'dtr_core/user_defined_type'
|
13
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dtr_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
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-06-
|
11
|
+
date: 2024-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Core smart contract intermediate language (Digicus Textual Representation)
|
14
14
|
parser.
|
@@ -22,6 +22,8 @@ files:
|
|
22
22
|
- lib/dtr_core/common.rb
|
23
23
|
- lib/dtr_core/contract.rb
|
24
24
|
- lib/dtr_core/function.rb
|
25
|
+
- lib/dtr_core/instruction.rb
|
26
|
+
- lib/dtr_core/instruction_validator.rb
|
25
27
|
- lib/dtr_core/number.rb
|
26
28
|
- lib/dtr_core/parser.rb
|
27
29
|
- lib/dtr_core/state.rb
|