fix class functions

some extra tests in the process
This commit is contained in:
Torsten Ruger 2015-09-20 15:51:08 +03:00
parent c331bdd80a
commit 20b6c9b476
4 changed files with 174 additions and 10 deletions

View File

@ -74,12 +74,13 @@ module Parser
s(:expressions , *expressions))
end
rule(:type => simple(:type) , :receiver=> simple(:receiver),
rule(:type => simple(:type) ,
:receiver=> simple(:receiver),
:function_name => simple(:function_name),
:parameter_list => simple(:parameter_list),
:parameter_list => sequence(:parameter_list),
:expressions => sequence(:expressions) , :end => simple(:e)) do
s(:function, type.to_sym , function_name, s(:parameters , *parameter_list ),
s(:expressions , *expressions) , receiver)
s(:expressions , *expressions) , s(:receiver , *receiver))
end
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
@ -92,13 +93,13 @@ module Parser
end
#modules and classes are understandibly quite similar Class < Module
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
s(:module , module_name.to_s.to_sym , *module_expressions)
end
rule( :module_name => simple(:module_name) , :derived_name => simple(:derived_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
s(:class , module_name.to_s.to_sym ,
s(:derives, derived_name ? derived_name.to_a.first.to_sym : nil) , *class_expressions)
end
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
s(:module , module_name.to_s.to_sym , *module_expressions)
end
rule(:expression_list => sequence(:expression_list)) {
s(:expressions , *expression_list)

View File

@ -0,0 +1,160 @@
class String
ref self.new_string(int len )
len = len << 2
return super.new_object( len)
end
int length()
return self.length
end
int plus(ref str)
my_length = self.length
str_len = str.length()
my_length = str_len + my_length
new_string = self.new_string(my_length )
i = 0
while( i < my_length)
char = get(i)
new_string.set(i , char)
i = i + 1
end
i = 0
while( i < str_len)
char = str.get(i)
len = i + my_length
new_string.set( len , char)
i = i + 1
end
return new_string
end
end
-- -- --
s(:expressions,
s(:class, :String,
s(:derives, nil),
s(:function, :ref,
s(:name, :new_string),
s(:parameters,
s(:parameter, :int, :len)),
s(:expressions,
s(:assign,
s(:name, :len),
s(:operator, "<<",
s(:name, :len),
s(:int, 2))),
s(:return,
s(:call,
s(:name, :new_object),
s(:arguments,
s(:name, :len)),
s(:receiver,
s(:name, :super))))),
s(:receiver, :self)),
s(:function, :int,
s(:name, :length),
s(:parameters),
s(:expressions,
s(:return,
s(:field_access,
s(:receiver,
s(:name, :self)),
s(:field,
s(:name, :length)))))),
s(:function, :int,
s(:name, :plus),
s(:parameters,
s(:parameter, :ref, :str)),
s(:expressions,
s(:assign,
s(:name, :my_length),
s(:field_access,
s(:receiver,
s(:name, :self)),
s(:field,
s(:name, :length)))),
s(:assign,
s(:name, :str_len),
s(:call,
s(:name, :length),
s(:arguments),
s(:receiver,
s(:name, :str)))),
s(:assign,
s(:name, :my_length),
s(:operator, "+",
s(:name, :str_len),
s(:name, :my_length))),
s(:assign,
s(:name, :new_string),
s(:call,
s(:name, :new_string),
s(:arguments,
s(:name, :my_length)),
s(:receiver,
s(:name, :self)))),
s(:assign,
s(:name, :i),
s(:int, 0)),
s(:while,
s(:condition,
s(:operator, "<",
s(:name, :i),
s(:name, :my_length))),
s(:expressions,
s(:assign,
s(:name, :char),
s(:call,
s(:name, :get),
s(:arguments,
s(:name, :i)))),
s(:call,
s(:name, :set),
s(:arguments,
s(:name, :i),
s(:name, :char)),
s(:receiver,
s(:name, :new_string))),
s(:assign,
s(:name, :i),
s(:operator, "+",
s(:name, :i),
s(:int, 1))))),
s(:assign,
s(:name, :i),
s(:int, 0)),
s(:while,
s(:condition,
s(:operator, "<",
s(:name, :i),
s(:name, :str_len))),
s(:expressions,
s(:assign,
s(:name, :char),
s(:call,
s(:name, :get),
s(:arguments,
s(:name, :i)),
s(:receiver,
s(:name, :str)))),
s(:assign,
s(:name, :len),
s(:operator, "+",
s(:name, :i),
s(:name, :my_length))),
s(:call,
s(:name, :set),
s(:arguments,
s(:name, :len),
s(:name, :char)),
s(:receiver,
s(:name, :new_string))),
s(:assign,
s(:name, :i),
s(:operator, "+",
s(:name, :i),
s(:int, 1))))),
s(:return,
s(:name, :new_string))))))

View File

@ -1,6 +1,7 @@
class Pifi
ofthen(3 , var)
int ofthen(int n , ref m)
int self.ofthen(int n , ref m)
n = n + m.index
return n
end
@ -30,4 +31,5 @@ s(:expressions,
s(:field,
s(:name, :index))))),
s(:return,
s(:name, :n))))))
s(:name, :n))),
s(:receiver, :self))))

View File

@ -1,4 +1,4 @@
int length( ref x )
int self.length( ref x )
length
end
-- -- --
@ -8,4 +8,5 @@ s(:expressions,
s(:parameters,
s(:parameter, :ref, :x)),
s(:expressions,
s(:name, :length))))
s(:name, :length)),
s(:receiver, :self)))