first oo steps are wobbly, but a string.plus parses
This commit is contained in:
parent
86431120d5
commit
9ea0acf2e3
@ -42,8 +42,6 @@ module Ast
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class VariableExpression < NameExpression
|
|
||||||
end
|
|
||||||
class ModuleName < NameExpression
|
class ModuleName < NameExpression
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -35,4 +35,11 @@ module Ast
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class VariableExpression < CallSiteExpression
|
||||||
|
|
||||||
|
def initialize name
|
||||||
|
super( :_get_instance_variable , [StringExpression.new(name)] )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
@ -16,10 +16,9 @@ module Ast
|
|||||||
end
|
end
|
||||||
def compile context , into
|
def compile context , into
|
||||||
clazz = context.object_space.get_or_create_class name
|
clazz = context.object_space.get_or_create_class name
|
||||||
puts "Class #{clazz.inspect}"
|
context.current_class = clazz
|
||||||
context.class = clazz
|
|
||||||
expressions.each do |expression|
|
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
|
# 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
|
raise "only functions for now #{expression.inspect}" unless expression.is_a? Ast::FunctionExpression
|
||||||
expression_value = expression.compile(context , nil )
|
expression_value = expression.compile(context , nil )
|
||||||
|
@ -20,7 +20,27 @@ module Core
|
|||||||
def function_exit block , f_name
|
def function_exit block , f_name
|
||||||
Vm::RegisterMachine.instance.function_exit block , f_name
|
Vm::RegisterMachine.instance.function_exit block , f_name
|
||||||
end
|
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
|
#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
|
# while all other methods add their code into some block. --> kernel
|
||||||
def putstring context , string = Vm::Integer , length = Vm::Integer
|
def putstring context , string = Vm::Integer , length = Vm::Integer
|
||||||
|
@ -5,23 +5,38 @@ class TestClass < MiniTest::Test
|
|||||||
|
|
||||||
def test_class
|
def test_class
|
||||||
@string_input = <<HERE
|
@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
|
class String
|
||||||
def plus( str)
|
def length()
|
||||||
|
return @length
|
||||||
|
end
|
||||||
|
def plus(str)
|
||||||
my_length = self.length()
|
my_length = self.length()
|
||||||
str_len = str.length()
|
str_len = str.length()
|
||||||
new_string = String.new(my_length + str_len)
|
new_string = String.new(my_length + str_len)
|
||||||
i = 0
|
i = 0
|
||||||
while( i < my_length)
|
while( i < my_length) do
|
||||||
char = self.get(i)
|
char = self.get(i)
|
||||||
new_string.set(i , char)
|
new_string.set(i , char)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
i = 0
|
i = 0
|
||||||
while( i < str_len)
|
while( i < str_len) do
|
||||||
char = str.get(i)
|
char = str.get(i)
|
||||||
new_string.set( i + my_length , char)
|
new_string.set( i + my_length , char)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
|
return new_string
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user