Experimenting with resolving imports on front-end

This commit is contained in:
Justin Ethier 2015-05-12 14:14:35 -04:00
parent 7173a42149
commit 7b0d1a74a0
4 changed files with 43 additions and 19 deletions

View file

@ -42,11 +42,11 @@ test: $(TESTFILES) cyclone
# A temporary testing directive
.PHONY: test2
test2: examples/hello-library/int-test/hello.c libcyclone.a
# ./cyclone -t examples/hello-library/hello.scm
./cyclone -t examples/hello-library/libs/lib2.sld
gcc examples/hello-library/int-test/lib2.c -I. -g -c -o lib2.o
gcc examples/hello-library/int-test/hello.c -I. -g -c -o hello.o
gcc hello.o lib2.o -L. -lcyclone -lm -o hello
./cyclone -t examples/hello-library/hello.scm
# gcc examples/hello-library/int-test/lib2.c -I. -g -c -o lib2.o
# gcc examples/hello-library/int-test/hello.c -I. -g -c -o hello.o
# gcc hello.o lib2.o -L. -lcyclone -lm -o hello
# gcc examples/hello-library/hello.c -L. -lcyclone -lm -I. -g -o hello
icyc: cyclone icyc.scm eval.scm parser.scm runtime.h

View file

@ -30,6 +30,7 @@
(define globals '())
(define program? #t) ;; Are we building a program or a library?
(define imports '())
(define imported-vars '())
(define lib-name '())
(define lib-exports '())
@ -51,6 +52,18 @@
;(error (list 'imports (cdar input-program)))
))
;; Process library imports
;; TODO: this may not be good enough, may need to tag library
;; imports uniquely to reduce issues when the same variable
;; is used by multiple libraries, and to allow renaming of imports.
;; As of now, that will have to be dealt with later.
(trace:info "imports:")
(trace:info imports)
;; TODO: need to get basedir from env, this is just a placeholder
(set! imported-vars (lib:resolve-imports imports "examples/hello-library")) ;;"."))
(trace:info "resolved imports:")
(trace:info imported-vars)
;; TODO: how to handle stdlib when compiling a library??
;; either need to keep track of what was actually used,
;; or just assume all imports were used and include them
@ -79,7 +92,7 @@
; set!'s below, since all remaining phases operate on set!, not define.
;
; TODO: consider moving some of this alpha-conv logic below back into trans?
(set! globals (global-vars input-program))
(set! globals (append imported-vars (global-vars input-program)))
(set! input-program
(map
(lambda (expr)

View file

@ -1,12 +1,9 @@
; TODO: just adding temporarily until import is supported.
; idea is to try and see how the C code needs to change to
; support libraries
(define lib2-hello
"Hello from library #2")
;(import ;(scheme base)
; (libs lib2)
; ;(rename (prefix (libs lib1) test-))
; )
;
(import ;(scheme base)
(libs lib2)
;(rename (prefix (libs lib1) test-))
)
;(test-lib1-hello)
(write lib2-hello)

View file

@ -1774,15 +1774,21 @@
;; Convert name (as list of symbols) to a mangled string
(define (lib:name->string name)
(apply string-append (map mangle name)))
;; Helper function that returns an empty list as a default value
(define (lib:result result)
(if result result '()))
(define (lib:exports ast)
(and-let* ((code (assoc 'export (cddr ast))))
(cdr code)))
(lib:result
(and-let* ((code (assoc 'export (cddr ast))))
(cdr code))))
(define (lib:imports ast)
(and-let* ((code (assoc 'import (cddr ast))))
(cdr code)))
(lib:result
(and-let* ((code (assoc 'import (cddr ast))))
(cdr code))))
(define (lib:body ast)
(and-let* ((code (assoc 'begin (cddr ast))))
(cdr code)))
(lib:result
(and-let* ((code (assoc 'begin (cddr ast))))
(cdr code))))
;; TODO: include, include-ci, cond-expand
;; resolve library filename from an import
@ -1803,6 +1809,14 @@
(exports (lib:exports (car lib))))
(close-input-port fp)
exports))
;; Take a list of imports and resolve it to the imported vars
(define (lib:resolve-imports imports basedir)
(apply
append
(map
(lambda (import)
(lib:import->export-list import basedir))
imports)))
;; END Library section