template-ruby 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/Gemfile +2 -1
- data/Gemfile.lock +27 -54
- data/TODO +17 -0
- data/bin/code +56 -31
- data/bin/format +3 -0
- data/bin/template +62 -20
- data/bin/test +17 -0
- data/code-ruby.gemspec +1 -3
- data/docs/class.code +9 -0
- data/docs/euler/1.template +1 -5
- data/docs/euler/5.template +0 -1
- data/docs/meetup.code +12 -0
- data/docs/precedence.template +6 -39
- data/docs/rain.code +22 -0
- data/docs/slack.code +17 -0
- data/docs/stripe.code +7 -0
- data/docs/twitter.code +9 -0
- data/language-ruby.gemspec +18 -0
- data/lib/code/node/base_10.rb +29 -0
- data/lib/code/node/base_16.rb +13 -0
- data/lib/code/node/base_2.rb +13 -0
- data/lib/code/node/base_8.rb +13 -0
- data/lib/code/node/boolean.rb +7 -7
- data/lib/code/node/call.rb +32 -38
- data/lib/code/node/call_argument.rb +11 -27
- data/lib/code/node/chained_call.rb +10 -27
- data/lib/code/node/code.rb +4 -6
- data/lib/code/node/decimal.rb +26 -0
- data/lib/code/node/dictionnary.rb +20 -9
- data/lib/code/node/equal.rb +18 -20
- data/lib/code/node/function.rb +10 -7
- data/lib/code/node/function_parameter.rb +31 -0
- data/lib/code/node/if.rb +36 -32
- data/lib/code/node/if_modifier.rb +35 -36
- data/lib/code/node/list.rb +6 -8
- data/lib/code/node/negation.rb +5 -23
- data/lib/code/node/not.rb +15 -0
- data/lib/code/node/nothing.rb +1 -1
- data/lib/code/node/number.rb +14 -12
- data/lib/code/node/operation.rb +21 -16
- data/lib/code/node/power.rb +10 -6
- data/lib/code/node/rescue.rb +4 -3
- data/lib/code/node/splat.rb +15 -0
- data/lib/code/node/statement.rb +48 -70
- data/lib/code/node/string.rb +42 -16
- data/lib/code/node/ternary.rb +7 -9
- data/lib/code/node/unary_minus.rb +5 -12
- data/lib/code/node/while.rb +17 -24
- data/lib/code/node.rb +7 -8
- data/lib/code/object/argument.rb +2 -11
- data/lib/code/object/decimal.rb +45 -30
- data/lib/code/object/dictionnary.rb +6 -5
- data/lib/code/object/function.rb +20 -23
- data/lib/code/object/global.rb +11 -6
- data/lib/code/object/integer.rb +73 -30
- data/lib/code/object/list.rb +40 -34
- data/lib/code/object/range.rb +18 -17
- data/lib/code/object/ruby_function.rb +12 -6
- data/lib/code/object/string.rb +22 -12
- data/lib/code/object.rb +82 -24
- data/lib/code/parser/addition.rb +12 -20
- data/lib/code/parser/and_operator.rb +9 -20
- data/lib/code/parser/bitwise_and.rb +9 -20
- data/lib/code/parser/bitwise_or.rb +12 -20
- data/lib/code/parser/boolean.rb +10 -7
- data/lib/code/parser/call.rb +92 -60
- data/lib/code/parser/chained_call.rb +47 -0
- data/lib/code/parser/class.rb +45 -0
- data/lib/code/parser/code.rb +17 -10
- data/lib/code/parser/dictionnary.rb +56 -30
- data/lib/code/parser/equal.rb +87 -35
- data/lib/code/parser/equality.rb +23 -24
- data/lib/code/parser/equality_lower.rb +9 -0
- data/lib/code/parser/function.rb +67 -42
- data/lib/code/parser/greater.rb +25 -0
- data/lib/code/parser/group.rb +13 -8
- data/lib/code/parser/if.rb +51 -21
- data/lib/code/parser/if_modifier.rb +43 -16
- data/lib/code/parser/list.rb +32 -19
- data/lib/code/parser/multiplication.rb +15 -20
- data/lib/code/parser/name.rb +96 -84
- data/lib/code/parser/negation.rb +20 -9
- data/lib/code/parser/not_keyword.rb +14 -12
- data/lib/code/parser/nothing.rb +13 -8
- data/lib/code/parser/number.rb +124 -68
- data/lib/code/parser/operation.rb +35 -0
- data/lib/code/parser/or_keyword.rb +12 -20
- data/lib/code/parser/or_operator.rb +9 -20
- data/lib/code/parser/power.rb +32 -14
- data/lib/code/parser/range.rb +9 -17
- data/lib/code/parser/rescue.rb +29 -13
- data/lib/code/parser/shift.rb +11 -21
- data/lib/code/parser/splat.rb +31 -0
- data/lib/code/parser/statement.rb +4 -3
- data/lib/code/parser/string.rb +53 -62
- data/lib/code/parser/ternary.rb +36 -15
- data/lib/code/parser/unary_minus.rb +23 -5
- data/lib/code/parser/while.rb +26 -15
- data/lib/code/parser/whitespace.rb +49 -0
- data/lib/code/parser.rb +15 -0
- data/lib/code/ruby.rb +13 -12
- data/lib/code-ruby.rb +2 -11
- data/lib/code.rb +16 -13
- data/lib/language/atom.rb +343 -0
- data/lib/language/output.rb +130 -0
- data/lib/language/parser/absent/present.rb +8 -0
- data/lib/language/parser/absent.rb +6 -0
- data/lib/language/parser/end_of_input.rb +6 -0
- data/lib/language/parser/interuption.rb +38 -0
- data/lib/language/parser/not_end_of_input.rb +6 -0
- data/lib/language/parser/str/not_found.rb +16 -0
- data/lib/language/parser/str.rb +6 -0
- data/lib/language/parser.rb +53 -0
- data/lib/language-ruby.rb +10 -0
- data/lib/language.rb +80 -0
- data/lib/template/node/code_part.rb +1 -1
- data/lib/template/node/part.rb +1 -1
- data/lib/template/node/template.rb +1 -1
- data/lib/template/node/text_part.rb +1 -1
- data/lib/template/node.rb +1 -1
- data/lib/template/parser/template.rb +26 -17
- data/lib/template/parser.rb +15 -0
- data/lib/template/version.rb +1 -1
- data/lib/template-ruby.rb +2 -11
- data/lib/template.rb +6 -11
- data/spec/code/addition_spec.rb +13 -0
- data/spec/code/and_operator_spec.rb +13 -0
- data/spec/code/bitwise_and_spec.rb +13 -0
- data/spec/code/bitwise_or_spec.rb +13 -0
- data/spec/code/boolean_spec.rb +13 -0
- data/spec/code/call_spec.rb +21 -0
- data/spec/code/chained_call_spec.rb +16 -0
- data/spec/code/dictionnary_spec.rb +17 -0
- data/spec/code/equal_spec.rb +26 -0
- data/spec/code/equality_spec.rb +13 -0
- data/spec/code/function_spec.rb +18 -0
- data/spec/code/greater_spec.rb +18 -0
- data/spec/code/group_spec.rb +12 -0
- data/spec/code/if_modifier_spec.rb +20 -0
- data/spec/code/if_spec.rb +25 -0
- data/spec/code/list_spec.rb +17 -0
- data/spec/code/multiplication_spec.rb +18 -0
- data/spec/code/negation_spec.rb +20 -0
- data/spec/code/not_keyword_spec.rb +13 -0
- data/spec/code/nothing_spec.rb +17 -0
- data/spec/code/number_spec.rb +22 -0
- data/spec/code/or_keyword_spec.rb +17 -0
- data/spec/code/or_operator_spec.rb +16 -0
- data/spec/code/parser/boolean_spec.rb +5 -7
- data/spec/code/parser/call_spec.rb +16 -56
- data/spec/code/parser/chained_call.rb +17 -0
- data/spec/code/parser/dictionnary_spec.rb +8 -9
- data/spec/code/parser/function_spec.rb +5 -21
- data/spec/code/parser/group_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +9 -20
- data/spec/code/parser/number_spec.rb +4 -109
- data/spec/code/parser/string_spec.rb +9 -17
- data/spec/code/parser_spec.rb +23 -0
- data/spec/code/power_spec.rb +13 -0
- data/spec/code/range_spec.rb +16 -0
- data/spec/code/rescue_spec.rb +13 -0
- data/spec/code/shift_spec.rb +13 -0
- data/spec/code/splat_spec.rb +13 -0
- data/spec/code/string_spec.rb +25 -0
- data/spec/code/ternary_spec.rb +18 -0
- data/spec/code/unary_minus_spec.rb +13 -0
- data/spec/code/while_spec.rb +18 -0
- data/spec/spec_helper.rb +4 -1
- data/template-ruby.gemspec +2 -4
- metadata +112 -72
- data/lib/code/node/base_10_decimal.rb +0 -32
- data/lib/code/node/base_10_integer.rb +0 -32
- data/lib/code/node/base_10_number.rb +0 -19
- data/lib/code/node/base_16_number.rb +0 -19
- data/lib/code/node/base_2_number.rb +0 -19
- data/lib/code/node/base_8_number.rb +0 -19
- data/lib/code/node/block.rb +0 -17
- data/lib/code/node/defined.rb +0 -19
- data/lib/code/node/dictionnary_key_value.rb +0 -23
- data/lib/code/node/function_argument.rb +0 -45
- data/lib/code/node/group.rb +0 -13
- data/lib/code/node/keyword_call_argument.rb +0 -30
- data/lib/code/node/keyword_function_argument.rb +0 -33
- data/lib/code/node/name.rb +0 -28
- data/lib/code/node/not_keyword.rb +0 -13
- data/lib/code/node/or_keyword.rb +0 -34
- data/lib/code/node/range.rb +0 -31
- data/lib/code/node/regular_call_argument.rb +0 -34
- data/lib/code/node/regular_function_argument.rb +0 -36
- data/lib/code/node/string_characters.rb +0 -13
- data/lib/code/node/string_component.rb +0 -23
- data/lib/code/node/string_interpolation.rb +0 -13
- data/lib/code/parser/defined.rb +0 -20
- data/lib/code/parser/greater_than.rb +0 -33
- data/spec/call_spec.rb +0 -22
- data/spec/code/error/type_error_spec.rb +0 -63
- data/spec/code/parser/name_spec.rb +0 -15
- data/spec/code/parser/nothing_spec.rb +0 -19
- data/spec/code_spec.rb +0 -182
- data/spec/function_spec.rb +0 -26
- data/spec/template/parser/template_spec.rb +0 -19
- data/spec/template_spec.rb +0 -52
data/lib/code/node/nothing.rb
CHANGED
data/lib/code/node/number.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Number < Node
|
4
|
-
def initialize(
|
5
|
-
if
|
6
|
-
@
|
7
|
-
elsif
|
8
|
-
@
|
9
|
-
elsif
|
10
|
-
@
|
11
|
-
elsif
|
12
|
-
@
|
13
|
-
|
14
|
-
|
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
15
|
end
|
16
|
+
|
17
|
+
super(parsed)
|
16
18
|
end
|
17
19
|
|
18
20
|
def evaluate(**args)
|
19
|
-
@
|
21
|
+
@statement.evaluate(**args)
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
data/lib/code/node/operation.rb
CHANGED
@@ -1,32 +1,37 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Operation < Node
|
4
|
-
class
|
4
|
+
class Operator < Node
|
5
5
|
attr_reader :operator, :statement
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@operator =
|
9
|
-
@statement =
|
7
|
+
def initialize(parsed)
|
8
|
+
@operator = parsed.delete(:operator)
|
9
|
+
@statement = Node::Statement.new(parsed.delete(:statement))
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def initialize(
|
14
|
-
@first =
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
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)
|
19
21
|
end
|
20
22
|
|
21
23
|
def evaluate(**args)
|
22
|
-
|
24
|
+
first = @first.evaluate(**args)
|
23
25
|
|
24
|
-
@
|
25
|
-
|
26
|
-
object = simple_call(object, operation.operator, other, **args)
|
27
|
-
end
|
26
|
+
@others.reduce(first) do |left, right|
|
27
|
+
statement = right.statement.evaluate(**args)
|
28
28
|
|
29
|
-
|
29
|
+
left.call(
|
30
|
+
operator: right.operator,
|
31
|
+
arguments: [::Code::Object::Argument.new(statement)],
|
32
|
+
**args
|
33
|
+
)
|
34
|
+
end
|
30
35
|
end
|
31
36
|
end
|
32
37
|
end
|
data/lib/code/node/power.rb
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Power < Node
|
4
|
-
def initialize(
|
5
|
-
@
|
6
|
-
@
|
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)
|
7
9
|
end
|
8
10
|
|
9
11
|
def evaluate(**args)
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
@left.evaluate(**args).call(
|
13
|
+
operator: @operator,
|
14
|
+
arguments: [::Code::Object::Argument.new(@right.evaluate(**args))],
|
15
|
+
**args
|
16
|
+
)
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
data/lib/code/node/rescue.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Rescue < Node
|
4
|
-
def initialize(
|
5
|
-
@left =
|
6
|
-
@right =
|
4
|
+
def initialize(parsed)
|
5
|
+
@left = Node::Statement.new(parsed.delete(:left))
|
6
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
7
|
+
super(parsed)
|
7
8
|
end
|
8
9
|
|
9
10
|
def evaluate(**args)
|
@@ -0,0 +1,15 @@
|
|
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
|
data/lib/code/node/statement.rb
CHANGED
@@ -1,80 +1,58 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Statement < Node
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@statement =
|
9
|
-
elsif
|
10
|
-
@statement = ::Code
|
11
|
-
elsif
|
12
|
-
@statement =
|
13
|
-
elsif
|
14
|
-
@statement =
|
15
|
-
elsif
|
16
|
-
@statement =
|
17
|
-
elsif
|
18
|
-
@statement =
|
19
|
-
elsif
|
20
|
-
@statement =
|
21
|
-
elsif
|
22
|
-
@statement =
|
23
|
-
elsif
|
24
|
-
@statement =
|
25
|
-
elsif
|
26
|
-
@statement =
|
27
|
-
elsif
|
28
|
-
@statement =
|
29
|
-
elsif
|
30
|
-
@statement =
|
31
|
-
elsif
|
32
|
-
@statement =
|
33
|
-
elsif
|
34
|
-
@statement =
|
35
|
-
elsif
|
36
|
-
@statement =
|
37
|
-
elsif
|
38
|
-
@statement =
|
39
|
-
elsif
|
40
|
-
@statement =
|
41
|
-
elsif
|
42
|
-
@statement =
|
43
|
-
elsif
|
44
|
-
@statement =
|
45
|
-
elsif
|
46
|
-
@statement =
|
47
|
-
elsif
|
48
|
-
@statement =
|
49
|
-
elsif statement.key?(:ternary)
|
50
|
-
@statement = ::Code::Node::Ternary.new(statement[:ternary])
|
51
|
-
elsif statement.key?(:rescue)
|
52
|
-
@statement = ::Code::Node::Rescue.new(statement[:rescue])
|
53
|
-
elsif statement.key?(:equal)
|
54
|
-
@statement = ::Code::Node::Equal.new(statement[:equal])
|
55
|
-
elsif statement.key?(:defined)
|
56
|
-
@statement = ::Code::Node::Defined.new(statement[:defined])
|
57
|
-
elsif statement.key?(:not_keyword)
|
58
|
-
@statement = ::Code::Node::NotKeyword.new(statement[:not_keyword])
|
59
|
-
elsif statement.key?(:or_keyword)
|
60
|
-
@statement = ::Code::Node::OrKeyword.new(statement[:or_keyword])
|
61
|
-
elsif statement.key?(:if_modifier)
|
62
|
-
@statement = ::Code::Node::IfModifier.new(statement[:if_modifier])
|
63
|
-
elsif statement.key?(:if)
|
64
|
-
@statement = ::Code::Node::If.new(statement[:if])
|
65
|
-
elsif statement.key?(:while)
|
66
|
-
@statement = ::Code::Node::While.new(statement[:while])
|
67
|
-
elsif statement.key?(:group)
|
68
|
-
@statement = ::Code::Node::Group.new(statement[:group])
|
69
|
-
elsif statement.key?(:function)
|
70
|
-
@statement = ::Code::Node::Function.new(statement[:function])
|
71
|
-
else
|
72
|
-
raise NotImplementedError.new(statement.inspect)
|
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))
|
73
49
|
end
|
50
|
+
|
51
|
+
super(parsed)
|
74
52
|
end
|
75
53
|
|
76
54
|
def evaluate(**args)
|
77
|
-
@statement.evaluate(**args
|
55
|
+
@statement.evaluate(**args)
|
78
56
|
end
|
79
57
|
end
|
80
58
|
end
|
data/lib/code/node/string.rb
CHANGED
@@ -1,26 +1,52 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class String < Node
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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)
|
14
37
|
end
|
15
38
|
end
|
16
39
|
|
40
|
+
def initialize(parsed)
|
41
|
+
parsed = [] if parsed == ""
|
42
|
+
|
43
|
+
@parts = parsed.map { |part| Node::String::Part.new(part) }
|
44
|
+
end
|
45
|
+
|
17
46
|
def evaluate(**args)
|
18
|
-
|
19
|
-
@
|
20
|
-
|
21
|
-
.map(&:to_s)
|
22
|
-
.join
|
23
|
-
::Code::Object::String.new(string)
|
47
|
+
::Code::Object::String.new(
|
48
|
+
@parts.map { |part| part.evaluate(**args) }.map(&:to_s).join
|
49
|
+
)
|
24
50
|
end
|
25
51
|
end
|
26
52
|
end
|
data/lib/code/node/ternary.rb
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Ternary < Node
|
4
|
-
def initialize(
|
5
|
-
@left =
|
6
|
-
@middle =
|
7
|
-
|
8
|
-
|
9
|
-
@right = ::Code::Node::Statement.new(ternary.fetch(:right))
|
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))
|
10
9
|
end
|
10
|
+
super(parsed)
|
11
11
|
end
|
12
12
|
|
13
13
|
def evaluate(**args)
|
14
|
-
|
15
|
-
|
16
|
-
if left.truthy?
|
14
|
+
if @left.evaluate(**args).truthy?
|
17
15
|
@middle.evaluate(**args)
|
18
16
|
elsif @right
|
19
17
|
@right.evaluate(**args)
|
@@ -1,21 +1,14 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class UnaryMinus < Node
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(parsed)
|
5
|
+
@operator = parsed.delete(:operator)
|
6
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
7
|
+
super(parsed)
|
6
8
|
end
|
7
9
|
|
8
10
|
def evaluate(**args)
|
9
|
-
|
10
|
-
|
11
|
-
case object
|
12
|
-
when ::Code::Object::Integer
|
13
|
-
::Code::Object::Integer.new(-object.raw)
|
14
|
-
when ::Code::Object::Decimal
|
15
|
-
::Code::Object::Decimal.new(-object.raw)
|
16
|
-
else
|
17
|
-
::Code::Object::Nothing.new
|
18
|
-
end
|
11
|
+
@right.evaluate(**args).call(operator: @operator, arguments: [], **args)
|
19
12
|
end
|
20
13
|
end
|
21
14
|
end
|
data/lib/code/node/while.rb
CHANGED
@@ -1,42 +1,35 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class While < Node
|
4
|
-
WHILE_KEYWORD =
|
5
|
-
UNTIL_KEYWORD =
|
6
|
-
|
7
|
-
def initialize(
|
8
|
-
@operator =
|
9
|
-
@statement =
|
10
|
-
@body =
|
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)
|
11
12
|
end
|
12
13
|
|
13
14
|
def evaluate(**args)
|
14
|
-
if operator == WHILE_KEYWORD
|
15
|
-
|
15
|
+
if @operator == WHILE_KEYWORD
|
16
|
+
last = ::Code::Object::Nothing.new
|
16
17
|
|
17
18
|
while @statement.evaluate(**args).truthy?
|
18
|
-
|
19
|
+
last = @body.evaluate(**args)
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
-
elsif operator == UNTIL_KEYWORD
|
23
|
-
|
22
|
+
last
|
23
|
+
elsif @operator == UNTIL_KEYWORD
|
24
|
+
last = ::Code::Object::Nothing.new
|
24
25
|
|
25
|
-
|
26
|
-
object = @body.evaluate(**args)
|
27
|
-
end
|
26
|
+
last = @body.evaluate(**args) while @statement.evaluate(**args).falsy?
|
28
27
|
|
29
|
-
|
28
|
+
last
|
30
29
|
else
|
31
|
-
raise NotImplementedError.new(operator
|
30
|
+
raise NotImplementedError.new(@operator)
|
32
31
|
end
|
33
32
|
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def operator
|
38
|
-
@operator.to_sym
|
39
|
-
end
|
40
33
|
end
|
41
34
|
end
|
42
35
|
end
|
data/lib/code/node.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
|
-
|
3
|
+
def initialize(parsed)
|
4
|
+
if parsed.any?
|
5
|
+
raise NotImplementedError.new(self.class.name + ": " + parsed.inspect)
|
6
|
+
end
|
7
|
+
end
|
4
8
|
|
5
|
-
def
|
6
|
-
|
7
|
-
object.call(
|
8
|
-
operator: operator && ::Code::Object::String.new(operator.to_s),
|
9
|
-
arguments: [value && ::Code::Object::Argument.new(value)].compact,
|
10
|
-
**args
|
11
|
-
)
|
9
|
+
def evaluate(**args)
|
10
|
+
raise NotImplementedError.new(self.class.name + "#evaluate")
|
12
11
|
end
|
13
12
|
end
|
14
13
|
end
|
data/lib/code/object/argument.rb
CHANGED
@@ -1,20 +1,11 @@
|
|
1
1
|
class Code
|
2
2
|
class Object
|
3
3
|
class Argument
|
4
|
-
attr_reader :value, :name
|
4
|
+
attr_reader :value, :name
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
value,
|
8
|
-
name: nil,
|
9
|
-
splat: false,
|
10
|
-
keyword_splat: false,
|
11
|
-
block: false
|
12
|
-
)
|
6
|
+
def initialize(value, name: nil)
|
13
7
|
@value = value
|
14
8
|
@name = name
|
15
|
-
@splat = !!splat
|
16
|
-
@keyword_splat = !!keyword_splat
|
17
|
-
@block = !!block
|
18
9
|
end
|
19
10
|
|
20
11
|
def regular?
|
data/lib/code/object/decimal.rb
CHANGED
@@ -18,52 +18,63 @@ class Code
|
|
18
18
|
def call(**args)
|
19
19
|
operator = args.fetch(:operator, nil)
|
20
20
|
arguments = args.fetch(:arguments, [])
|
21
|
+
value = arguments.first&.value
|
21
22
|
|
22
23
|
if operator == "%"
|
23
|
-
sig(arguments
|
24
|
-
modulo(
|
24
|
+
sig(arguments) { ::Code::Object::Number }
|
25
|
+
modulo(value)
|
25
26
|
elsif operator == "+"
|
26
|
-
|
27
|
-
|
27
|
+
if value
|
28
|
+
sig(arguments) { ::Code::Object }
|
29
|
+
plus(value)
|
30
|
+
else
|
31
|
+
sig(arguments)
|
32
|
+
self
|
33
|
+
end
|
28
34
|
elsif operator == "-"
|
29
|
-
|
30
|
-
|
35
|
+
if value
|
36
|
+
sig(arguments) { ::Code::Object::Number }
|
37
|
+
minus(value)
|
38
|
+
else
|
39
|
+
sig(arguments)
|
40
|
+
unary_minus
|
41
|
+
end
|
31
42
|
elsif operator == "/"
|
32
|
-
sig(arguments
|
33
|
-
division(
|
43
|
+
sig(arguments) { ::Code::Object::Number }
|
44
|
+
division(value)
|
34
45
|
elsif operator == "*"
|
35
|
-
sig(arguments
|
36
|
-
multiplication(
|
46
|
+
sig(arguments) { ::Code::Object::Number }
|
47
|
+
multiplication(value)
|
37
48
|
elsif operator == "**"
|
38
|
-
sig(arguments
|
39
|
-
power(
|
49
|
+
sig(arguments) { ::Code::Object::Number }
|
50
|
+
power(value)
|
40
51
|
elsif operator == "<"
|
41
|
-
sig(arguments
|
42
|
-
inferior(
|
52
|
+
sig(arguments) { ::Code::Object::Number }
|
53
|
+
inferior(value)
|
43
54
|
elsif operator == "<="
|
44
|
-
sig(arguments
|
45
|
-
inferior_or_equal(
|
55
|
+
sig(arguments) { ::Code::Object::Number }
|
56
|
+
inferior_or_equal(value)
|
46
57
|
elsif operator == ">"
|
47
|
-
sig(arguments
|
48
|
-
superior(
|
58
|
+
sig(arguments) { ::Code::Object::Number }
|
59
|
+
superior(value)
|
49
60
|
elsif operator == ">="
|
50
|
-
sig(arguments
|
51
|
-
superior_or_equal(
|
61
|
+
sig(arguments) { ::Code::Object::Number }
|
62
|
+
superior_or_equal(value)
|
52
63
|
elsif operator == "<<"
|
53
|
-
sig(arguments
|
54
|
-
left_shift(
|
64
|
+
sig(arguments) { ::Code::Object::Number }
|
65
|
+
left_shift(value)
|
55
66
|
elsif operator == ">>"
|
56
|
-
sig(arguments
|
57
|
-
right_shift(
|
67
|
+
sig(arguments) { ::Code::Object::Number }
|
68
|
+
right_shift(value)
|
58
69
|
elsif operator == "&"
|
59
|
-
sig(arguments
|
60
|
-
bitwise_and(
|
70
|
+
sig(arguments) { ::Code::Object::Number }
|
71
|
+
bitwise_and(value)
|
61
72
|
elsif operator == "|"
|
62
|
-
sig(arguments
|
63
|
-
bitwise_or(
|
73
|
+
sig(arguments) { ::Code::Object::Number }
|
74
|
+
bitwise_or(value)
|
64
75
|
elsif operator == "^"
|
65
|
-
sig(arguments
|
66
|
-
bitwise_xor(
|
76
|
+
sig(arguments) { ::Code::Object::Number }
|
77
|
+
bitwise_xor(value)
|
67
78
|
else
|
68
79
|
super
|
69
80
|
end
|
@@ -142,6 +153,10 @@ class Code
|
|
142
153
|
def bitwise_xor(other)
|
143
154
|
::Code::Object::Integer.new(raw.to_i ^ other.raw.to_i)
|
144
155
|
end
|
156
|
+
|
157
|
+
def unary_minus
|
158
|
+
::Code::Object::Decimal.new(-raw)
|
159
|
+
end
|
145
160
|
end
|
146
161
|
end
|
147
162
|
end
|
@@ -10,7 +10,8 @@ class Code
|
|
10
10
|
def call(**args)
|
11
11
|
operator = args.fetch(:operator, nil)
|
12
12
|
arguments = args.fetch(:arguments, [])
|
13
|
-
globals =
|
13
|
+
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
+
value = arguments.first&.value
|
14
15
|
|
15
16
|
if operator == "values"
|
16
17
|
sig(arguments)
|
@@ -19,8 +20,8 @@ class Code
|
|
19
20
|
sig(arguments)
|
20
21
|
keys
|
21
22
|
elsif operator == "each"
|
22
|
-
sig(arguments
|
23
|
-
each(
|
23
|
+
sig(arguments) { ::Code::Object::Function }
|
24
|
+
each(value, **globals)
|
24
25
|
elsif key?(operator)
|
25
26
|
result = fetch(operator)
|
26
27
|
|
@@ -82,9 +83,9 @@ class Code
|
|
82
83
|
argument.call(
|
83
84
|
arguments: [
|
84
85
|
::Code::Object::Argument.new(key),
|
85
|
-
::Code::Object::Argument.new(value)
|
86
|
+
::Code::Object::Argument.new(value)
|
86
87
|
],
|
87
|
-
**globals
|
88
|
+
**globals
|
88
89
|
)
|
89
90
|
end
|
90
91
|
|