mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-07 05:06:36 +02:00
Experimenting with resolving imports on front-end
This commit is contained in:
parent
7173a42149
commit
7b0d1a74a0
4 changed files with 43 additions and 19 deletions
8
Makefile
8
Makefile
|
@ -42,11 +42,11 @@ test: $(TESTFILES) cyclone
|
||||||
# A temporary testing directive
|
# A temporary testing directive
|
||||||
.PHONY: test2
|
.PHONY: test2
|
||||||
test2: examples/hello-library/int-test/hello.c libcyclone.a
|
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
|
./cyclone -t examples/hello-library/libs/lib2.sld
|
||||||
gcc examples/hello-library/int-test/lib2.c -I. -g -c -o lib2.o
|
./cyclone -t examples/hello-library/hello.scm
|
||||||
gcc examples/hello-library/int-test/hello.c -I. -g -c -o hello.o
|
# gcc examples/hello-library/int-test/lib2.c -I. -g -c -o lib2.o
|
||||||
gcc hello.o lib2.o -L. -lcyclone -lm -o hello
|
# 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
|
# gcc examples/hello-library/hello.c -L. -lcyclone -lm -I. -g -o hello
|
||||||
|
|
||||||
icyc: cyclone icyc.scm eval.scm parser.scm runtime.h
|
icyc: cyclone icyc.scm eval.scm parser.scm runtime.h
|
||||||
|
|
15
cyclone.scm
15
cyclone.scm
|
@ -30,6 +30,7 @@
|
||||||
(define globals '())
|
(define globals '())
|
||||||
(define program? #t) ;; Are we building a program or a library?
|
(define program? #t) ;; Are we building a program or a library?
|
||||||
(define imports '())
|
(define imports '())
|
||||||
|
(define imported-vars '())
|
||||||
(define lib-name '())
|
(define lib-name '())
|
||||||
(define lib-exports '())
|
(define lib-exports '())
|
||||||
|
|
||||||
|
@ -51,6 +52,18 @@
|
||||||
;(error (list 'imports (cdar input-program)))
|
;(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??
|
;; TODO: how to handle stdlib when compiling a library??
|
||||||
;; either need to keep track of what was actually used,
|
;; either need to keep track of what was actually used,
|
||||||
;; or just assume all imports were used and include them
|
;; 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.
|
; 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?
|
; 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
|
(set! input-program
|
||||||
(map
|
(map
|
||||||
(lambda (expr)
|
(lambda (expr)
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
; TODO: just adding temporarily until import is supported.
|
; TODO: just adding temporarily until import is supported.
|
||||||
; idea is to try and see how the C code needs to change to
|
; idea is to try and see how the C code needs to change to
|
||||||
; support libraries
|
(import ;(scheme base)
|
||||||
(define lib2-hello
|
(libs lib2)
|
||||||
"Hello from library #2")
|
;(rename (prefix (libs lib1) test-))
|
||||||
;(import ;(scheme base)
|
)
|
||||||
; (libs lib2)
|
|
||||||
; ;(rename (prefix (libs lib1) test-))
|
|
||||||
; )
|
|
||||||
;
|
|
||||||
;(test-lib1-hello)
|
;(test-lib1-hello)
|
||||||
(write lib2-hello)
|
(write lib2-hello)
|
||||||
|
|
20
trans.scm
20
trans.scm
|
@ -1774,15 +1774,21 @@
|
||||||
;; Convert name (as list of symbols) to a mangled string
|
;; Convert name (as list of symbols) to a mangled string
|
||||||
(define (lib:name->string name)
|
(define (lib:name->string name)
|
||||||
(apply string-append (map mangle 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)
|
(define (lib:exports ast)
|
||||||
|
(lib:result
|
||||||
(and-let* ((code (assoc 'export (cddr ast))))
|
(and-let* ((code (assoc 'export (cddr ast))))
|
||||||
(cdr code)))
|
(cdr code))))
|
||||||
(define (lib:imports ast)
|
(define (lib:imports ast)
|
||||||
|
(lib:result
|
||||||
(and-let* ((code (assoc 'import (cddr ast))))
|
(and-let* ((code (assoc 'import (cddr ast))))
|
||||||
(cdr code)))
|
(cdr code))))
|
||||||
(define (lib:body ast)
|
(define (lib:body ast)
|
||||||
|
(lib:result
|
||||||
(and-let* ((code (assoc 'begin (cddr ast))))
|
(and-let* ((code (assoc 'begin (cddr ast))))
|
||||||
(cdr code)))
|
(cdr code))))
|
||||||
;; TODO: include, include-ci, cond-expand
|
;; TODO: include, include-ci, cond-expand
|
||||||
|
|
||||||
;; resolve library filename from an import
|
;; resolve library filename from an import
|
||||||
|
@ -1803,6 +1809,14 @@
|
||||||
(exports (lib:exports (car lib))))
|
(exports (lib:exports (car lib))))
|
||||||
(close-input-port fp)
|
(close-input-port fp)
|
||||||
exports))
|
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
|
;; END Library section
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue