code-ruby 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/Gemfile +1 -4
- data/Gemfile.lock +10 -22
- data/code-ruby.gemspec +4 -3
- data/lib/code/error.rb +16 -5
- data/lib/code/node/base_10.rb +4 -2
- data/lib/code/node/base_16.rb +3 -1
- data/lib/code/node/base_2.rb +3 -1
- data/lib/code/node/base_8.rb +3 -1
- data/lib/code/node/boolean.rb +4 -2
- data/lib/code/node/call.rb +35 -12
- data/lib/code/node/call_argument.rb +16 -5
- data/lib/code/node/code.rb +5 -4
- data/lib/code/node/decimal.rb +2 -0
- data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
- data/lib/code/node/function.rb +7 -4
- data/lib/code/node/function_parameter.rb +3 -1
- data/lib/code/node/if.rb +5 -3
- data/lib/code/node/left_operation.rb +63 -0
- data/lib/code/node/list.rb +2 -0
- data/lib/code/node/negation.rb +2 -0
- data/lib/code/node/not.rb +2 -0
- data/lib/code/node/nothing.rb +3 -1
- data/lib/code/node/number.rb +2 -0
- data/lib/code/node/right_operation.rb +70 -0
- data/lib/code/node/splat.rb +2 -0
- data/lib/code/node/square_bracket.rb +37 -0
- data/lib/code/node/statement.rb +13 -5
- data/lib/code/node/string.rb +3 -1
- data/lib/code/node/ternary.rb +5 -3
- data/lib/code/node/unary_minus.rb +2 -0
- data/lib/code/node/while.rb +6 -4
- data/lib/code/node.rb +11 -5
- data/lib/code/object/argument.rb +13 -7
- data/lib/code/object/boolean.rb +43 -5
- data/lib/code/object/class.rb +17 -0
- data/lib/code/object/context.rb +36 -0
- data/lib/code/object/decimal.rb +252 -100
- data/lib/code/object/dictionary.rb +641 -0
- data/lib/code/object/function.rb +54 -27
- data/lib/code/object/global.rb +65 -19
- data/lib/code/object/identifier_list.rb +47 -0
- data/lib/code/object/integer.rb +320 -137
- data/lib/code/object/list.rb +140 -138
- data/lib/code/object/nothing.rb +10 -4
- data/lib/code/object/number.rb +6 -1
- data/lib/code/object/range.rb +85 -88
- data/lib/code/object/ruby_function.rb +11 -6
- data/lib/code/object/string.rb +51 -48
- data/lib/code/object.rb +117 -139
- data/lib/code/parser/addition.rb +4 -2
- data/lib/code/parser/and_operator.rb +4 -2
- data/lib/code/parser/bitwise_and.rb +4 -2
- data/lib/code/parser/bitwise_or.rb +4 -2
- data/lib/code/parser/boolean.rb +3 -1
- data/lib/code/parser/call.rb +17 -11
- data/lib/code/parser/chained_call.rb +10 -22
- data/lib/code/parser/class.rb +9 -6
- data/lib/code/parser/code.rb +6 -4
- data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
- data/lib/code/parser/equal.rb +9 -36
- data/lib/code/parser/equality.rb +4 -2
- data/lib/code/parser/function.rb +24 -9
- data/lib/code/parser/greater.rb +6 -3
- data/lib/code/parser/group.rb +4 -2
- data/lib/code/parser/if.rb +6 -4
- data/lib/code/parser/if_modifier.rb +5 -25
- data/lib/code/parser/left_operation.rb +40 -0
- data/lib/code/parser/list.rb +6 -5
- data/lib/code/parser/multiplication.rb +4 -2
- data/lib/code/parser/name.rb +19 -4
- data/lib/code/parser/negation.rb +4 -2
- data/lib/code/parser/not_keyword.rb +5 -3
- data/lib/code/parser/nothing.rb +3 -10
- data/lib/code/parser/number.rb +4 -2
- data/lib/code/parser/or_keyword.rb +4 -2
- data/lib/code/parser/or_operator.rb +4 -2
- data/lib/code/parser/power.rb +4 -28
- data/lib/code/parser/range.rb +4 -2
- data/lib/code/parser/rescue.rb +6 -26
- data/lib/code/parser/right_operation.rb +40 -0
- data/lib/code/parser/shift.rb +4 -2
- data/lib/code/parser/splat.rb +5 -3
- data/lib/code/parser/square_bracket.rb +48 -0
- data/lib/code/parser/statement.rb +3 -1
- data/lib/code/parser/string.rb +12 -10
- data/lib/code/parser/ternary.rb +10 -11
- data/lib/code/parser/unary_minus.rb +5 -3
- data/lib/code/parser/while.rb +5 -3
- data/lib/code/parser/whitespace.rb +2 -0
- data/lib/code/parser.rb +10 -3
- data/lib/code/ruby.rb +4 -2
- data/lib/code/type/hash.rb +38 -0
- data/lib/code/type/maybe.rb +29 -0
- data/lib/code/type/or.rb +38 -0
- data/lib/code/type/repeat.rb +38 -0
- data/lib/code/type/sig.rb +130 -0
- data/lib/code/type.rb +25 -0
- data/lib/code/version.rb +3 -0
- data/lib/code-ruby.rb +1 -2
- data/lib/code.rb +15 -16
- data/spec/code/node/call_spec.rb +39 -0
- data/spec/code/object/boolean_spec.rb +18 -0
- data/spec/code/object/decimal_spec.rb +51 -0
- data/spec/code/object/dictionary_spec.rb +98 -0
- data/spec/code/object/function_spec.rb +42 -0
- data/spec/code/object/integer_spec.rb +43 -0
- data/spec/code/object/nothing_spec.rb +14 -0
- data/spec/code/object/range_spec.rb +23 -0
- data/spec/code/parser/boolean_spec.rb +5 -10
- data/spec/code/parser/chained_call.rb +4 -5
- data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
- data/spec/code/parser/function_spec.rb +4 -5
- data/spec/code/parser/group_spec.rb +5 -12
- data/spec/code/parser/if_modifier_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +4 -5
- data/spec/code/parser/number_spec.rb +4 -5
- data/spec/code/parser/string_spec.rb +4 -5
- data/spec/code/parser_spec.rb +22 -16
- data/spec/code/type_spec.rb +21 -0
- data/spec/code_spec.rb +171 -0
- data/spec/spec_helper.rb +1 -6
- metadata +61 -137
- data/.cherry.js +0 -21
- data/.editorconfig +0 -9
- data/.github/workflows/rspec.yml +0 -14
- data/.gitignore +0 -2
- data/.prettierrc +0 -3
- data/.tool-versions +0 -1
- data/CHANGELOG.md +0 -55
- data/LICENSE +0 -7
- data/README.md +0 -103
- data/TODO +0 -17
- data/bin/code +0 -76
- data/bin/format +0 -3
- data/bin/publish +0 -19
- data/bin/template +0 -85
- data/bin/test +0 -17
- data/docs/class.code +0 -9
- data/docs/euler/1.template +0 -10
- data/docs/euler/2.template +0 -16
- data/docs/euler/3.template +0 -16
- data/docs/euler/4.template +0 -10
- data/docs/euler/5.template +0 -13
- data/docs/fibonnaci.template +0 -14
- data/docs/meetup.code +0 -12
- data/docs/precedence.template +0 -36
- data/docs/rain.code +0 -22
- data/docs/slack.code +0 -17
- data/docs/stripe.code +0 -7
- data/docs/twitter.code +0 -9
- data/language-ruby.gemspec +0 -17
- data/lib/code/node/chained_call.rb +0 -23
- data/lib/code/node/equal.rb +0 -34
- data/lib/code/node/if_modifier.rb +0 -47
- data/lib/code/node/operation.rb +0 -38
- data/lib/code/node/power.rb +0 -20
- data/lib/code/node/rescue.rb +0 -17
- data/lib/code/object/dictionnary.rb +0 -96
- data/lib/code/parser/equality_lower.rb +0 -9
- data/lib/code/parser/operation.rb +0 -35
- data/lib/language/atom.rb +0 -342
- data/lib/language/output.rb +0 -130
- data/lib/language/parser/absent/present.rb +0 -8
- data/lib/language/parser/absent.rb +0 -6
- data/lib/language/parser/end_of_input.rb +0 -6
- data/lib/language/parser/interuption.rb +0 -38
- data/lib/language/parser/not_end_of_input.rb +0 -6
- data/lib/language/parser/str/not_found.rb +0 -16
- data/lib/language/parser/str.rb +0 -6
- data/lib/language/parser.rb +0 -53
- data/lib/language-ruby.rb +0 -10
- data/lib/language.rb +0 -80
- data/lib/template/node/code_part.rb +0 -13
- data/lib/template/node/part.rb +0 -19
- data/lib/template/node/template.rb +0 -15
- data/lib/template/node/text_part.rb +0 -13
- data/lib/template/node.rb +0 -4
- data/lib/template/parser/template.rb +0 -39
- data/lib/template/parser.rb +0 -19
- data/lib/template/version.rb +0 -3
- data/lib/template-ruby.rb +0 -10
- data/lib/template.rb +0 -50
- data/spec/code/addition_spec.rb +0 -13
- data/spec/code/and_operator_spec.rb +0 -13
- data/spec/code/bitwise_and_spec.rb +0 -13
- data/spec/code/bitwise_or_spec.rb +0 -13
- data/spec/code/boolean_spec.rb +0 -13
- data/spec/code/call_spec.rb +0 -21
- data/spec/code/chained_call_spec.rb +0 -16
- data/spec/code/code_spec.rb +0 -29
- data/spec/code/dictionnary_spec.rb +0 -17
- data/spec/code/equal_spec.rb +0 -26
- data/spec/code/equality_spec.rb +0 -13
- data/spec/code/function_spec.rb +0 -18
- data/spec/code/greater_spec.rb +0 -18
- data/spec/code/group_spec.rb +0 -12
- data/spec/code/if_modifier_spec.rb +0 -20
- data/spec/code/if_spec.rb +0 -25
- data/spec/code/list_spec.rb +0 -19
- data/spec/code/multiplication_spec.rb +0 -18
- data/spec/code/negation_spec.rb +0 -20
- data/spec/code/not_keyword_spec.rb +0 -13
- data/spec/code/nothing_spec.rb +0 -17
- data/spec/code/number_spec.rb +0 -22
- data/spec/code/or_keyword_spec.rb +0 -17
- data/spec/code/or_operator_spec.rb +0 -16
- data/spec/code/parser/call_spec.rb +0 -26
- data/spec/code/power_spec.rb +0 -13
- data/spec/code/range_spec.rb +0 -16
- data/spec/code/rescue_spec.rb +0 -13
- data/spec/code/shift_spec.rb +0 -13
- data/spec/code/splat_spec.rb +0 -13
- data/spec/code/string_spec.rb +0 -27
- data/spec/code/ternary_spec.rb +0 -18
- data/spec/code/unary_minus_spec.rb +0 -13
- data/spec/code/while_spec.rb +0 -18
- data/template-ruby.gemspec +0 -19
data/lib/code/object/list.rb
CHANGED
@@ -1,206 +1,208 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Object
|
3
5
|
class List < ::Code::Object
|
4
6
|
attr_reader :raw
|
5
7
|
|
6
8
|
def initialize(raw = [])
|
7
|
-
@raw = raw
|
9
|
+
@raw = raw.to_a
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.name
|
13
|
+
"List"
|
8
14
|
end
|
9
15
|
|
10
16
|
def call(**args)
|
11
17
|
operator = args.fetch(:operator, nil)
|
12
18
|
arguments = args.fetch(:arguments, [])
|
13
|
-
globals = multi_fetch(args,
|
19
|
+
globals = multi_fetch(args, *GLOBALS)
|
14
20
|
value = arguments.first&.value
|
15
21
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
22
|
+
case operator.to_s
|
23
|
+
when "sort"
|
24
|
+
sig(args)
|
25
|
+
code_sort
|
26
|
+
when "<<", "append"
|
27
|
+
sig(args) { Object }
|
28
|
+
code_append(value)
|
29
|
+
when "any?"
|
30
|
+
sig(args) { Function }
|
31
|
+
code_any?(value, **globals)
|
32
|
+
when "detect"
|
33
|
+
sig(args) { Function }
|
34
|
+
code_detect(value, **globals)
|
35
|
+
when "each"
|
36
|
+
sig(args) { Function }
|
37
|
+
code_each(value, **globals)
|
38
|
+
when "first"
|
39
|
+
sig(args)
|
40
|
+
code_first
|
41
|
+
when "flatten"
|
42
|
+
sig(args) { Integer.maybe }
|
43
|
+
code_flatten
|
44
|
+
when "include?"
|
45
|
+
sig(args) { Object }
|
46
|
+
code_include?(value)
|
47
|
+
when "last"
|
48
|
+
sig(args)
|
49
|
+
code_last
|
50
|
+
when "map"
|
51
|
+
sig(args) { Function }
|
52
|
+
code_map(value, **globals)
|
53
|
+
when "max"
|
54
|
+
sig(args)
|
55
|
+
code_max
|
56
|
+
when "max_by"
|
57
|
+
sig(args) { Function }
|
58
|
+
code_max_by(value, **globals)
|
59
|
+
when "none?"
|
60
|
+
sig(args) { Function }
|
61
|
+
code_none?(value, **globals)
|
62
|
+
when "reduce"
|
63
|
+
sig(args) { Function }
|
64
|
+
code_reduce(value, **globals)
|
65
|
+
when "reverse"
|
66
|
+
sig(args)
|
67
|
+
code_reverse
|
68
|
+
when "select"
|
69
|
+
sig(args) { Function }
|
70
|
+
code_select(value, **globals)
|
71
|
+
when "size"
|
72
|
+
sig(args)
|
73
|
+
code_size
|
74
|
+
when "uniq"
|
75
|
+
sig(args)
|
76
|
+
code_uniq
|
61
77
|
else
|
62
78
|
super
|
63
79
|
end
|
64
80
|
end
|
65
81
|
|
66
|
-
def
|
67
|
-
|
68
|
-
raw.
|
69
|
-
|
70
|
-
acc + element.flatten.raw
|
71
|
-
else
|
72
|
-
acc + [element]
|
73
|
-
end
|
82
|
+
def code_any?(argument, **globals)
|
83
|
+
Boolean.new(
|
84
|
+
raw.any? do |element|
|
85
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
74
86
|
end
|
75
87
|
)
|
76
88
|
end
|
77
89
|
|
78
|
-
def
|
79
|
-
::Code::Object::List.new(raw.deep_dup)
|
80
|
-
end
|
81
|
-
|
82
|
-
def <<(other)
|
90
|
+
def code_append(other)
|
83
91
|
raw << other
|
84
92
|
self
|
85
93
|
end
|
86
94
|
|
87
|
-
def
|
88
|
-
|
95
|
+
def code_detect(argument, **globals)
|
96
|
+
raw.detect do |element|
|
97
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
98
|
+
end || Nothing.new
|
89
99
|
end
|
90
100
|
|
91
|
-
def
|
92
|
-
|
101
|
+
def code_each(argument, **globals)
|
102
|
+
raw.each do |element|
|
103
|
+
argument.call(arguments: [Argument.new(element)], **globals)
|
104
|
+
end
|
105
|
+
self
|
93
106
|
end
|
94
107
|
|
95
|
-
|
108
|
+
def code_first
|
109
|
+
raw.first || Nothing.new
|
110
|
+
end
|
96
111
|
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
112
|
+
def code_flatten(level = nil)
|
113
|
+
level ||= Integer.new(-1)
|
114
|
+
level = level.raw if level.is_a?(Integer)
|
115
|
+
|
116
|
+
List.new(
|
117
|
+
raw.reduce([]) do |acc, element|
|
118
|
+
element = element.to_list if element.is_a?(Dictionary)
|
119
|
+
|
120
|
+
if element.is_a?(List) && level != 0
|
121
|
+
if level.positive?
|
122
|
+
acc + element.code_flatten(level - 1).raw
|
123
|
+
else
|
124
|
+
acc + element.code_flatten(level).raw
|
125
|
+
end
|
126
|
+
else
|
127
|
+
acc + [element]
|
128
|
+
end
|
104
129
|
end
|
105
130
|
)
|
106
131
|
end
|
107
132
|
|
108
|
-
def
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
133
|
+
def code_include?(other)
|
134
|
+
Boolean.new(raw.include?(other))
|
135
|
+
end
|
136
|
+
|
137
|
+
def code_last
|
138
|
+
raw.last || Nothing.new
|
139
|
+
end
|
140
|
+
|
141
|
+
def code_map(argument, **globals)
|
142
|
+
List.new(
|
143
|
+
raw.map do |element|
|
144
|
+
argument.call(arguments: [Argument.new(element)], **globals)
|
115
145
|
end
|
116
146
|
)
|
117
147
|
end
|
118
148
|
|
119
|
-
def
|
149
|
+
def code_max
|
150
|
+
raw.max || Nothing.new
|
151
|
+
end
|
152
|
+
|
153
|
+
def code_max_by(argument, **globals)
|
120
154
|
raw.max_by do |element|
|
121
|
-
argument.call(
|
122
|
-
|
123
|
-
**globals
|
124
|
-
)
|
125
|
-
end || ::Code::Object::Nothing.new
|
155
|
+
argument.call(arguments: [Argument.new(element)], **globals)
|
156
|
+
end || Nothing.new
|
126
157
|
end
|
127
158
|
|
128
|
-
def
|
129
|
-
|
130
|
-
|
131
|
-
arguments: [
|
132
|
-
|
133
|
-
|
134
|
-
end || ::Code::Object::Nothing.new
|
159
|
+
def code_none?(argument, **globals)
|
160
|
+
Boolean.new(
|
161
|
+
raw.none? do |element|
|
162
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
163
|
+
end
|
164
|
+
)
|
135
165
|
end
|
136
166
|
|
137
|
-
def
|
167
|
+
def code_reduce(argument, **globals)
|
138
168
|
raw.reduce do |acc, element|
|
139
169
|
argument.call(
|
140
|
-
arguments: [
|
141
|
-
::Code::Object::Argument.new(acc),
|
142
|
-
::Code::Object::Argument.new(element)
|
143
|
-
],
|
170
|
+
arguments: [Argument.new(acc), Argument.new(element)],
|
144
171
|
**globals
|
145
172
|
)
|
146
|
-
end ||
|
173
|
+
end || Nothing.new
|
147
174
|
end
|
148
175
|
|
149
|
-
def
|
150
|
-
raw.
|
151
|
-
argument.call(
|
152
|
-
arguments: [::Code::Object::Argument.new(element)],
|
153
|
-
**globals
|
154
|
-
)
|
155
|
-
end
|
156
|
-
self
|
176
|
+
def code_reverse
|
177
|
+
List.new(raw.reverse)
|
157
178
|
end
|
158
179
|
|
159
|
-
def
|
160
|
-
|
180
|
+
def code_select(argument, **globals)
|
181
|
+
List.new(
|
161
182
|
raw.select do |element|
|
162
|
-
argument.call(
|
163
|
-
arguments: [::Code::Object::Argument.new(element)],
|
164
|
-
**globals
|
165
|
-
).truthy?
|
183
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
166
184
|
end
|
167
185
|
)
|
168
186
|
end
|
169
187
|
|
170
|
-
def
|
171
|
-
|
172
|
-
raw.map do |element|
|
173
|
-
argument.call(
|
174
|
-
arguments: [::Code::Object::Argument.new(element)],
|
175
|
-
**globals
|
176
|
-
)
|
177
|
-
end
|
178
|
-
)
|
188
|
+
def code_sort
|
189
|
+
List.new(raw.sort)
|
179
190
|
end
|
180
191
|
|
181
|
-
def
|
182
|
-
raw
|
183
|
-
self
|
192
|
+
def code_size
|
193
|
+
Integer.new(raw.size)
|
184
194
|
end
|
185
195
|
|
186
|
-
def
|
187
|
-
|
196
|
+
def code_uniq
|
197
|
+
List.new(raw.uniq)
|
188
198
|
end
|
189
199
|
|
190
|
-
def
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
def max
|
195
|
-
raw.max || ::Code::Object::Nothing.new
|
196
|
-
end
|
197
|
-
|
198
|
-
def reverse
|
199
|
-
::Code::Object::List.new(raw.reverse)
|
200
|
+
def inspect
|
201
|
+
to_s
|
200
202
|
end
|
201
203
|
|
202
|
-
def
|
203
|
-
raw.
|
204
|
+
def to_s
|
205
|
+
"[#{raw.map(&:inspect).join(", ")}]"
|
204
206
|
end
|
205
207
|
end
|
206
208
|
end
|
data/lib/code/object/nothing.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Object
|
3
5
|
class Nothing < ::Code::Object
|
@@ -7,16 +9,20 @@ class Code
|
|
7
9
|
@raw = nil
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
+
def self.name
|
13
|
+
"Nothing"
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
"nothing"
|
12
18
|
end
|
13
19
|
|
14
20
|
def to_s
|
15
21
|
""
|
16
22
|
end
|
17
23
|
|
18
|
-
def
|
19
|
-
|
24
|
+
def truthy?
|
25
|
+
false
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
data/lib/code/object/number.rb
CHANGED
data/lib/code/object/range.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Object
|
3
|
-
class Range <
|
4
|
-
attr_reader :raw
|
5
|
+
class Range < Object
|
6
|
+
attr_reader :raw, :exclude_end, :left, :right
|
5
7
|
|
6
8
|
def initialize(left, right, exclude_end: false)
|
7
9
|
@left = left
|
@@ -10,136 +12,131 @@ class Code
|
|
10
12
|
@raw = ::Range.new(left, right, exclude_end)
|
11
13
|
end
|
12
14
|
|
15
|
+
def self.name
|
16
|
+
"Range"
|
17
|
+
end
|
18
|
+
|
13
19
|
def call(**args)
|
14
20
|
operator = args.fetch(:operator, nil)
|
15
21
|
arguments = args.fetch(:arguments, [])
|
16
|
-
globals = multi_fetch(args,
|
22
|
+
globals = multi_fetch(args, *GLOBALS)
|
17
23
|
value = arguments.first&.value
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
25
|
+
case operator.to_s
|
26
|
+
when "all?"
|
27
|
+
sig(args) { Function }
|
28
|
+
code_all?(value, **globals)
|
29
|
+
when "any?"
|
30
|
+
sig(args) { Function }
|
31
|
+
code_any?(value, **globals)
|
32
|
+
when "each"
|
33
|
+
sig(args) { Function }
|
34
|
+
code_each(value, **globals)
|
35
|
+
when "first"
|
36
|
+
sig(args)
|
37
|
+
code_first
|
38
|
+
when "last"
|
39
|
+
sig(args)
|
40
|
+
code_last
|
41
|
+
when "map"
|
42
|
+
sig(args) { Function }
|
43
|
+
code_map(value, **globals)
|
44
|
+
when "select"
|
45
|
+
sig(args) { Function }
|
46
|
+
code_select(value, **globals)
|
47
|
+
when "step"
|
48
|
+
sig(args) { Number }
|
49
|
+
code_step(value)
|
50
|
+
when "to_list"
|
51
|
+
sig(args)
|
52
|
+
code_to_list
|
46
53
|
else
|
47
54
|
super
|
48
55
|
end
|
49
56
|
end
|
50
57
|
|
51
|
-
def
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def inspect
|
56
|
-
to_s
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def any?(argument, **globals)
|
62
|
-
::Code::Object::Boolean.new(
|
63
|
-
raw.any? do |element|
|
64
|
-
argument.call(
|
65
|
-
arguments: [::Code::Object::Argument.new(element)],
|
66
|
-
**globals
|
67
|
-
).truthy?
|
58
|
+
def code_all?(argument, **globals)
|
59
|
+
Boolean.new(
|
60
|
+
raw.all? do |element|
|
61
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
68
62
|
end
|
69
63
|
)
|
70
64
|
end
|
71
65
|
|
72
|
-
def
|
73
|
-
|
74
|
-
raw.
|
75
|
-
argument.call(
|
76
|
-
arguments: [::Code::Object::Argument.new(element)],
|
77
|
-
**globals
|
78
|
-
).truthy?
|
66
|
+
def code_any?(argument, **globals)
|
67
|
+
Boolean.new(
|
68
|
+
raw.any? do |element|
|
69
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
79
70
|
end
|
80
71
|
)
|
81
72
|
end
|
82
73
|
|
83
|
-
def
|
74
|
+
def code_each(argument, **globals)
|
84
75
|
raw.each do |element|
|
85
|
-
argument.call(
|
86
|
-
arguments: [::Code::Object::Argument.new(element)],
|
87
|
-
**globals
|
88
|
-
)
|
76
|
+
argument.call(arguments: [Argument.new(element)], **globals)
|
89
77
|
end
|
90
78
|
self
|
91
79
|
end
|
92
80
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
81
|
+
def code_first
|
82
|
+
raw.first || Nothing.new
|
83
|
+
end
|
84
|
+
|
85
|
+
def code_last
|
86
|
+
raw.last || Nothing.new
|
87
|
+
end
|
88
|
+
|
89
|
+
def code_map(argument, **globals)
|
90
|
+
List.new(
|
91
|
+
raw.map do |element|
|
92
|
+
argument.call(arguments: [Argument.new(element)], **globals)
|
100
93
|
end
|
101
94
|
)
|
102
95
|
end
|
103
96
|
|
104
|
-
def
|
105
|
-
|
106
|
-
raw.
|
107
|
-
argument.call(
|
108
|
-
arguments: [::Code::Object::Argument.new(element)],
|
109
|
-
**globals
|
110
|
-
)
|
97
|
+
def code_select(argument, **globals)
|
98
|
+
List.new(
|
99
|
+
raw.select do |element|
|
100
|
+
argument.call(arguments: [Argument.new(element)], **globals).truthy?
|
111
101
|
end
|
112
102
|
)
|
113
103
|
end
|
114
104
|
|
115
|
-
def
|
116
|
-
list =
|
117
|
-
element =
|
118
|
-
list
|
105
|
+
def code_step(argument)
|
106
|
+
list = List.new
|
107
|
+
element = left
|
108
|
+
list.code_append(element)
|
119
109
|
|
120
|
-
|
121
|
-
|
122
|
-
|
110
|
+
element = element.code_plus(argument)
|
111
|
+
if exclude_end?
|
112
|
+
while element.code_inferior(right).truthy?
|
113
|
+
list.code_append(element)
|
114
|
+
element = element.code_plus(argument)
|
123
115
|
end
|
124
116
|
else
|
125
|
-
while (
|
126
|
-
list
|
117
|
+
while element.code_inferior_or_equal(right).truthy?
|
118
|
+
list.code_append(element)
|
119
|
+
element = element.code_plus(argument)
|
127
120
|
end
|
128
121
|
end
|
129
122
|
|
130
123
|
list
|
131
124
|
end
|
132
125
|
|
133
|
-
def
|
134
|
-
|
126
|
+
def code_to_list
|
127
|
+
List.new(raw.to_a)
|
135
128
|
end
|
136
129
|
|
137
|
-
def
|
138
|
-
|
130
|
+
def exclude_end?
|
131
|
+
!!exclude_end
|
139
132
|
end
|
140
133
|
|
141
|
-
def
|
142
|
-
|
134
|
+
def inspect
|
135
|
+
to_s
|
136
|
+
end
|
137
|
+
|
138
|
+
def to_s
|
139
|
+
raw.to_s
|
143
140
|
end
|
144
141
|
end
|
145
142
|
end
|
@@ -1,30 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Object
|
3
|
-
class RubyFunction <
|
5
|
+
class RubyFunction < Function
|
4
6
|
attr_reader :raw
|
5
7
|
|
6
8
|
def initialize(raw)
|
7
9
|
@raw = raw
|
8
10
|
end
|
9
11
|
|
10
|
-
|
12
|
+
def self.name
|
13
|
+
"RubyFunction"
|
14
|
+
end
|
11
15
|
|
12
|
-
|
16
|
+
# TODO: fix / refactor
|
17
|
+
def code_call(args:, globals:)
|
13
18
|
regular_arguments =
|
14
19
|
args
|
15
20
|
.select(&:regular?)
|
16
21
|
.map(&:value)
|
17
|
-
.map { |argument|
|
22
|
+
.map { |argument| Ruby.from_code(argument) }
|
18
23
|
|
19
24
|
keyword_arguments =
|
20
25
|
args
|
21
26
|
.select(&:keyword?)
|
22
27
|
.map do |argument|
|
23
|
-
[argument.name.to_sym,
|
28
|
+
[argument.name.to_sym, Ruby.from_code(argument.value)]
|
24
29
|
end
|
25
30
|
.to_h
|
26
31
|
|
27
|
-
|
32
|
+
Ruby.to_code(raw.call(*regular_arguments, **keyword_arguments))
|
28
33
|
end
|
29
34
|
end
|
30
35
|
end
|