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.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -4
  3. data/Gemfile.lock +10 -22
  4. data/code-ruby.gemspec +4 -3
  5. data/lib/code/error.rb +16 -5
  6. data/lib/code/node/base_10.rb +4 -2
  7. data/lib/code/node/base_16.rb +3 -1
  8. data/lib/code/node/base_2.rb +3 -1
  9. data/lib/code/node/base_8.rb +3 -1
  10. data/lib/code/node/boolean.rb +4 -2
  11. data/lib/code/node/call.rb +35 -12
  12. data/lib/code/node/call_argument.rb +16 -5
  13. data/lib/code/node/code.rb +5 -4
  14. data/lib/code/node/decimal.rb +2 -0
  15. data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
  16. data/lib/code/node/function.rb +7 -4
  17. data/lib/code/node/function_parameter.rb +3 -1
  18. data/lib/code/node/if.rb +5 -3
  19. data/lib/code/node/left_operation.rb +63 -0
  20. data/lib/code/node/list.rb +2 -0
  21. data/lib/code/node/negation.rb +2 -0
  22. data/lib/code/node/not.rb +2 -0
  23. data/lib/code/node/nothing.rb +3 -1
  24. data/lib/code/node/number.rb +2 -0
  25. data/lib/code/node/right_operation.rb +70 -0
  26. data/lib/code/node/splat.rb +2 -0
  27. data/lib/code/node/square_bracket.rb +37 -0
  28. data/lib/code/node/statement.rb +13 -5
  29. data/lib/code/node/string.rb +3 -1
  30. data/lib/code/node/ternary.rb +5 -3
  31. data/lib/code/node/unary_minus.rb +2 -0
  32. data/lib/code/node/while.rb +6 -4
  33. data/lib/code/node.rb +11 -5
  34. data/lib/code/object/argument.rb +13 -7
  35. data/lib/code/object/boolean.rb +43 -5
  36. data/lib/code/object/class.rb +17 -0
  37. data/lib/code/object/context.rb +36 -0
  38. data/lib/code/object/decimal.rb +252 -100
  39. data/lib/code/object/dictionary.rb +641 -0
  40. data/lib/code/object/function.rb +54 -27
  41. data/lib/code/object/global.rb +65 -19
  42. data/lib/code/object/identifier_list.rb +47 -0
  43. data/lib/code/object/integer.rb +320 -137
  44. data/lib/code/object/list.rb +140 -138
  45. data/lib/code/object/nothing.rb +10 -4
  46. data/lib/code/object/number.rb +6 -1
  47. data/lib/code/object/range.rb +85 -88
  48. data/lib/code/object/ruby_function.rb +11 -6
  49. data/lib/code/object/string.rb +51 -48
  50. data/lib/code/object.rb +117 -139
  51. data/lib/code/parser/addition.rb +4 -2
  52. data/lib/code/parser/and_operator.rb +4 -2
  53. data/lib/code/parser/bitwise_and.rb +4 -2
  54. data/lib/code/parser/bitwise_or.rb +4 -2
  55. data/lib/code/parser/boolean.rb +3 -1
  56. data/lib/code/parser/call.rb +17 -11
  57. data/lib/code/parser/chained_call.rb +10 -22
  58. data/lib/code/parser/class.rb +9 -6
  59. data/lib/code/parser/code.rb +6 -4
  60. data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
  61. data/lib/code/parser/equal.rb +9 -36
  62. data/lib/code/parser/equality.rb +4 -2
  63. data/lib/code/parser/function.rb +24 -9
  64. data/lib/code/parser/greater.rb +6 -3
  65. data/lib/code/parser/group.rb +4 -2
  66. data/lib/code/parser/if.rb +6 -4
  67. data/lib/code/parser/if_modifier.rb +5 -25
  68. data/lib/code/parser/left_operation.rb +40 -0
  69. data/lib/code/parser/list.rb +6 -5
  70. data/lib/code/parser/multiplication.rb +4 -2
  71. data/lib/code/parser/name.rb +19 -4
  72. data/lib/code/parser/negation.rb +4 -2
  73. data/lib/code/parser/not_keyword.rb +5 -3
  74. data/lib/code/parser/nothing.rb +3 -10
  75. data/lib/code/parser/number.rb +4 -2
  76. data/lib/code/parser/or_keyword.rb +4 -2
  77. data/lib/code/parser/or_operator.rb +4 -2
  78. data/lib/code/parser/power.rb +4 -28
  79. data/lib/code/parser/range.rb +4 -2
  80. data/lib/code/parser/rescue.rb +6 -26
  81. data/lib/code/parser/right_operation.rb +40 -0
  82. data/lib/code/parser/shift.rb +4 -2
  83. data/lib/code/parser/splat.rb +5 -3
  84. data/lib/code/parser/square_bracket.rb +48 -0
  85. data/lib/code/parser/statement.rb +3 -1
  86. data/lib/code/parser/string.rb +12 -10
  87. data/lib/code/parser/ternary.rb +10 -11
  88. data/lib/code/parser/unary_minus.rb +5 -3
  89. data/lib/code/parser/while.rb +5 -3
  90. data/lib/code/parser/whitespace.rb +2 -0
  91. data/lib/code/parser.rb +10 -3
  92. data/lib/code/ruby.rb +4 -2
  93. data/lib/code/type/hash.rb +38 -0
  94. data/lib/code/type/maybe.rb +29 -0
  95. data/lib/code/type/or.rb +38 -0
  96. data/lib/code/type/repeat.rb +38 -0
  97. data/lib/code/type/sig.rb +130 -0
  98. data/lib/code/type.rb +25 -0
  99. data/lib/code/version.rb +3 -0
  100. data/lib/code-ruby.rb +1 -2
  101. data/lib/code.rb +15 -16
  102. data/spec/code/node/call_spec.rb +39 -0
  103. data/spec/code/object/boolean_spec.rb +18 -0
  104. data/spec/code/object/decimal_spec.rb +51 -0
  105. data/spec/code/object/dictionary_spec.rb +98 -0
  106. data/spec/code/object/function_spec.rb +42 -0
  107. data/spec/code/object/integer_spec.rb +43 -0
  108. data/spec/code/object/nothing_spec.rb +14 -0
  109. data/spec/code/object/range_spec.rb +23 -0
  110. data/spec/code/parser/boolean_spec.rb +5 -10
  111. data/spec/code/parser/chained_call.rb +4 -5
  112. data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
  113. data/spec/code/parser/function_spec.rb +4 -5
  114. data/spec/code/parser/group_spec.rb +5 -12
  115. data/spec/code/parser/if_modifier_spec.rb +18 -0
  116. data/spec/code/parser/list_spec.rb +4 -5
  117. data/spec/code/parser/number_spec.rb +4 -5
  118. data/spec/code/parser/string_spec.rb +4 -5
  119. data/spec/code/parser_spec.rb +22 -16
  120. data/spec/code/type_spec.rb +21 -0
  121. data/spec/code_spec.rb +171 -0
  122. data/spec/spec_helper.rb +1 -6
  123. metadata +61 -137
  124. data/.cherry.js +0 -21
  125. data/.editorconfig +0 -9
  126. data/.github/workflows/rspec.yml +0 -14
  127. data/.gitignore +0 -2
  128. data/.prettierrc +0 -3
  129. data/.tool-versions +0 -1
  130. data/CHANGELOG.md +0 -55
  131. data/LICENSE +0 -7
  132. data/README.md +0 -103
  133. data/TODO +0 -17
  134. data/bin/code +0 -76
  135. data/bin/format +0 -3
  136. data/bin/publish +0 -19
  137. data/bin/template +0 -85
  138. data/bin/test +0 -17
  139. data/docs/class.code +0 -9
  140. data/docs/euler/1.template +0 -10
  141. data/docs/euler/2.template +0 -16
  142. data/docs/euler/3.template +0 -16
  143. data/docs/euler/4.template +0 -10
  144. data/docs/euler/5.template +0 -13
  145. data/docs/fibonnaci.template +0 -14
  146. data/docs/meetup.code +0 -12
  147. data/docs/precedence.template +0 -36
  148. data/docs/rain.code +0 -22
  149. data/docs/slack.code +0 -17
  150. data/docs/stripe.code +0 -7
  151. data/docs/twitter.code +0 -9
  152. data/language-ruby.gemspec +0 -17
  153. data/lib/code/node/chained_call.rb +0 -23
  154. data/lib/code/node/equal.rb +0 -34
  155. data/lib/code/node/if_modifier.rb +0 -47
  156. data/lib/code/node/operation.rb +0 -38
  157. data/lib/code/node/power.rb +0 -20
  158. data/lib/code/node/rescue.rb +0 -17
  159. data/lib/code/object/dictionnary.rb +0 -96
  160. data/lib/code/parser/equality_lower.rb +0 -9
  161. data/lib/code/parser/operation.rb +0 -35
  162. data/lib/language/atom.rb +0 -342
  163. data/lib/language/output.rb +0 -130
  164. data/lib/language/parser/absent/present.rb +0 -8
  165. data/lib/language/parser/absent.rb +0 -6
  166. data/lib/language/parser/end_of_input.rb +0 -6
  167. data/lib/language/parser/interuption.rb +0 -38
  168. data/lib/language/parser/not_end_of_input.rb +0 -6
  169. data/lib/language/parser/str/not_found.rb +0 -16
  170. data/lib/language/parser/str.rb +0 -6
  171. data/lib/language/parser.rb +0 -53
  172. data/lib/language-ruby.rb +0 -10
  173. data/lib/language.rb +0 -80
  174. data/lib/template/node/code_part.rb +0 -13
  175. data/lib/template/node/part.rb +0 -19
  176. data/lib/template/node/template.rb +0 -15
  177. data/lib/template/node/text_part.rb +0 -13
  178. data/lib/template/node.rb +0 -4
  179. data/lib/template/parser/template.rb +0 -39
  180. data/lib/template/parser.rb +0 -19
  181. data/lib/template/version.rb +0 -3
  182. data/lib/template-ruby.rb +0 -10
  183. data/lib/template.rb +0 -50
  184. data/spec/code/addition_spec.rb +0 -13
  185. data/spec/code/and_operator_spec.rb +0 -13
  186. data/spec/code/bitwise_and_spec.rb +0 -13
  187. data/spec/code/bitwise_or_spec.rb +0 -13
  188. data/spec/code/boolean_spec.rb +0 -13
  189. data/spec/code/call_spec.rb +0 -21
  190. data/spec/code/chained_call_spec.rb +0 -16
  191. data/spec/code/code_spec.rb +0 -29
  192. data/spec/code/dictionnary_spec.rb +0 -17
  193. data/spec/code/equal_spec.rb +0 -26
  194. data/spec/code/equality_spec.rb +0 -13
  195. data/spec/code/function_spec.rb +0 -18
  196. data/spec/code/greater_spec.rb +0 -18
  197. data/spec/code/group_spec.rb +0 -12
  198. data/spec/code/if_modifier_spec.rb +0 -20
  199. data/spec/code/if_spec.rb +0 -25
  200. data/spec/code/list_spec.rb +0 -19
  201. data/spec/code/multiplication_spec.rb +0 -18
  202. data/spec/code/negation_spec.rb +0 -20
  203. data/spec/code/not_keyword_spec.rb +0 -13
  204. data/spec/code/nothing_spec.rb +0 -17
  205. data/spec/code/number_spec.rb +0 -22
  206. data/spec/code/or_keyword_spec.rb +0 -17
  207. data/spec/code/or_operator_spec.rb +0 -16
  208. data/spec/code/parser/call_spec.rb +0 -26
  209. data/spec/code/power_spec.rb +0 -13
  210. data/spec/code/range_spec.rb +0 -16
  211. data/spec/code/rescue_spec.rb +0 -13
  212. data/spec/code/shift_spec.rb +0 -13
  213. data/spec/code/splat_spec.rb +0 -13
  214. data/spec/code/string_spec.rb +0 -27
  215. data/spec/code/ternary_spec.rb +0 -18
  216. data/spec/code/unary_minus_spec.rb +0 -13
  217. data/spec/code/while_spec.rb +0 -18
  218. data/template-ruby.gemspec +0 -19
@@ -1,74 +1,85 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Object
3
- class String < ::Code::Object
5
+ class String < Object
4
6
  attr_reader :raw
5
7
 
6
8
  def initialize(string)
7
- @raw = string
9
+ @raw = string.to_s
10
+ end
11
+
12
+ def self.name
13
+ "String"
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, *::Code::GLOBALS)
19
+ globals = multi_fetch(args, *GLOBALS)
14
20
  value = arguments.first&.value
15
21
 
16
- if operator == "&" || operator == "to_function"
17
- sig(arguments)
18
- to_function(**globals)
19
- elsif operator == "+"
20
- sig(arguments) { ::Code::Object }
21
- plus(value)
22
- elsif operator == "*"
23
- sig(arguments) { ::Code::Object::Number }
24
- multiplication(value)
25
- elsif operator == "reverse"
26
- sig(arguments)
27
- reverse
28
- elsif operator == "downcase"
29
- sig(arguments)
30
- downcase
31
- elsif operator == "include?"
32
- sig(arguments) { ::Code::Object::String }
33
- include?(value)
22
+ case operator.to_s
23
+ when "&", "to_function"
24
+ sig(args)
25
+ code_to_function(**globals)
26
+ when "*"
27
+ sig(args) { Number }
28
+ code_multiplication(value)
29
+ when "+"
30
+ sig(args) { Object }
31
+ code_plus(value)
32
+ when "downcase"
33
+ sig(args)
34
+ code_downcase
35
+ when "include?"
36
+ sig(args) { String }
37
+ code_include?(value)
38
+ when "reverse"
39
+ sig(args)
40
+ code_reverse
34
41
  else
35
42
  super
36
43
  end
37
44
  end
38
45
 
39
- def succ
40
- ::Code::Object::String.new(raw.succ)
46
+ def code_downcase
47
+ String.new(raw.downcase)
41
48
  end
42
49
 
43
- def to_sym
44
- raw.to_sym
50
+ def code_include?(value)
51
+ Boolean.new(raw.include?(value.raw))
45
52
  end
46
53
 
47
- def to_s
48
- raw
54
+ def code_multiplication(other)
55
+ String.new(raw * other.raw)
49
56
  end
50
57
 
51
- def inspect
52
- raw.inspect
58
+ def code_plus(other)
59
+ String.new(raw + other.to_s)
53
60
  end
54
61
 
55
- private
62
+ def code_reverse
63
+ String.new(raw.reverse)
64
+ end
56
65
 
57
- def to_function(**globals)
58
- ::Code::Node::Code.new(
66
+ def code_to_function(**globals)
67
+ Code::Node::Code.new(
59
68
  [
60
69
  {
61
70
  function: {
62
71
  parameters: [{ name: "_" }],
63
72
  body: [
64
73
  {
65
- chained_call: {
74
+ left_operation: {
66
75
  first: {
67
76
  call: {
68
77
  name: "_"
69
78
  }
70
79
  },
71
- others: [{ call: { name: raw } }]
80
+ others: [
81
+ { operator: ".", statement: { call: { name: raw } } }
82
+ ]
72
83
  }
73
84
  }
74
85
  ]
@@ -78,24 +89,16 @@ class Code
78
89
  ).evaluate(**globals)
79
90
  end
80
91
 
81
- def plus(other)
82
- ::Code::Object::String.new(raw + other.to_s)
83
- end
84
-
85
- def multiplication(other)
86
- ::Code::Object::String.new(raw * other.raw)
87
- end
88
-
89
- def reverse
90
- ::Code::Object::String.new(raw.reverse)
92
+ def inspect
93
+ raw.inspect
91
94
  end
92
95
 
93
- def downcase
94
- ::Code::Object::String.new(raw.downcase)
96
+ def succ
97
+ String.new(raw.succ)
95
98
  end
96
99
 
97
- def include?(value)
98
- ::Code::Object::Boolean.new(raw.include?(value.raw))
100
+ def to_s
101
+ raw
99
102
  end
100
103
  end
101
104
  end
data/lib/code/object.rb CHANGED
@@ -1,63 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Object
3
- include Comparable
4
-
5
- def call(**args)
6
- operator = args.fetch(:operator, nil)
7
- arguments = args.fetch(:arguments, [])
8
- value = arguments.first&.value
5
+ def self.maybe
6
+ Type::Maybe.new(self)
7
+ end
9
8
 
10
- if operator == "=="
11
- sig(arguments) { ::Code::Object }
12
- equal(value)
13
- elsif operator == "==="
14
- sig(arguments) { ::Code::Object }
15
- strict_equal(value)
16
- elsif operator == "!="
17
- sig(arguments) { ::Code::Object }
18
- different(value)
19
- elsif operator == "<=>"
20
- sig(arguments) { ::Code::Object }
21
- compare(value)
22
- elsif operator == "&&" || operator == "and"
23
- sig(arguments) { ::Code::Object }
24
- and_operator(value)
25
- elsif operator == "||" || operator == "or"
26
- sig(arguments) { ::Code::Object }
27
- or_operator(value)
28
- elsif operator == "!" || operator == "not"
29
- sig(arguments)
30
- exclamation_point
31
- elsif operator == "+"
32
- sig(arguments)
33
- self
34
- elsif operator == ".."
35
- sig(arguments) { ::Code::Object }
36
- inclusive_range(value)
37
- elsif operator == "..."
38
- sig(arguments) { ::Code::Object }
39
- exclusive_range(value)
40
- elsif operator == "to_string"
41
- sig(arguments)
42
- to_string
43
- else
44
- raise(
45
- Code::Error::Undefined.new("#{operator} not defined on #{inspect}")
46
- )
47
- end
9
+ def self.name
10
+ "Object"
48
11
  end
49
12
 
50
- def truthy?
51
- true
13
+ def self.repeat(minimum = 0, maximum = nil)
14
+ Type::Repeat.new(self, minimum:, maximum:)
52
15
  end
53
16
 
54
- def falsy?
55
- !truthy?
17
+ def self.|(other)
18
+ Type::Or.new(self, other)
56
19
  end
57
20
 
58
21
  def <=>(other)
59
22
  if respond_to?(:raw)
60
- other.respond_to?(:raw) ? raw <=> other.raw : raw <=> other
23
+ raw <=> (other.respond_to?(:raw) ? other.raw : other)
61
24
  else
62
25
  other <=> self
63
26
  end
@@ -65,133 +28,148 @@ class Code
65
28
 
66
29
  def ==(other)
67
30
  if respond_to?(:raw)
68
- other.respond_to?(:raw) ? raw == other.raw : raw == other
31
+ raw == (other.respond_to?(:raw) ? other.raw : other)
69
32
  else
70
33
  other == self
71
34
  end
72
35
  end
73
- alias_method :eql?, :==
36
+ alias eql? ==
74
37
 
75
- def hash
76
- if respond_to?(:raw)
77
- [self.class, raw].hash
38
+ def call(**args)
39
+ operator = args.fetch(:operator, nil)
40
+ arguments = args.fetch(:arguments, [])
41
+ value = arguments.first&.value
42
+
43
+ case operator.to_s
44
+ when "!", "not"
45
+ sig(args)
46
+ code_exclamation_point
47
+ when "!=", "different"
48
+ sig(args) { Object }
49
+ code_different(value)
50
+ when "&&", "and"
51
+ sig(args) { Object }
52
+ code_and_operator(value)
53
+ when "+", "self"
54
+ sig(args)
55
+ code_self
56
+ when "..", "inclusive_range"
57
+ sig(args) { Object }
58
+ code_inclusive_range(value)
59
+ when "...", "exclusive_range"
60
+ sig(args) { Object }
61
+ code_exclusive_range(value)
62
+ when "==", "equal"
63
+ sig(args) { Object }
64
+ code_equal_equal(value)
65
+ when "===", "strict_equal"
66
+ sig(args) { Object }
67
+ code_equal_equal_equal(value)
68
+ when "falsy?"
69
+ sig(args)
70
+ Boolean.new(falsy?)
71
+ when "to_string"
72
+ sig(args)
73
+ code_to_string
74
+ when "truthy?"
75
+ sig(args)
76
+ Boolean.new(truthy?)
77
+ when "||", "or"
78
+ sig(args) { Object }
79
+ code_or_operator(value)
80
+ when /=$/
81
+ sig(args) { Object }
82
+
83
+ if operator == "="
84
+ context = args[:context]
85
+ context.code_set(self, value)
86
+ else
87
+ context = args[:context].lookup!(self)
88
+ context.code_set(
89
+ self,
90
+ context.code_fetch(self).call(
91
+ **args,
92
+ operator: operator[..-2],
93
+ arguments: [Argument.new(value)]
94
+ )
95
+ )
96
+ end
97
+
98
+ context.code_fetch(self)
78
99
  else
79
- raise NotImplementedError.new(self.class.name)
100
+ raise(
101
+ Code::Error::Undefined,
102
+ "#{operator} not defined on #{inspect}:#{self.class.name}"
103
+ )
80
104
  end
81
105
  end
82
106
 
83
- def to_s
84
- raise NotImplementedError.new(self.class.name)
107
+ def code_and_operator(other)
108
+ truthy? ? other : self
85
109
  end
86
110
 
87
- private
88
-
89
- def multi_fetch(hash, *keys)
90
- keys.map { |key| [key, hash.fetch(key)] }.to_h
111
+ def code_different(other)
112
+ Boolean.new(self != other)
91
113
  end
92
114
 
93
- def deep_dup(object)
94
- if object.is_a?(Array)
95
- object.map { |element| deep_dup(element) }
96
- elsif object.is_a?(Hash)
97
- object.map { |key, value| [deep_dup(key), deep_dup(value)] }.to_h
98
- elsif object.is_a?(::Code::Object::List)
99
- ::Code::Object::List.new(object.raw.map { |element| deep_dup(element) })
100
- elsif object.is_a?(::Code::Object::Dictionnary)
101
- ::Code::Object::Dictionnary.new(
102
- object.raw.map { |key, value| [deep_dup(key), deep_dup(value)] }.to_h
103
- )
104
- else
105
- object.dup
106
- end
115
+ def code_equal_equal(other)
116
+ Boolean.new(self == other)
107
117
  end
108
118
 
109
- def sig(actual_arguments, &block)
110
- if block
111
- expected_arguments = block.call
112
- expected_arguments = [
113
- expected_arguments
114
- ] unless expected_arguments.is_a?(Array)
115
- else
116
- expected_arguments = []
117
- end
119
+ def code_exclamation_point
120
+ truthy? ? Object::Boolean.new(false) : Object::Boolean.new(true)
121
+ end
118
122
 
119
- if actual_arguments.size != expected_arguments.size
120
- raise(
121
- ::Code::Error::ArgumentError.new(
122
- "Expected #{expected_arguments.size} arguments, " \
123
- "got #{actual_arguments.size} arguments"
124
- )
125
- )
126
- end
123
+ def code_exclusive_range(value)
124
+ Range.new(self, value, exclude_end: true)
125
+ end
127
126
 
128
- expected_arguments.each.with_index do |expected_argument, index|
129
- actual_argument = actual_arguments[index].value
130
-
131
- if expected_argument.is_a?(Array)
132
- if expected_argument.none? { |expected_arg|
133
- actual_argument.is_a?(expected_arg)
134
- }
135
- raise(
136
- ::Code::Error::TypeError.new(
137
- "Expected #{expected_argument}, got #{actual_argument.class}"
138
- )
139
- )
140
- end
141
- else
142
- if !actual_argument.is_a?(expected_argument)
143
- raise(
144
- ::Code::Error::TypeError.new(
145
- "Expected #{expected_argument}, got #{actual_argument.class}"
146
- )
147
- )
148
- end
149
- end
150
- end
127
+ def code_inclusive_range(value)
128
+ Range.new(self, value, exclude_end: false)
151
129
  end
152
130
 
153
- def equal(other)
154
- ::Code::Object::Boolean.new(self == other)
131
+ def code_or_operator(other)
132
+ truthy? ? self : other
155
133
  end
156
134
 
157
- def strict_equal(other)
158
- ::Code::Object::Boolean.new(self === other)
135
+ def code_self
136
+ self
159
137
  end
160
138
 
161
- def different(other)
162
- ::Code::Object::Boolean.new(self != other)
139
+ def code_equal_equal_equal(other)
140
+ Boolean.new(self === other)
163
141
  end
164
142
 
165
- def compare(other)
166
- ::Code::Object::Integer.new(self <=> other)
143
+ def code_to_string
144
+ String.new(to_s)
167
145
  end
168
146
 
169
- def and_operator(other)
170
- truthy? ? other : self
147
+ def falsy?
148
+ !truthy?
171
149
  end
172
150
 
173
- def or_operator(other)
174
- truthy? ? self : other
151
+ def hash
152
+ unless respond_to?(:raw)
153
+ raise NotImplementedError, "#{self.class.name}#hash"
154
+ end
155
+
156
+ [self.class, raw].hash
175
157
  end
176
158
 
177
- def to_string
178
- ::Code::Object::String.new(to_s)
159
+ def multi_fetch(hash, *keys)
160
+ keys.map { |key| [key, hash.fetch(key)] }.to_h
179
161
  end
180
162
 
181
- def inclusive_range(value)
182
- ::Code::Object::Range.new(self, value, exclude_end: false)
163
+ def sig(args, &block)
164
+ Type::Sig.sig(args, object: self, &block)
183
165
  end
184
166
 
185
- def exclusive_range(value)
186
- ::Code::Object::Range.new(self, value, exclude_end: true)
167
+ def to_s
168
+ raise NotImplementedError, "#{self.class.name}#to_s"
187
169
  end
188
170
 
189
- def exclamation_point
190
- if truthy?
191
- ::Code::Object::Boolean.new(false)
192
- else
193
- ::Code::Object::Boolean.new(true)
194
- end
171
+ def truthy?
172
+ true
195
173
  end
196
174
  end
197
175
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
- class Addition < Operation
5
+ class Addition < LeftOperation
4
6
  def statement
5
- ::Code::Parser::Multiplication
7
+ Multiplication
6
8
  end
7
9
 
8
10
  def plus
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
- class AndOperator < Operation
5
+ class AndOperator < LeftOperation
4
6
  def statement
5
- ::Code::Parser::EqualityLower
7
+ Equality
6
8
  end
7
9
 
8
10
  def ampersand
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
- class BitwiseAnd < Operation
5
+ class BitwiseAnd < LeftOperation
4
6
  def statement
5
- ::Code::Parser::Shift
7
+ Shift
6
8
  end
7
9
 
8
10
  def ampersand
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
- class BitwiseOr < Operation
5
+ class BitwiseOr < LeftOperation
4
6
  def statement
5
- ::Code::Parser::BitwiseAnd
7
+ BitwiseAnd
6
8
  end
7
9
 
8
10
  def pipe
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
5
  class Boolean < Language
@@ -10,7 +12,7 @@ class Code
10
12
  end
11
13
 
12
14
  def root
13
- (true_keyword | false_keyword).aka(:boolean) | ::Code::Parser::Nothing
15
+ (true_keyword | false_keyword).aka(:boolean) | Nothing
14
16
  end
15
17
  end
16
18
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
5
  class Call < Language
4
6
  def name
5
- ::Code::Parser::Name
7
+ Name
6
8
  end
7
9
 
8
10
  def whitespace
9
- ::Code::Parser::Whitespace
11
+ Whitespace
10
12
  end
11
13
 
12
14
  def whitespace?
@@ -14,11 +16,11 @@ class Code
14
16
  end
15
17
 
16
18
  def code
17
- ::Code::Parser::Code
19
+ Code
18
20
  end
19
21
 
20
22
  def code_present
21
- ::Code::Parser::Code.new.present
23
+ Code.new.present
22
24
  end
23
25
 
24
26
  def colon
@@ -62,7 +64,7 @@ class Code
62
64
  end
63
65
 
64
66
  def keyword_argument
65
- name.aka(:name) << colon << code_present.aka(:value)
67
+ name.aka(:name) << whitespace? << colon << code.aka(:value)
66
68
  end
67
69
 
68
70
  def regular_argument
@@ -74,9 +76,11 @@ class Code
74
76
  end
75
77
 
76
78
  def arguments
77
- opening_parenthesis.ignore << whitespace? << argument.repeat(0, 1) <<
78
- (comma << whitespace? << argument << whitespace?).repeat <<
79
- whitespace? << closing_parenthesis.ignore.maybe
79
+ opening_parenthesis.ignore << whitespace? <<
80
+ (
81
+ whitespace? << argument <<
82
+ (whitespace? << comma << whitespace?).maybe
83
+ ).repeat << whitespace? << closing_parenthesis.ignore.maybe
80
84
  end
81
85
 
82
86
  def keyword_parameter
@@ -94,9 +98,11 @@ class Code
94
98
  end
95
99
 
96
100
  def parameters
97
- pipe.ignore << whitespace? << parameter.repeat(0, 1) <<
98
- (whitespace? << comma << whitespace? << parameter).repeat <<
99
- whitespace? << pipe.ignore.maybe
101
+ pipe.ignore << whitespace? <<
102
+ (
103
+ whitespace? << parameter <<
104
+ (whitespace? << comma << whitespace?).maybe
105
+ ).repeat << (whitespace? << pipe.ignore).maybe
100
106
  end
101
107
 
102
108
  def block
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Parser
3
- class ChainedCall < Operation
5
+ class ChainedCall < LeftOperation
4
6
  def statement
5
- ::Code::Parser::Dictionnary
7
+ SquareBracket
6
8
  end
7
9
 
8
10
  def whitespace
9
- ::Code::Parser::Whitespace
11
+ Whitespace
10
12
  end
11
13
 
12
14
  def whitespace?
@@ -17,30 +19,16 @@ class Code
17
19
  str(".")
18
20
  end
19
21
 
22
+ def ampersand
23
+ str("&")
24
+ end
25
+
20
26
  def colon
21
27
  str(":")
22
28
  end
23
29
 
24
30
  def operator
25
- dot | (colon << colon)
26
- end
27
-
28
- def root
29
- (
30
- statement.aka(:first) <<
31
- (whitespace? << operator << whitespace? << statement)
32
- .repeat(1)
33
- .aka(:others)
34
- .maybe
35
- )
36
- .aka(:chained_call)
37
- .then do |output|
38
- if output[:chained_call][:others]
39
- output
40
- else
41
- output[:chained_call][:first]
42
- end
43
- end
31
+ dot | (colon << colon) | (ampersand << dot)
44
32
  end
45
33
  end
46
34
  end