fisl.scm (1504B) [raw]
1 #!/usr/local/bin/chicken-csi -ss 2 ;; fisl -- fisl is scheme lox 3 (import srfi-18 4 nrepl 5 (chicken io) 6 (chicken repl) 7 (chicken base) 8 (chicken format)) 9 10 (include "util.scm") 11 (include "scanner.scm") 12 (include "parser.scm") 13 (include "interpreter.scm") 14 15 (define in-repl #f) 16 17 (define (run code) 18 (let ((tokens (scan code))) 19 (if tokens 20 (let ((stmts (parse tokens))) 21 (unless had-err 22 (if stmts 23 (interpret stmts))))))) 24 25 (define (prompt) 26 ;; HACK: srfi-18 blocks for IO, so having run-prompt 27 ;; use read-line means that nrepl just doesn't work :( 28 ;; the "solution" is to set the i/o to non-blocking 29 ;; (adapted from the srfi-18 egg itself) 30 (display "> ") 31 (flush-output) 32 (##sys#thread-block-for-i/o! ##sys#current-thread 0 #:input) 33 (thread-yield!)) 34 35 (define (run-prompt) 36 (parameterize ((repl-prompt "> ")) 37 (prompt) 38 (let ((l (read-line))) 39 (if (not (or (eof-object? l) (equal? l ",q"))) 40 (begin 41 (run l) 42 (clear-err!) 43 (run-prompt)))))) 44 45 (define (run-file fname) 46 (set-fname! fname) 47 (call-with-input-file fname (lambda (p) 48 (run (read-string #f p)) 49 (exit (if had-err 1 0))))) 50 51 (define (main args) 52 (let ((argc (length args))) 53 (cond 54 ((eq? argc 0) 55 (set! in-repl #t) 56 (thread-start! (lambda () (run-prompt) (exit 0))) 57 (nrepl 1234)) 58 ((eq? argc 1) (run-file (car args))) 59 (else (die "Too many arguments. Usage: fisl [FILE]")))))