commit be6df7f081b8c1d9c10d6149d44c9f512680c229 (patch)
parent 255875cef1a90409e8e002e185ba938838d22d27
Author: Alex Karle <alex@alexkarle.com>
Date: Tue, 4 Oct 2022 23:47:16 -0400
scanner: Fix line numbers and add basic error handling
This really just adds the first use of set! to maintain the global
had-err value on err!
Diffstat:
3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/fisl.scm b/fisl.scm
@@ -10,9 +10,7 @@
util)
(define (run code fname)
- (let ((exit-code 0))
- (map print (scan code fname))
- exit-code))
+ (map print (scan code fname)))
(define (run-prompt)
(display "> ")
@@ -20,12 +18,14 @@
(if (not (eof-object? l))
(begin
(run l "repl")
+ (clear-err!)
(run-prompt))
(exit 0))))
(define (run-file fname)
(call-with-input-file fname (lambda (p)
- (exit (run (read-string #f p) fname)))))
+ (run (read-string #f p) fname)
+ (exit (if had-err 1 0)))))
(define (main args)
(let ((argc (length args)))
diff --git a/scanner.scm b/scanner.scm
@@ -91,7 +91,7 @@
(advance line)))
((eq? in 'string)
(cond
- ((not c) (err (format "~A:~A:unterminated string" fname line)))
+ ((not c) (err! (format "~A:~A:unterminated string" fname line)))
((eq? #\" c) (tok 'STRING))
((eq? #\newline c) (advance (add1 line)))
(else (advance line))))
@@ -129,8 +129,8 @@
((alpha? c) (get-tokens s (add1 i) line 'alpha))
((eq? #\space c) (skip))
((eq? #\tab c) (skip))
- ((eq? #\newline c) (skip))
- (else (err (format "~A:~A:unexpected character: ~A" fname 0 c)) (skip))))))))
+ ((eq? #\newline c) (get-tokens (add1 i) (add1 i) (add1 line) in))
+ (else (err! (format "~A:~A:unexpected character: ~A" fname line c)) (skip))))))))
(get-tokens 0 0 1 #f))
) ; end of module
diff --git a/util.scm b/util.scm
@@ -1,14 +1,20 @@
-(module util (die get err)
+(module util (die get err! had-err clear-err!)
(import scheme
(chicken base)
(chicken io)
(chicken format))
- (define (err str)
+ (define had-err #f)
+
+ (define (err! str)
+ (set! had-err #t)
(fprintf (current-error-port) "~A\n" str))
+ (define (clear-err!)
+ (set! had-err #f))
+
(define (die str)
- (err str)
+ (err! str)
(exit 1))
(define (get assoc-arr key)