forked from alex-eg/sex
process imported forms as toplevel, not as text
1. register imported types 2. protect against multiple imports (diamond, circular)
This commit is contained in:
14
semen.scm
14
semen.scm
@@ -93,17 +93,9 @@
|
||||
(else (sex-error sex-form "unknown top level form" sex-form))))
|
||||
|
||||
(define (process-imports module-public-forms acc)
|
||||
;; Recursively process imports: register public macros, cons all
|
||||
;; other public things to our acc
|
||||
(if (null? module-public-forms) acc
|
||||
(match (car module-public-forms)
|
||||
(('defmacro . rest)
|
||||
(defmacro rest)
|
||||
(process-imports (cdr module-public-forms) acc))
|
||||
(else
|
||||
(process-imports (cdr module-public-forms)
|
||||
(cons (car module-public-forms)
|
||||
acc))))))
|
||||
;; consume (import ...) form and process imports so
|
||||
;; data types end up in types db
|
||||
(fold match-sex-form acc module-public-forms))
|
||||
|
||||
(define (macro-expand form)
|
||||
"Walk the form recursively and expand all macros, until none is left."
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
|
||||
(define +persistent-module-paths+ (list))
|
||||
|
||||
;;; for guarding against multiple imports (sort of mandatory #pragma
|
||||
;;; once)
|
||||
(define +imported-modules+ (list))
|
||||
|
||||
(define (get-modules-public-forms module-list)
|
||||
;; Module list is a list of symbols
|
||||
;; How Sex handles modules:
|
||||
@@ -29,7 +33,11 @@
|
||||
(let ((module-path (locate-module name)))
|
||||
(assert module-path (fmt #f "Failed to find module " name " in "
|
||||
(get-module-paths)))
|
||||
(read-public-interface module-path)))
|
||||
(if (member module-path +imported-modules+)
|
||||
(list)
|
||||
(begin
|
||||
(set! +imported-modules+ (cons module-path +imported-modules+))
|
||||
(read-public-interface module-path)))))
|
||||
|
||||
(define (get-module-paths)
|
||||
(cons (current-directory)
|
||||
|
||||
@@ -7,10 +7,14 @@
|
||||
# module's object survives being passed after `--'. Get any of them
|
||||
# wrong and this fails to link -- or, in the `pub var' case, links and
|
||||
# quietly counts into a private copy.
|
||||
#
|
||||
# It also checks what only a second translation unit can check: that an
|
||||
# imported type reaches the type database, by expanding a macro that
|
||||
# reads the imported struct's fields.
|
||||
|
||||
SEXC ?= ../../sexc
|
||||
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times
|
||||
|
||||
check:
|
||||
@$(SEXC) greet.sex -c -o greet.o
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
;;; as a prototype and `greet-count' as an extern. Both keep external
|
||||
;;; linkage, so they refer to the one definition in greet.o rather than
|
||||
;;; to private copies.
|
||||
;;;
|
||||
;;; Also check that import populates type-database, by means of
|
||||
;;; describe-fields macro, which should work on imported type.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
@@ -16,4 +19,6 @@
|
||||
(greet "world")
|
||||
(greet "sex")
|
||||
(printf "%d greetings\n" greet-count)
|
||||
(describe-fields greeting)
|
||||
(printf "\n")
|
||||
(return 0))
|
||||
|
||||
@@ -9,6 +9,16 @@
|
||||
|
||||
(pub var greet-count int 0)
|
||||
|
||||
(pub struct greeting ((text (* const char)) (times int)))
|
||||
|
||||
;;; Compile-time reflection across the module boundary: both the macro
|
||||
;;; and the struct it asks about are exported, and the importing unit
|
||||
;;; has to know the struct's fields to expand this.
|
||||
(pub defmacro (describe-fields type)
|
||||
`(do ,@(map-fields type
|
||||
(lambda (name field-type)
|
||||
`(printf "%s " ,(symbol->string name))))))
|
||||
|
||||
(pub fn greet ((name (* const char))) void
|
||||
(++ greet-count)
|
||||
(printf "hello, %s\n" name))
|
||||
|
||||
Reference in New Issue
Block a user