From 7c7e58ea62cac446e01a561fc6344b27fa60541f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 5 May 2014 09:51:16 +0300 Subject: [PATCH] upgrades ast to first class --- lib/{vm => ast}/conversion.rb | 8 ++++---- lib/{parser/nodes.rb => ast/expression.rb} | 2 +- lib/parser/transform.rb | 20 ++++++++++---------- lib/vm/values.rb | 2 +- test/parser/test_arguments.rb | 8 ++++---- test/parser/test_basic.rb | 8 ++++---- test/parser/test_conditional.rb | 4 ++-- test/parser/test_expressions.rb | 10 +++++----- test/parser/test_function_call.rb | 8 ++++---- test/parser/test_function_definition.rb | 10 +++++----- 10 files changed, 40 insertions(+), 40 deletions(-) rename lib/{vm => ast}/conversion.rb (87%) rename lib/{parser/nodes.rb => ast/expression.rb} (99%) diff --git a/lib/vm/conversion.rb b/lib/ast/conversion.rb similarity index 87% rename from lib/vm/conversion.rb rename to lib/ast/conversion.rb index 5755a5ec..cd774669 100644 --- a/lib/vm/conversion.rb +++ b/lib/ast/conversion.rb @@ -1,4 +1,4 @@ -module Vm +module Ast # Convert ast to vm-values via visitor pattern # We do this (what would otherwise seem like foot-shuffling) to keep the layers seperated # Ie towards the feature goal of reusing the same parse for several binary outputs @@ -19,8 +19,8 @@ module Vm end end -require_relative "../parser/nodes" +require_relative "expression" -Parser::Expression.class_eval do - include Vm::Conversion +Ast::Expression.class_eval do + include Ast::Conversion end \ No newline at end of file diff --git a/lib/parser/nodes.rb b/lib/ast/expression.rb similarity index 99% rename from lib/parser/nodes.rb rename to lib/ast/expression.rb index 63e0ae76..2d21cd75 100644 --- a/lib/parser/nodes.rb +++ b/lib/ast/expression.rb @@ -1,5 +1,5 @@ # ast classes -module Parser +module Ast class Expression def eval raise "abstract" diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index 3f40bc50..56e7015d 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -1,11 +1,11 @@ require 'parslet' -require_relative 'nodes' +require 'ast/expression' module Parser class Transform < Parslet::Transform - rule(:integer => simple(:value)) { IntegerExpression.new(value.to_i) } - rule(:name => simple(:name)) { NameExpression.new(name.to_s) } - rule(:string => simple(:string)) { StringExpression.new(string.to_s) } + rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) } + rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) } + rule(:string => simple(:string)) { Ast::StringExpression.new(string.to_s) } rule(:argument => simple(:argument)) { argument } rule(:argument_list => sequence(:argument_list)) { argument_list } @@ -13,17 +13,17 @@ module Parser # need TWO transform rules, for one/many arguments (see the[] wrapping in the first) rule(:function_call => simple(:function_call), :argument_list => simple(:argument)) do - FuncallExpression.new(function_call.name, [argument]) + Ast::FuncallExpression.new(function_call.name, [argument]) end rule( :function_call => simple(:function_call), :argument_list => sequence(:argument_list)) do - FuncallExpression.new(function_call.name, argument_list) + Ast::FuncallExpression.new(function_call.name, argument_list) end rule(:conditional => simple(:conditional), :if_true => {:expressions => sequence(:if_true)}, :if_false => {:expressions => sequence(:if_false)}) do - ConditionalExpression.new(conditional, if_true, if_false) + Ast::ConditionalExpression.new(conditional, if_true, if_false) end rule(:parmeter => simple(:parmeter)) { parmeter } @@ -33,17 +33,17 @@ module Parser rule(:function_definition => simple(:function_definition), :parmeter_list => simple(:parmeter), :expressions => sequence(:expressions)) do - FunctionExpression.new(function_definition.name, [parmeter], expressions) + Ast::FunctionExpression.new(function_definition.name, [parmeter], expressions) end rule(:function_definition => simple(:function_definition), :parmeter_list => sequence(:parmeter_list), :expressions => sequence(:expressions)) do - FunctionExpression.new(function_definition.name, parmeter_list, expressions) + Ast::FunctionExpression.new(function_definition.name, parmeter_list, expressions) end rule(:asignee => simple(:left) , :asigned => simple(:right) ) do - AssignmentExpression.new(left.name, right ) + Ast::AssignmentExpression.new(left.name, right ) end #shortcut to get the ast tree for a given string # optional second arguement specifies a rule that will be parsed (mainly for testing) diff --git a/lib/vm/values.rb b/lib/vm/values.rb index 368ed01e..1c9f18e7 100644 --- a/lib/vm/values.rb +++ b/lib/vm/values.rb @@ -97,4 +97,4 @@ module Vm end -require_relative "conversion" \ No newline at end of file +require "ast/conversion" \ No newline at end of file diff --git a/test/parser/test_arguments.rb b/test/parser/test_arguments.rb index ed5899a1..b11eb976 100644 --- a/test/parser/test_arguments.rb +++ b/test/parser/test_arguments.rb @@ -7,7 +7,7 @@ class TestArguments < MiniTest::Test def test_one_argument @string_input = '(42)' @parse_output = {:argument_list => [{:argument => {:integer => '42'}}] } - @transform_output = [Parser::IntegerExpression.new(42) ] + @transform_output = [Ast::IntegerExpression.new(42) ] @parser = @parser.argument_list end @@ -15,14 +15,14 @@ class TestArguments < MiniTest::Test @string_input = '(42, foo)' @parse_output = {:argument_list => [{:argument => {:integer => '42'}}, {:argument => {:name => 'foo'}}]} - @transform_output = [Parser::IntegerExpression.new(42), Parser::NameExpression.new('foo')] + @transform_output = [Ast::IntegerExpression.new(42), Ast::NameExpression.new('foo')] @parser = @parser.argument_list end def test_parmeter @string_input = "(foo)" @parse_output = {:parmeter_list => [{:parmeter => { :name => "foo"}} ]} - @transform_output = [Parser::NameExpression.new('foo')] + @transform_output = [Ast::NameExpression.new('foo')] @parser = @parser.parmeter_list end @@ -30,7 +30,7 @@ class TestArguments < MiniTest::Test @string_input = "( foo , bar)" @parse_output = {:parmeter_list => [{:parmeter => { :name => "foo"}}, {:parmeter => { :name => "bar"}} ]} - @transform_output = [Parser::NameExpression.new('foo') , Parser::NameExpression.new('bar')] + @transform_output = [Ast::NameExpression.new('foo') , Ast::NameExpression.new('bar')] @parser = @parser.parmeter_list end diff --git a/test/parser/test_basic.rb b/test/parser/test_basic.rb index 1b16c993..46cc7681 100644 --- a/test/parser/test_basic.rb +++ b/test/parser/test_basic.rb @@ -7,14 +7,14 @@ class TestBasic < MiniTest::Test def test_number @string_input = '42 ' @parse_output = {:integer => '42'} - @transform_output = Parser::IntegerExpression.new(42) + @transform_output = Ast::IntegerExpression.new(42) @parser = @parser.integer end def test_name @string_input = 'foo ' @parse_output = {:name => 'foo'} - @transform_output = Parser::NameExpression.new('foo') + @transform_output = Ast::NameExpression.new('foo') @parser = @parser.name end @@ -23,14 +23,14 @@ class TestBasic < MiniTest::Test "hello" HERE @parse_output = {:string=>"hello"} - @transform_output = Parser::StringExpression.new('hello') + @transform_output = Ast::StringExpression.new('hello') @parser = @parser.string end def test_assignment @string_input = "a = 5" @parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } } - @transform_output = Parser::AssignmentExpression.new("a", Parser::IntegerExpression.new(5) ) + @transform_output = Ast::AssignmentExpression.new("a", Ast::IntegerExpression.new(5) ) @parser = @parser.assignment end diff --git a/test/parser/test_conditional.rb b/test/parser/test_conditional.rb index 3699f2a1..e7877f78 100644 --- a/test/parser/test_conditional.rb +++ b/test/parser/test_conditional.rb @@ -15,8 +15,8 @@ HERE @parse_output = { :conditional => { :integer => "0"}, :if_true => { :expressions => [ { :integer => "42" } ] } , :if_false => { :expressions => [ { :integer => "667" } ] } } - @transform_output = Parser::ConditionalExpression.new( Parser::IntegerExpression.new(0), - [Parser::IntegerExpression.new(42)], [Parser::IntegerExpression.new(667)]) + @transform_output = Ast::ConditionalExpression.new( Ast::IntegerExpression.new(0), + [Ast::IntegerExpression.new(42)], [Ast::IntegerExpression.new(667)]) @parser = @parser.conditional end diff --git a/test/parser/test_expressions.rb b/test/parser/test_expressions.rb index b5a088d3..f9c079ba 100644 --- a/test/parser/test_expressions.rb +++ b/test/parser/test_expressions.rb @@ -11,7 +11,7 @@ class TestExpressions < MiniTest::Test else HERE @parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]} - @transform_output = {:expressions=>[ Parser::IntegerExpression.new(4), Parser::IntegerExpression.new(5)]} + @transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)]} @parser = @parser.expressions_else end @@ -27,10 +27,10 @@ HERE { :function_call => { :name => "call" } , :argument_list => [ {:argument => { :integer => "4" } } , {:argument => { :integer => "6" } } ] } ]} - args = [ Parser::IntegerExpression.new(4) , Parser::IntegerExpression.new(6) ] - @transform_output = {:expressions=>[ Parser::IntegerExpression.new(5), - Parser::NameExpression.new("name") , - Parser::FuncallExpression.new("call", args ) ] } + args = [ Ast::IntegerExpression.new(4) , Ast::IntegerExpression.new(6) ] + @transform_output = {:expressions=>[ Ast::IntegerExpression.new(5), + Ast::NameExpression.new("name") , + Ast::FuncallExpression.new("call", args ) ] } @parser = @parser.expressions_end end diff --git a/test/parser/test_function_call.rb b/test/parser/test_function_call.rb index ddb5efd8..ce074583 100644 --- a/test/parser/test_function_call.rb +++ b/test/parser/test_function_call.rb @@ -8,7 +8,7 @@ class TestFunctionCall < MiniTest::Test @string_input = 'foo(42)' @parse_output = {:function_call => {:name => 'foo'}, :argument_list => [{:argument => {:integer => '42'} }] } - @transform_output = Parser::FuncallExpression.new 'foo', [Parser::IntegerExpression.new(42)] + @transform_output = Ast::FuncallExpression.new 'foo', [Ast::IntegerExpression.new(42)] end def test_function_call_multi @@ -16,8 +16,8 @@ class TestFunctionCall < MiniTest::Test @parse_output = {:function_call => {:name => 'baz' }, :argument_list => [{:argument => {:integer => '42'}}, {:argument => {:name => 'foo'}}]} - @transform_output = Parser::FuncallExpression.new 'baz', - [Parser::IntegerExpression.new(42), Parser::NameExpression.new("foo") ] + @transform_output = Ast::FuncallExpression.new 'baz', + [Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo") ] @parser = @parser.function_call end @@ -25,7 +25,7 @@ class TestFunctionCall < MiniTest::Test @string_input = 'puts( "hello")' @parse_output = {:function_call => {:name => 'puts' }, :argument_list => [{:argument => {:string => 'hello'}}]} - @transform_output = Parser::FuncallExpression.new "puts", [Parser::StringExpression.new("hello")] + @transform_output = Ast::FuncallExpression.new "puts", [Ast::StringExpression.new("hello")] @parser = @parser.function_call end diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index cbd14743..1b57af5e 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -13,9 +13,9 @@ HERE @parse_output = {:function_definition => {:name => 'foo'}, :parmeter_list => [{:parmeter => {:name => 'x'}}], :expressions => [{:integer => '5'}]} - @transform_output = Parser::FunctionExpression.new('foo', - [Parser::NameExpression.new('x')], - [Parser::IntegerExpression.new(5)]) + @transform_output = Ast::FunctionExpression.new('foo', + [Ast::NameExpression.new('x')], + [Ast::IntegerExpression.new(5)]) end def test_function_assignment @@ -28,8 +28,8 @@ HERE :parmeter_list => [{ :parmeter => { :name => "x" } }], :expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] } - @transform_output = Parser::FunctionExpression.new( "foo", [Parser::NameExpression.new("x")], - [Parser::AssignmentExpression.new( "abba", Parser::IntegerExpression.new(5) ) ]) + @transform_output = Ast::FunctionExpression.new( "foo", [Ast::NameExpression.new("x")], + [Ast::AssignmentExpression.new( "abba", Ast::IntegerExpression.new(5) ) ]) end