;; interpreter's data types: word and program ;; TERMINOLOGY: ;; a WORD is a data atom with semantics attached, and a procedure cache. ;; a PROGRAM is a list of words = chained cons cells. (AST) ;; an ATOM is any data type serving as the argument for COMPILE ;; a PROCEDURE is a scheme procedure: stack -> stack ;; COMPILE represents word semantics as: atom -> procedure ;; FIND is: symbol -> word ;; PARSING is the translation of source to PROGRAM, basicly ;; associating ATOM to COMPILE semantics. (module word mzscheme (provide (all-defined)) (define-struct word (source ;; source representation compile ;; delayed compilation (thing -> executable) cached)) ;; cached executable code (define (program? p) (or (null? p) (and (pair? p) (word? (car p)) (program? (cdr p))))) (define (word->atom w) (word-source w)) (define (program->list p) (map word->atom p)) (define (atom->word atom compile) (make-word atom compile #f)) (define (list->program lst compile) (map (lambda (a) (make-word a compile #f)) lst)) ;; compile (define (word-recompile! word) (set-word-cached! word ((word-compile word) (word-source word))) ;; (write (word-source word)) (newline) ) ;; cache/compile (define (word->procedure word) (or (word-cached word) (begin (word-recompile! word) (word-cached word)))) ;; wrap a primitive (define (procedure->word name proc) (if (procedure? proc) (make-word name #f proc) (raise `(not-a-procedure ,proc)))) ) ;; NOTES ;; i'm not sure if 'program' should be abstract too (more than just a ;; list of words). it would be benificial to reflection if this wasn't ;; necessary: keeps code transparent. ;; since CAT is written in scheme, choices need to be made to what ;; point CAT needs to know its own implementation. one of those ;; choices is the representation of compile semantics. (another is ;; representation of compilers and parsers) should this be extendable ;; in CAT code, or do we keep it as a 'constant' implemented in scheme ;; only?