language-ruby 0.5.6 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.ruby-version +1 -0
  4. data/.tool-versions +1 -1
  5. data/Gemfile +5 -4
  6. data/Gemfile.lock +12 -23
  7. data/README.md +2 -102
  8. data/language-ruby.gemspec +7 -5
  9. data/lib/language/atom/absent.rb +29 -0
  10. data/lib/language/atom/aka.rb +32 -0
  11. data/lib/language/atom/and.rb +32 -0
  12. data/lib/language/atom/any.rb +15 -0
  13. data/lib/language/atom/ignore.rb +27 -0
  14. data/lib/language/atom/maybe.rb +32 -0
  15. data/lib/language/atom/or.rb +46 -0
  16. data/lib/language/atom/repeat.rb +57 -0
  17. data/lib/language/atom/str.rb +21 -0
  18. data/lib/language/atom/then.rb +21 -0
  19. data/lib/language/atom.rb +10 -291
  20. data/lib/language/output.rb +13 -11
  21. data/lib/language/parser/absent/present.rb +2 -0
  22. data/lib/language/parser/absent.rb +2 -0
  23. data/lib/language/parser/end_of_input.rb +2 -0
  24. data/lib/language/parser/interuption.rb +2 -0
  25. data/lib/language/parser/not_end_of_input.rb +2 -0
  26. data/lib/language/parser/str/not_found.rb +2 -0
  27. data/lib/language/parser/str.rb +2 -0
  28. data/lib/language/parser.rb +10 -10
  29. data/lib/language/version.rb +5 -0
  30. data/lib/language-ruby.rb +2 -5
  31. data/lib/language.rb +9 -7
  32. data/spec/language_spec.rb +21 -0
  33. data/spec/spec_helper.rb +2 -5
  34. metadata +18 -218
  35. data/.cherry.js +0 -21
  36. data/.editorconfig +0 -9
  37. data/.github/workflows/rspec.yml +0 -14
  38. data/.prettierrc +0 -3
  39. data/CHANGELOG.md +0 -55
  40. data/LICENSE +0 -7
  41. data/TODO +0 -17
  42. data/bin/code +0 -76
  43. data/bin/format +0 -3
  44. data/bin/publish +0 -19
  45. data/bin/template +0 -85
  46. data/bin/test +0 -17
  47. data/code-ruby.gemspec +0 -17
  48. data/docs/class.code +0 -9
  49. data/docs/euler/1.template +0 -10
  50. data/docs/euler/2.template +0 -16
  51. data/docs/euler/3.template +0 -16
  52. data/docs/euler/4.template +0 -10
  53. data/docs/euler/5.template +0 -13
  54. data/docs/fibonnaci.template +0 -14
  55. data/docs/meetup.code +0 -12
  56. data/docs/precedence.template +0 -36
  57. data/docs/rain.code +0 -22
  58. data/docs/slack.code +0 -17
  59. data/docs/stripe.code +0 -7
  60. data/docs/twitter.code +0 -9
  61. data/lib/code/error.rb +0 -18
  62. data/lib/code/node/base_10.rb +0 -29
  63. data/lib/code/node/base_16.rb +0 -13
  64. data/lib/code/node/base_2.rb +0 -13
  65. data/lib/code/node/base_8.rb +0 -13
  66. data/lib/code/node/boolean.rb +0 -22
  67. data/lib/code/node/call.rb +0 -47
  68. data/lib/code/node/call_argument.rb +0 -21
  69. data/lib/code/node/chained_call.rb +0 -23
  70. data/lib/code/node/code.rb +0 -20
  71. data/lib/code/node/decimal.rb +0 -26
  72. data/lib/code/node/dictionnary.rb +0 -33
  73. data/lib/code/node/equal.rb +0 -34
  74. data/lib/code/node/function.rb +0 -20
  75. data/lib/code/node/function_parameter.rb +0 -31
  76. data/lib/code/node/if.rb +0 -59
  77. data/lib/code/node/if_modifier.rb +0 -47
  78. data/lib/code/node/list.rb +0 -16
  79. data/lib/code/node/negation.rb +0 -15
  80. data/lib/code/node/not.rb +0 -15
  81. data/lib/code/node/nothing.rb +0 -12
  82. data/lib/code/node/number.rb +0 -25
  83. data/lib/code/node/operation.rb +0 -38
  84. data/lib/code/node/power.rb +0 -20
  85. data/lib/code/node/rescue.rb +0 -17
  86. data/lib/code/node/splat.rb +0 -15
  87. data/lib/code/node/statement.rb +0 -59
  88. data/lib/code/node/string.rb +0 -53
  89. data/lib/code/node/ternary.rb +0 -24
  90. data/lib/code/node/unary_minus.rb +0 -15
  91. data/lib/code/node/while.rb +0 -35
  92. data/lib/code/node.rb +0 -13
  93. data/lib/code/object/argument.rb +0 -32
  94. data/lib/code/object/boolean.rb +0 -27
  95. data/lib/code/object/decimal.rb +0 -162
  96. data/lib/code/object/dictionnary.rb +0 -96
  97. data/lib/code/object/function.rb +0 -64
  98. data/lib/code/object/global.rb +0 -42
  99. data/lib/code/object/integer.rb +0 -221
  100. data/lib/code/object/list.rb +0 -207
  101. data/lib/code/object/nothing.rb +0 -23
  102. data/lib/code/object/number.rb +0 -6
  103. data/lib/code/object/range.rb +0 -146
  104. data/lib/code/object/ruby_function.rb +0 -31
  105. data/lib/code/object/string.rb +0 -102
  106. data/lib/code/object.rb +0 -197
  107. data/lib/code/parser/addition.rb +0 -21
  108. data/lib/code/parser/and_operator.rb +0 -17
  109. data/lib/code/parser/bitwise_and.rb +0 -17
  110. data/lib/code/parser/bitwise_or.rb +0 -21
  111. data/lib/code/parser/boolean.rb +0 -17
  112. data/lib/code/parser/call.rb +0 -122
  113. data/lib/code/parser/chained_call.rb +0 -47
  114. data/lib/code/parser/class.rb +0 -45
  115. data/lib/code/parser/code.rb +0 -25
  116. data/lib/code/parser/dictionnary.rb +0 -67
  117. data/lib/code/parser/equal.rb +0 -94
  118. data/lib/code/parser/equality.rb +0 -35
  119. data/lib/code/parser/equality_lower.rb +0 -9
  120. data/lib/code/parser/function.rb +0 -85
  121. data/lib/code/parser/greater.rb +0 -25
  122. data/lib/code/parser/group.rb +0 -22
  123. data/lib/code/parser/if.rb +0 -63
  124. data/lib/code/parser/if_modifier.rb +0 -55
  125. data/lib/code/parser/list.rb +0 -42
  126. data/lib/code/parser/multiplication.rb +0 -25
  127. data/lib/code/parser/name.rb +0 -107
  128. data/lib/code/parser/negation.rb +0 -30
  129. data/lib/code/parser/not_keyword.rb +0 -23
  130. data/lib/code/parser/nothing.rb +0 -22
  131. data/lib/code/parser/number.rb +0 -154
  132. data/lib/code/parser/operation.rb +0 -35
  133. data/lib/code/parser/or_keyword.rb +0 -21
  134. data/lib/code/parser/or_operator.rb +0 -17
  135. data/lib/code/parser/power.rb +0 -43
  136. data/lib/code/parser/range.rb +0 -17
  137. data/lib/code/parser/rescue.rb +0 -39
  138. data/lib/code/parser/shift.rb +0 -21
  139. data/lib/code/parser/splat.rb +0 -31
  140. data/lib/code/parser/statement.rb +0 -9
  141. data/lib/code/parser/string.rb +0 -78
  142. data/lib/code/parser/ternary.rb +0 -46
  143. data/lib/code/parser/unary_minus.rb +0 -31
  144. data/lib/code/parser/while.rb +0 -36
  145. data/lib/code/parser/whitespace.rb +0 -49
  146. data/lib/code/parser.rb +0 -19
  147. data/lib/code/ruby.rb +0 -174
  148. data/lib/code-ruby.rb +0 -10
  149. data/lib/code.rb +0 -47
  150. data/lib/template/node/code_part.rb +0 -13
  151. data/lib/template/node/part.rb +0 -19
  152. data/lib/template/node/template.rb +0 -15
  153. data/lib/template/node/text_part.rb +0 -13
  154. data/lib/template/node.rb +0 -4
  155. data/lib/template/parser/template.rb +0 -39
  156. data/lib/template/parser.rb +0 -19
  157. data/lib/template/version.rb +0 -3
  158. data/lib/template-ruby.rb +0 -10
  159. data/lib/template.rb +0 -50
  160. data/spec/code/addition_spec.rb +0 -13
  161. data/spec/code/and_operator_spec.rb +0 -13
  162. data/spec/code/bitwise_and_spec.rb +0 -13
  163. data/spec/code/bitwise_or_spec.rb +0 -13
  164. data/spec/code/boolean_spec.rb +0 -13
  165. data/spec/code/call_spec.rb +0 -21
  166. data/spec/code/chained_call_spec.rb +0 -16
  167. data/spec/code/code_spec.rb +0 -29
  168. data/spec/code/dictionnary_spec.rb +0 -17
  169. data/spec/code/equal_spec.rb +0 -26
  170. data/spec/code/equality_spec.rb +0 -13
  171. data/spec/code/function_spec.rb +0 -18
  172. data/spec/code/greater_spec.rb +0 -18
  173. data/spec/code/group_spec.rb +0 -12
  174. data/spec/code/if_modifier_spec.rb +0 -20
  175. data/spec/code/if_spec.rb +0 -25
  176. data/spec/code/list_spec.rb +0 -19
  177. data/spec/code/multiplication_spec.rb +0 -18
  178. data/spec/code/negation_spec.rb +0 -20
  179. data/spec/code/not_keyword_spec.rb +0 -13
  180. data/spec/code/nothing_spec.rb +0 -17
  181. data/spec/code/number_spec.rb +0 -22
  182. data/spec/code/or_keyword_spec.rb +0 -17
  183. data/spec/code/or_operator_spec.rb +0 -16
  184. data/spec/code/parser/boolean_spec.rb +0 -16
  185. data/spec/code/parser/call_spec.rb +0 -26
  186. data/spec/code/parser/chained_call.rb +0 -17
  187. data/spec/code/parser/dictionnary_spec.rb +0 -18
  188. data/spec/code/parser/function_spec.rb +0 -16
  189. data/spec/code/parser/group_spec.rb +0 -18
  190. data/spec/code/parser/list_spec.rb +0 -18
  191. data/spec/code/parser/number_spec.rb +0 -12
  192. data/spec/code/parser/string_spec.rb +0 -21
  193. data/spec/code/parser_spec.rb +0 -23
  194. data/spec/code/power_spec.rb +0 -13
  195. data/spec/code/range_spec.rb +0 -16
  196. data/spec/code/rescue_spec.rb +0 -13
  197. data/spec/code/shift_spec.rb +0 -13
  198. data/spec/code/splat_spec.rb +0 -13
  199. data/spec/code/string_spec.rb +0 -27
  200. data/spec/code/ternary_spec.rb +0 -18
  201. data/spec/code/unary_minus_spec.rb +0 -13
  202. data/spec/code/while_spec.rb +0 -18
  203. data/template-ruby.gemspec +0 -19
data/lib/code/node/not.rb DELETED
@@ -1,15 +0,0 @@
1
- class Code
2
- class Node
3
- class Not < Node
4
- def initialize(parsed)
5
- @operator = parsed.delete(:operator)
6
- @right = Node::Statement.new(parsed.delete(:right))
7
- super(parsed)
8
- end
9
-
10
- def evaluate(**args)
11
- @right.evaluate(**args).call(operator: @operator, arguments: [], **args)
12
- end
13
- end
14
- end
15
- end
@@ -1,12 +0,0 @@
1
- class Code
2
- class Node
3
- class Nothing < Node
4
- def initialize(parsed)
5
- end
6
-
7
- def evaluate(**args)
8
- ::Code::Object::Nothing.new
9
- end
10
- end
11
- end
12
- end
@@ -1,25 +0,0 @@
1
- class Code
2
- class Node
3
- class Number < Node
4
- def initialize(parsed)
5
- if parsed.key?(:decimal)
6
- @statement = Node::Decimal.new(parsed.delete(:decimal))
7
- elsif parsed.key?(:base_16)
8
- @statement = Node::Base16.new(parsed.delete(:base_16))
9
- elsif parsed.key?(:base_10)
10
- @statement = Node::Base10.new(parsed.delete(:base_10))
11
- elsif parsed.key?(:base_8)
12
- @statement = Node::Base8.new(parsed.delete(:base_8))
13
- elsif parsed.key?(:base_2)
14
- @statement = Node::Base2.new(parsed.delete(:base_2))
15
- end
16
-
17
- super(parsed)
18
- end
19
-
20
- def evaluate(**args)
21
- @statement.evaluate(**args)
22
- end
23
- end
24
- end
25
- end
@@ -1,38 +0,0 @@
1
- class Code
2
- class Node
3
- class Operation < Node
4
- class Operator < Node
5
- attr_reader :operator, :statement
6
-
7
- def initialize(parsed)
8
- @operator = parsed.delete(:operator)
9
- @statement = Node::Statement.new(parsed.delete(:statement))
10
- end
11
- end
12
-
13
- def initialize(parsed)
14
- @first = Node::Statement.new(parsed.delete(:first))
15
- @others =
16
- parsed
17
- .delete(:others)
18
- .map { |operator| Node::Operation::Operator.new(operator) }
19
-
20
- super(parsed)
21
- end
22
-
23
- def evaluate(**args)
24
- first = @first.evaluate(**args)
25
-
26
- @others.reduce(first) do |left, right|
27
- statement = right.statement.evaluate(**args)
28
-
29
- left.call(
30
- operator: right.operator,
31
- arguments: [::Code::Object::Argument.new(statement)],
32
- **args
33
- )
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,20 +0,0 @@
1
- class Code
2
- class Node
3
- class Power < Node
4
- def initialize(parsed)
5
- @operator = parsed.delete(:operator)
6
- @left = Node::Statement.new(parsed.delete(:left))
7
- @right = Node::Statement.new(parsed.delete(:right))
8
- super(parsed)
9
- end
10
-
11
- def evaluate(**args)
12
- @left.evaluate(**args).call(
13
- operator: @operator,
14
- arguments: [::Code::Object::Argument.new(@right.evaluate(**args))],
15
- **args
16
- )
17
- end
18
- end
19
- end
20
- end
@@ -1,17 +0,0 @@
1
- class Code
2
- class Node
3
- class Rescue < Node
4
- def initialize(parsed)
5
- @left = Node::Statement.new(parsed.delete(:left))
6
- @right = Node::Statement.new(parsed.delete(:right))
7
- super(parsed)
8
- end
9
-
10
- def evaluate(**args)
11
- @left.evaluate(**args)
12
- rescue ::Code::Error
13
- @right.evaluate(**args)
14
- end
15
- end
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- class Code
2
- class Node
3
- class Splat < Node
4
- def initialize(parsed)
5
- @operator = parsed.delete(:operator)
6
- @right = Node::Statement.new(parsed.delete(:right))
7
- super(parsed)
8
- end
9
-
10
- def evaluate(**args)
11
- @right.evaluate(**args).call(operator: @operator, arguments: [], **args)
12
- end
13
- end
14
- end
15
- end
@@ -1,59 +0,0 @@
1
- class Code
2
- class Node
3
- class Statement < Node
4
- def initialize(parsed)
5
- if parsed.key?(:nothing)
6
- @statement = Node::Nothing.new(parsed.delete(:nothing))
7
- elsif parsed.key?(:boolean)
8
- @statement = Node::Boolean.new(parsed.delete(:boolean))
9
- elsif parsed.key?(:group)
10
- @statement = Node::Code.new(parsed.delete(:group))
11
- elsif parsed.key?(:call)
12
- @statement = Node::Call.new(parsed.delete(:call))
13
- elsif parsed.key?(:number)
14
- @statement = Node::Number.new(parsed.delete(:number))
15
- elsif parsed.key?(:string)
16
- @statement = Node::String.new(parsed.delete(:string))
17
- elsif parsed.key?(:list)
18
- @statement = Node::List.new(parsed.delete(:list))
19
- elsif parsed.key?(:dictionnary)
20
- @statement = Node::Dictionnary.new(parsed.delete(:dictionnary))
21
- elsif parsed.key?(:chained_call)
22
- @statement = Node::ChainedCall.new(parsed.delete(:chained_call))
23
- elsif parsed.key?(:operation)
24
- @statement = Node::Operation.new(parsed.delete(:operation))
25
- elsif parsed.key?(:equal)
26
- @statement = Node::Equal.new(parsed.delete(:equal))
27
- elsif parsed.key?(:function)
28
- @statement = Node::Function.new(parsed.delete(:function))
29
- elsif parsed.key?(:negation)
30
- @statement = Node::Negation.new(parsed.delete(:negation))
31
- elsif parsed.key?(:power)
32
- @statement = Node::Power.new(parsed.delete(:power))
33
- elsif parsed.key?(:unary_minus)
34
- @statement = Node::UnaryMinus.new(parsed.delete(:unary_minus))
35
- elsif parsed.key?(:ternary)
36
- @statement = Node::Ternary.new(parsed.delete(:ternary))
37
- elsif parsed.key?(:rescue)
38
- @statement = Node::Rescue.new(parsed.delete(:rescue))
39
- elsif parsed.key?(:not)
40
- @statement = Node::Not.new(parsed.delete(:not))
41
- elsif parsed.key?(:if_modifier)
42
- @statement = Node::IfModifier.new(parsed.delete(:if_modifier))
43
- elsif parsed.key?(:if)
44
- @statement = Node::If.new(parsed.delete(:if))
45
- elsif parsed.key?(:while)
46
- @statement = Node::While.new(parsed.delete(:while))
47
- elsif parsed.key?(:splat)
48
- @statement = Node::Splat.new(parsed.delete(:splat))
49
- end
50
-
51
- super(parsed)
52
- end
53
-
54
- def evaluate(**args)
55
- @statement.evaluate(**args)
56
- end
57
- end
58
- end
59
- end
@@ -1,53 +0,0 @@
1
- class Code
2
- class Node
3
- class String < Node
4
- class Part < Node
5
- class Code < Node
6
- def initialize(parsed)
7
- @code = Node::Code.new(parsed)
8
- end
9
-
10
- def evaluate(**args)
11
- @code.evaluate(**args)
12
- end
13
- end
14
-
15
- class Text < Node
16
- def initialize(parsed)
17
- @text = parsed
18
- end
19
-
20
- def evaluate(**args)
21
- ::Code::Object::String.new(@text)
22
- end
23
- end
24
-
25
- def initialize(parsed)
26
- if parsed.key?(:text)
27
- @part = Node::String::Part::Text.new(parsed.delete(:text))
28
- elsif parsed.key?(:code)
29
- @part = Node::String::Part::Code.new(parsed.delete(:code))
30
- end
31
-
32
- super(parsed)
33
- end
34
-
35
- def evaluate(**args)
36
- @part.evaluate(**args)
37
- end
38
- end
39
-
40
- def initialize(parsed)
41
- parsed = [] if parsed == ""
42
-
43
- @parts = parsed.map { |part| Node::String::Part.new(part) }
44
- end
45
-
46
- def evaluate(**args)
47
- ::Code::Object::String.new(
48
- @parts.map { |part| part.evaluate(**args) }.map(&:to_s).join
49
- )
50
- end
51
- end
52
- end
53
- end
@@ -1,24 +0,0 @@
1
- class Code
2
- class Node
3
- class Ternary < Node
4
- def initialize(parsed)
5
- @left = Node::Statement.new(parsed.delete(:left))
6
- @middle = Node::Statement.new(parsed.delete(:middle))
7
- if parsed.key?(:right)
8
- @right = Node::Statement.new(parsed.delete(:right))
9
- end
10
- super(parsed)
11
- end
12
-
13
- def evaluate(**args)
14
- if @left.evaluate(**args).truthy?
15
- @middle.evaluate(**args)
16
- elsif @right
17
- @right.evaluate(**args)
18
- else
19
- ::Code::Object::Nothing.new
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,15 +0,0 @@
1
- class Code
2
- class Node
3
- class UnaryMinus < Node
4
- def initialize(parsed)
5
- @operator = parsed.delete(:operator)
6
- @right = Node::Statement.new(parsed.delete(:right))
7
- super(parsed)
8
- end
9
-
10
- def evaluate(**args)
11
- @right.evaluate(**args).call(operator: @operator, arguments: [], **args)
12
- end
13
- end
14
- end
15
- end
@@ -1,35 +0,0 @@
1
- class Code
2
- class Node
3
- class While < Node
4
- WHILE_KEYWORD = "while"
5
- UNTIL_KEYWORD = "until"
6
-
7
- def initialize(parsed)
8
- @operator = parsed.delete(:operator)
9
- @statement = Node::Statement.new(parsed.delete(:statement))
10
- @body = Node::Code.new(parsed.delete(:body))
11
- super(parsed)
12
- end
13
-
14
- def evaluate(**args)
15
- if @operator == WHILE_KEYWORD
16
- last = ::Code::Object::Nothing.new
17
-
18
- while @statement.evaluate(**args).truthy?
19
- last = @body.evaluate(**args)
20
- end
21
-
22
- last
23
- elsif @operator == UNTIL_KEYWORD
24
- last = ::Code::Object::Nothing.new
25
-
26
- last = @body.evaluate(**args) while @statement.evaluate(**args).falsy?
27
-
28
- last
29
- else
30
- raise NotImplementedError.new(@operator)
31
- end
32
- end
33
- end
34
- end
35
- end
data/lib/code/node.rb DELETED
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- def initialize(parsed)
4
- if parsed.any?
5
- raise NotImplementedError.new(self.class.name + ": " + parsed.inspect)
6
- end
7
- end
8
-
9
- def evaluate(**args)
10
- raise NotImplementedError.new(self.class.name + "#evaluate")
11
- end
12
- end
13
- end
@@ -1,32 +0,0 @@
1
- class Code
2
- class Object
3
- class Argument
4
- attr_reader :value, :name
5
-
6
- def initialize(value, name: nil)
7
- @value = value
8
- @name = name
9
- end
10
-
11
- def regular?
12
- !name
13
- end
14
-
15
- def keyword?
16
- !regular?
17
- end
18
-
19
- def name_value
20
- [name, value]
21
- end
22
-
23
- def to_s
24
- "<Argument #{value.inspect}>"
25
- end
26
-
27
- def inspect
28
- to_s
29
- end
30
- end
31
- end
32
- end
@@ -1,27 +0,0 @@
1
- class Code
2
- class Object
3
- class Boolean < ::Code::Object
4
- attr_reader :raw
5
-
6
- def initialize(raw)
7
- @raw = raw
8
- end
9
-
10
- def truthy?
11
- raw
12
- end
13
-
14
- def succ
15
- ::Code::Object::Boolean.new(!raw)
16
- end
17
-
18
- def to_s
19
- raw.to_s
20
- end
21
-
22
- def inspect
23
- to_s
24
- end
25
- end
26
- end
27
- end
@@ -1,162 +0,0 @@
1
- class Code
2
- class Object
3
- class Decimal < ::Code::Object::Number
4
- attr_reader :raw
5
-
6
- def initialize(decimal, exponent: nil)
7
- @raw = BigDecimal(decimal)
8
-
9
- if exponent
10
- if exponent.is_a?(::Code::Object::Number)
11
- @raw = @raw * 10**exponent.raw
12
- else
13
- raise ::Code::Error::TypeError.new("exponent is not a number")
14
- end
15
- end
16
- end
17
-
18
- def call(**args)
19
- operator = args.fetch(:operator, nil)
20
- arguments = args.fetch(:arguments, [])
21
- value = arguments.first&.value
22
-
23
- if operator == "%"
24
- sig(arguments) { ::Code::Object::Number }
25
- modulo(value)
26
- elsif operator == "+"
27
- if value
28
- sig(arguments) { ::Code::Object }
29
- plus(value)
30
- else
31
- sig(arguments)
32
- self
33
- end
34
- elsif operator == "-"
35
- if value
36
- sig(arguments) { ::Code::Object::Number }
37
- minus(value)
38
- else
39
- sig(arguments)
40
- unary_minus
41
- end
42
- elsif operator == "/"
43
- sig(arguments) { ::Code::Object::Number }
44
- division(value)
45
- elsif operator == "*"
46
- sig(arguments) { ::Code::Object::Number }
47
- multiplication(value)
48
- elsif operator == "**"
49
- sig(arguments) { ::Code::Object::Number }
50
- power(value)
51
- elsif operator == "<"
52
- sig(arguments) { ::Code::Object::Number }
53
- inferior(value)
54
- elsif operator == "<="
55
- sig(arguments) { ::Code::Object::Number }
56
- inferior_or_equal(value)
57
- elsif operator == ">"
58
- sig(arguments) { ::Code::Object::Number }
59
- superior(value)
60
- elsif operator == ">="
61
- sig(arguments) { ::Code::Object::Number }
62
- superior_or_equal(value)
63
- elsif operator == "<<"
64
- sig(arguments) { ::Code::Object::Number }
65
- left_shift(value)
66
- elsif operator == ">>"
67
- sig(arguments) { ::Code::Object::Number }
68
- right_shift(value)
69
- elsif operator == "&"
70
- sig(arguments) { ::Code::Object::Number }
71
- bitwise_and(value)
72
- elsif operator == "|"
73
- sig(arguments) { ::Code::Object::Number }
74
- bitwise_or(value)
75
- elsif operator == "^"
76
- sig(arguments) { ::Code::Object::Number }
77
- bitwise_xor(value)
78
- else
79
- super
80
- end
81
- end
82
-
83
- def to_s
84
- raw.to_s("F")
85
- end
86
-
87
- def inspect
88
- to_s
89
- end
90
-
91
- private
92
-
93
- def modulo(other)
94
- ::Code::Object::Decimal.new(raw % other.raw)
95
- end
96
-
97
- def plus(other)
98
- if other.is_a?(::Code::Object::Number)
99
- ::Code::Object::Decimal.new(raw + other.raw)
100
- else
101
- ::Code::Object::String.new(to_s + other.to_s)
102
- end
103
- end
104
-
105
- def minus(other)
106
- ::Code::Object::Decimal.new(raw - other.raw)
107
- end
108
-
109
- def division(other)
110
- ::Code::Object::Decimal.new(raw / other.raw)
111
- end
112
-
113
- def multiplication(other)
114
- ::Code::Object::Decimal.new(raw * other.raw)
115
- end
116
-
117
- def power(other)
118
- ::Code::Object::Decimal.new(raw**other.raw)
119
- end
120
-
121
- def inferior(other)
122
- ::Code::Object::Boolean.new(raw < other.raw)
123
- end
124
-
125
- def inferior_or_equal(other)
126
- ::Code::Object::Boolean.new(raw <= other.raw)
127
- end
128
-
129
- def superior(other)
130
- ::Code::Object::Boolean.new(raw > other.raw)
131
- end
132
-
133
- def superior_or_equal(other)
134
- ::Code::Object::Boolean.new(raw >= other.raw)
135
- end
136
-
137
- def left_shift(other)
138
- ::Code::Object::Integer.new(raw.to_i << other.raw.to_i)
139
- end
140
-
141
- def right_shift(other)
142
- ::Code::Object::Integer.new(raw.to_i >> other.raw.to_i)
143
- end
144
-
145
- def bitwise_and(other)
146
- ::Code::Object::Integer.new(raw.to_i & other.raw.to_i)
147
- end
148
-
149
- def bitwise_or(other)
150
- ::Code::Object::Integer.new(raw.to_i | other.raw.to_i)
151
- end
152
-
153
- def bitwise_xor(other)
154
- ::Code::Object::Integer.new(raw.to_i ^ other.raw.to_i)
155
- end
156
-
157
- def unary_minus
158
- ::Code::Object::Decimal.new(-raw)
159
- end
160
- end
161
- end
162
- end
@@ -1,96 +0,0 @@
1
- class Code
2
- class Object
3
- class Dictionnary < ::Code::Object
4
- attr_reader :raw
5
-
6
- def initialize(raw = {})
7
- @raw = raw
8
- end
9
-
10
- def call(**args)
11
- operator = args.fetch(:operator, nil)
12
- arguments = args.fetch(:arguments, [])
13
- globals = multi_fetch(args, *::Code::GLOBALS)
14
- value = arguments.first&.value
15
-
16
- if operator == "values"
17
- sig(arguments)
18
- values
19
- elsif operator == "keys"
20
- sig(arguments)
21
- keys
22
- elsif operator == "each"
23
- sig(arguments) { ::Code::Object::Function }
24
- each(value, **globals)
25
- elsif key?(operator)
26
- result = fetch(operator)
27
-
28
- if result.is_a?(::Code::Object::Function)
29
- result.call(**args.merge(operator: nil))
30
- else
31
- sig(arguments)
32
- result
33
- end
34
- else
35
- super
36
- end
37
- end
38
-
39
- def merge(other)
40
- ::Code::Object::Dictionnary.new(raw.merge(other.raw))
41
- end
42
-
43
- def fetch(key)
44
- raw.fetch(key)
45
- end
46
-
47
- def [](key)
48
- raw[key]
49
- end
50
-
51
- def []=(key, value)
52
- raw[key] = value
53
- end
54
-
55
- def key?(key)
56
- raw.key?(key)
57
- end
58
-
59
- def deep_dup
60
- ::Code::Object::Dictionnary.new(raw.deep_dup)
61
- end
62
-
63
- def to_s
64
- "{#{raw.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")}}"
65
- end
66
-
67
- def inspect
68
- to_s
69
- end
70
-
71
- private
72
-
73
- def keys
74
- ::Code::Object::List.new(raw.keys)
75
- end
76
-
77
- def values
78
- ::Code::Object::List.new(raw.values)
79
- end
80
-
81
- def each(argument, **globals)
82
- raw.each do |key, value|
83
- argument.call(
84
- arguments: [
85
- ::Code::Object::Argument.new(key),
86
- ::Code::Object::Argument.new(value)
87
- ],
88
- **globals
89
- )
90
- end
91
-
92
- self
93
- end
94
- end
95
- end
96
- end