expanded many rule names, added some tests to find the bugs introduced
This commit is contained in:
parent
408cf98ea5
commit
74060d6ab6
@ -20,10 +20,10 @@ module Parser
|
||||
|
||||
rule(:function_call) { name.as(:function_call) >> argument_list }
|
||||
|
||||
rule(:expression) { cond | function_call | integer | name }
|
||||
rule(:expression) { conditional | function_call | integer | name }
|
||||
|
||||
rule(:cond) {
|
||||
keyword_if >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
|
||||
rule(:conditional) {
|
||||
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >>
|
||||
block.as(:if_true) >>
|
||||
keyword_else >>
|
||||
block.as(:if_false)
|
||||
@ -32,12 +32,12 @@ module Parser
|
||||
rule(:block) { left_brace >> expression.as(:block) >> right_brace }
|
||||
|
||||
rule(:function_definition) {
|
||||
keyword_def >> name.as(:function_definition) >> params >> block
|
||||
keyword_def >> name.as(:function_definition) >> parmeter_list >> block
|
||||
}
|
||||
|
||||
rule(:params) {
|
||||
rule(:parmeter_list) {
|
||||
left_parenthesis >>
|
||||
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
|
||||
((name.as(:parmeter) >> (comma >> name.as(:parmeter)).repeat(0)).maybe).as(:parmeter_list) >>
|
||||
right_parenthesis
|
||||
}
|
||||
rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list }
|
||||
|
@ -9,21 +9,32 @@ module Parser
|
||||
rule(:argument => simple(:argument)) { argument }
|
||||
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
||||
|
||||
rule(:function_call => simple(:function_call), :argument_list => sequence(:argument_list)) do
|
||||
rule(:function_call => simple(:function_call),
|
||||
:argument_list => simple(:argument)) do
|
||||
Vm::FuncallExpression.new(function_call.name, [argument])
|
||||
end
|
||||
rule( :function_call => simple(:function_call),
|
||||
:argument_list => sequence(:argument_list)) do
|
||||
Vm::FuncallExpression.new(function_call.name, argument_list)
|
||||
end
|
||||
|
||||
rule(:cond => simple(:cond),
|
||||
rule(:conditional => simple(:conditional),
|
||||
:if_true => {:block => simple(:if_true)},
|
||||
:if_false => {:block => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) }
|
||||
:if_false => {:block => simple(:if_false)}) { Vm::ConditionalExpression.new(conditional, if_true, if_false) }
|
||||
|
||||
rule(:param => simple(:param)) { param }
|
||||
rule(:params => sequence(:params)) { params }
|
||||
rule(:parmeter => simple(:parmeter)) { parmeter }
|
||||
rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
|
||||
|
||||
rule(:function_definition => simple(:function_definition),
|
||||
:params => sequence(:params),
|
||||
:parmeter_list => simple(:parmeter),
|
||||
:block => simple(:block)) do
|
||||
Vm::FunctionExpression.new(function_definition.name, params, block)
|
||||
Vm::FunctionExpression.new(function_definition.name, [parmeter], block)
|
||||
end
|
||||
|
||||
rule(:function_definition => simple(:function_definition),
|
||||
:parmeter_list => sequence(:parmeter_list),
|
||||
:block => simple(:block)) do
|
||||
Vm::FunctionExpression.new(function_definition.name, parmeter_list, block)
|
||||
end
|
||||
|
||||
#shortcut to get the ast tree for a given string
|
||||
|
@ -43,7 +43,7 @@ def foo(x) {
|
||||
HERE
|
||||
@parser = @parser.function_definition
|
||||
tree = parse(input)
|
||||
assert_kind_of Vm::IntegerExpression , tree
|
||||
assert_kind_of Vm::FunctionExpression , tree
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -25,6 +25,13 @@ class ParserTest < MiniTest::Test
|
||||
check
|
||||
end
|
||||
|
||||
def test_one_argument
|
||||
@input = '(42)'
|
||||
@expected = {:argument_list => {:argument => {:integer => '42'}} }
|
||||
@parser = @parser.argument_list
|
||||
check
|
||||
end
|
||||
|
||||
def test_argument_list
|
||||
@input = '(42, foo)'
|
||||
@expected = {:argument_list => [{:argument => {:integer => '42'}},
|
||||
@ -51,10 +58,10 @@ if (0) {
|
||||
667
|
||||
}
|
||||
HERE
|
||||
@expected = {:cond => {:integer => '0'},
|
||||
@expected = {:conditional => {:integer => '0'},
|
||||
:if_true => {:block => {:integer => '42'}},
|
||||
:if_false => {:block => {:integer => '667'}}}
|
||||
@parser = @parser.cond
|
||||
@parser = @parser.conditional
|
||||
check
|
||||
end
|
||||
|
||||
@ -65,7 +72,7 @@ def foo(x) {
|
||||
}
|
||||
HERE
|
||||
@expected = {:function_definition => {:name => 'foo'},
|
||||
:params => {:param => {:name => 'x'}},
|
||||
:parmeter_list => {:parmeter => {:name => 'x'}},
|
||||
:block => {:integer => '5'}}
|
||||
@parser = @parser.function_definition
|
||||
check
|
||||
|
@ -35,7 +35,7 @@ class TransformTest < MiniTest::Test
|
||||
|
||||
def test_single_argument
|
||||
@input = {:function_call => {:name => 'foo'},
|
||||
:argument_list => [{:argument => {:integer => '42'}}]}
|
||||
:argument_list => {:argument => {:integer => '42'} } }
|
||||
@expected = Vm::FuncallExpression.new 'foo', [Vm::IntegerExpression.new(42)]
|
||||
|
||||
check_equals
|
||||
@ -52,7 +52,7 @@ class TransformTest < MiniTest::Test
|
||||
end
|
||||
|
||||
def test_conditional
|
||||
@input = {:cond => {:integer => '0'},
|
||||
@input = {:conditional => {:integer => '0'},
|
||||
:if_true => {:block => {:integer => '42'}},
|
||||
:if_false => {:block => {:integer => '667'}}}
|
||||
@expected = Vm::ConditionalExpression.new \
|
||||
@ -62,20 +62,20 @@ class TransformTest < MiniTest::Test
|
||||
check_equals
|
||||
end
|
||||
|
||||
def test_param
|
||||
@input = {:param => { :name => "foo"}}
|
||||
def test_parmeter
|
||||
@input = {:parmeter => { :name => "foo"}}
|
||||
@expected = Vm::NameExpression.new('foo')
|
||||
check_equals
|
||||
end
|
||||
def test_params
|
||||
@input = {:params => [{:param => { :name => "foo"}}]}
|
||||
def test_parmeter_list
|
||||
@input = {:parmeter_list => [{:parmeter => { :name => "foo"}}]}
|
||||
@expected = [Vm::NameExpression.new('foo')]
|
||||
check_equals
|
||||
end
|
||||
|
||||
def test_function_definition
|
||||
@input = {:function_definition => { :name => "foo"},
|
||||
:params => [{ :param => { :name => "x" }}] ,
|
||||
:parmeter_list => { :parmeter => { :name => "x" }} ,
|
||||
:block => { :integer => "5" }}
|
||||
@expected = Vm::FunctionExpression.new('foo', [Vm::NameExpression.new('x')], Vm::IntegerExpression.new(5))
|
||||
check_equals
|
||||
|
Loading…
Reference in New Issue
Block a user