renamed parser rules for clarity. fixed

This commit is contained in:
Torsten Ruger 2014-04-27 21:12:42 +03:00
parent 583e6f0c9f
commit 408cf98ea5
6 changed files with 88 additions and 72 deletions

View File

@ -1,5 +1,6 @@
require_relative "basic_types" require_relative "basic_types"
require_relative "tokens" require_relative "tokens"
require_relative "keywords"
module Parser module Parser
@ -9,39 +10,36 @@ module Parser
class Composed < Parslet::Parser class Composed < Parslet::Parser
include BasicTypes include BasicTypes
include Tokens include Tokens
include Keywords
rule(:args) { rule(:argument_list) {
left_parenthesis >> left_parenthesis >>
((expression.as(:arg) >> (comma >> expression.as(:arg)).repeat(0)).maybe).as(:args) >> ((expression.as(:argument) >> (comma >> expression.as(:argument)).repeat(0)).maybe).as(:argument_list) >>
right_parenthesis right_parenthesis
} }
rule(:funcall) { name.as(:funcall) >> args } rule(:function_call) { name.as(:function_call) >> argument_list }
rule(:expression) { cond | funcall | integer | name } rule(:expression) { cond | function_call | integer | name }
rule(:cond) { rule(:cond) {
if_kw >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >> keyword_if >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
body.as(:if_true) >> block.as(:if_true) >>
else_kw >> keyword_else >>
body.as(:if_false) block.as(:if_false)
} }
rule(:body) { left_brace >> expression.as(:body) >> right_brace } rule(:block) { left_brace >> expression.as(:block) >> right_brace }
rule(:if_kw) { str('if') >> space? }
rule(:else_kw) { str('else') >> space? }
rule(:func) { rule(:function_definition) {
func_kw >> name.as(:func) >> params >> body keyword_def >> name.as(:function_definition) >> params >> block
} }
rule(:func_kw) { str('function') >> space? }
rule(:params) { rule(:params) {
left_parenthesis >> left_parenthesis >>
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >> ((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
right_parenthesis right_parenthesis
} }
rule(:root){ func.repeat(0) >> expression | expression | args } rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list }
end end
end end

View File

@ -6,29 +6,25 @@ module Parser
rule(:integer => simple(:value)) { Vm::IntegerExpression.new(value.to_i) } rule(:integer => simple(:value)) { Vm::IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { Vm::NameExpression.new(name.to_s) } rule(:name => simple(:name)) { Vm::NameExpression.new(name.to_s) }
rule(:arg => simple(:arg)) { arg } rule(:argument => simple(:argument)) { argument }
rule(:args => sequence(:args)) { args } rule(:argument_list => sequence(:argument_list)) { argument_list }
rule(:funcall => simple(:funcall), rule(:function_call => simple(:function_call), :argument_list => sequence(:argument_list)) do
:args => simple(:args)) { Vm::FuncallExpression.new(funcall.name, [args]) } Vm::FuncallExpression.new(function_call.name, argument_list)
end
rule(:funcall => simple(:funcall),
:args => sequence(:args)) { Vm::FuncallExpression.new(funcall.name, args) }
rule(:cond => simple(:cond), rule(:cond => simple(:cond),
:if_true => {:body => simple(:if_true)}, :if_true => {:block => simple(:if_true)},
:if_false => {:body => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) } :if_false => {:block => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) }
rule(:param => simple(:param)) { param } rule(:param => simple(:param)) { param }
rule(:params => sequence(:params)) { params } rule(:params => sequence(:params)) { params }
rule(:func => simple(:func), rule(:function_definition => simple(:function_definition),
:params => simple(:name),
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, [name], body) }
rule(:func => simple(:func),
:params => sequence(:params), :params => sequence(:params),
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, params, body) } :block => simple(:block)) do
Vm::FunctionExpression.new(function_definition.name, params, block)
end
#shortcut to get the ast tree for a given string #shortcut to get the ast tree for a given string
# optional second arguement specifies a rule that will be parsed (mainly for testing) # optional second arguement specifies a rule that will be parsed (mainly for testing)

View File

@ -64,9 +64,9 @@ module Vm
end end
class FunctionExpression < Expression class FunctionExpression < Expression
attr_reader :name, :params, :body attr_reader :name, :params, :block
def initialize name, params, body def initialize name, params, block
@name, @params, @body = name, params, body @name, @params, @block = name, params, block
end end
def eval(context, builder) def eval(context, builder)
param_names = [params].flatten.map(&:name) param_names = [params].flatten.map(&:name)
@ -74,7 +74,7 @@ module Vm
types = [builder.int] * (param_names.count + 1) types = [builder.int] * (param_names.count + 1)
builder.public_static_method(self.name, [], *types) do |method| builder.public_static_method(self.name, [], *types) do |method|
self.body.eval(context, method) self.block.eval(context, method)
method.ireturn method.ireturn
end end
end end

View File

@ -25,16 +25,26 @@ class TestNodes < MiniTest::Test
def test_args def test_args
tree = parse "( 42 )" tree = parse "( 42 )"
assert_kind_of Hash , tree assert_kind_of Hash , tree
assert_kind_of Vm::IntegerExpression , tree[:args] assert_kind_of Vm::IntegerExpression , tree[:argument_list]
assert_equal 42 , tree[:args].value assert_equal 42 , tree[:argument_list].value
end end
def test_arg_list def test_arg_list
@parser = @parser.args @parser = @parser.argument_list
tree = parse "(42, foo)" tree = parse "(42, foo)"
assert_instance_of Array , tree assert_instance_of Array , tree
assert_equal 42 , tree.first.value assert_equal 42 , tree.first.value
assert_equal "foo" , tree.last.name assert_equal "foo" , tree.last.name
end end
def test_definition
input = <<HERE
def foo(x) {
5
}
HERE
@parser = @parser.function_definition
tree = parse(input)
assert_kind_of Vm::IntegerExpression , tree
end
end end

View File

@ -7,7 +7,7 @@ class ParserTest < MiniTest::Test
end end
def check def check
is = @parser.parse(@@input) is = @parser.parse(@input)
assert is assert is
assert_equal @expected , is assert_equal @expected , is
end end
@ -15,28 +15,32 @@ class ParserTest < MiniTest::Test
@input = '42 ' @input = '42 '
@expected = {:integer => '42'} @expected = {:integer => '42'}
@parser = @parser.integer @parser = @parser.integer
check
end end
def test_name def test_name
@input = 'foo ' @input = 'foo '
@expected = {:name => 'foo'} @expected = {:name => 'foo'}
@parser = @parser.name @parser = @parser.name
check
end end
def test_argument_list def test_argument_list
@input = '(42, foo)' @input = '(42, foo)'
@expected = {:args => [{:arg => {:integer => '42'}}, @expected = {:argument_list => [{:argument => {:integer => '42'}},
{:arg => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@parser = @parser.args @parser = @parser.argument_list
check
end end
def test_function_call def test_function_call
@input = 'baz(42, foo)' @input = 'baz(42, foo)'
@expected = {:funcall => {:name => 'baz' }, @expected = {:function_call => {:name => 'baz' },
:args => [{:arg => {:integer => '42'}}, :argument_list => [{:argument => {:integer => '42'}},
{:arg => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@parser = @parser.funcall @parser = @parser.function_call
check
end end
def test_conditional def test_conditional
@ -48,20 +52,22 @@ if (0) {
} }
HERE HERE
@expected = {:cond => {:integer => '0'}, @expected = {:cond => {:integer => '0'},
:if_true => {:body => {:integer => '42'}}, :if_true => {:block => {:integer => '42'}},
:if_false => {:body => {:integer => '667'}}} :if_false => {:block => {:integer => '667'}}}
@parser = @parser.cond @parser = @parser.cond
check
end end
def test_function_definition def test_function_definition
@input = <<HERE @input = <<HERE
function foo(x) { def foo(x) {
5 5
} }
HERE HERE
@expected = {:func => {:name => 'foo'}, @expected = {:function_definition => {:name => 'foo'},
:params => {:param => {:name => 'x'}}, :params => {:param => {:name => 'x'}},
:body => {:integer => '5'}} :block => {:integer => '5'}}
@parser = @parser.func @parser = @parser.function_definition
check
end end
end end

View File

@ -26,25 +26,25 @@ class TransformTest < MiniTest::Test
end end
def test_argument_list def test_argument_list
@input = {:args => [{:arg => {:integer => '42'}}, @input = {:argument_list => [{:argument => {:integer => '42'}},
{:arg => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@expected = [Vm::IntegerExpression.new(42), @expected = [Vm::IntegerExpression.new(42),
Vm::NameExpression.new('foo')] Vm::NameExpression.new('foo')]
check_equals check_equals
end end
def test_single_argument def test_single_argument
@input = {:funcall => {:name => 'foo'}, @input = {:function_call => {:name => 'foo'},
:args => [{:arg => {: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
end end
def test_multi_argument def test_multi_argument
@input = {:funcall => {:name => 'baz'}, @input = {:function_call => {:name => 'baz'},
:args => [{:arg => {:integer => '42'}}, :argument_list => [{:argument => {:integer => '42'}},
{:arg => {:name => 'foo'}}]} {:argument => {:name => 'foo'}}]}
@expected = Vm::FuncallExpression.new 'baz', [Vm::IntegerExpression.new(42), @expected = Vm::FuncallExpression.new 'baz', [Vm::IntegerExpression.new(42),
Vm::NameExpression.new('foo')] Vm::NameExpression.new('foo')]
@ -53,25 +53,31 @@ class TransformTest < MiniTest::Test
def test_conditional def test_conditional
@input = {:cond => {:integer => '0'}, @input = {:cond => {:integer => '0'},
:if_true => {:body => {:integer => '42'}}, :if_true => {:block => {:integer => '42'}},
:if_false => {:body => {:integer => '667'}}} :if_false => {:block => {:integer => '667'}}}
@expected = Vm::ConditionalExpression.new \ @expected = Vm::ConditionalExpression.new \
Vm::IntegerExpression.new(0), Vm::IntegerExpression.new(0),
Vm::IntegerExpression.new(42), Vm::IntegerExpression.new(42),
Vm::IntegerExpression.new(667) Vm::IntegerExpression.new(667)
check_equals check_equals
end end
def test__function_definition def test_param
@input = {:func => {:name => 'foo'}, @input = {:param => { :name => "foo"}}
:params => {:param => {:name => 'x'}}, @expected = Vm::NameExpression.new('foo')
:body => {:integer => '5'}} check_equals
@expected = Vm::FunctionExpression.new \ end
'foo', def test_params
[Vm::NameExpression.new('x')], @input = {:params => [{:param => { :name => "foo"}}]}
Vm::IntegerExpression.new(5) @expected = [Vm::NameExpression.new('foo')]
check_equals
end
def test_function_definition
@input = {:function_definition => { :name => "foo"},
:params => [{ :param => { :name => "x" }}] ,
:block => { :integer => "5" }}
@expected = Vm::FunctionExpression.new('foo', [Vm::NameExpression.new('x')], Vm::IntegerExpression.new(5))
check_equals check_equals
end end
end end