start on blocks
This commit is contained in:
parent
4103da7490
commit
c6a903073a
@ -37,6 +37,18 @@ module Vool
|
||||
arg.first
|
||||
end
|
||||
|
||||
def on_block(block_node)
|
||||
sendd = process(block_node.children[0])
|
||||
args = process(block_node.children[1])
|
||||
body = process(block_node.children[2])
|
||||
sendd.block = BlockStatement.new(args , body)
|
||||
sendd
|
||||
end
|
||||
|
||||
def on_args(args)
|
||||
args.children.collect{|a| process(a)}
|
||||
end
|
||||
|
||||
#basic Values
|
||||
def on_self exp
|
||||
SelfExpression.new
|
||||
|
@ -65,6 +65,7 @@ end
|
||||
require_relative "statements/assign_statement"
|
||||
require_relative "statements/array_statement"
|
||||
require_relative "statements/basic_values"
|
||||
require_relative "statements/block_statement"
|
||||
require_relative "statements/class_statement"
|
||||
require_relative "statements/hash_statement"
|
||||
require_relative "statements/if_statement"
|
||||
|
29
lib/vool/statements/block_statement.rb
Normal file
29
lib/vool/statements/block_statement.rb
Normal file
@ -0,0 +1,29 @@
|
||||
module Vool
|
||||
class BlockStatement < Statement
|
||||
attr_reader :args , :body , :clazz
|
||||
|
||||
def initialize( args , body , clazz = nil)
|
||||
@args , @body = args , body
|
||||
raise "no bod" unless @body
|
||||
@clazz = clazz
|
||||
end
|
||||
|
||||
def to_mom( _ )
|
||||
raise "should not be called (call create_objects)"
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
block.call(self)
|
||||
@body.each(&block)
|
||||
end
|
||||
|
||||
def normalize
|
||||
BlockStatement.new( @args , @body.normalize)
|
||||
end
|
||||
|
||||
def create_objects(clazz)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
@ -10,13 +10,17 @@ module Vool
|
||||
# As cache key we must use the type of the object (which is the first word of _every_ object)
|
||||
# as that is constant, and function implementations depend on the type (not class)
|
||||
class SendStatement < Statement
|
||||
attr_reader :name , :receiver , :arguments
|
||||
attr_reader :name , :receiver , :arguments , :block
|
||||
|
||||
def initialize(name , receiver , arguments )
|
||||
@name , @receiver , @arguments = name , receiver , arguments
|
||||
@arguments ||= []
|
||||
end
|
||||
|
||||
def block=( block )
|
||||
@block = block
|
||||
end
|
||||
|
||||
def normalize
|
||||
statements = Statements.new([])
|
||||
arguments = []
|
||||
|
29
test/vool/ruby_compiler/test_block_statement.rb
Normal file
29
test/vool/ruby_compiler/test_block_statement.rb
Normal file
@ -0,0 +1,29 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Vool
|
||||
class TestBlockStatement < MiniTest::Test
|
||||
|
||||
def setup()
|
||||
input = "plus_one{|arg1| arg1 + 1 } "
|
||||
@lst = RubyCompiler.compile( input )
|
||||
end
|
||||
def test_method
|
||||
assert_equal SendStatement , @lst.class
|
||||
end
|
||||
def test_block
|
||||
assert_equal BlockStatement , @lst.block.class
|
||||
end
|
||||
|
||||
def test_method_name
|
||||
assert_equal :plus_one , @lst.name
|
||||
end
|
||||
|
||||
def test_block_args
|
||||
assert_equal [:arg1] , @lst.block.args
|
||||
end
|
||||
def test_block_body
|
||||
assert_equal SendStatement , @lst.block.body.class
|
||||
assert_equal 1 , @lst.block.body.arguments.length
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user