From 3713d0874872ca6363ab7258570d2dc5d27941c1 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 31 May 2014 12:52:29 +0300 Subject: [PATCH] renamed program to boot_space, as in object_space at boot time. thats the way its going --- lib/ast/basic_expressions.rb | 2 +- lib/ast/call_site_expression.rb | 2 +- lib/ast/function_expression.rb | 2 +- lib/ast/module_expression.rb | 7 ++++++ lib/core/array.rb | 36 ++++++++++++++++++++++++++++ lib/core/kernel.rb | 4 ++-- lib/crystal.rb | 2 +- lib/elf/object_writer.rb | 2 +- lib/vm/boot_class.rb | 5 ++++ lib/vm/boot_memory.rb | 0 lib/vm/{program.rb => boot_space.rb} | 7 +++--- lib/vm/context.rb | 6 ++--- test/arm/test_small_program.rb | 18 +++++++------- test/fragments/helper.rb | 8 +++---- test/fragments/test_class.rb | 34 ++++++++++++++++++++++++++ test/test_runner.rb | 2 +- 16 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 lib/core/array.rb create mode 100644 lib/vm/boot_class.rb create mode 100644 lib/vm/boot_memory.rb rename lib/vm/{program.rb => boot_space.rb} (92%) create mode 100644 test/fragments/test_class.rb diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index f73193a5..6d998ed7 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -58,7 +58,7 @@ module Ast def compile context , into value = Vm::StringConstant.new(string) - context.program.add_object value + context.object_space.add_object value value end def attributes diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index 73a63e17..9805daf7 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -8,7 +8,7 @@ module Ast end def compile context , into params = args.collect{ |a| a.compile(context, into) } - function = context.program.get_or_create_function(name) + function = context.object_space.get_or_create_function(name) raise "Forward declaration not implemented (#{name}) #{inspect}" if function == nil call = Vm::CallSite.new( name , params , function) current_function = context.function diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index b3836682..d5abf096 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -27,7 +27,7 @@ module Ast args << arg_value end function = Vm::Function.new(name , args ) - context.program.add_function function + context.object_space.add_function function parent_locals = context.locals parent_function = context.function diff --git a/lib/ast/module_expression.rb b/lib/ast/module_expression.rb index d4802067..a331f668 100644 --- a/lib/ast/module_expression.rb +++ b/lib/ast/module_expression.rb @@ -15,6 +15,13 @@ module Ast [:name , :expressions] end def compile context , into + # create the class or module + # check if it'sa function definition and add + # if not, execute it, but that does means we should be in crystal (executable), not ruby. ie throw an error for now + clazz = context.object_space.get_or_create_class name + + + expression_value = expression.compile(context , into) puts "compiled return expression #{expression_value.inspect}, now return in 7" # copied from function expression: TODO make function diff --git a/lib/core/array.rb b/lib/core/array.rb new file mode 100644 index 00000000..ab588db7 --- /dev/null +++ b/lib/core/array.rb @@ -0,0 +1,36 @@ +# this is not a "normal" ruby file, ie it is not required by crystal +# instead it is parsed by crystal to define part of the crystal that runs + +class BaseObject + + def _set_instance_variable(name , value) + + end + + def _get_instance_variable( name ) + + end + + def _get_singleton_method(name ) + + end + def _add_singleton_method(method) + + end + def initialize + end +end + +class Array < BaseObject + def initialize size + + end + + def at(index) + + end + + def set(index , value) + + end +end diff --git a/lib/core/kernel.rb b/lib/core/kernel.rb index 6c67f78c..19b762c8 100644 --- a/lib/core/kernel.rb +++ b/lib/core/kernel.rb @@ -35,10 +35,10 @@ module Core def putint context , arg = Vm::Integer putint_function = Vm::Function.new(:putint , [arg] , arg ) buffer = Vm::StringConstant.new(" ") # create a buffer - context.program.add_object buffer # and save it (function local variable: a no no) + context.object_space.add_object buffer # and save it (function local variable: a no no) int = putint_function.args.first moved_int = putint_function.new_local - utoa = context.program.get_or_create_function(:utoa) + utoa = context.object_space.get_or_create_function(:utoa) b = putint_function.body b.mov( moved_int , int ) #move arg up #b.a buffer => int # string to write to diff --git a/lib/crystal.rb b/lib/crystal.rb index 260ee282..c41807c0 100644 --- a/lib/crystal.rb +++ b/lib/crystal.rb @@ -4,6 +4,6 @@ require "elf/object_writer" require 'parser/crystal' require 'parser/transform' require "vm/register_machine" -require "vm/program" +require "vm/boot_space" require "stream_reader" require "core/kernel" diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index a243aa3e..f7a09198 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -8,7 +8,7 @@ module Elf class ObjectWriter def initialize(program , target) @object = Elf::ObjectFile.new(target) - @program = program + @object_space = program sym_strtab = Elf::StringTableSection.new(".strtab") @object.add_section sym_strtab @symbol_table = Elf::SymbolTableSection.new(".symtab", sym_strtab) diff --git a/lib/vm/boot_class.rb b/lib/vm/boot_class.rb new file mode 100644 index 00000000..cf53e4ef --- /dev/null +++ b/lib/vm/boot_class.rb @@ -0,0 +1,5 @@ + +module Vm + class BootClass + end +end \ No newline at end of file diff --git a/lib/vm/boot_memory.rb b/lib/vm/boot_memory.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/vm/program.rb b/lib/vm/boot_space.rb similarity index 92% rename from lib/vm/program.rb rename to lib/vm/boot_space.rb index dc3b8cc4..982b035e 100644 --- a/lib/vm/program.rb +++ b/lib/vm/boot_space.rb @@ -5,8 +5,8 @@ require "arm/arm_machine" require "core/kernel" module Vm - # A Program represents an executable that we want to build - # it has a list of functions and (global) objects + # The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo + # it is a collection of objects, some of which are data, some classes, some functions # The main entry is a function called (of all things) "main", This _must be supplied by the compling # There is a start and exit block that call main, which receives an array of strings @@ -18,7 +18,7 @@ module Vm # throwing in a context for unspecified use (well one is to pass the programm/globals around) - class Program < Code + class BootSpace < Code # Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX # with a XXXMachine in it that derives from Vm::RegisterMachine @@ -44,6 +44,7 @@ module Vm def add_object o return if @objects.include? o + raise "must be derived from Code #{o.inspect}" unless o.is_a? Code @objects << o # TODO check type , no basic values allowed (must be wrapped) end diff --git a/lib/vm/context.rb b/lib/vm/context.rb index 47acc1ee..67b7d0d9 100644 --- a/lib/vm/context.rb +++ b/lib/vm/context.rb @@ -1,14 +1,14 @@ require "support/hash_attributes" module Vm - #currently just holding the program in here so we can have global access + #currently just holding the object_space in here so we can have global access class Context # Make hash attributes to object attributes include Support::HashAttributes - def initialize program + def initialize object_space @attributes = {} - @attributes[:program] = program + @attributes[:object_space] = object_space end attr_reader :attributes end diff --git a/test/arm/test_small_program.rb b/test/arm/test_small_program.rb index e9c655df..064d04ca 100644 --- a/test/arm/test_small_program.rb +++ b/test/arm/test_small_program.rb @@ -9,12 +9,12 @@ require_relative 'helper' class TestSmallProg < MiniTest::Test # need a code generator, for arm def setup - @program = Vm::Program.new "Arm" + @object_space = Vm::BootSpace.new "Arm" end def test_loop r0 = Vm::Integer.new(0) - m = @program.main.scope binding + m = @object_space.main.scope binding m.r0 = 5 #1 s = m.new_block("loop").scope binding @@ -26,13 +26,13 @@ class TestSmallProg < MiniTest::Test def test_hello hello = Vm::StringConstant.new "Hello Raisa\n" - @program.add_object hello + @object_space.add_object hello # these are only here because it's a test program, usually all coding happens with values r0 = Vm::Integer.new(0) r1 = Vm::Integer.new(1) r2 = Vm::Integer.new(2) r7 = Vm::Integer.new(7) - b = @program.main.scope binding + b = @object_space.main.scope binding b.r7 = 4 # 4 == write b.r0 = 1 # stdout b.r1 = hello # address of "hello Raisa" @@ -46,20 +46,20 @@ class TestSmallProg < MiniTest::Test # not my hand off course, found in the net from a basic introduction def test_fibo int = Vm::Integer.new(1) - fibo = @program.get_or_create_function(:fibo) - main = @program.main.scope binding + fibo = @object_space.get_or_create_function(:fibo) + main = @object_space.main.scope binding main.int = 10 ret = main.call( fibo ) main.mov( :r1 , :r7 ) - putint = @program.get_or_create_function(:putint) - @program.main.call( putint ) + putint = @object_space.get_or_create_function(:putint) + @object_space.main.call( putint ) @should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x4,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x22,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0x1,0x70,0xa0,0xd1,0xe,0xf0,0xa0,0xd1,0x1c,0x40,0x2d,0xe9,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x2,0x20,0x41,0xe2,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x52,0xe2,0xfb,0xff,0xff,0x5a,0x3,0x70,0xa0,0xe1,0x1c,0x80,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20] write "fibo" end #helper to write the file def write name - writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) + writer = Elf::ObjectWriter.new(@object_space , Elf::Constants::TARGET_ARM) assembly = writer.text # use this for getting the bytes to compare to : #puts assembly diff --git a/test/fragments/helper.rb b/test/fragments/helper.rb index 808c44ca..01c8d91f 100644 --- a/test/fragments/helper.rb +++ b/test/fragments/helper.rb @@ -13,7 +13,7 @@ require 'parslet/convenience' module Fragments # need a code generator, for arm def setup - @program = Vm::Program.new "Arm" + @object_space = Vm::BootSpace.new "Arm" end def parse @@ -24,9 +24,9 @@ module Fragments # and the last is wrapped as a main parts.each_with_index do |part,index| if index == (parts.length - 1) - expr = part.compile( @program.context , @program.main ) + expr = part.compile( @object_space.context , @object_space.main ) else - expr = part.compile( @program.context , nil ) + expr = part.compile( @object_space.context , nil ) raise "should be function definition for now" unless expr.is_a? Vm::Function end end @@ -34,7 +34,7 @@ module Fragments # helper to write the file def write name - writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) + writer = Elf::ObjectWriter.new(@object_space , Elf::Constants::TARGET_ARM) assembly = writer.text # use this for getting the bytes to compare to : #puts assembly diff --git a/test/fragments/test_class.rb b/test/fragments/test_class.rb new file mode 100644 index 00000000..e0eaa46b --- /dev/null +++ b/test/fragments/test_class.rb @@ -0,0 +1,34 @@ +require_relative 'helper' + +class TestClass < MiniTest::Test + include Fragments + + def test_base_class + @string_input = <