modulize class_field and class_statement

This commit is contained in:
Torsten Ruger 2016-12-09 13:46:26 +02:00
parent 2b008dd3da
commit bab2fe32df
6 changed files with 22 additions and 20 deletions

View File

@ -1,9 +1,11 @@
module Typed
class ClassStatement < Statement
attr_accessor :name , :derives , :statements
end
module Tree
class ClassStatement < Statement
attr_accessor :name , :derives , :statements
end
class ClassField < Statement
attr_accessor :name , :type
class ClassField < Statement
attr_accessor :name , :type
end
end
end

View File

@ -1,5 +1,7 @@
module Typed
class FunctionStatement < Statement
attr_accessor :return_type , :name , :parameters, :statements , :receiver
module Tree
class FunctionStatement < Statement
attr_accessor :return_type , :name , :parameters, :statements , :receiver
end
end
end

View File

@ -13,7 +13,7 @@ module Typed
def on_class statement
name , derives , statements = *statement
w = ClassStatement.new()
w = Tree::ClassStatement.new()
w.name = name
w.derives = derives.children.first
w.statements = process(statements)
@ -22,7 +22,7 @@ module Typed
def on_function statement
return_type , name , parameters, statements , receiver = *statement
w = FunctionStatement.new()
w = Tree::FunctionStatement.new()
w.return_type = return_type
w.name = name.children.first
w.parameters = parameters.to_a.collect do |p|

View File

@ -1,7 +1,8 @@
module Typed
CompilerModules = [ "assignment" , "basic_values" , "call_site", "if_statement" ,
"name_expression"]
CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" ,
"class_statement" , "if_statement" , "name_expression"]
CompilerModules.each do |mod|
require_relative "compiler/" + mod
end
@ -188,9 +189,6 @@ end
require_relative "ast_helper"
require_relative "ast/code"
require_relative "compiler/basic_values"
require_relative "compiler/class_field"
require_relative "compiler/class_statement"
require_relative "compiler/collections"
require_relative "compiler/field_def"
require_relative "compiler/field_access"

View File

@ -1,15 +1,14 @@
module Typed
Compiler.class_eval do
module ClassField
def on_ClassField statement
#puts statement.inspect
#type , name , value = *statement
for_class = @clazz
raise "no class" unless for_class
index = for_class.instance_type.variable_index(statement.name)
#raise "class field already defined:#{name} for class #{for_class.name}" if index
#puts "Define field #{name} on class #{for_class.name}"
raise "class field already defined:#{name} for class #{for_class.name}" if index
for_class.instance_type.add_instance_variable( statement.name , statement.type )
return nil # statements don't reurn values, only expressions

View File

@ -1,9 +1,10 @@
module Typed
Compiler.class_eval do
module ClassStatement
def on_ClassStatement statement
#puts statement.inspect
raise "classes dont yet play babushka, get coding #{name}" if @clazz
@clazz = Parfait::Space.object_space.get_class_by_name! statement.name
#puts "Compiling class #{@clazz.name.inspect}"
statement_value = process(statement.statements).last