-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Description
I m creating a Shen OO capability based on Paul Graham's book On Lisp,
Ch 25, Object-Oriented LISP.
A version of this capability is in the file oo.shen.
An attempt to track the function (create-class...) makes Shen crash:
Shen, copyright (C) 2010-2015 Mark Tarver
www.shenlanguage.org, Shen 21
running under Common Lisp, implementation: Clozure CL
port 2.2 ported by Mark Tarver
(0-) (load "oo.shen")
putp
getp
defclass-macro
oo.process-slots
instance
oo.type#o-o
oo.attribute-type
class?
oo.create-class
instance
assign
cl-slots
cl-parents
cl-name
make-instance
iget
iput
[]
run time: 0.4719999972730875 secs
loaded
(1-) (track oo.create-class)
oo.create-class
(2-) (step +)
true
(3-) (defclass int (object) val number 0) \\ integer class
<1> Inputs to oo.create-class
int, [object], [[val 0]], ==>The value #<BASIC-CHARACTER-INPUT-STREAM UTF-8 (TTY/0) #x3020006233FD> is not of the expected type (AND INPUT-STREAM CCL::BINARY-STREAM).
yours, Dr Antti J Ylikoski
Helsinki, Finland, the EU
I did not find a way to attach a file, I will insert the Shen code below:
(package oo [defclass defclass-macro assign instance attribute class
make-instance iget iput object
cl-slots cl-parents cl-name
class?
putp getp ]
(define putp
Obj Prop Val ->
(lisp.setf (lisp.get Obj Prop) Val))
(define getp
Obj Prop ->
(lisp.get Obj Prop))
\\ (defclass class-name (list of superclasses) (list of slots))
(defmacro defclass-macro
[defclass Class SuperClasses | Slots]
-> (create-class Class SuperClasses (process-slots Class Slots)))
(define process-slots
Class [Attribute Type Value] -> \\ one slot?
(do
(putp Class Attribute Type) \\ stored as properties
[[Attribute Value]])
Class [Attribute Type] -> \\ without the value
(do
(putp Class Attribute Type) [[Attribute]])
Class [Attribute Type Value , | Slots] -> \\ a list of slots?
(do
(putp Class Attribute Type)
[[Attribute Value] | (process-slots Class Slots)])
Class [Attribute Type , | Slots] -> \\ list of slots?
(do
(putp Class Attribute Type)
[[Attribute] | (process-slots Class Slots)])
Class [] -> [[]] \\ no slots: modify nothing
Class _ -> \\ otherwise: there is a syntax error
(error "syntax error in class definition of ~A~%" Class))
(declare instance [[class Class] --> [instance Class]])
(datatype o-o
if (class? Class)
______________________
Class : (class Class);
if (= (attribute-type Class Attribute) Type)
__________________________________
Attribute : (attribute Class Type);
Instance : (instance Class);
Value : A;
Attribute : (attribute Class A);
_____________________________________________________
(assign Instance Attribute Value) : (instance Class);)
(define attribute-type
Class Attribute -> (trap-error (getp Class Attribute) (/. E [])))
(define class?
Class -> (cons? (trap-error (getp Class slots) (/. E false))))
(define create-class
Class SuperClasses Slots ->
(let SuperSlots (mapcan (function instance) SuperClasses)
ClassSlots (append Slots SuperSlots)
Create (putp Class slots ClassSlots) \\ property slots: list
Paren (putp Class parents SuperClasses) \\ super i e parents
Name (putp Class class-name Class) \\ her name
Class))
\\ instance creates an instance of the class,
\\ which is a list of lists
(define instance
Class -> (getp Class slots))
\\ assign assigns the value of a slot of an instance
(define assign
[[Attribute | _] | Slots] Attribute Value ->
[[Attribute Value] | Slots]
[Slot | Slots] Attribute Value ->
[Slot | (assign Slots Attribute Value)])
\\ Call: (cl-slots classname)
(define cl-slots
Class -> (getp Class slots))
(define cl-parents
Class -> (getp Class parents))
(define cl-name
Class -> (getp Class class-name))
\\ Call: (set my-instance (make-instance object))
\\ Returns the class object which is the representation of the
\\ class instance
(define make-instance
Class ->
(let
Ignore (putp
Class
instanceSlots
(instance (value Class)))
Class))
\\ Call: (iget (value my-instance) slot-name)
(define iget
InstanceName SlotName ->
(head (tail (head (assoc SlotName
(getp InstanceName instanceSlots))))))
\\ Call: (iput (value my-instance) slot-name value)
(define iput
InstanceName SlotName Val ->
(let
Lst (getp InstanceName instanceSlots)
NewLst (assign Lst SlotName Val)
(putp InstanceName instanceSlots NewLst)))
\*
object is the common superclass of all objects. The class object is
its own superclass, ie. parent AJY 2018-04-18
*\
(do
(putp object slots [])
(putp object parents [object]) \\ super i e parents: her own super
(putp object class-name object) \\ her name
[])
)Reactions are currently unavailable