From 7b0d1a74a0fc6382bfb9c382ba51f3b368b83186 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Tue, 12 May 2015 14:14:35 -0400 Subject: [PATCH] Experimenting with resolving imports on front-end --- Makefile | 8 ++++---- cyclone.scm | 15 ++++++++++++++- examples/hello-library/hello.scm | 13 +++++-------- trans.scm | 26 ++++++++++++++++++++------ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 7076cc1c..42b91108 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cyclone.scm b/cyclone.scm index 9716eac9..21c4fe08 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -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) diff --git a/examples/hello-library/hello.scm b/examples/hello-library/hello.scm index 92b8fe89..98fc2aa8 100644 --- a/examples/hello-library/hello.scm +++ b/examples/hello-library/hello.scm @@ -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) diff --git a/trans.scm b/trans.scm index a8af92df..ddcb2df8 100644 --- a/trans.scm +++ b/trans.scm @@ -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