[Grace-core] Class Encoding
Eric Mullen
emullen at hmc.edu
Wed Jun 22 12:10:06 PDT 2011
I'm currently working for Professor Bruce on implementing an
interpreter for Grace, and I was wondering about peoples' thoughts on
a class encoding inspired by and closely based on Andrew Black's
(https://projects.cecs.pdx.edu:8443/~black/NewOOL/index.cgi/wiki/ClassesObjectAndConstructors)
but with some slight modifications.
The following code:
class a {(n:Number) ->
var x:Number := n
}
class b {(m:Number) ->
extends a.new(m+1)
var y:Number := m
}
would be rewritten as:
object a {
method _allocate() {
//returns uninitialized object skeleton
object obj {
var x:Number := Uninitialized
}
obj
}
method _new(obj,n) {
obj := Any._new(obj)
obj.x:=(n)
obj
}
method new(n) {
_new(_allocate(),n)
}
}
object b {
method _allocate() {
//returns uninitialized object skeleton
//has all fields of both class b and all super classes (that
weren't overridden)
object obj {
var x:Number := Uninitialized
var y:Number := Uninitialized
}
obj
}
method _new(obj,m) {
obj := a._new(obj,m+1)
obj.y:=(m)
obj
}
method new(m) {
_new(_allocate(),m)
}
}
This allows for classes to be implemented within the language, while
still allowing concatenation (as opposed to delegation) as the method
for inheritance (http://portal.acm.org/citation.cfm?id=219264, cited
by Andrew Black on the wiki).
More information about the Grace-core
mailing list