code-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 (218) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -3
  3. data/Gemfile.lock +11 -21
  4. data/README.md +2 -102
  5. data/bin/code +34 -16
  6. data/code-ruby.gemspec +5 -3
  7. data/lib/code/error.rb +16 -5
  8. data/lib/code/node/base_10.rb +4 -2
  9. data/lib/code/node/base_16.rb +3 -1
  10. data/lib/code/node/base_2.rb +3 -1
  11. data/lib/code/node/base_8.rb +3 -1
  12. data/lib/code/node/boolean.rb +4 -2
  13. data/lib/code/node/call.rb +35 -12
  14. data/lib/code/node/call_argument.rb +16 -5
  15. data/lib/code/node/code.rb +5 -4
  16. data/lib/code/node/decimal.rb +2 -0
  17. data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
  18. data/lib/code/node/function.rb +7 -4
  19. data/lib/code/node/function_parameter.rb +3 -1
  20. data/lib/code/node/if.rb +5 -3
  21. data/lib/code/node/left_operation.rb +63 -0
  22. data/lib/code/node/list.rb +2 -0
  23. data/lib/code/node/negation.rb +2 -0
  24. data/lib/code/node/not.rb +2 -0
  25. data/lib/code/node/nothing.rb +3 -1
  26. data/lib/code/node/number.rb +2 -0
  27. data/lib/code/node/right_operation.rb +70 -0
  28. data/lib/code/node/splat.rb +2 -0
  29. data/lib/code/node/square_bracket.rb +37 -0
  30. data/lib/code/node/statement.rb +13 -5
  31. data/lib/code/node/string.rb +3 -1
  32. data/lib/code/node/ternary.rb +5 -3
  33. data/lib/code/node/unary_minus.rb +2 -0
  34. data/lib/code/node/while.rb +6 -4
  35. data/lib/code/node.rb +11 -5
  36. data/lib/code/object/argument.rb +13 -7
  37. data/lib/code/object/boolean.rb +43 -5
  38. data/lib/code/object/class.rb +17 -0
  39. data/lib/code/object/context.rb +36 -0
  40. data/lib/code/object/decimal.rb +252 -100
  41. data/lib/code/object/dictionary.rb +641 -0
  42. data/lib/code/object/function.rb +54 -27
  43. data/lib/code/object/global.rb +65 -19
  44. data/lib/code/object/identifier_list.rb +47 -0
  45. data/lib/code/object/integer.rb +320 -137
  46. data/lib/code/object/list.rb +140 -138
  47. data/lib/code/object/nothing.rb +10 -4
  48. data/lib/code/object/number.rb +6 -1
  49. data/lib/code/object/range.rb +85 -88
  50. data/lib/code/object/ruby_function.rb +11 -6
  51. data/lib/code/object/string.rb +51 -48
  52. data/lib/code/object.rb +117 -139
  53. data/lib/code/parser/addition.rb +4 -2
  54. data/lib/code/parser/and_operator.rb +4 -2
  55. data/lib/code/parser/bitwise_and.rb +4 -2
  56. data/lib/code/parser/bitwise_or.rb +4 -2
  57. data/lib/code/parser/boolean.rb +3 -1
  58. data/lib/code/parser/call.rb +17 -11
  59. data/lib/code/parser/chained_call.rb +10 -22
  60. data/lib/code/parser/class.rb +9 -6
  61. data/lib/code/parser/code.rb +6 -4
  62. data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
  63. data/lib/code/parser/equal.rb +9 -36
  64. data/lib/code/parser/equality.rb +4 -2
  65. data/lib/code/parser/function.rb +24 -9
  66. data/lib/code/parser/greater.rb +6 -3
  67. data/lib/code/parser/group.rb +4 -2
  68. data/lib/code/parser/if.rb +6 -4
  69. data/lib/code/parser/if_modifier.rb +5 -25
  70. data/lib/code/parser/left_operation.rb +40 -0
  71. data/lib/code/parser/list.rb +6 -5
  72. data/lib/code/parser/multiplication.rb +4 -2
  73. data/lib/code/parser/name.rb +19 -4
  74. data/lib/code/parser/negation.rb +4 -2
  75. data/lib/code/parser/not_keyword.rb +5 -3
  76. data/lib/code/parser/nothing.rb +3 -10
  77. data/lib/code/parser/number.rb +4 -2
  78. data/lib/code/parser/or_keyword.rb +4 -2
  79. data/lib/code/parser/or_operator.rb +4 -2
  80. data/lib/code/parser/power.rb +4 -28
  81. data/lib/code/parser/range.rb +4 -2
  82. data/lib/code/parser/rescue.rb +6 -26
  83. data/lib/code/parser/right_operation.rb +40 -0
  84. data/lib/code/parser/shift.rb +4 -2
  85. data/lib/code/parser/splat.rb +5 -3
  86. data/lib/code/parser/square_bracket.rb +48 -0
  87. data/lib/code/parser/statement.rb +3 -1
  88. data/lib/code/parser/string.rb +12 -10
  89. data/lib/code/parser/ternary.rb +10 -11
  90. data/lib/code/parser/unary_minus.rb +5 -3
  91. data/lib/code/parser/while.rb +5 -3
  92. data/lib/code/parser/whitespace.rb +2 -0
  93. data/lib/code/parser.rb +10 -3
  94. data/lib/code/ruby.rb +4 -2
  95. data/lib/code/type/hash.rb +38 -0
  96. data/lib/code/type/maybe.rb +29 -0
  97. data/lib/code/type/or.rb +38 -0
  98. data/lib/code/type/repeat.rb +38 -0
  99. data/lib/code/type/sig.rb +130 -0
  100. data/lib/code/type.rb +25 -0
  101. data/lib/code/version.rb +3 -0
  102. data/lib/code-ruby.rb +1 -2
  103. data/lib/code.rb +15 -16
  104. data/spec/code/node/call_spec.rb +39 -0
  105. data/spec/code/object/boolean_spec.rb +18 -0
  106. data/spec/code/object/decimal_spec.rb +51 -0
  107. data/spec/code/object/dictionary_spec.rb +98 -0
  108. data/spec/code/object/function_spec.rb +42 -0
  109. data/spec/code/object/integer_spec.rb +43 -0
  110. data/spec/code/object/nothing_spec.rb +14 -0
  111. data/spec/code/object/range_spec.rb +23 -0
  112. data/spec/code/parser/boolean_spec.rb +5 -10
  113. data/spec/code/parser/chained_call.rb +4 -5
  114. data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
  115. data/spec/code/parser/function_spec.rb +4 -5
  116. data/spec/code/parser/group_spec.rb +5 -12
  117. data/spec/code/parser/if_modifier_spec.rb +18 -0
  118. data/spec/code/parser/list_spec.rb +4 -5
  119. data/spec/code/parser/number_spec.rb +4 -5
  120. data/spec/code/parser/string_spec.rb +4 -5
  121. data/spec/code/parser_spec.rb +22 -16
  122. data/spec/code/type_spec.rb +21 -0
  123. data/spec/code_spec.rb +171 -0
  124. data/spec/spec_helper.rb +1 -6
  125. metadata +63 -136
  126. data/.cherry.js +0 -21
  127. data/.editorconfig +0 -9
  128. data/.github/workflows/rspec.yml +0 -14
  129. data/.gitignore +0 -2
  130. data/.prettierrc +0 -3
  131. data/.tool-versions +0 -1
  132. data/CHANGELOG.md +0 -55
  133. data/LICENSE +0 -7
  134. data/TODO +0 -17
  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,221 +1,404 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Object
3
- class Integer < ::Code::Object::Number
5
+ class Integer < Number
4
6
  attr_reader :raw
5
7
 
6
8
  def initialize(whole, exponent: nil)
7
9
  @raw = whole.to_i
8
10
 
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
11
+ return unless exponent
12
+
13
+ unless exponent.is_a?(Number)
14
+ raise Code::Error::TypeError, "exponent is not a number"
15
15
  end
16
+
17
+ @raw *= 10**exponent.raw
18
+ end
19
+
20
+ def self.name
21
+ "Integer"
16
22
  end
17
23
 
18
24
  def call(**args)
19
25
  operator = args.fetch(:operator, nil)
20
26
  arguments = args.fetch(:arguments, [])
21
- globals = multi_fetch(args, *::Code::GLOBALS)
27
+ globals = multi_fetch(args, *GLOBALS)
22
28
  value = arguments.first&.value
23
29
 
24
- if operator == "even?"
25
- sig(arguments)
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)
33
- elsif operator == "*"
34
- sig(arguments) { [[::Code::Object::Number, ::Code::Object::String]] }
35
- multiplication(value)
36
- elsif operator == "/"
37
- sig(arguments) { ::Code::Object::Number }
38
- division(value)
39
- elsif operator == "+"
40
- if value
41
- sig(arguments) { ::Code::Object }
42
- plus(value)
43
- else
44
- sig(arguments)
45
- self
46
- end
47
- elsif operator == "%"
48
- sig(arguments) { ::Code::Object::Number }
49
- modulo(value)
50
- elsif operator == "-"
51
- if value
52
- sig(arguments) { ::Code::Object::Number }
53
- minus(value)
54
- else
55
- sig(arguments)
56
- unary_minus
57
- end
58
- elsif operator == "**"
59
- sig(arguments) { ::Code::Object::Number }
60
- power(value)
61
- elsif operator == "<"
62
- sig(arguments) { ::Code::Object::Number }
63
- inferior(value)
64
- elsif operator == "<="
65
- sig(arguments) { ::Code::Object::Number }
66
- inferior_or_equal(value)
67
- elsif operator == ">"
68
- sig(arguments) { ::Code::Object::Number }
69
- superior(value)
70
- elsif operator == ">="
71
- sig(arguments) { ::Code::Object::Number }
72
- superior_or_equal(value)
73
- elsif operator == "<<"
74
- sig(arguments) { ::Code::Object::Number }
75
- left_shift(value)
76
- elsif operator == ">>"
77
- sig(arguments) { ::Code::Object::Number }
78
- right_shift(value)
79
- elsif operator == "&"
80
- sig(arguments) { ::Code::Object::Number }
81
- bitwise_and(value)
82
- elsif operator == "|"
83
- sig(arguments) { ::Code::Object::Number }
84
- bitwise_or(value)
85
- elsif operator == "^"
86
- sig(arguments) { ::Code::Object::Number }
87
- bitwise_xor(value)
30
+ case operator.to_s
31
+ when "%", "modulo"
32
+ sig(args) { Number }
33
+ code_modulo(value)
34
+ when "&", "bitwise_and"
35
+ sig(args) { Number }
36
+ code_bitwise_and(value)
37
+ when "*", "multiplication"
38
+ sig(args) { Number | String }
39
+ code_multiplication(value)
40
+ when "**", "power"
41
+ sig(args) { Number }
42
+ code_power(value)
43
+ when "+", "plus"
44
+ sig(args) { Object.maybe }
45
+ value ? code_plus(value) : self
46
+ when "+", "plus", "self"
47
+ sig(args) { Object.maybe }
48
+ value ? code_plus(value) : code_self
49
+ when "-", "minus"
50
+ sig(args) { Number.maybe }
51
+ value ? code_minus(value) : code_unary_minus
52
+ when "-", "minus", "unary_minus"
53
+ sig(args) { Number.maybe }
54
+ value ? code_minus(value) : code_unary_minus
55
+ when "/", "division"
56
+ sig(args) { Number }
57
+ code_division(value)
58
+ when "<", "inferior"
59
+ sig(args) { Number }
60
+ code_inferior(value)
61
+ when "<<", "left_shift"
62
+ sig(args) { Number }
63
+ code_left_shift(value)
64
+ when "<=", "inferior_or_equal"
65
+ sig(args) { Number }
66
+ code_inferior_or_equal(value)
67
+ when "<=>", "compare"
68
+ sig(args) { Number }
69
+ code_compare(value)
70
+ when ">", "superior"
71
+ sig(args) { Number }
72
+ code_superior(value)
73
+ when ">=", "superior_or_equal"
74
+ sig(args) { Number }
75
+ code_superior_or_equal(value)
76
+ when ">>", "right_shift"
77
+ sig(args) { Number }
78
+ code_right_shift(value)
79
+ when "^", "bitwise_xor"
80
+ sig(args) { Number }
81
+ code_bitwise_xor(value)
82
+ when "abs"
83
+ sig(args)
84
+ code_abs
85
+ when "ceil"
86
+ sig(args) { Integer.maybe }
87
+ code_ceil(value)
88
+ when "clone"
89
+ sig(args)
90
+ code_clone
91
+ when "decrement!"
92
+ sig(args) { Integer.maybe }
93
+ code_decrement!(value)
94
+ when "decrement"
95
+ sig(args) { Integer.maybe }
96
+ code_decrement(value)
97
+ when "eight?"
98
+ sig(args)
99
+ code_eight?
100
+ when "even?"
101
+ sig(args)
102
+ code_even?
103
+ when "five?"
104
+ sig(args)
105
+ code_five?
106
+ when "floor"
107
+ sig(args) { Integer.maybe }
108
+ code_floor(value)
109
+ when "four?"
110
+ sig(args)
111
+ code_four?
112
+ when "increment!"
113
+ sig(args) { Integer.maybe }
114
+ code_increment!(value)
115
+ when "increment"
116
+ sig(args) { Integer.maybe }
117
+ code_increment(value)
118
+ when "nine?"
119
+ sig(args)
120
+ code_nine?
121
+ when "odd?"
122
+ sig(args)
123
+ code_odd?
124
+ when "one?"
125
+ sig(args)
126
+ code_one?
127
+ when "round"
128
+ sig(args) { Integer.maybe }
129
+ code_round(value)
130
+ when "seven?"
131
+ sig(args)
132
+ code_seven?
133
+ when "six?"
134
+ sig(args)
135
+ code_six?
136
+ when "sqrt"
137
+ sig(args)
138
+ code_sqrt
139
+ when "ten?"
140
+ sig(args)
141
+ code_ten?
142
+ when "three?"
143
+ sig(args)
144
+ code_three?
145
+ when "times"
146
+ sig(args) { Function }
147
+ code_times(value, **globals)
148
+ when "to_decimal"
149
+ sig(args)
150
+ code_to_decimal
151
+ when "to_integer"
152
+ sig(args)
153
+ code_to_integer
154
+ when "to_string"
155
+ sig(args)
156
+ code_to_string
157
+ when "truncate"
158
+ sig(args) { Integer.maybe }
159
+ code_truncate(value)
160
+ when "two?"
161
+ sig(args)
162
+ code_two?
163
+ when "zero?"
164
+ sig(args)
165
+ code_zero?
166
+ when "|", "bitwise_or"
167
+ sig(args) { Number }
168
+ code_bitwise_or(value)
88
169
  else
89
170
  super
90
171
  end
91
172
  end
92
173
 
93
- def succ
94
- ::Code::Object::Integer.new(raw + 1)
174
+ def code_abs
175
+ Decimal.new(raw.abs)
95
176
  end
96
177
 
97
- def +(other)
98
- ::Code::Object::Integer.new(raw + other.raw)
178
+ def code_bitwise_and(other)
179
+ Integer.new(raw & other.raw.to_i)
99
180
  end
100
181
 
101
- def to_s
102
- raw.to_s
182
+ def code_bitwise_or(other)
183
+ Integer.new(raw | other.raw.to_i)
103
184
  end
104
185
 
105
- def inspect
106
- to_s
186
+ def code_bitwise_xor(other)
187
+ Integer.new(raw ^ other.raw.to_i)
188
+ end
189
+
190
+ def code_ceil(n = nil)
191
+ n ||= Integer.new(0)
192
+ Integer.new(raw.ceil(n.raw))
193
+ end
194
+
195
+ def code_clone
196
+ Integer.new(raw)
197
+ end
198
+
199
+ def code_compare(other)
200
+ Integer.new(raw <=> other.raw)
201
+ end
202
+
203
+ def code_decrement!(n = nil)
204
+ n ||= Integer.new(1)
205
+ @raw -= n.raw
206
+ self
207
+ end
208
+
209
+ def code_decrement(n = nil)
210
+ n ||= Integer.new(1)
211
+ Integer.new(raw - n.raw)
212
+ end
213
+
214
+ def code_division(other)
215
+ Decimal.new(BigDecimal(raw) / other.raw)
216
+ end
217
+
218
+ def code_eight?
219
+ Boolean.new(raw == 8)
220
+ end
221
+
222
+ def code_even?
223
+ Boolean.new(raw.even?)
224
+ end
225
+
226
+ def code_five?
227
+ Boolean.new(raw == 5)
228
+ end
229
+
230
+ def code_floor(n = nil)
231
+ n ||= Integer.new(0)
232
+ Integer.new(raw.floor(n.raw))
233
+ end
234
+
235
+ def code_four?
236
+ Boolean.new(raw == 4)
237
+ end
238
+
239
+ def code_increment!(n = nil)
240
+ n ||= Integer.new(1)
241
+ @raw += n.raw
242
+ self
107
243
  end
108
244
 
109
- private
245
+ def code_increment(n = nil)
246
+ n ||= Integer.new(1)
247
+ Integer.new(raw + n.raw)
248
+ end
249
+
250
+ def code_inferior(other)
251
+ Boolean.new(raw < other.raw)
252
+ end
110
253
 
111
- def even?
112
- ::Code::Object::Boolean.new(raw.even?)
254
+ def code_inferior_or_equal(other)
255
+ Boolean.new(raw <= other.raw)
113
256
  end
114
257
 
115
- def odd?
116
- ::Code::Object::Boolean.new(raw.odd?)
258
+ def code_left_shift(other)
259
+ Integer.new(raw << other.raw.to_i)
260
+ end
261
+
262
+ def code_minus(other)
263
+ if other.is_a?(Integer)
264
+ Integer.new(raw - other.raw)
265
+ else
266
+ Decimal.new(raw - other.raw)
267
+ end
117
268
  end
118
269
 
119
- def multiplication(other)
120
- if other.is_a?(::Code::Object::Integer)
121
- ::Code::Object::Integer.new(raw * other.raw)
122
- elsif other.is_a?(::Code::Object::Decimal)
123
- ::Code::Object::Decimal.new(raw * other.raw)
270
+ def code_modulo(other)
271
+ if other.is_a?(Integer)
272
+ Integer.new(raw % other.raw)
124
273
  else
125
- ::Code::Object::String.new(other.raw * raw)
274
+ Decimal.new(raw % other.raw)
126
275
  end
127
276
  end
128
277
 
129
- def plus(other)
130
- if other.is_a?(::Code::Object::Integer)
131
- ::Code::Object::Integer.new(raw + other.raw)
132
- elsif other.is_a?(::Code::Object::Decimal)
133
- ::Code::Object::Decimal.new(raw + other.raw)
278
+ def code_multiplication(other)
279
+ if other.is_a?(Integer)
280
+ Integer.new(raw * other.raw)
281
+ elsif other.is_a?(Decimal)
282
+ Decimal.new(raw * other.raw)
134
283
  else
135
- ::Code::Object::String.new(to_s + other.to_s)
284
+ String.new(other.raw * raw)
136
285
  end
137
286
  end
138
287
 
139
- def division(other)
140
- ::Code::Object::Decimal.new(BigDecimal(raw) / other.raw)
288
+ def code_nine?
289
+ Boolean.new(raw == 9)
141
290
  end
142
291
 
143
- def modulo(other)
144
- if other.is_a?(::Code::Object::Integer)
145
- ::Code::Object::Integer.new(raw % other.raw)
292
+ def code_odd?
293
+ Boolean.new(raw.odd?)
294
+ end
295
+
296
+ def code_one?
297
+ Boolean.new(raw == 1)
298
+ end
299
+
300
+ def code_plus(other)
301
+ if other.is_a?(Integer)
302
+ Integer.new(raw + other.raw)
303
+ elsif other.is_a?(Decimal)
304
+ Decimal.new(raw + other.raw)
146
305
  else
147
- ::Code::Object::Decimal.new(raw % other.raw)
306
+ String.new(to_s + other.to_s)
148
307
  end
149
308
  end
150
309
 
151
- def minus(other)
152
- if other.is_a?(::Code::Object::Integer)
153
- ::Code::Object::Integer.new(raw - other.raw)
310
+ def code_power(other)
311
+ if other.is_a?(Integer)
312
+ Integer.new(raw**other.raw)
154
313
  else
155
- ::Code::Object::Decimal.new(raw - other.raw)
314
+ Decimal.new(raw**other.raw)
156
315
  end
157
316
  end
158
317
 
159
- def unary_minus
160
- ::Code::Object::Integer.new(-raw)
318
+ def code_right_shift(other)
319
+ Integer.new(raw >> other.raw.to_i)
161
320
  end
162
321
 
163
- def power(other)
164
- if other.is_a?(::Code::Object::Integer)
165
- ::Code::Object::Integer.new(raw**other.raw)
166
- else
167
- ::Code::Object::Decimal.new(raw**other.raw)
168
- end
322
+ def code_round(n = nil)
323
+ n ||= Integer.new(0)
324
+ Integer.new(raw.round(n.raw))
169
325
  end
170
326
 
171
- def inferior(other)
172
- ::Code::Object::Boolean.new(raw < other.raw)
327
+ def code_seven?
328
+ Boolean.new(raw == 7)
173
329
  end
174
330
 
175
- def inferior_or_equal(other)
176
- ::Code::Object::Boolean.new(raw <= other.raw)
331
+ def code_six?
332
+ Boolean.new(raw == 6)
177
333
  end
178
334
 
179
- def superior(other)
180
- ::Code::Object::Boolean.new(raw > other.raw)
335
+ def code_sqrt
336
+ Decimal.new(Math.sqrt(raw).to_s)
181
337
  end
182
338
 
183
- def superior_or_equal(other)
184
- ::Code::Object::Boolean.new(raw >= other.raw)
339
+ def code_superior(other)
340
+ Boolean.new(raw > other.raw)
185
341
  end
186
342
 
187
- def left_shift(other)
188
- ::Code::Object::Integer.new(raw << other.raw.to_i)
343
+ def code_superior_or_equal(other)
344
+ Boolean.new(raw >= other.raw)
189
345
  end
190
346
 
191
- def right_shift(other)
192
- ::Code::Object::Integer.new(raw >> other.raw.to_i)
347
+ def code_ten?
348
+ Boolean.new(raw == 10)
193
349
  end
194
350
 
195
- def bitwise_and(other)
196
- ::Code::Object::Integer.new(raw & other.raw.to_i)
351
+ def code_three?
352
+ Boolean.new(raw == 3)
197
353
  end
198
354
 
199
- def bitwise_or(other)
200
- ::Code::Object::Integer.new(raw | other.raw.to_i)
355
+ def code_to_decimal
356
+ Decimal.new(raw)
201
357
  end
202
358
 
203
- def bitwise_xor(other)
204
- ::Code::Object::Integer.new(raw ^ other.raw.to_i)
359
+ def code_to_integer
360
+ Integer.new(raw)
205
361
  end
206
362
 
207
- def times(argument, **globals)
363
+ def code_times(argument, **globals)
208
364
  raw.times do |element|
209
365
  argument.call(
210
- arguments: [
211
- ::Code::Object::Argument.new(::Code::Object::Integer.new(element))
212
- ],
366
+ arguments: [Argument.new(Integer.new(element))],
213
367
  **globals
214
368
  )
215
369
  end
216
370
 
217
371
  self
218
372
  end
373
+
374
+ def code_truncate(n = nil)
375
+ n ||= Integer.new(0)
376
+ Integer.new(raw.truncate(n.raw))
377
+ end
378
+
379
+ def code_two?
380
+ Boolean.new(raw == 2)
381
+ end
382
+
383
+ def code_unary_minus
384
+ Integer.new(-raw)
385
+ end
386
+
387
+ def code_zero?
388
+ Boolean.new(raw.zero?)
389
+ end
390
+
391
+ def inspect
392
+ to_s
393
+ end
394
+
395
+ def succ
396
+ Integer.new(raw + 1)
397
+ end
398
+
399
+ def to_s
400
+ raw.to_s
401
+ end
219
402
  end
220
403
  end
221
404
  end