renamed parser rules for clarity. fixed
This commit is contained in:
parent
583e6f0c9f
commit
408cf98ea5
@ -1,5 +1,6 @@
|
||||
require_relative "basic_types"
|
||||
require_relative "tokens"
|
||||
require_relative "keywords"
|
||||
|
||||
module Parser
|
||||
|
||||
@ -9,39 +10,36 @@ module Parser
|
||||
class Composed < Parslet::Parser
|
||||
include BasicTypes
|
||||
include Tokens
|
||||
include Keywords
|
||||
|
||||
rule(:args) {
|
||||
rule(:argument_list) {
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
if_kw >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
|
||||
body.as(:if_true) >>
|
||||
else_kw >>
|
||||
body.as(:if_false)
|
||||
keyword_if >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
|
||||
block.as(:if_true) >>
|
||||
keyword_else >>
|
||||
block.as(:if_false)
|
||||
}
|
||||
|
||||
rule(:body) { left_brace >> expression.as(:body) >> right_brace }
|
||||
rule(:if_kw) { str('if') >> space? }
|
||||
rule(:else_kw) { str('else') >> space? }
|
||||
rule(:block) { left_brace >> expression.as(:block) >> right_brace }
|
||||
|
||||
rule(:func) {
|
||||
func_kw >> name.as(:func) >> params >> body
|
||||
rule(:function_definition) {
|
||||
keyword_def >> name.as(:function_definition) >> params >> block
|
||||
}
|
||||
|
||||
rule(:func_kw) { str('function') >> space? }
|
||||
|
||||
rule(:params) {
|
||||
left_parenthesis >>
|
||||
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
|
||||
right_parenthesis
|
||||
}
|
||||
rule(:root){ func.repeat(0) >> expression | expression | args }
|
||||
rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list }
|
||||
end
|
||||
end
|
||||
|
@ -6,29 +6,25 @@ module Parser
|
||||
rule(:integer => simple(:value)) { Vm::IntegerExpression.new(value.to_i) }
|
||||
rule(:name => simple(:name)) { Vm::NameExpression.new(name.to_s) }
|
||||
|
||||
rule(:arg => simple(:arg)) { arg }
|
||||
rule(:args => sequence(:args)) { args }
|
||||
rule(:argument => simple(:argument)) { argument }
|
||||
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
||||
|
||||
rule(:funcall => simple(:funcall),
|
||||
:args => simple(:args)) { Vm::FuncallExpression.new(funcall.name, [args]) }
|
||||
|
||||
rule(:funcall => simple(:funcall),
|
||||
:args => sequence(:args)) { Vm::FuncallExpression.new(funcall.name, args) }
|
||||
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),
|
||||
:if_true => {:body => simple(:if_true)},
|
||||
:if_false => {:body => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) }
|
||||
:if_true => {:block => simple(:if_true)},
|
||||
:if_false => {:block => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) }
|
||||
|
||||
rule(:param => simple(:param)) { param }
|
||||
rule(:params => sequence(:params)) { params }
|
||||
|
||||
rule(:func => simple(:func),
|
||||
:params => simple(:name),
|
||||
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, [name], body) }
|
||||
|
||||
rule(:func => simple(:func),
|
||||
rule(:function_definition => simple(:function_definition),
|
||||
: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
|
||||
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
||||
|
@ -64,9 +64,9 @@ module Vm
|
||||
end
|
||||
|
||||
class FunctionExpression < Expression
|
||||
attr_reader :name, :params, :body
|
||||
def initialize name, params, body
|
||||
@name, @params, @body = name, params, body
|
||||
attr_reader :name, :params, :block
|
||||
def initialize name, params, block
|
||||
@name, @params, @block = name, params, block
|
||||
end
|
||||
def eval(context, builder)
|
||||
param_names = [params].flatten.map(&:name)
|
||||
@ -74,7 +74,7 @@ module Vm
|
||||
types = [builder.int] * (param_names.count + 1)
|
||||
|
||||
builder.public_static_method(self.name, [], *types) do |method|
|
||||
self.body.eval(context, method)
|
||||
self.block.eval(context, method)
|
||||
method.ireturn
|
||||
end
|
||||
end
|
||||
|
@ -25,16 +25,26 @@ class TestNodes < MiniTest::Test
|
||||
def test_args
|
||||
tree = parse "( 42 )"
|
||||
assert_kind_of Hash , tree
|
||||
assert_kind_of Vm::IntegerExpression , tree[:args]
|
||||
assert_equal 42 , tree[:args].value
|
||||
assert_kind_of Vm::IntegerExpression , tree[:argument_list]
|
||||
assert_equal 42 , tree[:argument_list].value
|
||||
end
|
||||
def test_arg_list
|
||||
@parser = @parser.args
|
||||
@parser = @parser.argument_list
|
||||
tree = parse "(42, foo)"
|
||||
assert_instance_of Array , tree
|
||||
assert_equal 42 , tree.first.value
|
||||
assert_equal "foo" , tree.last.name
|
||||
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
|
||||
|
||||
|
||||
|
@ -7,7 +7,7 @@ class ParserTest < MiniTest::Test
|
||||
end
|
||||
|
||||
def check
|
||||
is = @parser.parse(@@input)
|
||||
is = @parser.parse(@input)
|
||||
assert is
|
||||
assert_equal @expected , is
|
||||
end
|
||||
@ -15,28 +15,32 @@ class ParserTest < MiniTest::Test
|
||||
@input = '42 '
|
||||
@expected = {:integer => '42'}
|
||||
@parser = @parser.integer
|
||||
check
|
||||
end
|
||||
|
||||
def test_name
|
||||
@input = 'foo '
|
||||
@expected = {:name => 'foo'}
|
||||
@parser = @parser.name
|
||||
check
|
||||
end
|
||||
|
||||
def test_argument_list
|
||||
@input = '(42, foo)'
|
||||
@expected = {:args => [{:arg => {:integer => '42'}},
|
||||
{:arg => {:name => 'foo'}}]}
|
||||
@parser = @parser.args
|
||||
@expected = {:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
@parser = @parser.argument_list
|
||||
check
|
||||
end
|
||||
|
||||
def test_function_call
|
||||
@input = 'baz(42, foo)'
|
||||
@expected = {:funcall => {:name => 'baz' },
|
||||
:args => [{:arg => {:integer => '42'}},
|
||||
{:arg => {:name => 'foo'}}]}
|
||||
@expected = {:function_call => {:name => 'baz' },
|
||||
:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
|
||||
@parser = @parser.funcall
|
||||
@parser = @parser.function_call
|
||||
check
|
||||
end
|
||||
|
||||
def test_conditional
|
||||
@ -48,20 +52,22 @@ if (0) {
|
||||
}
|
||||
HERE
|
||||
@expected = {:cond => {:integer => '0'},
|
||||
:if_true => {:body => {:integer => '42'}},
|
||||
:if_false => {:body => {:integer => '667'}}}
|
||||
:if_true => {:block => {:integer => '42'}},
|
||||
:if_false => {:block => {:integer => '667'}}}
|
||||
@parser = @parser.cond
|
||||
check
|
||||
end
|
||||
|
||||
def test_function_definition
|
||||
@input = <<HERE
|
||||
function foo(x) {
|
||||
def foo(x) {
|
||||
5
|
||||
}
|
||||
HERE
|
||||
@expected = {:func => {:name => 'foo'},
|
||||
@expected = {:function_definition => {:name => 'foo'},
|
||||
:params => {:param => {:name => 'x'}},
|
||||
:body => {:integer => '5'}}
|
||||
@parser = @parser.func
|
||||
:block => {:integer => '5'}}
|
||||
@parser = @parser.function_definition
|
||||
check
|
||||
end
|
||||
end
|
||||
|
@ -26,25 +26,25 @@ class TransformTest < MiniTest::Test
|
||||
end
|
||||
|
||||
def test_argument_list
|
||||
@input = {:args => [{:arg => {:integer => '42'}},
|
||||
{:arg => {:name => 'foo'}}]}
|
||||
@input = {:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
@expected = [Vm::IntegerExpression.new(42),
|
||||
Vm::NameExpression.new('foo')]
|
||||
check_equals
|
||||
end
|
||||
|
||||
def test_single_argument
|
||||
@input = {:funcall => {:name => 'foo'},
|
||||
:args => [{:arg => {:integer => '42'}}]}
|
||||
@input = {:function_call => {:name => 'foo'},
|
||||
:argument_list => [{:argument => {:integer => '42'}}]}
|
||||
@expected = Vm::FuncallExpression.new 'foo', [Vm::IntegerExpression.new(42)]
|
||||
|
||||
check_equals
|
||||
end
|
||||
|
||||
def test_multi_argument
|
||||
@input = {:funcall => {:name => 'baz'},
|
||||
:args => [{:arg => {:integer => '42'}},
|
||||
{:arg => {:name => 'foo'}}]}
|
||||
@input = {:function_call => {:name => 'baz'},
|
||||
:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
@expected = Vm::FuncallExpression.new 'baz', [Vm::IntegerExpression.new(42),
|
||||
Vm::NameExpression.new('foo')]
|
||||
|
||||
@ -53,25 +53,31 @@ class TransformTest < MiniTest::Test
|
||||
|
||||
def test_conditional
|
||||
@input = {:cond => {:integer => '0'},
|
||||
:if_true => {:body => {:integer => '42'}},
|
||||
:if_false => {:body => {:integer => '667'}}}
|
||||
:if_true => {:block => {:integer => '42'}},
|
||||
:if_false => {:block => {:integer => '667'}}}
|
||||
@expected = Vm::ConditionalExpression.new \
|
||||
Vm::IntegerExpression.new(0),
|
||||
Vm::IntegerExpression.new(42),
|
||||
Vm::IntegerExpression.new(667)
|
||||
|
||||
check_equals
|
||||
end
|
||||
|
||||
def test__function_definition
|
||||
@input = {:func => {:name => 'foo'},
|
||||
:params => {:param => {:name => 'x'}},
|
||||
:body => {:integer => '5'}}
|
||||
@expected = Vm::FunctionExpression.new \
|
||||
'foo',
|
||||
[Vm::NameExpression.new('x')],
|
||||
Vm::IntegerExpression.new(5)
|
||||
def test_param
|
||||
@input = {:param => { :name => "foo"}}
|
||||
@expected = Vm::NameExpression.new('foo')
|
||||
check_equals
|
||||
end
|
||||
def test_params
|
||||
@input = {:params => [{:param => { :name => "foo"}}]}
|
||||
@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
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user