introducing class variable and typed arguments

This commit is contained in:
Torsten Ruger
2015-09-27 14:30:41 +03:00
parent 94c08f7129
commit 18935366fe
8 changed files with 37 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ module Parfait
# static description of a method
# name
# arg_names
# arguments
# known local variable names
# executable code
@@ -18,17 +18,20 @@ module Parfait
class Method < Object
def initialize clazz , name , arg_names
def initialize clazz , name , arguments
super()
raise "No class #{name}" unless clazz
self.for_class = clazz
self.name = name
self.code = BinaryCode.new name
raise "Wrong type, expect List not #{arg_names.class}" unless arg_names.is_a? List
self.arg_names = arg_names
raise "Wrong type, expect List not #{arguments.class}" unless arguments.is_a? List
arguments.each do |var|
raise "Must be variable argument, not #{var}" unless var.is_a? Variable
end
self.arguments = arguments
self.locals = List.new
end
attributes [:name , :arg_names , :for_class , :code , :locals ]
attributes [:name , :arguments , :for_class , :code , :locals ]
# determine whether this method has a variable by the given name
@@ -45,7 +48,7 @@ module Parfait
# determine whether this method has an argument by the name
def has_arg name
raise "has_arg #{name}.#{name.class}" unless name.is_a? Symbol
self.arg_names.index_of name
self.arguments.index_of name
end
# determine if method has a local variable or tmp (anonymous local) by given name
@@ -64,7 +67,7 @@ module Parfait
def get_var name
var = has_var name
raise "no var #{name} in method #{self.name} , #{self.locals} #{self.arg_names}" unless var
raise "no var #{name} in method #{self.name} , #{self.locals} #{self.arguments}" unless var
var
end

View File

@@ -58,12 +58,12 @@ module Parfait
self.instance_methods.delete found
end
def create_instance_method method_name , arg_names
def create_instance_method method_name , arguments
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
clazz = object_layout().object_class()
raise "??? #{method_name}" unless clazz
#puts "Self: #{self.class} clazz: #{clazz.name}"
Method.new( clazz , method_name , arg_names )
Method.new( clazz , method_name , arguments )
end
# this needs to be done during booting as we can't have all the classes and superclassses

9
lib/parfait/variable.rb Normal file
View File

@@ -0,0 +1,9 @@
module Parfait
class Variable < Object
def initialize type , name , value = nil
@type , @name , @value = type , name , value
@value = 0 if @type == :int and value == nil
end
attributes [:type , :name, :value]
end
end