(import (scheme base) (only (scheme read) read) (prefix (scheme file) file.) (prefix (scheme process-context) process-context.) (prefix (csc compiler) compiler.)) (define (help) (write-string "Usage: csc [-I path] [-o file] file.scheme" (current-error-port))) (define (bad-usage) (help) (process-context.exit 1)) (define (read-program file) (file.call-with-input-file file (lambda (f) (let loop () (define x (read f)) (if (eof-object? x) '() (cons x (loop))))))) (define (write-file file bytes) (call-with-port (file.open-binary-output-file file) (lambda (w) (write-bytevector bytes w)))) (define (main) (define args (cdr (process-context.command-line))) (define out "a.out") (define library-paths '()) (let loop () (if (null? args) (bad-usage) (let ((arg (car args))) (cond ((string=? "--" arg) (set! args (cdr args)) #;break) ((or (string=? "" arg) (string=? "-" arg) (not (char=? #\- (string-ref arg 0)))) #;break) (else (set! args (cdr args)) (let ((opt (string-ref arg 1))) (define (val) (if (> (string-length arg) 2) (string-copy arg 2) (let ((val (car args))) (set! args (cdr args)) val))) (case opt ((#\I) (set! library-paths (append library-paths (list (val))))) ((#\o) (set! out (val))) ((#\h) (help) (process-context.exit 0)) (else (bad-usage)))) (loop)))))) (unless (= 1 (length args)) (bad-usage)) (write-file out (compiler.compile (read-program (car args)) library-paths))) (main)