aboutsummaryrefslogtreecommitdiffstats
path: root/csc.scheme
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2023-05-01 07:56:42 -0700
committerRose Hogenson <rhogenson@posteo.net>2023-05-01 07:56:42 -0700
commita89d6c82e981fec7d6e4c975e083d2b9e04467ad (patch)
treed5445ceb797473dd45ac006c337d990e5dd6f0d4 /csc.scheme
parent112ce5291da35e54c38c4ff1d7a2408a64a98e71 (diff)
downloadchromatopelma-a89d6c82e981fec7d6e4c975e083d2b9e04467ad.tar.zst
Rewrite most of the compiler.
This represents a major step back in terms of functionality, and amount of code. The latter I think constitutes a major win. Next steps are to reimplement syntax-rules, call/cc, and call-with-values.
Diffstat (limited to 'csc.scheme')
-rw-r--r--csc.scheme74
1 files changed, 74 insertions, 0 deletions
diff --git a/csc.scheme b/csc.scheme
new file mode 100644
index 0000000..1ac9267
--- /dev/null
+++ b/csc.scheme
@@ -0,0 +1,74 @@
+(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)