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 identifierDescription:
classmergeallows merging two existing classes and adding to a third class. This could be added up tonclasses and all the methods and fields innclasses are inherited to the class(n+1)^th.
setfield-exp: Identifier x Identifier x Expression -> Unspecifiedgetfield-exp: Identifier x Identifier -> ExpvalExpval = Int + Bool + Proc + listof(expval) + objectDenval = 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-classdatatype 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-envof both classes, and update this to the global class environmentthe-class-envby 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-namesandconstruct-fields:- Construct the
fieldsandfield-namesinto 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
c1andc2, then add to classc4. Now, we create an instanceowith all properties of classc4which inherits from classc1andc2. -
When
field 1in classc1is set to5, andf1in instanceois set to20, and setf4in classc2to15, and then setf4in the instanceoto30, we get the list of result as follows(20 5 15 30). This means that the instanceohas all properties of classc4which is inherited from classc1andc2.