From 74060d6ab64e624eb5ac405f48dbc358e2423545 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 27 Apr 2014 21:41:38 +0300 Subject: [PATCH] expanded many rule names, added some tests to find the bugs introduced --- lib/parser/composed.rb | 12 ++++++------ lib/parser/transform.rb | 25 ++++++++++++++++++------- test/test_nodes.rb | 2 +- test/test_parser.rb | 13 ++++++++++--- test/test_transform.rb | 14 +++++++------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/lib/parser/composed.rb b/lib/parser/composed.rb index ff94fec5..cccdac96 100644 --- a/lib/parser/composed.rb +++ b/lib/parser/composed.rb @@ -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 } diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index d7cc7568..9224f621 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -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 diff --git a/test/test_nodes.rb b/test/test_nodes.rb index b3a0d26e..e1cc45bb 100644 --- a/test/test_nodes.rb +++ b/test/test_nodes.rb @@ -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 diff --git a/test/test_parser.rb b/test/test_parser.rb index eb00bf75..b6d7ae59 100644 --- a/test/test_parser.rb +++ b/test/test_parser.rb @@ -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 diff --git a/test/test_transform.rb b/test/test_transform.rb index 41d3e870..16c5962b 100644 --- a/test/test_transform.rb +++ b/test/test_transform.rb @@ -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