template-ruby 0.4.0 → 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/Gemfile +2 -1
- data/Gemfile.lock +27 -54
- 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 -3
- 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/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 -38
- data/lib/code/node/call_argument.rb +11 -27
- data/lib/code/node/chained_call.rb +10 -27
- 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 -8
- 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 +48 -70
- data/lib/code/node/string.rb +42 -16
- 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 +2 -11
- data/lib/code/object/decimal.rb +45 -30
- data/lib/code/object/dictionnary.rb +6 -5
- data/lib/code/object/function.rb +20 -23
- data/lib/code/object/global.rb +11 -6
- data/lib/code/object/integer.rb +73 -30
- data/lib/code/object/list.rb +40 -34
- data/lib/code/object/range.rb +18 -17
- data/lib/code/object/ruby_function.rb +12 -6
- data/lib/code/object/string.rb +22 -12
- data/lib/code/object.rb +82 -24
- 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 -42
- 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 +13 -12
- data/lib/code-ruby.rb +2 -11
- data/lib/code.rb +16 -13
- 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 -11
- data/lib/template.rb +6 -11
- 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 -4
- metadata +112 -72
- 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 -28
- 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 -182
- data/spec/function_spec.rb +0 -26
- data/spec/template/parser/template_spec.rb +0 -19
- data/spec/template_spec.rb +0 -52
data/lib/code/object/function.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
class Code
|
2
2
|
class Object
|
3
3
|
class Function < ::Code::Object
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(parameters:, body:)
|
5
|
+
@parameters = parameters
|
6
6
|
@body = body
|
7
7
|
end
|
8
8
|
|
9
9
|
def call(**args)
|
10
10
|
operator = args.fetch(:operator, nil)
|
11
11
|
arguments = args.fetch(:arguments, [])
|
12
|
-
globals =
|
12
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
13
13
|
|
14
14
|
if operator.nil? || operator == "call"
|
15
15
|
call_function(args: arguments, globals: globals)
|
@@ -28,39 +28,36 @@ class Code
|
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
|
-
attr_reader :
|
31
|
+
attr_reader :parameters, :body
|
32
32
|
|
33
33
|
def call_function(args:, globals:)
|
34
|
-
new_context = globals[:context]
|
34
|
+
new_context = deep_dup(globals[:context])
|
35
35
|
|
36
|
-
|
37
|
-
if
|
38
|
-
if
|
39
|
-
new_context[
|
40
|
-
args.select(&:regular?).map(&:value)
|
36
|
+
parameters.each.with_index do |parameter, index|
|
37
|
+
if parameter.regular?
|
38
|
+
if parameter.regular_splat?
|
39
|
+
new_context[parameter.name] = ::Code::Object::List.new(
|
40
|
+
args.select(&:regular?).map(&:value)
|
41
41
|
)
|
42
|
-
elsif
|
43
|
-
new_context[
|
44
|
-
args.select(&:keyword?).map(&:name_value).to_h
|
42
|
+
elsif parameter.keyword_splat?
|
43
|
+
new_context[parameter.name] = ::Code::Object::Dictionnary.new(
|
44
|
+
args.select(&:keyword?).map(&:name_value).to_h
|
45
45
|
)
|
46
46
|
else
|
47
47
|
arg = args[index]&.value
|
48
|
-
arg =
|
49
|
-
new_context[
|
48
|
+
arg = parameter.evaluate(**globals) if arg.nil?
|
49
|
+
new_context[parameter.name] = arg
|
50
50
|
end
|
51
|
-
elsif
|
52
|
-
arg = args.detect { |arg| arg.name ==
|
53
|
-
arg =
|
54
|
-
new_context[
|
51
|
+
elsif parameter.keyword?
|
52
|
+
arg = args.detect { |arg| arg.name == parameter.name }&.value
|
53
|
+
arg = parameter.evaluate(**globals) if arg.nil?
|
54
|
+
new_context[parameter.name] = arg
|
55
55
|
else
|
56
56
|
raise NotImplementedError
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
body.evaluate(
|
61
|
-
**globals,
|
62
|
-
context: new_context,
|
63
|
-
)
|
60
|
+
body.evaluate(**globals, context: new_context)
|
64
61
|
end
|
65
62
|
end
|
66
63
|
end
|
data/lib/code/object/global.rb
CHANGED
@@ -6,7 +6,8 @@ class Code
|
|
6
6
|
arguments = args.fetch(:arguments, [])
|
7
7
|
context = args.fetch(:context)
|
8
8
|
io = args.fetch(:io)
|
9
|
-
globals =
|
9
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
10
|
+
value = arguments.first&.value
|
10
11
|
|
11
12
|
if operator == "print"
|
12
13
|
io.print(*arguments.map(&:value))
|
@@ -15,11 +16,11 @@ class Code
|
|
15
16
|
io.puts(*arguments.map(&:value))
|
16
17
|
::Code::Object::Nothing.new
|
17
18
|
elsif operator == "context"
|
18
|
-
sig(arguments
|
19
|
-
context[
|
20
|
-
elsif operator == "
|
21
|
-
sig(arguments
|
22
|
-
Code.evaluate(
|
19
|
+
sig(arguments) { ::Code::Object::String }
|
20
|
+
context[value] || ::Code::Object::Nothing.new
|
21
|
+
elsif operator == "evaluate"
|
22
|
+
sig(arguments) { ::Code::Object::String }
|
23
|
+
Code.evaluate(value.raw)
|
23
24
|
else
|
24
25
|
result = context[operator]
|
25
26
|
|
@@ -32,6 +33,10 @@ class Code
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"global"
|
39
|
+
end
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
data/lib/code/object/integer.rb
CHANGED
@@ -18,55 +18,73 @@ class Code
|
|
18
18
|
def call(**args)
|
19
19
|
operator = args.fetch(:operator, nil)
|
20
20
|
arguments = args.fetch(:arguments, [])
|
21
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
22
|
+
value = arguments.first&.value
|
21
23
|
|
22
24
|
if operator == "even?"
|
23
25
|
sig(arguments)
|
24
26
|
even?
|
27
|
+
elsif operator == "odd?"
|
28
|
+
sig(arguments)
|
29
|
+
odd?
|
30
|
+
elsif operator == "times"
|
31
|
+
sig(arguments) { ::Code::Object::Function }
|
32
|
+
times(value, **globals)
|
25
33
|
elsif operator == "*"
|
26
|
-
sig(arguments
|
27
|
-
multiplication(
|
34
|
+
sig(arguments) { [[::Code::Object::Number, ::Code::Object::String]] }
|
35
|
+
multiplication(value)
|
28
36
|
elsif operator == "/"
|
29
|
-
sig(arguments
|
30
|
-
division(
|
37
|
+
sig(arguments) { ::Code::Object::Number }
|
38
|
+
division(value)
|
31
39
|
elsif operator == "+"
|
32
|
-
|
33
|
-
|
40
|
+
if value
|
41
|
+
sig(arguments) { ::Code::Object }
|
42
|
+
plus(value)
|
43
|
+
else
|
44
|
+
sig(arguments)
|
45
|
+
self
|
46
|
+
end
|
34
47
|
elsif operator == "%"
|
35
|
-
sig(arguments
|
36
|
-
modulo(
|
48
|
+
sig(arguments) { ::Code::Object::Number }
|
49
|
+
modulo(value)
|
37
50
|
elsif operator == "-"
|
38
|
-
|
39
|
-
|
51
|
+
if value
|
52
|
+
sig(arguments) { ::Code::Object::Number }
|
53
|
+
minus(value)
|
54
|
+
else
|
55
|
+
sig(arguments)
|
56
|
+
unary_minus
|
57
|
+
end
|
40
58
|
elsif operator == "**"
|
41
|
-
sig(arguments
|
42
|
-
power(
|
59
|
+
sig(arguments) { ::Code::Object::Number }
|
60
|
+
power(value)
|
43
61
|
elsif operator == "<"
|
44
|
-
sig(arguments
|
45
|
-
inferior(
|
62
|
+
sig(arguments) { ::Code::Object::Number }
|
63
|
+
inferior(value)
|
46
64
|
elsif operator == "<="
|
47
|
-
sig(arguments
|
48
|
-
inferior_or_equal(
|
65
|
+
sig(arguments) { ::Code::Object::Number }
|
66
|
+
inferior_or_equal(value)
|
49
67
|
elsif operator == ">"
|
50
|
-
sig(arguments
|
51
|
-
superior(
|
68
|
+
sig(arguments) { ::Code::Object::Number }
|
69
|
+
superior(value)
|
52
70
|
elsif operator == ">="
|
53
|
-
sig(arguments
|
54
|
-
superior_or_equal(
|
71
|
+
sig(arguments) { ::Code::Object::Number }
|
72
|
+
superior_or_equal(value)
|
55
73
|
elsif operator == "<<"
|
56
|
-
sig(arguments
|
57
|
-
left_shift(
|
74
|
+
sig(arguments) { ::Code::Object::Number }
|
75
|
+
left_shift(value)
|
58
76
|
elsif operator == ">>"
|
59
|
-
sig(arguments
|
60
|
-
right_shift(
|
77
|
+
sig(arguments) { ::Code::Object::Number }
|
78
|
+
right_shift(value)
|
61
79
|
elsif operator == "&"
|
62
|
-
sig(arguments
|
63
|
-
bitwise_and(
|
80
|
+
sig(arguments) { ::Code::Object::Number }
|
81
|
+
bitwise_and(value)
|
64
82
|
elsif operator == "|"
|
65
|
-
sig(arguments
|
66
|
-
bitwise_or(
|
83
|
+
sig(arguments) { ::Code::Object::Number }
|
84
|
+
bitwise_or(value)
|
67
85
|
elsif operator == "^"
|
68
|
-
sig(arguments
|
69
|
-
bitwise_xor(
|
86
|
+
sig(arguments) { ::Code::Object::Number }
|
87
|
+
bitwise_xor(value)
|
70
88
|
else
|
71
89
|
super
|
72
90
|
end
|
@@ -76,6 +94,10 @@ class Code
|
|
76
94
|
::Code::Object::Integer.new(raw + 1)
|
77
95
|
end
|
78
96
|
|
97
|
+
def +(other)
|
98
|
+
::Code::Object::Integer.new(raw + other.raw)
|
99
|
+
end
|
100
|
+
|
79
101
|
def to_s
|
80
102
|
raw.to_s
|
81
103
|
end
|
@@ -90,6 +112,10 @@ class Code
|
|
90
112
|
::Code::Object::Boolean.new(raw.even?)
|
91
113
|
end
|
92
114
|
|
115
|
+
def odd?
|
116
|
+
::Code::Object::Boolean.new(raw.odd?)
|
117
|
+
end
|
118
|
+
|
93
119
|
def multiplication(other)
|
94
120
|
if other.is_a?(::Code::Object::Integer)
|
95
121
|
::Code::Object::Integer.new(raw * other.raw)
|
@@ -130,6 +156,10 @@ class Code
|
|
130
156
|
end
|
131
157
|
end
|
132
158
|
|
159
|
+
def unary_minus
|
160
|
+
::Code::Object::Integer.new(-raw)
|
161
|
+
end
|
162
|
+
|
133
163
|
def power(other)
|
134
164
|
if other.is_a?(::Code::Object::Integer)
|
135
165
|
::Code::Object::Integer.new(raw**other.raw)
|
@@ -173,6 +203,19 @@ class Code
|
|
173
203
|
def bitwise_xor(other)
|
174
204
|
::Code::Object::Integer.new(raw ^ other.raw.to_i)
|
175
205
|
end
|
206
|
+
|
207
|
+
def times(argument, **globals)
|
208
|
+
raw.times do |element|
|
209
|
+
argument.call(
|
210
|
+
arguments: [
|
211
|
+
::Code::Object::Argument.new(::Code::Object::Integer.new(element))
|
212
|
+
],
|
213
|
+
**globals
|
214
|
+
)
|
215
|
+
end
|
216
|
+
|
217
|
+
self
|
218
|
+
end
|
176
219
|
end
|
177
220
|
end
|
178
221
|
end
|
data/lib/code/object/list.rb
CHANGED
@@ -10,32 +10,33 @@ class Code
|
|
10
10
|
def call(**args)
|
11
11
|
operator = args.fetch(:operator, nil)
|
12
12
|
arguments = args.fetch(:arguments, [])
|
13
|
-
globals =
|
13
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
+
value = arguments.first&.value
|
14
15
|
|
15
16
|
if operator == "any?"
|
16
|
-
sig(arguments
|
17
|
-
any?(
|
17
|
+
sig(arguments) { ::Code::Object::Function }
|
18
|
+
any?(value, **globals)
|
18
19
|
elsif operator == "none?"
|
19
|
-
sig(arguments
|
20
|
-
none?(
|
20
|
+
sig(arguments) { ::Code::Object::Function }
|
21
|
+
none?(value, **globals)
|
21
22
|
elsif operator == "detect"
|
22
|
-
sig(arguments
|
23
|
-
detect(
|
23
|
+
sig(arguments) { ::Code::Object::Function }
|
24
|
+
detect(value, **globals)
|
24
25
|
elsif operator == "reduce"
|
25
|
-
sig(arguments
|
26
|
-
reduce(
|
26
|
+
sig(arguments) { ::Code::Object::Function }
|
27
|
+
reduce(value, **globals)
|
27
28
|
elsif operator == "each"
|
28
|
-
sig(arguments
|
29
|
-
each(
|
29
|
+
sig(arguments) { ::Code::Object::Function }
|
30
|
+
each(value, **globals)
|
30
31
|
elsif operator == "select"
|
31
|
-
sig(arguments
|
32
|
-
select(
|
32
|
+
sig(arguments) { ::Code::Object::Function }
|
33
|
+
select(value, **globals)
|
33
34
|
elsif operator == "map"
|
34
|
-
sig(arguments
|
35
|
-
map(
|
35
|
+
sig(arguments) { ::Code::Object::Function }
|
36
|
+
map(value, **globals)
|
36
37
|
elsif operator == "max_by"
|
37
|
-
sig(arguments
|
38
|
-
max_by(
|
38
|
+
sig(arguments) { ::Code::Object::Function }
|
39
|
+
max_by(value, **globals)
|
39
40
|
elsif operator == "max"
|
40
41
|
sig(arguments)
|
41
42
|
max
|
@@ -52,8 +53,8 @@ class Code
|
|
52
53
|
sig(arguments)
|
53
54
|
last
|
54
55
|
elsif operator == "<<"
|
55
|
-
sig(arguments
|
56
|
-
append(
|
56
|
+
sig(arguments) { ::Code::Object }
|
57
|
+
append(value)
|
57
58
|
else
|
58
59
|
super
|
59
60
|
end
|
@@ -67,7 +68,7 @@ class Code
|
|
67
68
|
else
|
68
69
|
acc + [element]
|
69
70
|
end
|
70
|
-
end
|
71
|
+
end
|
71
72
|
)
|
72
73
|
end
|
73
74
|
|
@@ -75,6 +76,11 @@ class Code
|
|
75
76
|
::Code::Object::List.new(raw.deep_dup)
|
76
77
|
end
|
77
78
|
|
79
|
+
def <<(other)
|
80
|
+
raw << other
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
78
84
|
def to_s
|
79
85
|
"[#{raw.map(&:inspect).join(", ")}]"
|
80
86
|
end
|
@@ -90,9 +96,9 @@ class Code
|
|
90
96
|
raw.any? do |element|
|
91
97
|
argument.call(
|
92
98
|
arguments: [::Code::Object::Argument.new(element)],
|
93
|
-
**globals
|
99
|
+
**globals
|
94
100
|
).truthy?
|
95
|
-
end
|
101
|
+
end
|
96
102
|
)
|
97
103
|
end
|
98
104
|
|
@@ -101,9 +107,9 @@ class Code
|
|
101
107
|
raw.none? do |element|
|
102
108
|
argument.call(
|
103
109
|
arguments: [::Code::Object::Argument.new(element)],
|
104
|
-
**globals
|
110
|
+
**globals
|
105
111
|
).truthy?
|
106
|
-
end
|
112
|
+
end
|
107
113
|
)
|
108
114
|
end
|
109
115
|
|
@@ -111,7 +117,7 @@ class Code
|
|
111
117
|
raw.max_by do |element|
|
112
118
|
argument.call(
|
113
119
|
arguments: [::Code::Object::Argument.new(element)],
|
114
|
-
**globals
|
120
|
+
**globals
|
115
121
|
)
|
116
122
|
end || ::Code::Object::Nothing.new
|
117
123
|
end
|
@@ -120,7 +126,7 @@ class Code
|
|
120
126
|
raw.detect do |element|
|
121
127
|
argument.call(
|
122
128
|
arguments: [::Code::Object::Argument.new(element)],
|
123
|
-
**globals
|
129
|
+
**globals
|
124
130
|
).truthy?
|
125
131
|
end || ::Code::Object::Nothing.new
|
126
132
|
end
|
@@ -130,9 +136,9 @@ class Code
|
|
130
136
|
argument.call(
|
131
137
|
arguments: [
|
132
138
|
::Code::Object::Argument.new(acc),
|
133
|
-
::Code::Object::Argument.new(element)
|
139
|
+
::Code::Object::Argument.new(element)
|
134
140
|
],
|
135
|
-
**globals
|
141
|
+
**globals
|
136
142
|
)
|
137
143
|
end || ::Code::Object::Nothing.new
|
138
144
|
end
|
@@ -141,7 +147,7 @@ class Code
|
|
141
147
|
raw.each do |element|
|
142
148
|
argument.call(
|
143
149
|
arguments: [::Code::Object::Argument.new(element)],
|
144
|
-
**globals
|
150
|
+
**globals
|
145
151
|
)
|
146
152
|
end
|
147
153
|
self
|
@@ -152,20 +158,20 @@ class Code
|
|
152
158
|
raw.select do |element|
|
153
159
|
argument.call(
|
154
160
|
arguments: [::Code::Object::Argument.new(element)],
|
155
|
-
**globals
|
161
|
+
**globals
|
156
162
|
).truthy?
|
157
|
-
end
|
163
|
+
end
|
158
164
|
)
|
159
165
|
end
|
160
166
|
|
161
|
-
def map(argument,
|
167
|
+
def map(argument, **globals)
|
162
168
|
::Code::Object::List.new(
|
163
169
|
raw.map do |element|
|
164
170
|
argument.call(
|
165
171
|
arguments: [::Code::Object::Argument.new(element)],
|
166
|
-
**globals
|
172
|
+
**globals
|
167
173
|
)
|
168
|
-
end
|
174
|
+
end
|
169
175
|
)
|
170
176
|
end
|
171
177
|
|
data/lib/code/object/range.rb
CHANGED
@@ -13,26 +13,27 @@ class Code
|
|
13
13
|
def call(**args)
|
14
14
|
operator = args.fetch(:operator, nil)
|
15
15
|
arguments = args.fetch(:arguments, [])
|
16
|
-
globals =
|
16
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
17
|
+
value = arguments.first&.value
|
17
18
|
|
18
19
|
if operator == "any?"
|
19
|
-
sig(arguments
|
20
|
-
any?(
|
20
|
+
sig(arguments) { ::Code::Object::Function }
|
21
|
+
any?(value, **globals)
|
21
22
|
elsif operator == "all?"
|
22
|
-
sig(arguments
|
23
|
-
all?(
|
23
|
+
sig(arguments) { ::Code::Object::Function }
|
24
|
+
all?(value, **globals)
|
24
25
|
elsif operator == "each"
|
25
|
-
sig(arguments
|
26
|
-
each(
|
26
|
+
sig(arguments) { ::Code::Object::Function }
|
27
|
+
each(value, **globals)
|
27
28
|
elsif operator == "select"
|
28
|
-
sig(arguments
|
29
|
-
select(
|
29
|
+
sig(arguments) { ::Code::Object::Function }
|
30
|
+
select(value, **globals)
|
30
31
|
elsif operator == "map"
|
31
|
-
sig(arguments
|
32
|
-
map(
|
32
|
+
sig(arguments) { ::Code::Object::Function }
|
33
|
+
map(value, **globals)
|
33
34
|
elsif operator == "step"
|
34
|
-
sig(arguments
|
35
|
-
step(
|
35
|
+
sig(arguments) { ::Code::Object::Number }
|
36
|
+
step(value)
|
36
37
|
elsif operator == "to_list"
|
37
38
|
sig(arguments)
|
38
39
|
to_list
|
@@ -64,7 +65,7 @@ class Code
|
|
64
65
|
arguments: [::Code::Object::Argument.new(element)],
|
65
66
|
**globals
|
66
67
|
).truthy?
|
67
|
-
end
|
68
|
+
end
|
68
69
|
)
|
69
70
|
end
|
70
71
|
|
@@ -75,7 +76,7 @@ class Code
|
|
75
76
|
arguments: [::Code::Object::Argument.new(element)],
|
76
77
|
**globals
|
77
78
|
).truthy?
|
78
|
-
end
|
79
|
+
end
|
79
80
|
)
|
80
81
|
end
|
81
82
|
|
@@ -96,7 +97,7 @@ class Code
|
|
96
97
|
arguments: [::Code::Object::Argument.new(element)],
|
97
98
|
**globals
|
98
99
|
).truthy?
|
99
|
-
end
|
100
|
+
end
|
100
101
|
)
|
101
102
|
end
|
102
103
|
|
@@ -107,7 +108,7 @@ class Code
|
|
107
108
|
arguments: [::Code::Object::Argument.new(element)],
|
108
109
|
**globals
|
109
110
|
)
|
110
|
-
end
|
111
|
+
end
|
111
112
|
)
|
112
113
|
end
|
113
114
|
|
@@ -10,13 +10,19 @@ class Code
|
|
10
10
|
private
|
11
11
|
|
12
12
|
def call_function(args:, globals:)
|
13
|
-
regular_arguments =
|
14
|
-
|
15
|
-
|
13
|
+
regular_arguments =
|
14
|
+
args
|
15
|
+
.select(&:regular?)
|
16
|
+
.map(&:value)
|
17
|
+
.map { |argument| ::Code::Ruby.from_code(argument) }
|
16
18
|
|
17
|
-
keyword_arguments =
|
18
|
-
|
19
|
-
|
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
|
20
26
|
|
21
27
|
::Code::Ruby.to_code(raw.call(*regular_arguments, **keyword_arguments))
|
22
28
|
end
|
data/lib/code/object/string.rb
CHANGED
@@ -10,17 +10,18 @@ class Code
|
|
10
10
|
def call(**args)
|
11
11
|
operator = args.fetch(:operator, nil)
|
12
12
|
arguments = args.fetch(:arguments, [])
|
13
|
-
globals =
|
13
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
+
value = arguments.first&.value
|
14
15
|
|
15
|
-
if operator == "to_function"
|
16
|
+
if operator == "&" || operator == "to_function"
|
16
17
|
sig(arguments)
|
17
18
|
to_function(**globals)
|
18
19
|
elsif operator == "+"
|
19
|
-
sig(arguments
|
20
|
-
plus(
|
20
|
+
sig(arguments) { ::Code::Object }
|
21
|
+
plus(value)
|
21
22
|
elsif operator == "*"
|
22
|
-
sig(arguments
|
23
|
-
multiplication(
|
23
|
+
sig(arguments) { ::Code::Object::Number }
|
24
|
+
multiplication(value)
|
24
25
|
elsif operator == "reverse"
|
25
26
|
sig(arguments)
|
26
27
|
reverse
|
@@ -52,13 +53,22 @@ class Code
|
|
52
53
|
[
|
53
54
|
{
|
54
55
|
function: {
|
55
|
-
|
56
|
+
parameters: [{ name: "_" }],
|
56
57
|
body: [
|
57
|
-
{
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
{
|
59
|
+
chained_call: {
|
60
|
+
first: {
|
61
|
+
call: {
|
62
|
+
name: "_"
|
63
|
+
}
|
64
|
+
},
|
65
|
+
others: [{ call: { name: raw } }]
|
66
|
+
}
|
67
|
+
}
|
68
|
+
]
|
69
|
+
}
|
70
|
+
}
|
71
|
+
]
|
62
72
|
).evaluate(**globals)
|
63
73
|
end
|
64
74
|
|