From: Timothy H. <tim...@ma...> - 2002-05-26 20:13:39
|
On Sunday, May 26, 2002, at 02:15 PM, Dazhi Liu wrote: > BTW, a quick question: After user program in JScheme for a while, is > there an easy way to erase all user defined bindings, that is, to return > to the initial state of JScheme? > Its not too hard. At the end of this note is a procedure that creates a SymbolTable restorer. You use it as > (define z (make-state-restorer)) .... > (z) and it will restore your state to the initial one. ;; This is a System restorer ;; It makes a copy of the Symbol table state which it stores in its returned value ;; > (define z (make-state-restorer)) ;; When you later invoke the thunk ;; > (z) ;; it resets the Symbol table to its ;; original values (but it doesn't reset the javadot primitives as this would just ;; waste time). (define (make-state-restorer) (let ((ST0 (java.util.Hashtable.)) (isJavadot (lambda (x) (or (> (.indexOf x ".") -1) (> (.indexOf x "$") -1)))) (enum->list (lambda(E) (let loop ((L ())) (if (.hasMoreElements E) (loop (cons (.nextElement E) L)) L))))) (for-each (lambda(x) (if (not (isJavadot x)) ;; don't remember javadots! (.put ST0 x (tryCatch (list (.getGlobalValue (string->symbol x))) (lambda(e) ()))))) (enum->list (.keys jsint.Symbol.symbolTable$))) (display (length (enum->list (.keys ST0))))(newline) (lambda() (let ((ST jsint.Symbol.symbolTable$)) (for-each (lambda(x) (let ((v (.get ST0 x))) (if (equal? #null v) (if (not (isJavadot x)) (begin (display x)(newline) (.remove ST x))) (let ((s (string->symbol x))) (.put ST x s) (if (not (null? v)) (.setGlobalValue s (first v))))))) (enum->list (.keys ST))))))) |