From 260a4e771a7abadab0c2bd3f04e3dad27284c2b9 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 2 Oct 2022 02:07:08 -0400 Subject: [PATCH] Fix line numbers in tokens This is an insteresting example of using a closure to just save some typing.. but I wonder if it gets redefined every time / is slow? --- scanner.scm | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/scanner.scm b/scanner.scm index 3a368a5..6cc7ded 100644 --- a/scanner.scm +++ b/scanner.scm @@ -32,12 +32,12 @@ (define (get-tokens start line) ; Gets all tokens after start - (define (tok type i len) - ; helper to make a token and recurse - (cons (make-token type (substring src i (+ i len)) #f line len) - (get-tokens (+ i len) line))) (let loop ((i start) (nline line) (in-comment #f) (in-string #f) (in-number #f) (in-identifier #f)) + (define (tok type len) + ; helper to make a token and recurse -- todo is redefining slow? macro? + (cons (make-token type (substring src i (+ i len)) #f nline len) + (get-tokens (+ i len) nline))) (let ((c (peek i)) (n (peek (add1 i)))) ;(printf "c: ~A, n: ~A, i: ~A, com: ~A\n" c n i in-comment) (if (not c) @@ -50,21 +50,21 @@ (in-number #f) (in-identifier #f) (else (cond - ((eq? #\( c) (tok 'LEFT_PAREN i 1)) - ((eq? #\) c) (tok 'RIGHT_PAREN i 1)) - ((eq? #\{ c) (tok 'LEFT_BRACE i 1)) - ((eq? #\} c) (tok 'RIGHT_BRACE i 1)) - ((eq? #\, c) (tok 'COMMA i 1)) - ((eq? #\. c) (tok 'DOT i 1)) - ((eq? #\- c) (tok 'MINUS i 1)) - ((eq? #\+ c) (tok 'PLUS i 1)) - ((eq? #\; c) (tok 'SEMICOLON i 1)) - ((eq? #\* c) (tok 'STAR i 1)) - ((eq? #\! c) (if (eq? #\= n) (tok 'BANG_EQUAL i 2) (tok 'BANG i 1))) - ((eq? #\= c) (if (eq? #\= n) (tok 'EQUAL_EQUAL i 2) (tok 'EQUAL i 1))) - ((eq? #\< c) (if (eq? #\< n) (tok 'LESS_EQUAL i 2) (tok 'LESS i 1))) - ((eq? #\> c) (if (eq? #\> n) (tok 'GREATER_EQUAL i 2) (tok 'GREATER i 1))) - ((eq? #\/ c) (if (eq? #\/ n) (loop (add1 i) nline #t #f #f #f) (tok 'SLASH i 1))) + ((eq? #\( c) (tok 'LEFT_PAREN 1)) + ((eq? #\) c) (tok 'RIGHT_PAREN 1)) + ((eq? #\{ c) (tok 'LEFT_BRACE 1)) + ((eq? #\} c) (tok 'RIGHT_BRACE 1)) + ((eq? #\, c) (tok 'COMMA 1)) + ((eq? #\. c) (tok 'DOT 1)) + ((eq? #\- c) (tok 'MINUS 1)) + ((eq? #\+ c) (tok 'PLUS 1)) + ((eq? #\; c) (tok 'SEMICOLON 1)) + ((eq? #\* c) (tok 'STAR 1)) + ((eq? #\! c) (if (eq? #\= n) (tok 'BANG_EQUAL 2) (tok 'BANG 1))) + ((eq? #\= c) (if (eq? #\= n) (tok 'EQUAL_EQUAL 2) (tok 'EQUAL 1))) + ((eq? #\< c) (if (eq? #\< n) (tok 'LESS_EQUAL 2) (tok 'LESS 1))) + ((eq? #\> c) (if (eq? #\> n) (tok 'GREATER_EQUAL 2) (tok 'GREATER 1))) + ((eq? #\/ c) (if (eq? #\/ n) (loop (add1 i) nline #t #f #f #f) (tok 'SLASH 1))) ((eq? #\space c) (loop (add1 i) nline #f #f #f #f)) ((eq? #\tab c) (loop (add1 i) nline #f #f #f #f)) ((eq? #\newline c) (loop (add1 i) (add1 nline) #f #f #f #f)) -- libgit2 1.1.1