Author: Tung Thanh Le
Contact: ttungl at gmail dot com
Used Scheme (Dr. Racket) to modify the interpreter for creating new functions of a language. In this work, multi-class inheritance is created. A new instance generated is inherited to all the methods from the joined classes. Used Scheme language for implementation.
This work extends a new capability of a Classes
language in Scheme. It allows a class that can be created from two existing classes and owns its inherited properties (fields & methods) from two existing classes. It also allows to set/get a value of a class’s field.
-
Program::= {ClassDecl}* {ClassExtendMerge} * expression
-
ClassDecl::= class Identifier {field Identifier}* {MethodDecl}*
-
MethodDecl::= method Identifier ({Identifier}*(,)) expression
-
ClassExtendMerge::= classmerge Identifier Identifier Identifier
-
Expression::= setfield identifier identifier identifier
-
Expression::= getfield identifier identifier
Description:
classmerge
allows merging two existing classes and adding to a third class. This could be added up ton
classes and all the methods and fields inn
classes are inherited to the class(n+1)^th
.
setfield-exp: Identifier x Identifier x Expression -> Unspecified
getfield-exp: Identifier x Identifier -> Expval
Expval = Int + Bool + Proc + listof(expval) + object
Denval = Ref(expval)
Data-types
:Object
: add a field-names.Method
: eliminate a super-name.Class
: replace super-name by prototype, and add the fields value.a-class
datatype has the prototype, the field-names and fields value, and method-env.
proto-merge function
:- This allows two existing classes can be merged and be added to a new class by appending
fields
,field-names
, and mergingmethod-env
of both classes, and update this to the global class environmentthe-class-env
by usingadd-to-class-env!
with the new class merged.
- This allows two existing classes can be merged and be added to a new class by appending
Find-method
:- Find a method based on the method name in the class’s prototype.
Construct-field-names
andconstruct-fields
:- Construct the
fields
andfield-names
into the class’s prototypes.
- Construct the
add-to-class-env!
:- Update a new class to the list of the global class environment
the-class-env
. It also checks the class is whether or not in the existing class of the list, if so, it updates the latest one.
- Update a new class to the list of the global class environment
Update-class-env
:- Updates a new class to the list of the global class environment.
class c1
field f1
field f2
field f4
method initialize() set f1 = 1
method getf4() f4
class c2
field f3
field f4
method initialize() set f2 = 2
method setf1(n)set f1 = n
method setf2(n)set f2 = n
method setf3(n)set f3 = n
method setf4(n)set f4 = n
method getf1() f1
method getf2() f2
method getf3() f3
classmerge c4 = c1 & c2
let o = new c4()
t1=0
t2=0
t3=0
t4=0
in begin
setfield c1 f1 = 5;
send o setf1(20);
set t2 = getfield c1 f1;
set t1 = send o getf1();
send o setf4(30);
setfield c2 f4 =15;
set t3 = getfield c2 f4;
set t4 = send o getf4();
list(t1,t2,t3,t4)
end
=> (20 5 15 30)
-
We merge all the fields and methods of class
c1
andc2
, then add to classc4
. Now, we create an instanceo
with all properties of classc4
which inherits from classc1
andc2
. -
When
field 1
in classc1
is set to5
, andf1
in instanceo
is set to20
, and setf4
in classc2
to15
, and then setf4
in the instanceo
to30
, we get the list of result as follows(20 5 15 30)
. This means that the instanceo
has all properties of classc4
which is inherited from classc1
andc2
.