Revert "object: change to a stateful table"
This reverts commit 3c34c4a5a7
.
This commit is contained in:
parent
625fdfeb39
commit
ba26544bec
|
@ -27,7 +27,7 @@
|
||||||
((head '())
|
((head '())
|
||||||
(tail '())
|
(tail '())
|
||||||
(this
|
(this
|
||||||
(object/immutable-attributes
|
(object/procedures
|
||||||
'push!
|
'push!
|
||||||
(lambda (val)
|
(lambda (val)
|
||||||
(set! head (cons val head))
|
(set! head (cons val head))
|
||||||
|
|
97
object.scm
97
object.scm
|
@ -11,64 +11,47 @@
|
||||||
;;; You should have received a copy of the GNU General Public License
|
;;; You should have received a copy of the GNU General Public License
|
||||||
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
;;; Create a new object.
|
|
||||||
;;;
|
;;;
|
||||||
;;; Messages:
|
;;; Handles messages passed to objects. The state of each object is
|
||||||
;;; (SET! KEY VAL): Set KEY in the table to VAL.
|
;;; expected to be in the table or in the environment. Each message
|
||||||
;;; (GET KEY): Get value of KEY in table.
|
;;; invokes a procedure whose name is the first argument to the object,
|
||||||
;;; (ADD-TYPE TYPE): Add TYPE to the list of types of the object.
|
;;; and the arguments to that procedure are the rest of the arguments to
|
||||||
;;; (OF-TYPE? TYPE): Check if object has TYPE in the type list.
|
;;; the object.
|
||||||
;;;
|
|
||||||
;;; Attributes:
|
|
||||||
;;; TYPES: List of types associated with the object.
|
|
||||||
(define object
|
|
||||||
(lambda ()
|
|
||||||
(let ((table (symbol-table)))
|
|
||||||
(table 'set! 'set! (lambda (key val) (table 'set! key val)))
|
|
||||||
(table 'set! 'get (lambda (key) (table 'get key)))
|
|
||||||
(table 'set! 'delete! (lambda (key) (table 'delete! key)))
|
|
||||||
(table 'set! 'types '(object))
|
|
||||||
(table 'set! 'add-type (lambda (type)
|
|
||||||
(table 'set! 'type
|
|
||||||
(cons type (table 'get 'types)))))
|
|
||||||
(table 'set! 'of-type? (lambda (type)
|
|
||||||
(memq type (table 'get 'types))))
|
|
||||||
|
|
||||||
(let ((must-execute-default
|
;;; Lookup NAME-AS-SYMBOL in TABLE and returns the handler, or the default
|
||||||
(lambda (op args)
|
;;; handler if not available.
|
||||||
(let ((proc (table 'get 'default)))
|
(define object:lookup
|
||||||
(if (not proc)
|
(lambda (table name-as-symbol)
|
||||||
(error 'object 'invalid-op op args)
|
(let ((node (smap:search table (symbol->string name-as-symbol))))
|
||||||
(apply proc args))))))
|
(if (null? node)
|
||||||
(lambda (op . args)
|
(set! node (smap:search table "default")))
|
||||||
(let ((proc (table 'get op)))
|
(if (null? node)
|
||||||
(if (not proc)
|
(error "object:lookup" "no handler found for" name-as-symbol)
|
||||||
(must-execute-default op args)
|
(map:val node)))))
|
||||||
(apply proc args))))))))
|
|
||||||
|
|
||||||
;;; (OBJECT/ATTRIBUTES NAME1 VAL1 NAME2 VAL2 ...)
|
;;; Create an object with TABLE as its procedure table.
|
||||||
;;; creates a new object with NAME1 bound to VAL1, NAME2 bound to VAL2,
|
(define object/table
|
||||||
;;; etc.
|
(lambda (table)
|
||||||
(define object/attributes
|
(lambda (op . args)
|
||||||
(lambda args
|
(apply (object:lookup table op) args))))
|
||||||
(let ((obj (object)))
|
|
||||||
(letrec ((process-args
|
|
||||||
(lambda (name val . rest)
|
|
||||||
(obj 'set! name val)
|
|
||||||
(apply check-args rest)))
|
|
||||||
(check-args
|
|
||||||
(lambda args
|
|
||||||
(if (null? args)
|
|
||||||
obj
|
|
||||||
(apply process-args args)))))
|
|
||||||
(apply check-args args)))))
|
|
||||||
|
|
||||||
;;; Like OBJECT/ATTRIBUTES, but with SET!, GET, and DELETE! removed.
|
;;; Append procedures to a table.
|
||||||
(define object/immutable-attributes
|
(define object:append-table
|
||||||
(lambda args
|
(lambda (table next-pairs)
|
||||||
(let ((obj (apply object/attributes args)))
|
(if (null? next-pairs)
|
||||||
(obj 'delete! 'set!)
|
table
|
||||||
(obj 'delete! 'get)
|
(let ((key (symbol->string (car next-pairs)))
|
||||||
(obj 'delete! 'delete!)
|
(proc (cadr next-pairs)))
|
||||||
obj)))
|
(object:append-table
|
||||||
|
(car (smap:insert table key proc))
|
||||||
|
(cddr next-pairs))))))
|
||||||
|
|
||||||
|
;;; Convert a list of 'SYMBOL PROCEDURE ... into a table.
|
||||||
|
(define object:list->table
|
||||||
|
(lambda pairs
|
||||||
|
(object:append-table '() pairs)))
|
||||||
|
|
||||||
|
|
||||||
|
(define object/procedures
|
||||||
|
(lambda procedures
|
||||||
|
(object/table (apply object:list->table procedures))))
|
2
read.scm
2
read.scm
|
@ -77,7 +77,7 @@
|
||||||
(datum-labels '())
|
(datum-labels '())
|
||||||
(fold-case? #f)
|
(fold-case? #f)
|
||||||
(this
|
(this
|
||||||
(object/immutable-attributes
|
(object/procedures
|
||||||
'process
|
'process
|
||||||
(lambda (ch)
|
(lambda (ch)
|
||||||
(this 'update-position! ch)
|
(this 'update-position! ch)
|
||||||
|
|
36
set.scm
36
set.scm
|
@ -467,42 +467,6 @@
|
||||||
smap
|
smap
|
||||||
pairs)))
|
pairs)))
|
||||||
|
|
||||||
;;; SYMBOL-TABLE:
|
|
||||||
;;;
|
|
||||||
;;; A stateful map from symbols to values.
|
|
||||||
;;;
|
|
||||||
;;; (SET! KEY VAL)
|
|
||||||
;;; (DELETE! KEY)
|
|
||||||
;;; (GET KEY)
|
|
||||||
;;; (TYPE)
|
|
||||||
(define symbol-table
|
|
||||||
(lambda ()
|
|
||||||
(let ((table '()))
|
|
||||||
(letrec ((insert!
|
|
||||||
(lambda (key val)
|
|
||||||
(let ((ret (smap:insert table
|
|
||||||
(symbol->string key)
|
|
||||||
val)))
|
|
||||||
(set! table (car ret))
|
|
||||||
(cdr ret))))
|
|
||||||
(delete! (lambda (key) (smap:delete table (symbol->string
|
|
||||||
key))))
|
|
||||||
(search
|
|
||||||
(lambda (key . default)
|
|
||||||
(let ((ret (smap:search table (symbol->string key))))
|
|
||||||
(if (null? ret)
|
|
||||||
(if (null? default)
|
|
||||||
#f
|
|
||||||
(car default))
|
|
||||||
(map:val ret))))))
|
|
||||||
(lambda (op . args)
|
|
||||||
(cond
|
|
||||||
((eq? op 'set!) (apply insert! args))
|
|
||||||
((eq? op 'delete!) (apply delete! args))
|
|
||||||
((eq? op 'get) (apply search args))
|
|
||||||
((eq? op 'type) 'symbol-table)
|
|
||||||
(else (error 'symbol-table 'unknown op args))))))))
|
|
||||||
|
|
||||||
;;; ;;;;;
|
;;; ;;;;;
|
||||||
;;; Tests
|
;;; Tests
|
||||||
;;; ;;;;;
|
;;; ;;;;;
|
||||||
|
|
Loading…
Reference in New Issue