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/boolean.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Boolean < Node
|
4
|
-
|
5
|
-
|
4
|
+
TRUE_KEYWORD = "true"
|
5
|
+
FALSE_KEYWORD = "false"
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@boolean =
|
7
|
+
def initialize(parsed)
|
8
|
+
@boolean = parsed
|
9
9
|
end
|
10
10
|
|
11
11
|
def evaluate(**args)
|
12
|
-
if @boolean ==
|
12
|
+
if @boolean == TRUE_KEYWORD
|
13
13
|
::Code::Object::Boolean.new(true)
|
14
|
-
elsif @boolean ==
|
14
|
+
elsif @boolean == FALSE_KEYWORD
|
15
15
|
::Code::Object::Boolean.new(false)
|
16
16
|
else
|
17
|
-
raise NotImplementedError
|
17
|
+
raise NotImplementedError.new(@boolean)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/code/node/call.rb
CHANGED
@@ -1,52 +1,46 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Call < Node
|
4
|
-
|
5
|
-
|
4
|
+
class Block < Node
|
5
|
+
def initialize(parsed)
|
6
|
+
@parameters =
|
7
|
+
parsed
|
8
|
+
.delete(:parameters) { [] }
|
9
|
+
.map { |parameter| Node::FunctionParameter.new(parameter) }
|
6
10
|
|
7
|
-
|
8
|
-
@arguments.map! { |argument| ::Code::Node::CallArgument.new(argument) }
|
11
|
+
@body = Node::Code.new(parsed.delete(:body))
|
9
12
|
|
10
|
-
|
11
|
-
@right =
|
12
|
-
call
|
13
|
-
.fetch(:right)
|
14
|
-
.map { |right| ::Code::Node::ChainedCall.new(right) }
|
13
|
+
super(parsed)
|
15
14
|
end
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
def evaluate(**args)
|
17
|
+
::Code::Object::Argument.new(
|
18
|
+
::Code::Object::Function.new(parameters: @parameters, body: @body)
|
19
|
+
)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@arguments.map do |argument|
|
32
|
-
::Code::Object::Argument.new(
|
33
|
-
argument.evaluate(**args),
|
34
|
-
name: argument.name,
|
35
|
-
splat: argument.splat?,
|
36
|
-
keyword_splat: argument.keyword_splat?,
|
37
|
-
block: argument.block?,
|
38
|
-
)
|
39
|
-
end
|
40
|
-
|
41
|
-
if @block
|
42
|
-
arguments << ::Code::Object::Argument.new(
|
43
|
-
@block.evaluate(**args),
|
44
|
-
block: true,
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
|
-
@left.evaluate(**args.merge(arguments: arguments))
|
23
|
+
def initialize(parsed)
|
24
|
+
@name = parsed.delete(:name)
|
25
|
+
@arguments =
|
26
|
+
parsed
|
27
|
+
.delete(:arguments) { [] }
|
28
|
+
.map { |argument| Node::CallArgument.new(argument) }
|
29
|
+
|
30
|
+
if parsed.key?(:block)
|
31
|
+
@block = Node::Call::Block.new(parsed.delete(:block))
|
49
32
|
end
|
33
|
+
|
34
|
+
super(parsed)
|
35
|
+
end
|
36
|
+
|
37
|
+
def evaluate(**args)
|
38
|
+
arguments = @arguments.map { |argument| argument.evaluate(**args) }
|
39
|
+
arguments << @block.evaluate(**args) if @block
|
40
|
+
|
41
|
+
name = ::Code::Object::String.new(@name)
|
42
|
+
|
43
|
+
args.fetch(:object).call(operator: name, arguments: arguments, **args)
|
50
44
|
end
|
51
45
|
end
|
52
46
|
end
|
@@ -1,36 +1,20 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class CallArgument < Node
|
4
|
-
def initialize(
|
5
|
-
|
6
|
-
|
7
|
-
::Code::Node::RegularCallArgument.new(argument.fetch(:regular))
|
8
|
-
elsif argument.key?(:keyword)
|
9
|
-
@argument =
|
10
|
-
::Code::Node::KeywordCallArgument.new(argument.fetch(:keyword))
|
11
|
-
else
|
12
|
-
raise NotImplementedError.new(argument.inspect)
|
13
|
-
end
|
4
|
+
def initialize(parsed)
|
5
|
+
@value = Node::Code.new(parsed.delete(:value))
|
6
|
+
@name = parsed.delete(:name)
|
14
7
|
end
|
15
8
|
|
16
9
|
def evaluate(**args)
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@argument.block?
|
26
|
-
end
|
27
|
-
|
28
|
-
def splat?
|
29
|
-
@argument.splat?
|
30
|
-
end
|
31
|
-
|
32
|
-
def keyword_splat?
|
33
|
-
@argument.keyword_splat?
|
10
|
+
if @name
|
11
|
+
::Code::Object::Argument.new(
|
12
|
+
@value.evaluate(**args),
|
13
|
+
name: ::Code::Object::String.new(@name)
|
14
|
+
)
|
15
|
+
else
|
16
|
+
::Code::Object::Argument.new(@value.evaluate(**args))
|
17
|
+
end
|
34
18
|
end
|
35
19
|
end
|
36
20
|
end
|
@@ -1,39 +1,22 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class ChainedCall < Node
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(parsed)
|
5
|
+
@first = Node::Statement.new(parsed.delete(:first))
|
6
|
+
@others =
|
7
|
+
parsed
|
8
|
+
.delete(:others)
|
9
|
+
.map { |operator| Node::Statement.new(operator) }
|
6
10
|
|
7
|
-
|
8
|
-
@arguments.map! { |argument| ::Code::Node::CallArgument.new(argument) }
|
9
|
-
|
10
|
-
if chained_call.key?(:block)
|
11
|
-
@block = ::Code::Node::Block.new(chained_call.fetch(:block))
|
12
|
-
end
|
11
|
+
super(parsed)
|
13
12
|
end
|
14
13
|
|
15
14
|
def evaluate(**args)
|
16
|
-
|
17
|
-
@arguments.map do |argument|
|
18
|
-
::Code::Object::Argument.new(
|
19
|
-
argument.evaluate(
|
20
|
-
**args.merge(object: ::Code::Object::Global.new),
|
21
|
-
),
|
22
|
-
name: argument.name,
|
23
|
-
splat: argument.splat?,
|
24
|
-
keyword_splat: argument.keyword_splat?,
|
25
|
-
block: argument.block?,
|
26
|
-
)
|
27
|
-
end
|
15
|
+
first = @first.evaluate(**args)
|
28
16
|
|
29
|
-
|
30
|
-
|
31
|
-
@block.evaluate(**args.merge(object: ::Code::Object::Global.new)),
|
32
|
-
block: true,
|
33
|
-
)
|
17
|
+
@others.reduce(first) do |acc, element|
|
18
|
+
element.evaluate(**args.merge(object: acc))
|
34
19
|
end
|
35
|
-
|
36
|
-
@name.evaluate(**args.merge(arguments: arguments))
|
37
20
|
end
|
38
21
|
end
|
39
22
|
end
|
data/lib/code/node/code.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Code < Node
|
4
|
-
def initialize(
|
5
|
-
statements =
|
6
|
-
|
7
|
-
@statements =
|
8
|
-
statements.map { |statement| ::Code::Node::Statement.new(statement) }
|
4
|
+
def initialize(parsed)
|
5
|
+
@statements = parsed.map { |statement| Node::Statement.new(statement) }
|
9
6
|
end
|
10
7
|
|
11
8
|
def evaluate(**args)
|
12
9
|
last = ::Code::Object::Nothing.new
|
13
10
|
|
14
11
|
@statements.each do |statement|
|
15
|
-
last =
|
12
|
+
last =
|
13
|
+
statement.evaluate(**args.merge(object: ::Code::Object::Global.new))
|
16
14
|
end
|
17
15
|
|
18
16
|
last
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Code
|
2
|
+
class Node
|
3
|
+
class Decimal < Node
|
4
|
+
def initialize(parsed)
|
5
|
+
@decimal = parsed.delete(:decimal)
|
6
|
+
|
7
|
+
if parsed.key?(:exponent)
|
8
|
+
@exponent = Node::Statement.new(parsed.delete(:exponent))
|
9
|
+
end
|
10
|
+
|
11
|
+
super(parsed)
|
12
|
+
end
|
13
|
+
|
14
|
+
def evaluate(**args)
|
15
|
+
if @exponent
|
16
|
+
::Code::Object::Decimal.new(
|
17
|
+
@decimal,
|
18
|
+
exponent: @exponent.evaluate(**args)
|
19
|
+
)
|
20
|
+
else
|
21
|
+
::Code::Object::Decimal.new(@decimal)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,20 +1,31 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Dictionnary < Node
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
class KeyValue < Node
|
5
|
+
def initialize(parsed)
|
6
|
+
if parsed.key?(:statement)
|
7
|
+
@key = Node::Statement.new(parsed.delete(:statement))
|
8
|
+
elsif parsed.key?(:name)
|
9
|
+
@key = Node::String.new([{ text: parsed.delete(:name) }])
|
10
|
+
end
|
11
|
+
|
12
|
+
@value = Node::Code.new(parsed.delete(:value))
|
13
|
+
end
|
14
|
+
|
15
|
+
def evaluate(**args)
|
16
|
+
[@key.evaluate(**args), @value.evaluate(**args)]
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
20
|
+
def initialize(parsed)
|
21
|
+
parsed = [] if parsed == ""
|
22
|
+
@key_values =
|
23
|
+
parsed.map { |key_value| Node::Dictionnary::KeyValue.new(key_value) }
|
24
|
+
end
|
25
|
+
|
15
26
|
def evaluate(**args)
|
16
27
|
::Code::Object::Dictionnary.new(
|
17
|
-
@key_values.map { |key_value| key_value.evaluate(**args) }.to_h
|
28
|
+
@key_values.map { |key_value| key_value.evaluate(**args) }.to_h
|
18
29
|
)
|
19
30
|
end
|
20
31
|
end
|
data/lib/code/node/equal.rb
CHANGED
@@ -1,35 +1,33 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Equal < Node
|
4
|
-
def initialize(
|
5
|
-
@
|
6
|
-
@
|
7
|
-
@right =
|
4
|
+
def initialize(parsed)
|
5
|
+
@operator = parsed.delete(:operator)
|
6
|
+
@left = parsed.delete(:left)
|
7
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
8
|
+
super(parsed)
|
8
9
|
end
|
9
10
|
|
10
11
|
def evaluate(**args)
|
11
12
|
right = @right.evaluate(**args)
|
13
|
+
left = ::Code::Object::String.new(@left)
|
12
14
|
context = args.fetch(:context)
|
13
15
|
|
14
|
-
if operator
|
15
|
-
if context[left]
|
16
|
-
context[left] = simple_call(context[left], operator, right, **args)
|
17
|
-
else
|
18
|
-
raise ::Code::Error::UndefinedVariable.new("#{left} is undefined")
|
19
|
-
end
|
20
|
-
else
|
16
|
+
if @operator == ""
|
21
17
|
context[left] = right
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
else
|
19
|
+
if context[left].nil?
|
20
|
+
raise ::Code::Error::Undefined.new("#{left} is not defined")
|
21
|
+
end
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
context[left] = context.fetch(left).call(
|
24
|
+
operator: @operator,
|
25
|
+
arguments: [::Code::Object::Argument.new(right)],
|
26
|
+
**args
|
27
|
+
)
|
28
|
+
end
|
30
29
|
|
31
|
-
|
32
|
-
::Code::Object::String.new(@left.to_s)
|
30
|
+
args[:context][left]
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
data/lib/code/node/function.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Function < Node
|
4
|
-
def initialize(
|
5
|
-
@
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def initialize(parsed)
|
5
|
+
@parameters =
|
6
|
+
parsed
|
7
|
+
.delete(:parameters) { [] }
|
8
|
+
.map { |parameter| Node::FunctionParameter.new(parameter) }
|
9
|
+
|
10
|
+
@body = Node::Code.new(parsed.delete(:body))
|
11
|
+
|
12
|
+
super(parsed)
|
10
13
|
end
|
11
14
|
|
12
15
|
def evaluate(**args)
|
13
|
-
::Code::Object::Function.new(
|
16
|
+
::Code::Object::Function.new(parameters: @parameters, body: @body)
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Code
|
2
|
+
class Node
|
3
|
+
class FunctionParameter < Node
|
4
|
+
def initialize(parsed)
|
5
|
+
@name = parsed.delete(:name)
|
6
|
+
@keyword = !!parsed.delete(:keyword)
|
7
|
+
super(parsed)
|
8
|
+
end
|
9
|
+
|
10
|
+
def name
|
11
|
+
::Code::Object::String.new(@name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def regular?
|
15
|
+
!@keyword
|
16
|
+
end
|
17
|
+
|
18
|
+
def keyword?
|
19
|
+
!!@keyword
|
20
|
+
end
|
21
|
+
|
22
|
+
def regular_splat?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
def keyword_splat?
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/code/node/if.rb
CHANGED
@@ -3,47 +3,51 @@ class Code
|
|
3
3
|
class If < Node
|
4
4
|
IF_KEYWORD = "if"
|
5
5
|
UNLESS_KEYWORD = "unless"
|
6
|
+
ELSIF_KEYWORD = "elsif"
|
7
|
+
ELSE_KEYWORD = "else"
|
6
8
|
|
7
|
-
class Else
|
8
|
-
|
9
|
-
if else_parsed.key?(:operator)
|
10
|
-
@operator = else_parsed.fetch(:operator)
|
11
|
-
@statement =
|
12
|
-
::Code::Node::Statement.new(else_parsed.fetch(:statement))
|
13
|
-
end
|
9
|
+
class Else < Node
|
10
|
+
attr_reader :operator, :statement, :body
|
14
11
|
|
15
|
-
|
16
|
-
|
12
|
+
def initialize(parsed)
|
13
|
+
@operator = parsed.delete(:operator)
|
14
|
+
@body = Node::Code.new(parsed.delete(:body))
|
17
15
|
|
18
|
-
|
16
|
+
if parsed.key?(:statement)
|
17
|
+
@statement = Node::Statement.new(parsed.delete(:statement))
|
18
|
+
end
|
19
|
+
end
|
19
20
|
end
|
20
21
|
|
21
|
-
def initialize(
|
22
|
-
@
|
23
|
-
@
|
24
|
-
|
25
|
-
@
|
26
|
-
|
27
|
-
|
22
|
+
def initialize(parsed)
|
23
|
+
@first_operator = parsed.delete(:first_operator)
|
24
|
+
@first_statement = Node::Statement.new(parsed.delete(:first_statement))
|
25
|
+
@first_body = Node::Code.new(parsed.delete(:first_body))
|
26
|
+
@elses =
|
27
|
+
parsed.delete(:elses) { [] }.map { |elses| Node::If::Else.new(elses) }
|
28
|
+
super(parsed)
|
28
29
|
end
|
29
30
|
|
30
31
|
def evaluate(**args)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@
|
32
|
+
if @first_operator == IF_KEYWORD &&
|
33
|
+
@first_statement.evaluate(**args).truthy?
|
34
|
+
@first_body.evaluate(**args)
|
35
|
+
elsif @first_operator == UNLESS_KEYWORD &&
|
36
|
+
@first_statement.evaluate(**args).falsy?
|
37
|
+
@first_body.evaluate(**args)
|
37
38
|
else
|
38
|
-
@elses.each do |
|
39
|
-
if
|
40
|
-
|
41
|
-
return
|
42
|
-
elsif
|
43
|
-
|
44
|
-
return
|
45
|
-
elsif
|
46
|
-
|
39
|
+
@elses.each do |elses|
|
40
|
+
if elses.operator == ELSIF_KEYWORD &&
|
41
|
+
elses.statement.evaluate(**args).truthy?
|
42
|
+
return elses.body.evaluate(**args)
|
43
|
+
elsif elses.operator == IF_KEYWORD &&
|
44
|
+
elses.statement.evaluate(**args).truthy?
|
45
|
+
return elses.body.evaluate(**args)
|
46
|
+
elsif elses.operator == UNLESS_KEYWORD &&
|
47
|
+
elses.statement.evaluate(**args).falsy?
|
48
|
+
return elses.body.evaluate(**args)
|
49
|
+
elsif elses.operator == ELSE_KEYWORD
|
50
|
+
return elses.body.evaluate(**args)
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -1,48 +1,47 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class IfModifier < Node
|
4
|
-
IF_KEYWORD =
|
5
|
-
UNLESS_KEYWORD =
|
6
|
-
WHILE_KEYWORD =
|
7
|
-
UNTIL_KEYWORD =
|
8
|
-
|
9
|
-
def initialize(
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@right =
|
4
|
+
IF_KEYWORD = "if"
|
5
|
+
UNLESS_KEYWORD = "unless"
|
6
|
+
WHILE_KEYWORD = "while"
|
7
|
+
UNTIL_KEYWORD = "until"
|
8
|
+
|
9
|
+
def initialize(parsed)
|
10
|
+
@operator = parsed.delete(:operator)
|
11
|
+
@left = Node::Statement.new(parsed.delete(:left))
|
12
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
13
|
+
super(parsed)
|
13
14
|
end
|
14
15
|
|
15
16
|
def evaluate(**args)
|
16
|
-
if operator == IF_KEYWORD
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
17
|
+
if @operator == IF_KEYWORD
|
18
|
+
if @right.evaluate(**args).truthy?
|
19
|
+
@left.evaluate(**args)
|
20
|
+
else
|
21
|
+
::Code::Object::Nothing.new
|
22
|
+
end
|
23
|
+
elsif @operator == UNLESS_KEYWORD
|
24
|
+
if @right.evaluate(**args).falsy?
|
25
|
+
@left.evaluate(**args)
|
26
|
+
else
|
27
|
+
::Code::Object::Nothing.new
|
28
|
+
end
|
29
|
+
elsif @operator == WHILE_KEYWORD
|
30
|
+
last = ::Code::Object::Nothing.new
|
31
|
+
|
32
|
+
last = @left.evaluate(**args) while @right.evaluate(**args).truthy?
|
33
|
+
|
34
|
+
last
|
35
|
+
elsif @operator == UNTIL_KEYWORD
|
36
|
+
last = ::Code::Object::Nothing.new
|
37
|
+
|
38
|
+
last = @left.evaluate(**args) while @right.evaluate(**args).falsy?
|
39
|
+
|
40
|
+
last
|
36
41
|
else
|
37
|
-
raise NotImplementedError.new(operator
|
42
|
+
raise NotImplementedError.new(@operator)
|
38
43
|
end
|
39
44
|
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def operator
|
44
|
-
@operator.to_sym
|
45
|
-
end
|
46
45
|
end
|
47
46
|
end
|
48
47
|
end
|
data/lib/code/node/list.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class List < Node
|
4
|
-
def initialize(
|
5
|
-
if
|
6
|
-
|
7
|
-
else
|
8
|
-
@codes =
|
9
|
-
codes.map { |code| ::Code::Node::Code.new(code.fetch(:code)) }
|
10
|
-
end
|
4
|
+
def initialize(parsed)
|
5
|
+
parsed = [] if parsed == ""
|
6
|
+
@elements = parsed.map { |element| Node::Code.new(element) }
|
11
7
|
end
|
12
8
|
|
13
9
|
def evaluate(**args)
|
14
|
-
::Code::Object::List.new(
|
10
|
+
::Code::Object::List.new(
|
11
|
+
@elements.map { |element| element.evaluate(**args) }
|
12
|
+
)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
end
|
data/lib/code/node/negation.rb
CHANGED
@@ -1,33 +1,15 @@
|
|
1
1
|
class Code
|
2
2
|
class Node
|
3
3
|
class Negation < Node
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@operator = negation.fetch(:operator)
|
9
|
-
@statement = ::Code::Node::Statement.new(negation.fetch(:statement))
|
4
|
+
def initialize(parsed)
|
5
|
+
@operator = parsed.delete(:operator)
|
6
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
7
|
+
super(parsed)
|
10
8
|
end
|
11
9
|
|
12
10
|
def evaluate(**args)
|
13
|
-
|
14
|
-
|
15
|
-
if operator == EXCLAMATION_POINT
|
16
|
-
if object.truthy?
|
17
|
-
::Code::Object::Boolean.new(false)
|
18
|
-
else
|
19
|
-
::Code::Object::Boolean.new(true)
|
20
|
-
end
|
21
|
-
elsif operator == PLUS
|
22
|
-
object
|
23
|
-
else
|
24
|
-
raise NotImplementedError.new(operator)
|
25
|
-
end
|
11
|
+
@right.evaluate(**args).call(operator: @operator, arguments: [], **args)
|
26
12
|
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
attr_reader :operator
|
31
13
|
end
|
32
14
|
end
|
33
15
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Code
|
2
|
+
class Node
|
3
|
+
class Not < Node
|
4
|
+
def initialize(parsed)
|
5
|
+
@operator = parsed.delete(:operator)
|
6
|
+
@right = Node::Statement.new(parsed.delete(:right))
|
7
|
+
super(parsed)
|
8
|
+
end
|
9
|
+
|
10
|
+
def evaluate(**args)
|
11
|
+
@right.evaluate(**args).call(operator: @operator, arguments: [], **args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|