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