upgrades ast to first class
This commit is contained in:
parent
7c0aa8ae7d
commit
7c7e58ea62
@ -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
|
@ -1,5 +1,5 @@
|
||||
# ast classes
|
||||
module Parser
|
||||
module Ast
|
||||
class Expression
|
||||
def eval
|
||||
raise "abstract"
|
@ -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)
|
||||
|
@ -97,4 +97,4 @@ module Vm
|
||||
|
||||
end
|
||||
|
||||
require_relative "conversion"
|
||||
require "ast/conversion"
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user