first oo steps are wobbly, but a string.plus parses

This commit is contained in:
Torsten Ruger 2014-06-01 14:24:54 +03:00
parent 86431120d5
commit 9ea0acf2e3
5 changed files with 48 additions and 9 deletions

View File

@ -42,8 +42,6 @@ module Ast
end
end
class VariableExpression < NameExpression
end
class ModuleName < NameExpression
end

View File

@ -35,4 +35,11 @@ module Ast
end
end
class VariableExpression < CallSiteExpression
def initialize name
super( :_get_instance_variable , [StringExpression.new(name)] )
end
end
end

View File

@ -16,10 +16,9 @@ module Ast
end
def compile context , into
clazz = context.object_space.get_or_create_class name
puts "Class #{clazz.inspect}"
context.class = clazz
context.current_class = clazz
expressions.each do |expression|
# check if it'sa function definition and add
# check if it's a 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
raise "only functions for now #{expression.inspect}" unless expression.is_a? Ast::FunctionExpression
expression_value = expression.compile(context , nil )

View File

@ -20,7 +20,27 @@ module Core
def function_exit block , f_name
Vm::RegisterMachine.instance.function_exit block , f_name
end
# in ruby, how this goes is
# def _get_instance_variable var
# i = self.index_of(var)
# return at_index(i)
# end
# The at_index is just "below" the api, somehting we need but don't want to expose, so we can't code the above in ruby
def _get_instance_variable context , name = Vm::Integer
get_function = Vm::Function.new(:_get_instance_variable , [Vm::Integer , Vm::Integer ] , Vm::Integer )
me = get_function.args[0]
var_name = get_function.args[1]
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
b = get_function.body
b.push( me )
index = b.call( index_function )
b.pop(me)
b.mov( var_name , index )
Vm::RegisterMachine.instance.at_index( get_function.body , number , remainder )
return get_function
end
#TODO this is in the wrong place. It is a function that returns a function object
# while all other methods add their code into some block. --> kernel
def putstring context , string = Vm::Integer , length = Vm::Integer

View File

@ -5,23 +5,38 @@ class TestClass < MiniTest::Test
def test_class
@string_input = <<HERE
class Object
def layout()
return @layout
end
def class()
return layout.class()
end
def index_of( name )
return layout.index_of(name)
end
end
class String
def plus( str)
def length()
return @length
end
def plus(str)
my_length = self.length()
str_len = str.length()
new_string = String.new(my_length + str_len)
i = 0
while( i < my_length)
while( i < my_length) do
char = self.get(i)
new_string.set(i , char)
i = i + 1
end
i = 0
while( i < str_len)
while( i < str_len) do
char = str.get(i)
new_string.set( i + my_length , char)
i = i + 1
end
return new_string
end
end
HERE