expanded many rule names, added some tests to find the bugs introduced

This commit is contained in:
Torsten Ruger 2014-04-27 21:41:38 +03:00
parent 408cf98ea5
commit 74060d6ab6
5 changed files with 42 additions and 24 deletions

View File

@ -20,10 +20,10 @@ module Parser
rule(:function_call) { name.as(:function_call) >> argument_list } 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) { rule(:conditional) {
keyword_if >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >> keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >>
block.as(:if_true) >> block.as(:if_true) >>
keyword_else >> keyword_else >>
block.as(:if_false) block.as(:if_false)
@ -32,12 +32,12 @@ module Parser
rule(:block) { left_brace >> expression.as(:block) >> right_brace } rule(:block) { left_brace >> expression.as(:block) >> right_brace }
rule(:function_definition) { 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 >> 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 right_parenthesis
} }
rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list } rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list }

View File

@ -9,21 +9,32 @@ module Parser
rule(:argument => simple(:argument)) { argument } rule(:argument => simple(:argument)) { argument }
rule(:argument_list => sequence(:argument_list)) { argument_list } 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) Vm::FuncallExpression.new(function_call.name, argument_list)
end end
rule(:cond => simple(:cond), rule(:conditional => simple(:conditional),
:if_true => {:block => simple(:if_true)}, :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(:parmeter => simple(:parmeter)) { parmeter }
rule(:params => sequence(:params)) { params } rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
rule(:function_definition => simple(:function_definition), rule(:function_definition => simple(:function_definition),
:params => sequence(:params), :parmeter_list => simple(:parmeter),
:block => simple(:block)) do :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 end
#shortcut to get the ast tree for a given string #shortcut to get the ast tree for a given string

View File

@ -43,7 +43,7 @@ def foo(x) {
HERE HERE
@parser = @parser.function_definition @parser = @parser.function_definition
tree = parse(input) tree = parse(input)
assert_kind_of Vm::IntegerExpression , tree assert_kind_of Vm::FunctionExpression , tree
end end
end end

View File

@ -25,6 +25,13 @@ class ParserTest < MiniTest::Test
check check
end end
def test_one_argument
@input = '(42)'
@expected = {:argument_list => {:argument => {:integer => '42'}} }
@parser = @parser.argument_list
check
end
def test_argument_list def test_argument_list
@input = '(42, foo)' @input = '(42, foo)'
@expected = {:argument_list => [{:argument => {:integer => '42'}}, @expected = {:argument_list => [{:argument => {:integer => '42'}},
@ -51,10 +58,10 @@ if (0) {
667 667
} }
HERE HERE
@expected = {:cond => {:integer => '0'}, @expected = {:conditional => {:integer => '0'},
:if_true => {:block => {:integer => '42'}}, :if_true => {:block => {:integer => '42'}},
:if_false => {:block => {:integer => '667'}}} :if_false => {:block => {:integer => '667'}}}
@parser = @parser.cond @parser = @parser.conditional
check check
end end
@ -65,7 +72,7 @@ def foo(x) {
} }
HERE HERE
@expected = {:function_definition => {:name => 'foo'}, @expected = {:function_definition => {:name => 'foo'},
:params => {:param => {:name => 'x'}}, :parmeter_list => {:parmeter => {:name => 'x'}},
:block => {:integer => '5'}} :block => {:integer => '5'}}
@parser = @parser.function_definition @parser = @parser.function_definition
check check

View File

@ -35,7 +35,7 @@ class TransformTest < MiniTest::Test
def test_single_argument def test_single_argument
@input = {:function_call => {:name => 'foo'}, @input = {:function_call => {:name => 'foo'},
:argument_list => [{:argument => {:integer => '42'}}]} :argument_list => {:argument => {:integer => '42'} } }
@expected = Vm::FuncallExpression.new 'foo', [Vm::IntegerExpression.new(42)] @expected = Vm::FuncallExpression.new 'foo', [Vm::IntegerExpression.new(42)]
check_equals check_equals
@ -52,7 +52,7 @@ class TransformTest < MiniTest::Test
end end
def test_conditional def test_conditional
@input = {:cond => {:integer => '0'}, @input = {:conditional => {:integer => '0'},
:if_true => {:block => {:integer => '42'}}, :if_true => {:block => {:integer => '42'}},
:if_false => {:block => {:integer => '667'}}} :if_false => {:block => {:integer => '667'}}}
@expected = Vm::ConditionalExpression.new \ @expected = Vm::ConditionalExpression.new \
@ -62,20 +62,20 @@ class TransformTest < MiniTest::Test
check_equals check_equals
end end
def test_param def test_parmeter
@input = {:param => { :name => "foo"}} @input = {:parmeter => { :name => "foo"}}
@expected = Vm::NameExpression.new('foo') @expected = Vm::NameExpression.new('foo')
check_equals check_equals
end end
def test_params def test_parmeter_list
@input = {:params => [{:param => { :name => "foo"}}]} @input = {:parmeter_list => [{:parmeter => { :name => "foo"}}]}
@expected = [Vm::NameExpression.new('foo')] @expected = [Vm::NameExpression.new('foo')]
check_equals check_equals
end end
def test_function_definition def test_function_definition
@input = {:function_definition => { :name => "foo"}, @input = {:function_definition => { :name => "foo"},
:params => [{ :param => { :name => "x" }}] , :parmeter_list => { :parmeter => { :name => "x" }} ,
:block => { :integer => "5" }} :block => { :integer => "5" }}
@expected = Vm::FunctionExpression.new('foo', [Vm::NameExpression.new('x')], Vm::IntegerExpression.new(5)) @expected = Vm::FunctionExpression.new('foo', [Vm::NameExpression.new('x')], Vm::IntegerExpression.new(5))
check_equals check_equals