code-ruby 1.1.3 → 1.2.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +13 -13
  3. data/.github/workflows/ci.yml +25 -24
  4. data/.npm-version +1 -0
  5. data/.rubocop.yml +14 -11
  6. data/Gemfile +5 -4
  7. data/Gemfile.lock +134 -28
  8. data/VERSION +1 -1
  9. data/bin/console +6 -0
  10. data/bin/dorian +31 -0
  11. data/code-ruby.gemspec +6 -1
  12. data/lib/code/error.rb +4 -25
  13. data/lib/code/node/code.rb +1 -1
  14. data/lib/code/node/function_parameter.rb +10 -8
  15. data/lib/code/node/while.rb +1 -1
  16. data/lib/code/object/boolean.rb +20 -16
  17. data/lib/code/object/class.rb +10 -4
  18. data/lib/code/object/code.rb +7 -3
  19. data/lib/code/object/context.rb +8 -8
  20. data/lib/code/object/date.rb +41 -7
  21. data/lib/code/object/decimal.rb +101 -56
  22. data/lib/code/object/dictionary.rb +245 -191
  23. data/lib/code/object/duration.rb +11 -7
  24. data/lib/code/object/function.rb +38 -25
  25. data/lib/code/object/global.rb +95 -42
  26. data/lib/code/object/html.rb +12 -14
  27. data/lib/code/object/http.rb +219 -0
  28. data/lib/code/object/identifier_list.rb +16 -16
  29. data/lib/code/object/integer.rb +129 -89
  30. data/lib/code/object/json.rb +18 -22
  31. data/lib/code/object/list.rb +141 -92
  32. data/lib/code/object/parameter.rb +9 -13
  33. data/lib/code/object/range.rb +77 -45
  34. data/lib/code/object/string.rb +15 -34
  35. data/lib/code/object/time.rb +17 -16
  36. data/lib/code/object.rb +126 -93
  37. data/lib/code/parser/string.rb +2 -1
  38. data/lib/code/type/sig.rb +3 -3
  39. data/lib/code-ruby.rb +119 -0
  40. data/package-lock.json +1 -1
  41. data/package.json +1 -1
  42. data/spec/code/object/http_spec.rb +91 -0
  43. data/spec/code/type_spec.rb +1 -1
  44. data/spec/code_spec.rb +10 -5
  45. data/spec/spec_helper.rb +18 -0
  46. metadata +50 -3
@@ -3,32 +3,48 @@
3
3
  class Code
4
4
  class Object
5
5
  class Range < Object
6
- attr_reader :left, :right, :options, :exclude_end
7
-
8
- def initialize(*args, **_kargs, &)
9
- @left = args.first.presence || Integer.new(0)
10
- @right = args.second.presence || Integer.new(0)
11
- @options = Dictionary.new(args.third.presence || {})
12
- @exclude_end = Boolean.new(options.code_get(String.new(:exclude_end)))
13
- @raw = ::Range.new(left, right, exclude_end?)
6
+ attr_reader :code_left, :code_right, :code_options, :code_exclude_end
7
+
8
+ def initialize(*args, **kargs, &)
9
+ if args.first.is_a?(Range)
10
+ @code_left = args.first.code_left
11
+ @code_right = args.first.code_right
12
+ @code_options = args.first.code_options
13
+ @code_exclude_end = args.first.code_exclude_end
14
+ else
15
+ @code_left =
16
+ (args.first.to_code.nothing? ? Integer.new(0) : args.first.to_code)
17
+
18
+ @code_right =
19
+ if args.second.to_code.nothing?
20
+ Integer.new(0)
21
+ else
22
+ args.second.to_code
23
+ end
24
+
25
+ @code_options = Dictionary.new(args.third.presence || kargs)
26
+ @code_exclude_end = Boolean.new(code_options.code_get(:exclude_end))
27
+ end
28
+
29
+ @raw = ::Range.new(code_left, code_right, exclude_end?)
14
30
  end
15
31
 
16
32
  def call(**args)
17
- operator = args.fetch(:operator, nil)
18
- arguments = args.fetch(:arguments, List.new)
33
+ code_operator = args.fetch(:operator, nil).to_code
34
+ code_arguments = args.fetch(:arguments, []).to_code
19
35
  globals = multi_fetch(args, *GLOBALS)
20
- value = arguments.code_first
36
+ code_value = code_arguments.code_first
21
37
 
22
- case operator.to_s
38
+ case code_operator.to_s
23
39
  when "all?"
24
40
  sig(args) { Function }
25
- code_all?(value, **globals)
41
+ code_all?(code_value, **globals)
26
42
  when "any?"
27
43
  sig(args) { Function }
28
- code_any?(value, **globals)
44
+ code_any?(code_value, **globals)
29
45
  when "each"
30
46
  sig(args) { Function }
31
- code_each(value, **globals)
47
+ code_each(code_value, **globals)
32
48
  when "first"
33
49
  sig(args)
34
50
  code_first
@@ -37,13 +53,13 @@ class Code
37
53
  code_last
38
54
  when "map"
39
55
  sig(args) { Function }
40
- code_map(value, **globals)
56
+ code_map(code_value, **globals)
41
57
  when "select"
42
58
  sig(args) { Function }
43
- code_select(value, **globals)
59
+ code_select(code_value, **globals)
44
60
  when "step"
45
61
  sig(args) { Integer | Decimal }
46
- code_step(value)
62
+ code_step(code_value)
47
63
  when "to_list"
48
64
  sig(args)
49
65
  code_to_list
@@ -53,12 +69,15 @@ class Code
53
69
  end
54
70
 
55
71
  def code_all?(argument, **globals)
72
+ code_argument = argument.to_code
73
+
56
74
  index = 0
75
+
57
76
  Boolean.new(
58
- raw.all? do |element|
59
- argument
77
+ raw.all? do |code_element|
78
+ code_argument
60
79
  .call(
61
- arguments: List.new([element, Integer.new(index), self]),
80
+ arguments: List.new([code_element, Integer.new(index), self]),
62
81
  **globals
63
82
  )
64
83
  .truthy?
@@ -68,12 +87,15 @@ class Code
68
87
  end
69
88
 
70
89
  def code_any?(argument, **globals)
90
+ code_argument = argument.to_code
91
+
71
92
  index = 0
93
+
72
94
  Boolean.new(
73
- raw.any? do |element|
74
- argument
95
+ raw.any? do |code_element|
96
+ code_argument
75
97
  .call(
76
- arguments: List.new([element, Integer.new(index), self]),
98
+ arguments: List.new([code_element, Integer.new(index), self]),
77
99
  **globals
78
100
  )
79
101
  .truthy?
@@ -83,12 +105,15 @@ class Code
83
105
  end
84
106
 
85
107
  def code_each(argument, **globals)
86
- raw.each.with_index do |element, index|
87
- argument.call(
88
- arguments: List.new([element, Integer.new(index), self]),
108
+ code_argument = argument.to_code
109
+
110
+ raw.each.with_index do |code_element, index|
111
+ code_argument.call(
112
+ arguments: List.new([code_element, Integer.new(index), self]),
89
113
  **globals
90
114
  )
91
115
  end
116
+
92
117
  self
93
118
  end
94
119
 
@@ -101,10 +126,12 @@ class Code
101
126
  end
102
127
 
103
128
  def code_map(argument, **globals)
129
+ code_argument = argument.to_code
130
+
104
131
  List.new(
105
- raw.map.with_index do |element, index|
106
- argument.call(
107
- arguments: List.new([element, Integer.new(index), self]),
132
+ raw.map.with_index do |code_element, index|
133
+ code_argument.call(
134
+ arguments: List.new([code_element, Integer.new(index), self]),
108
135
  **globals
109
136
  )
110
137
  end
@@ -112,10 +139,12 @@ class Code
112
139
  end
113
140
 
114
141
  def code_select(argument, **globals)
142
+ code_argument = argument.to_code
143
+
115
144
  List.new(
116
- raw.select.with_index do |element, index|
117
- argument.call(
118
- arguments: List.new([element, Integer.new(index), self]),
145
+ raw.select.with_index do |code_element, index|
146
+ code_argument.call(
147
+ arguments: List.new([code_element, Integer.new(index), self]),
119
148
  **globals
120
149
  ).truthy?
121
150
  end
@@ -123,28 +152,31 @@ class Code
123
152
  end
124
153
 
125
154
  def exclude_end?
126
- exclude_end.truthy?
155
+ code_exclude_end.truthy?
127
156
  end
128
157
 
129
158
  def code_step(argument)
130
- list = List.new
131
- element = left
132
- list.code_append(element)
159
+ code_argument = argument.to_code
160
+
161
+ code_list = List.new
162
+ code_element = code_left
163
+ code_list.code_append(code_element)
164
+
165
+ code_element = code_element.code_plus(code_argument)
133
166
 
134
- element = element.code_plus(argument)
135
167
  if exclude_end?
136
- while element.code_inferior(right).truthy?
137
- list.code_append(element)
138
- element = element.code_plus(argument)
168
+ while code_element.code_inferior(code_right).truthy?
169
+ code_list.code_append(code_element)
170
+ code_element = code_element.code_plus(code_argument)
139
171
  end
140
172
  else
141
- while element.code_inferior_or_equal(right).truthy?
142
- list.code_append(element)
143
- element = element.code_plus(argument)
173
+ while code_element.code_inferior_or_equal(code_right).truthy?
174
+ code_list.code_append(code_element)
175
+ code_element = code_element.code_plus(code_argument)
144
176
  end
145
177
  end
146
178
 
147
- list
179
+ code_list
148
180
  end
149
181
 
150
182
  def code_to_list
@@ -10,27 +10,27 @@ class Code
10
10
  end
11
11
 
12
12
  def call(**args)
13
- operator = args.fetch(:operator, nil)
14
- arguments = args.fetch(:arguments, List.new)
13
+ code_operator = args.fetch(:operator, nil).to_code
14
+ code_arguments = args.fetch(:arguments, List.new).to_code
15
15
  globals = multi_fetch(args, *GLOBALS)
16
- value = arguments.code_first
16
+ code_value = code_arguments.code_first
17
17
 
18
- case operator.to_s
18
+ case code_operator.to_s
19
19
  when "&", "to_function"
20
20
  sig(args)
21
21
  code_to_function(**globals)
22
22
  when "*"
23
23
  sig(args) { Integer | Decimal }
24
- code_multiplication(value)
24
+ code_multiplication(code_value)
25
25
  when "+"
26
26
  sig(args) { Object }
27
- code_plus(value)
27
+ code_plus(code_value)
28
28
  when "downcase"
29
29
  sig(args)
30
30
  code_downcase
31
31
  when "include?"
32
32
  sig(args) { String }
33
- code_include?(value)
33
+ code_include?(code_value)
34
34
  when "reverse"
35
35
  sig(args)
36
36
  code_reverse
@@ -44,45 +44,26 @@ class Code
44
44
  end
45
45
 
46
46
  def code_include?(value)
47
- Boolean.new(raw.include?(value.raw))
47
+ code_value = value.to_code
48
+ Boolean.new(raw.include?(code_value.raw))
48
49
  end
49
50
 
50
51
  def code_multiplication(other)
51
- String.new(raw * other.raw)
52
+ code_other = other.to_code
53
+ String.new(raw * code_other.raw)
52
54
  end
53
55
 
54
56
  def code_plus(other)
55
- String.new(raw + other.to_s)
57
+ code_other = other.to_code
58
+ String.new(raw + code_other.to_s)
56
59
  end
57
60
 
58
61
  def code_reverse
59
62
  String.new(raw.reverse)
60
63
  end
61
64
 
62
- def code_to_function(**globals)
63
- Node::Code.new(
64
- [
65
- {
66
- function: {
67
- parameters: [{ name: "_" }],
68
- body: [
69
- {
70
- left_operation: {
71
- first: {
72
- call: {
73
- name: "_"
74
- }
75
- },
76
- others: [
77
- { operator: ".", statement: { call: { name: raw } } }
78
- ]
79
- }
80
- }
81
- ]
82
- }
83
- }
84
- ]
85
- ).evaluate(**globals)
65
+ def code_to_function(**_globals)
66
+ Function.new([{ name: "_" }], "_.#{raw}")
86
67
  end
87
68
  end
88
69
  end
@@ -7,16 +7,13 @@ class Code
7
7
 
8
8
  def initialize(*args, **_kargs, &)
9
9
  ::Time.zone ||= DEFAULT_ZONE
10
- raw = args.first
11
- raw = raw.raw if raw.is_an?(Object)
12
- raw = raw.presence || ::Time.zone.now
13
- @raw = ::Time.zone.parse(raw.to_s).presence || ::Time.zone.now
10
+ @raw = ::Time.zone.parse(args.first.to_s) || ::Time.zone.now
14
11
  end
15
12
 
16
13
  def self.call(**args)
17
- operator = args.fetch(:operator, nil)
14
+ code_operator = args.fetch(:operator, nil).to_code
18
15
 
19
- case operator.to_s
16
+ case code_operator.to_s
20
17
  when "now"
21
18
  sig(args)
22
19
  code_now
@@ -54,17 +51,17 @@ class Code
54
51
  end
55
52
 
56
53
  def call(**args)
57
- operator = args.fetch(:operator, nil)
58
- arguments = args.fetch(:arguments, List.new)
59
- value = arguments.code_first
54
+ code_operator = args.fetch(:operator, nil).to_code
55
+ code_arguments = args.fetch(:arguments, []).to_code
56
+ code_value = code_arguments.code_first
60
57
 
61
- case operator.to_s
58
+ case code_operator.to_s
62
59
  when "after?"
63
60
  sig(args) { Time.maybe }
64
- code_after?(value)
61
+ code_after?(code_value)
65
62
  when "before?"
66
63
  sig(args) { Time.maybe }
67
- code_before?(value)
64
+ code_before?(code_value)
68
65
  when "past?"
69
66
  sig(args)
70
67
  code_past?
@@ -80,13 +77,17 @@ class Code
80
77
  end
81
78
 
82
79
  def code_after?(other = nil)
83
- other = Time.code_now if other.nil? || other.is_a?(Nothing)
84
- Boolean.new(raw.after?(other.raw))
80
+ code_other = other.to_code
81
+ code_other = Time.code_now if code_other.nothing?
82
+
83
+ Boolean.new(raw.after?(code_other.raw))
85
84
  end
86
85
 
87
86
  def code_before?(other = nil)
88
- other = Time.code_now if other.nil? || other.is_a?(Nothing)
89
- Boolean.new(raw.before?(other.raw))
87
+ code_other = other.to_code
88
+ code_other = Time.code_now if code_other.nothing?
89
+
90
+ Boolean.new(raw.before?(code_other.raw))
90
91
  end
91
92
 
92
93
  def code_past?