manage to create ruby methods
This commit is contained in:
parent
89f5badc16
commit
3f6c1bc3a3
@ -8,33 +8,39 @@ require_relative "compiler/ruby_method"
|
|||||||
module Melon
|
module Melon
|
||||||
class Compiler < AST::Processor
|
class Compiler < AST::Processor
|
||||||
|
|
||||||
def self.compile input
|
def self.compile( input )
|
||||||
ast = Parser::Ruby22.parse input
|
ast = Parser::Ruby22.parse( input )
|
||||||
compiler = self.new
|
self.new.process( ast )
|
||||||
compiler.process ast
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_name( statement )
|
|
||||||
return nil unless statement
|
|
||||||
statement.children[1]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_class statement
|
def on_class statement
|
||||||
name , sup , body = *statement
|
name , sup , body = *statement
|
||||||
class_name = get_name(name)
|
class_name = get_name(name)
|
||||||
clazz = Parfait.object_space.create_class(class_name , get_name(sup) )
|
clazz = Parfait.object_space.get_class_by_name!(class_name , get_name(sup) )
|
||||||
ivar_hash = TypeCollector.new.collect(body)
|
ivar_hash = TypeCollector.new.collect(body)
|
||||||
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
||||||
|
create_methods(clazz , body)
|
||||||
|
end
|
||||||
|
|
||||||
MethodCollector.new.collect(body)
|
def create_methods(clazz , body)
|
||||||
|
methods = MethodCollector.new.collect(body)
|
||||||
|
methods.each do |method|
|
||||||
|
clazz.add_method( method )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handler_missing(node)
|
def handler_missing(node)
|
||||||
|
# raise "Oh"
|
||||||
node.children.each do |kid |
|
node.children.each do |kid |
|
||||||
process(kid) if kid.is_a?(AST::Node)
|
process(kid) if kid.is_a?(AST::Node)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def get_name( statement )
|
||||||
|
return nil unless statement
|
||||||
|
statement.children[1]
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,6 +23,7 @@ module Parfait
|
|||||||
super()
|
super()
|
||||||
@name = name
|
@name = name
|
||||||
@super_class_name = superclass
|
@super_class_name = superclass
|
||||||
|
@methods = {}
|
||||||
set_instance_type( instance_type )
|
set_instance_type( instance_type )
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -34,6 +35,14 @@ module Parfait
|
|||||||
"Class(#{name})"
|
"Class(#{name})"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_method(method)
|
||||||
|
@methods[method.name] = method
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_method(name)
|
||||||
|
@methods[name]
|
||||||
|
end
|
||||||
|
|
||||||
# setting the type generates all methods for this type
|
# setting the type generates all methods for this type
|
||||||
# (or will do, once we store the methods code to do that)
|
# (or will do, once we store the methods code to do that)
|
||||||
def set_instance_type( type )
|
def set_instance_type( type )
|
||||||
|
@ -99,10 +99,10 @@ module Parfait
|
|||||||
|
|
||||||
# get or create the class by the (symbol) name
|
# get or create the class by the (symbol) name
|
||||||
# notice that this method of creating classes implies Object superclass
|
# notice that this method of creating classes implies Object superclass
|
||||||
def get_class_by_name! name
|
def get_class_by_name!(name , super_class = :Object)
|
||||||
c = get_class_by_name(name)
|
c = get_class_by_name(name)
|
||||||
return c if c
|
return c if c
|
||||||
create_class name
|
create_class( name ,super_class)
|
||||||
end
|
end
|
||||||
|
|
||||||
# this is the way to instantiate classes (not Parfait::Class.new)
|
# this is the way to instantiate classes (not Parfait::Class.new)
|
||||||
|
@ -1 +1,13 @@
|
|||||||
require_relative '../helper'
|
require_relative '../helper'
|
||||||
|
|
||||||
|
module Melon
|
||||||
|
module CompilerHelper
|
||||||
|
def in_Space(statements)
|
||||||
|
"class Space ; #{statements} ; end"
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_main(statements)
|
||||||
|
in_Space("def main ; #{statements}; end")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
require_relative "test_type_collector"
|
require_relative "test_type_collector"
|
||||||
require_relative "test_method_collector"
|
require_relative "test_method_collector"
|
||||||
require_relative "test_locals_collector"
|
require_relative "test_locals_collector"
|
||||||
|
require_relative "test_class_creation"
|
||||||
|
19
test/melon/compiler/test_method_creation.rb
Normal file
19
test/melon/compiler/test_method_creation.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Melon
|
||||||
|
class TestMethod < MiniTest::Test
|
||||||
|
include CompilerHelper
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Register.machine.boot
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_creates_method_in_class
|
||||||
|
Compiler.compile in_Space("def meth; @ivar;end")
|
||||||
|
space = Parfait.object_space.get_class
|
||||||
|
method = space.get_method(:meth)
|
||||||
|
assert method , "No method created"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -6,12 +6,15 @@ module Melon
|
|||||||
module MelonTests
|
module MelonTests
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@parser = Parser::Ruby22
|
Register.machine.boot
|
||||||
end
|
end
|
||||||
|
|
||||||
def check
|
def check
|
||||||
assert true
|
Compiler.compile @string_input
|
||||||
#puts @parser.parse @string_input
|
Register::Collector.collect_space
|
||||||
|
@interpreter = Register::Interpreter.new
|
||||||
|
@interpreter.start Register.machine.init
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,3 +3,8 @@ require_relative "test_calls"
|
|||||||
require_relative "test_hello"
|
require_relative "test_hello"
|
||||||
require_relative "test_itos"
|
require_relative "test_itos"
|
||||||
require_relative "test_loop"
|
require_relative "test_loop"
|
||||||
|
|
||||||
|
require_relative "test_many_adds"
|
||||||
|
require_relative "test_many_hello"
|
||||||
|
require_relative "test_many_calls"
|
||||||
|
require_relative "test_many_itos"
|
||||||
|
@ -6,11 +6,9 @@ module Melon
|
|||||||
|
|
||||||
|
|
||||||
def test_ruby_hello
|
def test_ruby_hello
|
||||||
@string_input = <<HERE
|
@string_input = 'puts "Hello there"'
|
||||||
puts "Hello there"
|
assert !check
|
||||||
HERE
|
# assert_equal "Hello there" , @interpreter.stdout
|
||||||
@stdout = "Hello there"
|
|
||||||
check
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
require_relative "compiler/test_all"
|
require_relative "compiler/test_all"
|
||||||
require_relative "fragments/test_all"
|
require_relative "fragments/test_all"
|
||||||
require_relative "test_compiler"
|
|
||||||
|
Loading…
Reference in New Issue
Block a user