language-ruby 0.5.6 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -4
- data/language-ruby.gemspec +3 -3
- data/lib/language/atom/any.rb +8 -0
- data/lib/language/atom/repeat.rb +15 -0
- data/lib/language/atom.rb +25 -42
- data/lib/language/output.rb +13 -11
- data/lib/language/parser/absent/present.rb +2 -0
- data/lib/language/parser/absent.rb +2 -0
- data/lib/language/parser/end_of_input.rb +2 -0
- data/lib/language/parser/interuption.rb +2 -0
- data/lib/language/parser/not_end_of_input.rb +2 -0
- data/lib/language/parser/str/not_found.rb +2 -0
- data/lib/language/parser/str.rb +2 -0
- data/lib/language/parser.rb +10 -10
- data/lib/language/version.rb +3 -0
- data/lib/language-ruby.rb +0 -5
- data/lib/language.rb +9 -7
- data/spec/spec_helper.rb +1 -6
- metadata +7 -221
- data/.cherry.js +0 -21
- data/.editorconfig +0 -9
- data/.github/workflows/rspec.yml +0 -14
- data/.gitignore +0 -2
- data/.prettierrc +0 -3
- data/.rspec +0 -1
- data/.tool-versions +0 -1
- data/CHANGELOG.md +0 -55
- data/Gemfile.lock +0 -49
- data/LICENSE +0 -7
- data/README.md +0 -103
- data/TODO +0 -17
- data/bin/code +0 -76
- data/bin/format +0 -3
- data/bin/publish +0 -19
- data/bin/template +0 -85
- data/bin/test +0 -17
- data/code-ruby.gemspec +0 -17
- data/docs/class.code +0 -9
- data/docs/euler/1.template +0 -10
- data/docs/euler/2.template +0 -16
- data/docs/euler/3.template +0 -16
- data/docs/euler/4.template +0 -10
- data/docs/euler/5.template +0 -13
- data/docs/fibonnaci.template +0 -14
- data/docs/meetup.code +0 -12
- data/docs/precedence.template +0 -36
- data/docs/rain.code +0 -22
- data/docs/slack.code +0 -17
- data/docs/stripe.code +0 -7
- data/docs/twitter.code +0 -9
- data/lib/code/error.rb +0 -18
- data/lib/code/node/base_10.rb +0 -29
- data/lib/code/node/base_16.rb +0 -13
- data/lib/code/node/base_2.rb +0 -13
- data/lib/code/node/base_8.rb +0 -13
- data/lib/code/node/boolean.rb +0 -22
- data/lib/code/node/call.rb +0 -47
- data/lib/code/node/call_argument.rb +0 -21
- data/lib/code/node/chained_call.rb +0 -23
- data/lib/code/node/code.rb +0 -20
- data/lib/code/node/decimal.rb +0 -26
- data/lib/code/node/dictionnary.rb +0 -33
- data/lib/code/node/equal.rb +0 -34
- data/lib/code/node/function.rb +0 -20
- data/lib/code/node/function_parameter.rb +0 -31
- data/lib/code/node/if.rb +0 -59
- data/lib/code/node/if_modifier.rb +0 -47
- data/lib/code/node/list.rb +0 -16
- data/lib/code/node/negation.rb +0 -15
- data/lib/code/node/not.rb +0 -15
- data/lib/code/node/nothing.rb +0 -12
- data/lib/code/node/number.rb +0 -25
- data/lib/code/node/operation.rb +0 -38
- data/lib/code/node/power.rb +0 -20
- data/lib/code/node/rescue.rb +0 -17
- data/lib/code/node/splat.rb +0 -15
- data/lib/code/node/statement.rb +0 -59
- data/lib/code/node/string.rb +0 -53
- data/lib/code/node/ternary.rb +0 -24
- data/lib/code/node/unary_minus.rb +0 -15
- data/lib/code/node/while.rb +0 -35
- data/lib/code/node.rb +0 -13
- data/lib/code/object/argument.rb +0 -32
- data/lib/code/object/boolean.rb +0 -27
- data/lib/code/object/decimal.rb +0 -162
- data/lib/code/object/dictionnary.rb +0 -96
- data/lib/code/object/function.rb +0 -64
- data/lib/code/object/global.rb +0 -42
- data/lib/code/object/integer.rb +0 -221
- data/lib/code/object/list.rb +0 -207
- data/lib/code/object/nothing.rb +0 -23
- data/lib/code/object/number.rb +0 -6
- data/lib/code/object/range.rb +0 -146
- data/lib/code/object/ruby_function.rb +0 -31
- data/lib/code/object/string.rb +0 -102
- data/lib/code/object.rb +0 -197
- data/lib/code/parser/addition.rb +0 -21
- data/lib/code/parser/and_operator.rb +0 -17
- data/lib/code/parser/bitwise_and.rb +0 -17
- data/lib/code/parser/bitwise_or.rb +0 -21
- data/lib/code/parser/boolean.rb +0 -17
- data/lib/code/parser/call.rb +0 -122
- data/lib/code/parser/chained_call.rb +0 -47
- data/lib/code/parser/class.rb +0 -45
- data/lib/code/parser/code.rb +0 -25
- data/lib/code/parser/dictionnary.rb +0 -67
- data/lib/code/parser/equal.rb +0 -94
- data/lib/code/parser/equality.rb +0 -35
- data/lib/code/parser/equality_lower.rb +0 -9
- data/lib/code/parser/function.rb +0 -85
- data/lib/code/parser/greater.rb +0 -25
- data/lib/code/parser/group.rb +0 -22
- data/lib/code/parser/if.rb +0 -63
- data/lib/code/parser/if_modifier.rb +0 -55
- data/lib/code/parser/list.rb +0 -42
- data/lib/code/parser/multiplication.rb +0 -25
- data/lib/code/parser/name.rb +0 -107
- data/lib/code/parser/negation.rb +0 -30
- data/lib/code/parser/not_keyword.rb +0 -23
- data/lib/code/parser/nothing.rb +0 -22
- data/lib/code/parser/number.rb +0 -154
- data/lib/code/parser/operation.rb +0 -35
- data/lib/code/parser/or_keyword.rb +0 -21
- data/lib/code/parser/or_operator.rb +0 -17
- data/lib/code/parser/power.rb +0 -43
- data/lib/code/parser/range.rb +0 -17
- data/lib/code/parser/rescue.rb +0 -39
- data/lib/code/parser/shift.rb +0 -21
- data/lib/code/parser/splat.rb +0 -31
- data/lib/code/parser/statement.rb +0 -9
- data/lib/code/parser/string.rb +0 -78
- data/lib/code/parser/ternary.rb +0 -46
- data/lib/code/parser/unary_minus.rb +0 -31
- data/lib/code/parser/while.rb +0 -36
- data/lib/code/parser/whitespace.rb +0 -49
- data/lib/code/parser.rb +0 -19
- data/lib/code/ruby.rb +0 -174
- data/lib/code-ruby.rb +0 -10
- data/lib/code.rb +0 -47
- data/lib/template/node/code_part.rb +0 -13
- data/lib/template/node/part.rb +0 -19
- data/lib/template/node/template.rb +0 -15
- data/lib/template/node/text_part.rb +0 -13
- data/lib/template/node.rb +0 -4
- data/lib/template/parser/template.rb +0 -39
- data/lib/template/parser.rb +0 -19
- data/lib/template/version.rb +0 -3
- data/lib/template-ruby.rb +0 -10
- data/lib/template.rb +0 -50
- data/spec/code/addition_spec.rb +0 -13
- data/spec/code/and_operator_spec.rb +0 -13
- data/spec/code/bitwise_and_spec.rb +0 -13
- data/spec/code/bitwise_or_spec.rb +0 -13
- data/spec/code/boolean_spec.rb +0 -13
- data/spec/code/call_spec.rb +0 -21
- data/spec/code/chained_call_spec.rb +0 -16
- data/spec/code/code_spec.rb +0 -29
- data/spec/code/dictionnary_spec.rb +0 -17
- data/spec/code/equal_spec.rb +0 -26
- data/spec/code/equality_spec.rb +0 -13
- data/spec/code/function_spec.rb +0 -18
- data/spec/code/greater_spec.rb +0 -18
- data/spec/code/group_spec.rb +0 -12
- data/spec/code/if_modifier_spec.rb +0 -20
- data/spec/code/if_spec.rb +0 -25
- data/spec/code/list_spec.rb +0 -19
- data/spec/code/multiplication_spec.rb +0 -18
- data/spec/code/negation_spec.rb +0 -20
- data/spec/code/not_keyword_spec.rb +0 -13
- data/spec/code/nothing_spec.rb +0 -17
- data/spec/code/number_spec.rb +0 -22
- data/spec/code/or_keyword_spec.rb +0 -17
- data/spec/code/or_operator_spec.rb +0 -16
- data/spec/code/parser/boolean_spec.rb +0 -16
- data/spec/code/parser/call_spec.rb +0 -26
- data/spec/code/parser/chained_call.rb +0 -17
- data/spec/code/parser/dictionnary_spec.rb +0 -18
- data/spec/code/parser/function_spec.rb +0 -16
- data/spec/code/parser/group_spec.rb +0 -18
- data/spec/code/parser/list_spec.rb +0 -18
- data/spec/code/parser/number_spec.rb +0 -12
- data/spec/code/parser/string_spec.rb +0 -21
- data/spec/code/parser_spec.rb +0 -23
- data/spec/code/power_spec.rb +0 -13
- data/spec/code/range_spec.rb +0 -16
- data/spec/code/rescue_spec.rb +0 -13
- data/spec/code/shift_spec.rb +0 -13
- data/spec/code/splat_spec.rb +0 -13
- data/spec/code/string_spec.rb +0 -27
- data/spec/code/ternary_spec.rb +0 -18
- data/spec/code/unary_minus_spec.rb +0 -13
- data/spec/code/while_spec.rb +0 -18
- data/template-ruby.gemspec +0 -19
data/lib/code/object/decimal.rb
DELETED
@@ -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
|
data/lib/code/object/function.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Object
|
3
|
-
class Function < ::Code::Object
|
4
|
-
def initialize(parameters:, body:)
|
5
|
-
@parameters = parameters
|
6
|
-
@body = body
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(**args)
|
10
|
-
operator = args.fetch(:operator, nil)
|
11
|
-
arguments = args.fetch(:arguments, [])
|
12
|
-
globals = multi_fetch(args, *::Code::GLOBALS)
|
13
|
-
|
14
|
-
if operator.nil? || operator == "call"
|
15
|
-
call_function(args: arguments, globals: globals)
|
16
|
-
else
|
17
|
-
super
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_s
|
22
|
-
""
|
23
|
-
end
|
24
|
-
|
25
|
-
def inspect
|
26
|
-
"function"
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
attr_reader :parameters, :body
|
32
|
-
|
33
|
-
def call_function(args:, globals:)
|
34
|
-
new_context = deep_dup(globals[:context])
|
35
|
-
|
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
|
-
)
|
42
|
-
elsif parameter.keyword_splat?
|
43
|
-
new_context[parameter.name] = ::Code::Object::Dictionnary.new(
|
44
|
-
args.select(&:keyword?).map(&:name_value).to_h
|
45
|
-
)
|
46
|
-
else
|
47
|
-
arg = args[index]&.value
|
48
|
-
arg = parameter.evaluate(**globals) if arg.nil?
|
49
|
-
new_context[parameter.name] = arg
|
50
|
-
end
|
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
|
-
else
|
56
|
-
raise NotImplementedError
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
body.evaluate(**globals, context: new_context)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
data/lib/code/object/global.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Object
|
3
|
-
class Global < ::Code::Object
|
4
|
-
def call(**args)
|
5
|
-
operator = args.fetch(:operator, nil)
|
6
|
-
arguments = args.fetch(:arguments, [])
|
7
|
-
context = args.fetch(:context)
|
8
|
-
io = args.fetch(:io)
|
9
|
-
globals = multi_fetch(args, *::Code::GLOBALS)
|
10
|
-
value = arguments.first&.value
|
11
|
-
|
12
|
-
if operator == "print"
|
13
|
-
io.print(*arguments.map(&:value))
|
14
|
-
::Code::Object::Nothing.new
|
15
|
-
elsif operator == "puts"
|
16
|
-
io.puts(*arguments.map(&:value))
|
17
|
-
::Code::Object::Nothing.new
|
18
|
-
elsif operator == "context"
|
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)
|
24
|
-
else
|
25
|
-
result = context[operator]
|
26
|
-
|
27
|
-
if result && result.is_a?(::Code::Object::Function)
|
28
|
-
result.call(**args.merge(operator: nil))
|
29
|
-
elsif result
|
30
|
-
result
|
31
|
-
else
|
32
|
-
raise ::Code::Error::Undefined.new("#{operator} is not defined")
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def to_s
|
38
|
-
"global"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
data/lib/code/object/integer.rb
DELETED
@@ -1,221 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Object
|
3
|
-
class Integer < ::Code::Object::Number
|
4
|
-
attr_reader :raw
|
5
|
-
|
6
|
-
def initialize(whole, exponent: nil)
|
7
|
-
@raw = whole.to_i
|
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
|
-
globals = multi_fetch(args, *::Code::GLOBALS)
|
22
|
-
value = arguments.first&.value
|
23
|
-
|
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)
|
88
|
-
else
|
89
|
-
super
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def succ
|
94
|
-
::Code::Object::Integer.new(raw + 1)
|
95
|
-
end
|
96
|
-
|
97
|
-
def +(other)
|
98
|
-
::Code::Object::Integer.new(raw + other.raw)
|
99
|
-
end
|
100
|
-
|
101
|
-
def to_s
|
102
|
-
raw.to_s
|
103
|
-
end
|
104
|
-
|
105
|
-
def inspect
|
106
|
-
to_s
|
107
|
-
end
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
def even?
|
112
|
-
::Code::Object::Boolean.new(raw.even?)
|
113
|
-
end
|
114
|
-
|
115
|
-
def odd?
|
116
|
-
::Code::Object::Boolean.new(raw.odd?)
|
117
|
-
end
|
118
|
-
|
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)
|
124
|
-
else
|
125
|
-
::Code::Object::String.new(other.raw * raw)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
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)
|
134
|
-
else
|
135
|
-
::Code::Object::String.new(to_s + other.to_s)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def division(other)
|
140
|
-
::Code::Object::Decimal.new(BigDecimal(raw) / other.raw)
|
141
|
-
end
|
142
|
-
|
143
|
-
def modulo(other)
|
144
|
-
if other.is_a?(::Code::Object::Integer)
|
145
|
-
::Code::Object::Integer.new(raw % other.raw)
|
146
|
-
else
|
147
|
-
::Code::Object::Decimal.new(raw % other.raw)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def minus(other)
|
152
|
-
if other.is_a?(::Code::Object::Integer)
|
153
|
-
::Code::Object::Integer.new(raw - other.raw)
|
154
|
-
else
|
155
|
-
::Code::Object::Decimal.new(raw - other.raw)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
def unary_minus
|
160
|
-
::Code::Object::Integer.new(-raw)
|
161
|
-
end
|
162
|
-
|
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
|
169
|
-
end
|
170
|
-
|
171
|
-
def inferior(other)
|
172
|
-
::Code::Object::Boolean.new(raw < other.raw)
|
173
|
-
end
|
174
|
-
|
175
|
-
def inferior_or_equal(other)
|
176
|
-
::Code::Object::Boolean.new(raw <= other.raw)
|
177
|
-
end
|
178
|
-
|
179
|
-
def superior(other)
|
180
|
-
::Code::Object::Boolean.new(raw > other.raw)
|
181
|
-
end
|
182
|
-
|
183
|
-
def superior_or_equal(other)
|
184
|
-
::Code::Object::Boolean.new(raw >= other.raw)
|
185
|
-
end
|
186
|
-
|
187
|
-
def left_shift(other)
|
188
|
-
::Code::Object::Integer.new(raw << other.raw.to_i)
|
189
|
-
end
|
190
|
-
|
191
|
-
def right_shift(other)
|
192
|
-
::Code::Object::Integer.new(raw >> other.raw.to_i)
|
193
|
-
end
|
194
|
-
|
195
|
-
def bitwise_and(other)
|
196
|
-
::Code::Object::Integer.new(raw & other.raw.to_i)
|
197
|
-
end
|
198
|
-
|
199
|
-
def bitwise_or(other)
|
200
|
-
::Code::Object::Integer.new(raw | other.raw.to_i)
|
201
|
-
end
|
202
|
-
|
203
|
-
def bitwise_xor(other)
|
204
|
-
::Code::Object::Integer.new(raw ^ other.raw.to_i)
|
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
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|