\f
;;;; Syntax
-(define-automatic-property 'scode
- '(name imports-used exports bound-names contents)
- #f
- (lambda (name imports exports bound-names contents)
- (make-scode-declaration
- `((target-metadata
- (library (name ,name)
- (imports ,@(map library-import->list imports))
- (exports ,@(map library-export->list exports))
- (bound-names ,@bound-names))))
- (make-scode-quotation contents))))
-
-(define (eval-r7rs-source source db)
- (let ((program (register-r7rs-source! source db)))
- (if program
- (scode-eval (library-contents program)
- (library-environment program)))))
-
(define-automatic-property '(contents bound-names imports-used)
'(parsed-contents imports exports imports-environment)
#f
import-environments-available?
make-environment-from-imports)
-(define-automatic-property 'environment '(imports-environment contents)
- #f
- (lambda (env contents)
- (scode-eval contents env)
- env))
-
(define (environment . import-sets)
(let ((parsed (map parse-import-set import-sets)))
(let ((unusable (remove parsed-import-expandable? parsed)))
(imports->environment
(expand-parsed-imports parsed host-library-db)
host-library-db)))
+\f
+;;;; SCode representation
+
+(define-automatic-property 'scode
+ '(name imports-used exports bound-names contents)
+ #f
+ (lambda (name imports exports bound-names contents)
+ (make-scode-library (make-scode-library-metadata name imports exports
+ bound-names)
+ contents)))
+
+(define (make-scode-library metadata contents)
+ (make-scode-declaration `((target-metadata ,metadata))
+ (make-scode-quotation contents)))
+
+(define (scode-library? object)
+ (and (scode-declaration? object)
+ (let ((text (scode-declaration-text object)))
+ (and (singleton-list? text)
+ (target-metadata? (car text))
+ (let ((metadata-values (metadata-elt-values (car text))))
+ (and (singleton-list? metadata-values)
+ (scode-library-metadata? (car metadata-values))))))
+ (scode-quotation? (scode-declaration-expression object))))
+
+(define (scode-library-metadata library)
+ (car (metadata-elt-values (car (scode-declaration-text library)))))
+
+(define (scode-library-contents library)
+ (scode-quotation-expression (scode-declaration-expression library)))
+
+(define (make-scode-library-metadata name imports exports bound-names)
+ `(scode-library (name ,name)
+ (imports ,@(map library-import->list imports))
+ (exports ,@(map library-export->list exports))
+ (bound-names ,@bound-names)))
+
+(define (scode-library-property keyword library)
+ (metadata-elt-values
+ (find (lambda (metadata)
+ (eq? (metadata-elt-keyword metadata) keyword))
+ (metadata-elt-values (scode-library-metadata library)))))
+
+(define (scode-library-name library)
+ (car (scode-library-property 'name library)))
+
+(define (scode-library-imports library)
+ (map list->library-import (scode-library-property 'imports library)))
+
+(define (scode-library-exports library)
+ (map list->library-export (scode-library-property 'exports library)))
+
+(define (singleton-list? object)
+ (and (pair? object)
+ (null? (cdr object))))
+
+(define (specific-metadata-predicate keyword)
+ (lambda (object)
+ (and (metadata-elt? object)
+ (eq? (metadata-elt-keyword object) keyword)
+ (every metadata-elt? (metadata-elt-values object)))))
+
+(define target-metadata? (specific-metadata-predicate 'target-metadata))
+(define scode-library-metadata? (specific-metadata-predicate 'scode-library))
+
+(define (metadata-elt? object)
+ (and (pair? object)
+ (symbol? (car object))
+ (list? (cdr object))))
+(register-predicate! metadata-elt? 'metadata-elt)
+
+(define (metadata-elt-keyword elt)
+ (guarantee metadata-elt? elt 'metadata-elt-keyword)
+ (car elt))
+
+(define (metadata-elt-values elt)
+ (guarantee metadata-elt? elt 'metadata-elt-values)
+ (cdr elt))
+\f
+;;;; Evaluation
+
+(define (eval-r7rs-source source db)
+ (let ((program (register-r7rs-source! source db)))
+ (if program
+ (library-eval-result program))))
+
+(define (r7rs-scode-file? scode)
+ (let ((scode (strip-comments scode)))
+ (or (scode-library? scode)
+ (and (scode-sequence? scode)
+ (every scode-library? (scode-sequence-actions scode))))))
+
+(define (r7rs-scode-file-libraries scode)
+ (let ((scode (strip-comments scode)))
+ (if (scode-library? scode)
+ (list scode)
+ (scode-sequence-actions scode))))
+
+(define (strip-comments object)
+ (if (and (scode-comment? object)
+ (not (scode-declaration? object)))
+ (strip-comments (scode-comment-expression object))
+ object))
+
+(define (eval-r7rs-scode-file scode pathname db)
+ (let ((libraries
+ (let ((filename (->namestring pathname)))
+ (map (lambda (library)
+ (scode-library->library library filename))
+ (r7rs-scode-file-libraries scode)))))
+ (register-libraries! libraries db)
+ (for-each library-eval-result libraries)))
+
+(define (scode-library->library library filename)
+ (make-library (scode-library-name library)
+ 'imports (scode-library-imports library)
+ 'exports (scode-library-exports library)
+ 'contents (scode-library-contents library)
+ 'filename filename))
+
+(define-automatic-property '(eval-result environment)
+ '(contents imports-environment)
+ #f
+ (lambda (contents env)
+ (values (scode-eval contents env)
+ env)))
(define-automatic-property 'exporter '(exports environment)
#f