code-ruby 0.5.5 → 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/Gemfile.lock +10 -22
- data/code-ruby.gemspec +4 -3
- data/lib/code/error.rb +16 -5
- data/lib/code/node/base_10.rb +4 -2
- data/lib/code/node/base_16.rb +3 -1
- data/lib/code/node/base_2.rb +3 -1
- data/lib/code/node/base_8.rb +3 -1
- data/lib/code/node/boolean.rb +4 -2
- data/lib/code/node/call.rb +35 -12
- data/lib/code/node/call_argument.rb +16 -5
- data/lib/code/node/code.rb +5 -4
- data/lib/code/node/decimal.rb +2 -0
- data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
- data/lib/code/node/function.rb +7 -4
- data/lib/code/node/function_parameter.rb +3 -1
- data/lib/code/node/if.rb +5 -3
- data/lib/code/node/left_operation.rb +63 -0
- data/lib/code/node/list.rb +2 -0
- data/lib/code/node/negation.rb +2 -0
- data/lib/code/node/not.rb +2 -0
- data/lib/code/node/nothing.rb +3 -1
- data/lib/code/node/number.rb +2 -0
- data/lib/code/node/right_operation.rb +70 -0
- data/lib/code/node/splat.rb +2 -0
- data/lib/code/node/square_bracket.rb +37 -0
- data/lib/code/node/statement.rb +13 -5
- data/lib/code/node/string.rb +3 -1
- data/lib/code/node/ternary.rb +5 -3
- data/lib/code/node/unary_minus.rb +2 -0
- data/lib/code/node/while.rb +6 -4
- data/lib/code/node.rb +11 -5
- data/lib/code/object/argument.rb +13 -7
- data/lib/code/object/boolean.rb +43 -5
- data/lib/code/object/class.rb +17 -0
- data/lib/code/object/context.rb +36 -0
- data/lib/code/object/decimal.rb +252 -100
- data/lib/code/object/dictionary.rb +641 -0
- data/lib/code/object/function.rb +54 -27
- data/lib/code/object/global.rb +65 -19
- data/lib/code/object/identifier_list.rb +47 -0
- data/lib/code/object/integer.rb +320 -137
- data/lib/code/object/list.rb +140 -138
- data/lib/code/object/nothing.rb +10 -4
- data/lib/code/object/number.rb +6 -1
- data/lib/code/object/range.rb +85 -88
- data/lib/code/object/ruby_function.rb +11 -6
- data/lib/code/object/string.rb +51 -48
- data/lib/code/object.rb +117 -139
- data/lib/code/parser/addition.rb +4 -2
- data/lib/code/parser/and_operator.rb +4 -2
- data/lib/code/parser/bitwise_and.rb +4 -2
- data/lib/code/parser/bitwise_or.rb +4 -2
- data/lib/code/parser/boolean.rb +3 -1
- data/lib/code/parser/call.rb +17 -11
- data/lib/code/parser/chained_call.rb +10 -22
- data/lib/code/parser/class.rb +9 -6
- data/lib/code/parser/code.rb +6 -4
- data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
- data/lib/code/parser/equal.rb +9 -36
- data/lib/code/parser/equality.rb +4 -2
- data/lib/code/parser/function.rb +24 -9
- data/lib/code/parser/greater.rb +6 -3
- data/lib/code/parser/group.rb +4 -2
- data/lib/code/parser/if.rb +6 -4
- data/lib/code/parser/if_modifier.rb +5 -25
- data/lib/code/parser/left_operation.rb +40 -0
- data/lib/code/parser/list.rb +6 -5
- data/lib/code/parser/multiplication.rb +4 -2
- data/lib/code/parser/name.rb +19 -4
- data/lib/code/parser/negation.rb +4 -2
- data/lib/code/parser/not_keyword.rb +5 -3
- data/lib/code/parser/nothing.rb +3 -10
- data/lib/code/parser/number.rb +4 -2
- data/lib/code/parser/or_keyword.rb +4 -2
- data/lib/code/parser/or_operator.rb +4 -2
- data/lib/code/parser/power.rb +4 -28
- data/lib/code/parser/range.rb +4 -2
- data/lib/code/parser/rescue.rb +6 -26
- data/lib/code/parser/right_operation.rb +40 -0
- data/lib/code/parser/shift.rb +4 -2
- data/lib/code/parser/splat.rb +5 -3
- data/lib/code/parser/square_bracket.rb +48 -0
- data/lib/code/parser/statement.rb +3 -1
- data/lib/code/parser/string.rb +12 -10
- data/lib/code/parser/ternary.rb +10 -11
- data/lib/code/parser/unary_minus.rb +5 -3
- data/lib/code/parser/while.rb +5 -3
- data/lib/code/parser/whitespace.rb +2 -0
- data/lib/code/parser.rb +10 -3
- data/lib/code/ruby.rb +4 -2
- data/lib/code/type/hash.rb +38 -0
- data/lib/code/type/maybe.rb +29 -0
- data/lib/code/type/or.rb +38 -0
- data/lib/code/type/repeat.rb +38 -0
- data/lib/code/type/sig.rb +130 -0
- data/lib/code/type.rb +25 -0
- data/lib/code/version.rb +3 -0
- data/lib/code-ruby.rb +1 -2
- data/lib/code.rb +15 -16
- data/spec/code/node/call_spec.rb +39 -0
- data/spec/code/object/boolean_spec.rb +18 -0
- data/spec/code/object/decimal_spec.rb +51 -0
- data/spec/code/object/dictionary_spec.rb +98 -0
- data/spec/code/object/function_spec.rb +42 -0
- data/spec/code/object/integer_spec.rb +43 -0
- data/spec/code/object/nothing_spec.rb +14 -0
- data/spec/code/object/range_spec.rb +23 -0
- data/spec/code/parser/boolean_spec.rb +5 -10
- data/spec/code/parser/chained_call.rb +4 -5
- data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
- data/spec/code/parser/function_spec.rb +4 -5
- data/spec/code/parser/group_spec.rb +5 -12
- data/spec/code/parser/if_modifier_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +4 -5
- data/spec/code/parser/number_spec.rb +4 -5
- data/spec/code/parser/string_spec.rb +4 -5
- data/spec/code/parser_spec.rb +22 -16
- data/spec/code/type_spec.rb +21 -0
- data/spec/code_spec.rb +171 -0
- data/spec/spec_helper.rb +1 -6
- metadata +61 -137
- 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/.tool-versions +0 -1
- data/CHANGELOG.md +0 -55
- 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/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/language-ruby.gemspec +0 -17
- data/lib/code/node/chained_call.rb +0 -23
- data/lib/code/node/equal.rb +0 -34
- data/lib/code/node/if_modifier.rb +0 -47
- 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/object/dictionnary.rb +0 -96
- data/lib/code/parser/equality_lower.rb +0 -9
- data/lib/code/parser/operation.rb +0 -35
- data/lib/language/atom.rb +0 -342
- data/lib/language/output.rb +0 -130
- data/lib/language/parser/absent/present.rb +0 -8
- data/lib/language/parser/absent.rb +0 -6
- data/lib/language/parser/end_of_input.rb +0 -6
- data/lib/language/parser/interuption.rb +0 -38
- data/lib/language/parser/not_end_of_input.rb +0 -6
- data/lib/language/parser/str/not_found.rb +0 -16
- data/lib/language/parser/str.rb +0 -6
- data/lib/language/parser.rb +0 -53
- data/lib/language-ruby.rb +0 -10
- data/lib/language.rb +0 -80
- 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 -11
- 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/call_spec.rb +0 -26
- 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/string.rb
CHANGED
@@ -1,74 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Object
|
3
|
-
class String <
|
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,
|
19
|
+
globals = multi_fetch(args, *GLOBALS)
|
14
20
|
value = arguments.first&.value
|
15
21
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
40
|
-
|
46
|
+
def code_downcase
|
47
|
+
String.new(raw.downcase)
|
41
48
|
end
|
42
49
|
|
43
|
-
def
|
44
|
-
raw.
|
50
|
+
def code_include?(value)
|
51
|
+
Boolean.new(raw.include?(value.raw))
|
45
52
|
end
|
46
53
|
|
47
|
-
def
|
48
|
-
raw
|
54
|
+
def code_multiplication(other)
|
55
|
+
String.new(raw * other.raw)
|
49
56
|
end
|
50
57
|
|
51
|
-
def
|
52
|
-
raw.
|
58
|
+
def code_plus(other)
|
59
|
+
String.new(raw + other.to_s)
|
53
60
|
end
|
54
61
|
|
55
|
-
|
62
|
+
def code_reverse
|
63
|
+
String.new(raw.reverse)
|
64
|
+
end
|
56
65
|
|
57
|
-
def
|
58
|
-
|
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
|
-
|
74
|
+
left_operation: {
|
66
75
|
first: {
|
67
76
|
call: {
|
68
77
|
name: "_"
|
69
78
|
}
|
70
79
|
},
|
71
|
-
others: [
|
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
|
82
|
-
|
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
|
94
|
-
|
96
|
+
def succ
|
97
|
+
String.new(raw.succ)
|
95
98
|
end
|
96
99
|
|
97
|
-
def
|
98
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
11
|
-
|
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
|
51
|
-
|
13
|
+
def self.repeat(minimum = 0, maximum = nil)
|
14
|
+
Type::Repeat.new(self, minimum:, maximum:)
|
52
15
|
end
|
53
16
|
|
54
|
-
def
|
55
|
-
|
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) ?
|
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) ?
|
31
|
+
raw == (other.respond_to?(:raw) ? other.raw : other)
|
69
32
|
else
|
70
33
|
other == self
|
71
34
|
end
|
72
35
|
end
|
73
|
-
|
36
|
+
alias eql? ==
|
74
37
|
|
75
|
-
def
|
76
|
-
|
77
|
-
|
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
|
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
|
84
|
-
|
107
|
+
def code_and_operator(other)
|
108
|
+
truthy? ? other : self
|
85
109
|
end
|
86
110
|
|
87
|
-
|
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
|
94
|
-
|
95
|
-
object.map { |element| deep_dup(o) }
|
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(o) })
|
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
|
110
|
-
|
111
|
-
|
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
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
129
|
-
|
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
|
154
|
-
|
131
|
+
def code_or_operator(other)
|
132
|
+
truthy? ? self : other
|
155
133
|
end
|
156
134
|
|
157
|
-
def
|
158
|
-
|
135
|
+
def code_self
|
136
|
+
self
|
159
137
|
end
|
160
138
|
|
161
|
-
def
|
162
|
-
|
139
|
+
def code_equal_equal_equal(other)
|
140
|
+
Boolean.new(self === other)
|
163
141
|
end
|
164
142
|
|
165
|
-
def
|
166
|
-
|
143
|
+
def code_to_string
|
144
|
+
String.new(to_s)
|
167
145
|
end
|
168
146
|
|
169
|
-
def
|
170
|
-
truthy?
|
147
|
+
def falsy?
|
148
|
+
!truthy?
|
171
149
|
end
|
172
150
|
|
173
|
-
def
|
174
|
-
|
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
|
178
|
-
|
159
|
+
def multi_fetch(hash, *keys)
|
160
|
+
keys.map { |key| [key, hash.fetch(key)] }.to_h
|
179
161
|
end
|
180
162
|
|
181
|
-
def
|
182
|
-
::
|
163
|
+
def sig(args, &block)
|
164
|
+
Type::Sig.sig(args, object: self, &block)
|
183
165
|
end
|
184
166
|
|
185
|
-
def
|
186
|
-
|
167
|
+
def to_s
|
168
|
+
raise NotImplementedError, "#{self.class.name}#to_s"
|
187
169
|
end
|
188
170
|
|
189
|
-
def
|
190
|
-
|
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
|
data/lib/code/parser/addition.rb
CHANGED
data/lib/code/parser/boolean.rb
CHANGED
@@ -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) |
|
15
|
+
(true_keyword | false_keyword).aka(:boolean) | Nothing
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
data/lib/code/parser/call.rb
CHANGED
@@ -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
|
-
|
7
|
+
Name
|
6
8
|
end
|
7
9
|
|
8
10
|
def whitespace
|
9
|
-
|
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
|
-
|
19
|
+
Code
|
18
20
|
end
|
19
21
|
|
20
22
|
def code_present
|
21
|
-
|
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 <<
|
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? <<
|
78
|
-
(
|
79
|
-
|
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? <<
|
98
|
-
(
|
99
|
-
|
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 <
|
5
|
+
class ChainedCall < LeftOperation
|
4
6
|
def statement
|
5
|
-
|
7
|
+
SquareBracket
|
6
8
|
end
|
7
9
|
|
8
10
|
def whitespace
|
9
|
-
|
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
|