template-ruby 0.3.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +28 -55
- data/TODO +17 -0
- data/bin/code +56 -31
- data/bin/format +3 -0
- data/bin/template +62 -20
- data/bin/test +17 -0
- data/code-ruby.gemspec +1 -6
- data/docs/class.code +9 -0
- data/docs/euler/1.template +1 -5
- data/docs/euler/5.template +0 -1
- data/docs/meetup.code +12 -0
- data/docs/precedence.template +6 -39
- data/docs/rain.code +22 -0
- data/docs/slack.code +17 -0
- data/docs/stripe.code +7 -0
- data/docs/twitter.code +9 -0
- data/language-ruby.gemspec +18 -0
- data/lib/code/error.rb +3 -0
- data/lib/code/node/base_10.rb +29 -0
- data/lib/code/node/base_16.rb +13 -0
- data/lib/code/node/base_2.rb +13 -0
- data/lib/code/node/base_8.rb +13 -0
- data/lib/code/node/boolean.rb +7 -7
- data/lib/code/node/call.rb +32 -37
- data/lib/code/node/call_argument.rb +11 -27
- data/lib/code/node/chained_call.rb +10 -25
- data/lib/code/node/code.rb +4 -6
- data/lib/code/node/decimal.rb +26 -0
- data/lib/code/node/dictionnary.rb +20 -9
- data/lib/code/node/equal.rb +18 -20
- data/lib/code/node/function.rb +10 -7
- data/lib/code/node/function_parameter.rb +31 -0
- data/lib/code/node/if.rb +36 -32
- data/lib/code/node/if_modifier.rb +35 -36
- data/lib/code/node/list.rb +6 -9
- data/lib/code/node/negation.rb +5 -23
- data/lib/code/node/not.rb +15 -0
- data/lib/code/node/nothing.rb +1 -1
- data/lib/code/node/number.rb +14 -12
- data/lib/code/node/operation.rb +21 -16
- data/lib/code/node/power.rb +10 -6
- data/lib/code/node/rescue.rb +4 -3
- data/lib/code/node/splat.rb +15 -0
- data/lib/code/node/statement.rb +47 -69
- data/lib/code/node/string.rb +41 -10
- data/lib/code/node/ternary.rb +7 -9
- data/lib/code/node/unary_minus.rb +5 -12
- data/lib/code/node/while.rb +17 -24
- data/lib/code/node.rb +7 -8
- data/lib/code/object/argument.rb +3 -12
- data/lib/code/object/decimal.rb +114 -27
- data/lib/code/object/dictionnary.rb +29 -12
- data/lib/code/object/function.rb +23 -24
- data/lib/code/object/global.rb +42 -0
- data/lib/code/object/integer.rb +142 -35
- data/lib/code/object/list.rb +74 -95
- data/lib/code/object/range.rb +38 -50
- data/lib/code/object/ruby_function.rb +31 -0
- data/lib/code/object/string.rb +36 -16
- data/lib/code/object.rb +114 -54
- data/lib/code/parser/addition.rb +12 -20
- data/lib/code/parser/and_operator.rb +9 -20
- data/lib/code/parser/bitwise_and.rb +9 -20
- data/lib/code/parser/bitwise_or.rb +12 -20
- data/lib/code/parser/boolean.rb +10 -7
- data/lib/code/parser/call.rb +92 -60
- data/lib/code/parser/chained_call.rb +47 -0
- data/lib/code/parser/class.rb +45 -0
- data/lib/code/parser/code.rb +17 -10
- data/lib/code/parser/dictionnary.rb +56 -30
- data/lib/code/parser/equal.rb +87 -35
- data/lib/code/parser/equality.rb +23 -24
- data/lib/code/parser/equality_lower.rb +9 -0
- data/lib/code/parser/function.rb +67 -40
- data/lib/code/parser/greater.rb +25 -0
- data/lib/code/parser/group.rb +13 -8
- data/lib/code/parser/if.rb +51 -21
- data/lib/code/parser/if_modifier.rb +43 -16
- data/lib/code/parser/list.rb +32 -19
- data/lib/code/parser/multiplication.rb +15 -20
- data/lib/code/parser/name.rb +96 -84
- data/lib/code/parser/negation.rb +20 -9
- data/lib/code/parser/not_keyword.rb +14 -12
- data/lib/code/parser/nothing.rb +13 -8
- data/lib/code/parser/number.rb +124 -68
- data/lib/code/parser/operation.rb +35 -0
- data/lib/code/parser/or_keyword.rb +12 -20
- data/lib/code/parser/or_operator.rb +9 -20
- data/lib/code/parser/power.rb +32 -14
- data/lib/code/parser/range.rb +9 -17
- data/lib/code/parser/rescue.rb +29 -13
- data/lib/code/parser/shift.rb +11 -21
- data/lib/code/parser/splat.rb +31 -0
- data/lib/code/parser/statement.rb +4 -3
- data/lib/code/parser/string.rb +53 -62
- data/lib/code/parser/ternary.rb +36 -15
- data/lib/code/parser/unary_minus.rb +23 -5
- data/lib/code/parser/while.rb +26 -15
- data/lib/code/parser/whitespace.rb +49 -0
- data/lib/code/parser.rb +15 -0
- data/lib/code/ruby.rb +162 -0
- data/lib/code-ruby.rb +2 -5
- data/lib/code.rb +24 -7
- data/lib/language/atom.rb +343 -0
- data/lib/language/output.rb +130 -0
- data/lib/language/parser/absent/present.rb +8 -0
- data/lib/language/parser/absent.rb +6 -0
- data/lib/language/parser/end_of_input.rb +6 -0
- data/lib/language/parser/interuption.rb +38 -0
- data/lib/language/parser/not_end_of_input.rb +6 -0
- data/lib/language/parser/str/not_found.rb +16 -0
- data/lib/language/parser/str.rb +6 -0
- data/lib/language/parser.rb +53 -0
- data/lib/language-ruby.rb +10 -0
- data/lib/language.rb +80 -0
- data/lib/template/node/code_part.rb +1 -1
- data/lib/template/node/part.rb +1 -1
- data/lib/template/node/template.rb +1 -1
- data/lib/template/node/text_part.rb +1 -1
- data/lib/template/node.rb +1 -1
- data/lib/template/parser/template.rb +26 -17
- data/lib/template/parser.rb +15 -0
- data/lib/template/version.rb +1 -1
- data/lib/template-ruby.rb +2 -5
- data/lib/template.rb +23 -13
- data/spec/code/addition_spec.rb +13 -0
- data/spec/code/and_operator_spec.rb +13 -0
- data/spec/code/bitwise_and_spec.rb +13 -0
- data/spec/code/bitwise_or_spec.rb +13 -0
- data/spec/code/boolean_spec.rb +13 -0
- data/spec/code/call_spec.rb +21 -0
- data/spec/code/chained_call_spec.rb +16 -0
- data/spec/code/dictionnary_spec.rb +17 -0
- data/spec/code/equal_spec.rb +26 -0
- data/spec/code/equality_spec.rb +13 -0
- data/spec/code/function_spec.rb +18 -0
- data/spec/code/greater_spec.rb +18 -0
- data/spec/code/group_spec.rb +12 -0
- data/spec/code/if_modifier_spec.rb +20 -0
- data/spec/code/if_spec.rb +25 -0
- data/spec/code/list_spec.rb +17 -0
- data/spec/code/multiplication_spec.rb +18 -0
- data/spec/code/negation_spec.rb +20 -0
- data/spec/code/not_keyword_spec.rb +13 -0
- data/spec/code/nothing_spec.rb +17 -0
- data/spec/code/number_spec.rb +22 -0
- data/spec/code/or_keyword_spec.rb +17 -0
- data/spec/code/or_operator_spec.rb +16 -0
- data/spec/code/parser/boolean_spec.rb +5 -7
- data/spec/code/parser/call_spec.rb +16 -56
- data/spec/code/parser/chained_call.rb +17 -0
- data/spec/code/parser/dictionnary_spec.rb +8 -9
- data/spec/code/parser/function_spec.rb +5 -21
- data/spec/code/parser/group_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +9 -20
- data/spec/code/parser/number_spec.rb +4 -109
- data/spec/code/parser/string_spec.rb +9 -17
- data/spec/code/parser_spec.rb +23 -0
- data/spec/code/power_spec.rb +13 -0
- data/spec/code/range_spec.rb +16 -0
- data/spec/code/rescue_spec.rb +13 -0
- data/spec/code/shift_spec.rb +13 -0
- data/spec/code/splat_spec.rb +13 -0
- data/spec/code/string_spec.rb +25 -0
- data/spec/code/ternary_spec.rb +18 -0
- data/spec/code/unary_minus_spec.rb +13 -0
- data/spec/code/while_spec.rb +18 -0
- data/spec/spec_helper.rb +4 -1
- data/template-ruby.gemspec +2 -7
- metadata +115 -100
- data/lib/code/node/base_10_decimal.rb +0 -32
- data/lib/code/node/base_10_integer.rb +0 -32
- data/lib/code/node/base_10_number.rb +0 -19
- data/lib/code/node/base_16_number.rb +0 -19
- data/lib/code/node/base_2_number.rb +0 -19
- data/lib/code/node/base_8_number.rb +0 -19
- data/lib/code/node/block.rb +0 -17
- data/lib/code/node/defined.rb +0 -19
- data/lib/code/node/dictionnary_key_value.rb +0 -23
- data/lib/code/node/function_argument.rb +0 -45
- data/lib/code/node/group.rb +0 -13
- data/lib/code/node/keyword_call_argument.rb +0 -30
- data/lib/code/node/keyword_function_argument.rb +0 -33
- data/lib/code/node/name.rb +0 -55
- data/lib/code/node/not_keyword.rb +0 -13
- data/lib/code/node/or_keyword.rb +0 -34
- data/lib/code/node/range.rb +0 -31
- data/lib/code/node/regular_call_argument.rb +0 -34
- data/lib/code/node/regular_function_argument.rb +0 -36
- data/lib/code/node/string_characters.rb +0 -13
- data/lib/code/node/string_component.rb +0 -23
- data/lib/code/node/string_interpolation.rb +0 -13
- data/lib/code/parser/defined.rb +0 -20
- data/lib/code/parser/greater_than.rb +0 -33
- data/spec/call_spec.rb +0 -22
- data/spec/code/error/type_error_spec.rb +0 -63
- data/spec/code/parser/name_spec.rb +0 -15
- data/spec/code/parser/nothing_spec.rb +0 -19
- data/spec/code_spec.rb +0 -120
- data/spec/function_spec.rb +0 -26
- data/spec/template/parser/template_spec.rb +0 -19
- data/spec/template_spec.rb +0 -33
data/lib/code/object/list.rb
CHANGED
@@ -10,56 +10,65 @@ class Code
|
|
10
10
|
def call(**args)
|
11
11
|
operator = args.fetch(:operator, nil)
|
12
12
|
arguments = args.fetch(:arguments, [])
|
13
|
-
|
14
|
-
|
13
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
+
value = arguments.first&.value
|
15
15
|
|
16
16
|
if operator == "any?"
|
17
|
-
|
17
|
+
sig(arguments) { ::Code::Object::Function }
|
18
|
+
any?(value, **globals)
|
18
19
|
elsif operator == "none?"
|
19
|
-
|
20
|
+
sig(arguments) { ::Code::Object::Function }
|
21
|
+
none?(value, **globals)
|
20
22
|
elsif operator == "detect"
|
21
|
-
|
23
|
+
sig(arguments) { ::Code::Object::Function }
|
24
|
+
detect(value, **globals)
|
22
25
|
elsif operator == "reduce"
|
23
|
-
|
26
|
+
sig(arguments) { ::Code::Object::Function }
|
27
|
+
reduce(value, **globals)
|
24
28
|
elsif operator == "each"
|
25
|
-
|
29
|
+
sig(arguments) { ::Code::Object::Function }
|
30
|
+
each(value, **globals)
|
26
31
|
elsif operator == "select"
|
27
|
-
|
32
|
+
sig(arguments) { ::Code::Object::Function }
|
33
|
+
select(value, **globals)
|
28
34
|
elsif operator == "map"
|
29
|
-
|
35
|
+
sig(arguments) { ::Code::Object::Function }
|
36
|
+
map(value, **globals)
|
37
|
+
elsif operator == "max_by"
|
38
|
+
sig(arguments) { ::Code::Object::Function }
|
39
|
+
max_by(value, **globals)
|
30
40
|
elsif operator == "max"
|
31
|
-
|
41
|
+
sig(arguments)
|
42
|
+
max
|
32
43
|
elsif operator == "flatten"
|
33
|
-
|
44
|
+
sig(arguments)
|
45
|
+
flatten
|
34
46
|
elsif operator == "reverse"
|
35
|
-
|
47
|
+
sig(arguments)
|
48
|
+
reverse
|
36
49
|
elsif operator == "first"
|
37
|
-
|
50
|
+
sig(arguments)
|
51
|
+
first
|
38
52
|
elsif operator == "last"
|
39
|
-
|
40
|
-
|
41
|
-
max_by(arguments, context: context, io: io)
|
53
|
+
sig(arguments)
|
54
|
+
last
|
42
55
|
elsif operator == "<<"
|
43
|
-
|
56
|
+
sig(arguments) { ::Code::Object }
|
57
|
+
append(value)
|
44
58
|
else
|
45
59
|
super
|
46
60
|
end
|
47
61
|
end
|
48
62
|
|
49
|
-
def
|
50
|
-
raw << element
|
51
|
-
end
|
52
|
-
|
53
|
-
def flatten(arguments)
|
54
|
-
sig(arguments)
|
63
|
+
def flatten
|
55
64
|
::Code::Object::List.new(
|
56
65
|
raw.reduce([]) do |acc, element|
|
57
66
|
if element.is_a?(::Code::Object::List)
|
58
|
-
acc + element.flatten
|
67
|
+
acc + element.flatten.raw
|
59
68
|
else
|
60
69
|
acc + [element]
|
61
70
|
end
|
62
|
-
end
|
71
|
+
end
|
63
72
|
)
|
64
73
|
end
|
65
74
|
|
@@ -67,6 +76,11 @@ class Code
|
|
67
76
|
::Code::Object::List.new(raw.deep_dup)
|
68
77
|
end
|
69
78
|
|
79
|
+
def <<(other)
|
80
|
+
raw << other
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
70
84
|
def to_s
|
71
85
|
"[#{raw.map(&:inspect).join(", ")}]"
|
72
86
|
end
|
@@ -77,144 +91,109 @@ class Code
|
|
77
91
|
|
78
92
|
private
|
79
93
|
|
80
|
-
def any?(
|
81
|
-
sig(arguments, ::Code::Object::Function)
|
82
|
-
argument = arguments.first
|
94
|
+
def any?(argument, **globals)
|
83
95
|
::Code::Object::Boolean.new(
|
84
96
|
raw.any? do |element|
|
85
|
-
argument
|
86
|
-
.
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
io: io,
|
91
|
-
)
|
92
|
-
.truthy?
|
93
|
-
end,
|
97
|
+
argument.call(
|
98
|
+
arguments: [::Code::Object::Argument.new(element)],
|
99
|
+
**globals
|
100
|
+
).truthy?
|
101
|
+
end
|
94
102
|
)
|
95
103
|
end
|
96
104
|
|
97
|
-
def none?(
|
98
|
-
sig(arguments, ::Code::Object::Function)
|
99
|
-
argument = arguments.first
|
105
|
+
def none?(argument, **globals)
|
100
106
|
::Code::Object::Boolean.new(
|
101
107
|
raw.none? do |element|
|
102
|
-
argument
|
103
|
-
.
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
io: io,
|
108
|
-
)
|
109
|
-
.truthy?
|
110
|
-
end,
|
108
|
+
argument.call(
|
109
|
+
arguments: [::Code::Object::Argument.new(element)],
|
110
|
+
**globals
|
111
|
+
).truthy?
|
112
|
+
end
|
111
113
|
)
|
112
114
|
end
|
113
115
|
|
114
|
-
def max_by(
|
115
|
-
sig(arguments, ::Code::Object::Function)
|
116
|
-
argument = arguments.first.value
|
116
|
+
def max_by(argument, **globals)
|
117
117
|
raw.max_by do |element|
|
118
118
|
argument.call(
|
119
119
|
arguments: [::Code::Object::Argument.new(element)],
|
120
|
-
|
121
|
-
io: io,
|
120
|
+
**globals
|
122
121
|
)
|
123
122
|
end || ::Code::Object::Nothing.new
|
124
123
|
end
|
125
124
|
|
126
|
-
def detect(
|
127
|
-
sig(arguments, ::Code::Object::Function)
|
128
|
-
argument = arguments.first.value
|
125
|
+
def detect(argument, **globals)
|
129
126
|
raw.detect do |element|
|
130
127
|
argument.call(
|
131
128
|
arguments: [::Code::Object::Argument.new(element)],
|
132
|
-
|
133
|
-
io: io,
|
129
|
+
**globals
|
134
130
|
).truthy?
|
135
131
|
end || ::Code::Object::Nothing.new
|
136
132
|
end
|
137
133
|
|
138
|
-
def reduce(
|
139
|
-
sig(arguments, ::Code::Object::Function)
|
140
|
-
argument = arguments.first.value
|
134
|
+
def reduce(argument, **globals)
|
141
135
|
raw.reduce do |acc, element|
|
142
136
|
argument.call(
|
143
137
|
arguments: [
|
144
138
|
::Code::Object::Argument.new(acc),
|
145
|
-
::Code::Object::Argument.new(element)
|
139
|
+
::Code::Object::Argument.new(element)
|
146
140
|
],
|
147
|
-
|
148
|
-
io: io,
|
141
|
+
**globals
|
149
142
|
)
|
150
143
|
end || ::Code::Object::Nothing.new
|
151
144
|
end
|
152
145
|
|
153
|
-
def each(
|
154
|
-
sig(arguments, ::Code::Object::Function)
|
155
|
-
argument = arguments.first.value
|
146
|
+
def each(argument, **globals)
|
156
147
|
raw.each do |element|
|
157
148
|
argument.call(
|
158
149
|
arguments: [::Code::Object::Argument.new(element)],
|
159
|
-
|
160
|
-
io: io,
|
150
|
+
**globals
|
161
151
|
)
|
162
152
|
end
|
163
153
|
self
|
164
154
|
end
|
165
155
|
|
166
|
-
def select(
|
167
|
-
sig(arguments, ::Code::Object::Function)
|
168
|
-
argument = arguments.first.value
|
156
|
+
def select(argument, **globals)
|
169
157
|
::Code::Object::List.new(
|
170
158
|
raw.select do |element|
|
171
159
|
argument.call(
|
172
160
|
arguments: [::Code::Object::Argument.new(element)],
|
173
|
-
|
174
|
-
io: io,
|
161
|
+
**globals
|
175
162
|
).truthy?
|
176
|
-
end
|
163
|
+
end
|
177
164
|
)
|
178
165
|
end
|
179
166
|
|
180
|
-
def map(
|
181
|
-
sig(arguments, ::Code::Object::Function)
|
182
|
-
argument = arguments.first.value
|
167
|
+
def map(argument, **globals)
|
183
168
|
::Code::Object::List.new(
|
184
169
|
raw.map do |element|
|
185
170
|
argument.call(
|
186
171
|
arguments: [::Code::Object::Argument.new(element)],
|
187
|
-
|
188
|
-
io: io,
|
172
|
+
**globals
|
189
173
|
)
|
190
|
-
end
|
174
|
+
end
|
191
175
|
)
|
192
176
|
end
|
193
177
|
|
194
|
-
def append(
|
195
|
-
|
196
|
-
raw << arguments.first.value
|
178
|
+
def append(other)
|
179
|
+
raw << other
|
197
180
|
self
|
198
181
|
end
|
199
182
|
|
200
|
-
def first
|
201
|
-
|
202
|
-
raw.first
|
183
|
+
def first
|
184
|
+
raw.first || ::Code::Object::Nothing.new
|
203
185
|
end
|
204
186
|
|
205
|
-
def max
|
206
|
-
sig(arguments)
|
187
|
+
def max
|
207
188
|
raw.max || ::Code::Object::Nothing.new
|
208
189
|
end
|
209
190
|
|
210
|
-
def reverse
|
211
|
-
sig(arguments)
|
191
|
+
def reverse
|
212
192
|
::Code::Object::List.new(raw.reverse)
|
213
193
|
end
|
214
194
|
|
215
|
-
def last
|
216
|
-
|
217
|
-
raw.last
|
195
|
+
def last
|
196
|
+
raw.last || ::Code::Object::Nothing.new
|
218
197
|
end
|
219
198
|
end
|
220
199
|
end
|
data/lib/code/object/range.rb
CHANGED
@@ -13,27 +13,36 @@ class Code
|
|
13
13
|
def call(**args)
|
14
14
|
operator = args.fetch(:operator, nil)
|
15
15
|
arguments = args.fetch(:arguments, [])
|
16
|
-
|
17
|
-
|
16
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
17
|
+
value = arguments.first&.value
|
18
18
|
|
19
19
|
if operator == "any?"
|
20
|
-
|
20
|
+
sig(arguments) { ::Code::Object::Function }
|
21
|
+
any?(value, **globals)
|
21
22
|
elsif operator == "all?"
|
22
|
-
|
23
|
+
sig(arguments) { ::Code::Object::Function }
|
24
|
+
all?(value, **globals)
|
23
25
|
elsif operator == "each"
|
24
|
-
|
26
|
+
sig(arguments) { ::Code::Object::Function }
|
27
|
+
each(value, **globals)
|
25
28
|
elsif operator == "select"
|
26
|
-
|
29
|
+
sig(arguments) { ::Code::Object::Function }
|
30
|
+
select(value, **globals)
|
27
31
|
elsif operator == "map"
|
28
|
-
|
32
|
+
sig(arguments) { ::Code::Object::Function }
|
33
|
+
map(value, **globals)
|
29
34
|
elsif operator == "step"
|
30
|
-
|
35
|
+
sig(arguments) { ::Code::Object::Number }
|
36
|
+
step(value)
|
31
37
|
elsif operator == "to_list"
|
32
|
-
|
38
|
+
sig(arguments)
|
39
|
+
to_list
|
33
40
|
elsif operator == "first"
|
34
|
-
|
41
|
+
sig(arguments)
|
42
|
+
first
|
35
43
|
elsif operator == "last"
|
36
|
-
|
44
|
+
sig(arguments)
|
45
|
+
last
|
37
46
|
else
|
38
47
|
super
|
39
48
|
end
|
@@ -49,79 +58,61 @@ class Code
|
|
49
58
|
|
50
59
|
private
|
51
60
|
|
52
|
-
def any?(
|
53
|
-
sig(arguments, ::Code::Object::Function)
|
54
|
-
argument = arguments.first.value
|
61
|
+
def any?(argument, **globals)
|
55
62
|
::Code::Object::Boolean.new(
|
56
63
|
raw.any? do |element|
|
57
64
|
argument.call(
|
58
65
|
arguments: [::Code::Object::Argument.new(element)],
|
59
|
-
|
60
|
-
io: io,
|
66
|
+
**globals
|
61
67
|
).truthy?
|
62
|
-
end
|
68
|
+
end
|
63
69
|
)
|
64
70
|
end
|
65
71
|
|
66
|
-
def all?(
|
67
|
-
sig(arguments, ::Code::Object::Function)
|
68
|
-
argument = arguments.first.value
|
72
|
+
def all?(argument, **globals)
|
69
73
|
::Code::Object::Boolean.new(
|
70
74
|
raw.all? do |element|
|
71
75
|
argument.call(
|
72
76
|
arguments: [::Code::Object::Argument.new(element)],
|
73
|
-
|
74
|
-
io: io,
|
77
|
+
**globals
|
75
78
|
).truthy?
|
76
|
-
end
|
79
|
+
end
|
77
80
|
)
|
78
81
|
end
|
79
82
|
|
80
|
-
def each(
|
81
|
-
sig(arguments, ::Code::Object::Function)
|
82
|
-
argument = arguments.first.value
|
83
|
+
def each(argument, **globals)
|
83
84
|
raw.each do |element|
|
84
85
|
argument.call(
|
85
86
|
arguments: [::Code::Object::Argument.new(element)],
|
86
|
-
|
87
|
-
io: io,
|
87
|
+
**globals
|
88
88
|
)
|
89
89
|
end
|
90
90
|
self
|
91
91
|
end
|
92
92
|
|
93
|
-
def select(
|
94
|
-
sig(arguments, ::Code::Object::Function)
|
95
|
-
argument = arguments.first.value
|
93
|
+
def select(argument, **globals)
|
96
94
|
::Code::Object::List.new(
|
97
95
|
raw.select do |element|
|
98
96
|
argument.call(
|
99
97
|
arguments: [::Code::Object::Argument.new(element)],
|
100
|
-
|
101
|
-
io: io,
|
98
|
+
**globals
|
102
99
|
).truthy?
|
103
|
-
end
|
100
|
+
end
|
104
101
|
)
|
105
102
|
end
|
106
103
|
|
107
|
-
def map(
|
108
|
-
sig(arguments, ::Code::Object::Function)
|
109
|
-
argument = arguments.first.value
|
104
|
+
def map(argument, **globals)
|
110
105
|
::Code::Object::List.new(
|
111
106
|
raw.map do |element|
|
112
107
|
argument.call(
|
113
108
|
arguments: [::Code::Object::Argument.new(element)],
|
114
|
-
|
115
|
-
io: io,
|
109
|
+
**globals
|
116
110
|
)
|
117
|
-
end
|
111
|
+
end
|
118
112
|
)
|
119
113
|
end
|
120
114
|
|
121
|
-
def step(
|
122
|
-
sig(arguments, ::Code::Object::Number)
|
123
|
-
argument = arguments.first.value
|
124
|
-
|
115
|
+
def step(argument)
|
125
116
|
list = ::Code::Object::List.new
|
126
117
|
element = @left
|
127
118
|
list << element
|
@@ -139,18 +130,15 @@ class Code
|
|
139
130
|
list
|
140
131
|
end
|
141
132
|
|
142
|
-
def to_list
|
143
|
-
sig(arguments)
|
133
|
+
def to_list
|
144
134
|
::Code::Object::List.new(raw.to_a)
|
145
135
|
end
|
146
136
|
|
147
|
-
def first
|
148
|
-
sig(arguments)
|
137
|
+
def first
|
149
138
|
raw.first
|
150
139
|
end
|
151
140
|
|
152
|
-
def last
|
153
|
-
sig(arguments)
|
141
|
+
def last
|
154
142
|
raw.last
|
155
143
|
end
|
156
144
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Code
|
2
|
+
class Object
|
3
|
+
class RubyFunction < ::Code::Object::Function
|
4
|
+
attr_reader :raw
|
5
|
+
|
6
|
+
def initialize(raw)
|
7
|
+
@raw = raw
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def call_function(args:, globals:)
|
13
|
+
regular_arguments =
|
14
|
+
args
|
15
|
+
.select(&:regular?)
|
16
|
+
.map(&:value)
|
17
|
+
.map { |argument| ::Code::Ruby.from_code(argument) }
|
18
|
+
|
19
|
+
keyword_arguments =
|
20
|
+
args
|
21
|
+
.select(&:keyword?)
|
22
|
+
.map do |argument|
|
23
|
+
[argument.name.to_sym, ::Code::Ruby.from_code(argument.value)]
|
24
|
+
end
|
25
|
+
.to_h
|
26
|
+
|
27
|
+
::Code::Ruby.to_code(raw.call(*regular_arguments, **keyword_arguments))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/code/object/string.rb
CHANGED
@@ -10,15 +10,21 @@ class Code
|
|
10
10
|
def call(**args)
|
11
11
|
operator = args.fetch(:operator, nil)
|
12
12
|
arguments = args.fetch(:arguments, [])
|
13
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
+
value = arguments.first&.value
|
13
15
|
|
14
|
-
if operator == "to_function"
|
15
|
-
|
16
|
+
if operator == "&" || operator == "to_function"
|
17
|
+
sig(arguments)
|
18
|
+
to_function(**globals)
|
16
19
|
elsif operator == "+"
|
17
|
-
|
20
|
+
sig(arguments) { ::Code::Object }
|
21
|
+
plus(value)
|
18
22
|
elsif operator == "*"
|
19
|
-
|
23
|
+
sig(arguments) { ::Code::Object::Number }
|
24
|
+
multiplication(value)
|
20
25
|
elsif operator == "reverse"
|
21
|
-
|
26
|
+
sig(arguments)
|
27
|
+
reverse
|
22
28
|
else
|
23
29
|
super
|
24
30
|
end
|
@@ -42,25 +48,39 @@ class Code
|
|
42
48
|
|
43
49
|
private
|
44
50
|
|
45
|
-
def to_function(
|
46
|
-
|
47
|
-
|
51
|
+
def to_function(**globals)
|
52
|
+
::Code::Node::Code.new(
|
53
|
+
[
|
54
|
+
{
|
55
|
+
function: {
|
56
|
+
parameters: [{ name: "_" }],
|
57
|
+
body: [
|
58
|
+
{
|
59
|
+
chained_call: {
|
60
|
+
first: {
|
61
|
+
call: {
|
62
|
+
name: "_"
|
63
|
+
}
|
64
|
+
},
|
65
|
+
others: [{ call: { name: raw } }]
|
66
|
+
}
|
67
|
+
}
|
68
|
+
]
|
69
|
+
}
|
70
|
+
}
|
71
|
+
]
|
72
|
+
).evaluate(**globals)
|
48
73
|
end
|
49
74
|
|
50
|
-
def plus(
|
51
|
-
sig(arguments, ::Code::Object)
|
52
|
-
other = arguments.first.value
|
75
|
+
def plus(other)
|
53
76
|
::Code::Object::String.new(raw + other.to_s)
|
54
77
|
end
|
55
78
|
|
56
|
-
def multiplication(
|
57
|
-
sig(arguments, ::Code::Object::Number)
|
58
|
-
other = arguments.first.value
|
79
|
+
def multiplication(other)
|
59
80
|
::Code::Object::String.new(raw * other.raw)
|
60
81
|
end
|
61
82
|
|
62
|
-
def reverse
|
63
|
-
sig(arguments)
|
83
|
+
def reverse
|
64
84
|
::Code::Object::String.new(raw.reverse)
|
65
85
|
end
|
66
86
|
end
|