commit 255875cef1a90409e8e002e185ba938838d22d27 (patch)
parent 95b6805f26858bd34f333c8f6f288116b7d89a46
Author: Alex Karle <alex@alexkarle.com>
Date: Tue, 4 Oct 2022 23:30:59 -0400
scanner: Fix over-consuming on !=<> characters
I started out with peeking at the current and next chars but
at some point I thought it would be equally clever to just set
the fact that I saw a two-width char in "in" and do it that way.
Turns out that that breaks simple cases like !), since recording
the BANG token with c=! and i=) will advance to the next character
after the ), skipping it.
Diffstat:
2 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/fisl.scm b/fisl.scm
@@ -1,4 +1,4 @@
-#!/usr/bin/chicken-csi -ss
+#!/usr/local/bin/chicken-csi -ss
;; fisl -- fisl is scheme lox
(load "scanner.scm")
(load "util.scm")
diff --git a/scanner.scm b/scanner.scm
@@ -82,7 +82,7 @@
; Helper to iterate; keeps start but increments range
(get-tokens s (add1 i) l2 in))
- (let ((c (peek i)))
+ (let ((c (peek i)) (n (peek (add1 i))))
(if (and (not in) (not c))
(list (make-token 'EOF "" #f line))
(cond
@@ -108,11 +108,6 @@
(cond
((alnum? c) (advance line))
(else (tok-range 'IDENTIFIER s (sub1 i)))))
- ((eq? in '=) (if (eq? #\= c) (tok 'EQUAL_EQUAL) (tok 'EQUAL)))
- ((eq? in '>) (if (eq? #\> c) (tok 'GREATER_EQUAL) (tok 'GREATER)))
- ((eq? in '<) (if (eq? #\< c) (tok 'LESS_EQUAL) (tok 'LESS)))
- ((eq? in '!) (if (eq? #\= c) (tok 'BANG_EQUAL) (tok 'BANG)))
- ((eq? in '/) (if (eq? #\/ c) (get-tokens s (add1 i) line 'comment) (tok 'SLASH)))
(else (cond
((eq? #\( c) (tok 'LEFT_PAREN))
((eq? #\) c) (tok 'RIGHT_PAREN))
@@ -124,11 +119,11 @@
((eq? #\+ c) (tok 'PLUS))
((eq? #\; c) (tok 'SEMICOLON))
((eq? #\* c) (tok 'STAR))
- ((eq? #\! c) (get-tokens s (add1 i) line '!))
- ((eq? #\= c) (get-tokens s (add1 i) line '=))
- ((eq? #\< c) (get-tokens s (add1 i) line '<))
- ((eq? #\> c) (get-tokens s (add1 i) line '>))
- ((eq? #\/ c) (get-tokens s (add1 i) line '/))
+ ((eq? #\! c) (if (eq? #\= n) (tok-range 'BANG_EQUAL s (add1 i)) (tok 'BANG)))
+ ((eq? #\= c) (if (eq? #\= n) (tok-range 'EQUAL_EQUAL s (add1 i) ) (tok 'EQUAL)))
+ ((eq? #\< c) (if (eq? #\= n) (tok-range 'LESS_EQUAL s (add1 i) ) (tok 'LESS)))
+ ((eq? #\> c) (if (eq? #\= n) (tok-range 'GREATER_EQUAL s (add1 i) ) (tok 'GREATER)))
+ ((eq? #\/ c) (if (eq? #\/ n) (get-tokens s (add1 i) line 'comment) (tok 'SLASH)))
((eq? #\" c) (get-tokens s (add1 i) line 'string))
((digit? c) (get-tokens s (add1 i) line 'number))
((alpha? c) (get-tokens s (add1 i) line 'alpha))