From 8157bf6ec996b1f208be79033b2b9c5c3a2e3542 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Sun, 30 Sep 2012 14:04:21 -0400 Subject: [PATCH] refactored lexer --- example.dl | 10 +++++----- source/lexer.scm | 27 ++++++++++++--------------- source/parse-utils.scm | 10 +++++++++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/example.dl b/example.dl index 2bda939..83d4b36 100644 --- a/example.dl +++ b/example.dl @@ -1,4 +1,4 @@ -## Literals +# Literals "foo" 'a' 1.0 @@ -17,16 +17,16 @@ foo(1, 2) foo(1, 2, 3) #(foo . $bar)(1, 2, 3) -# Definition and assignment + Definition and assignment def foo 5 ; set! foo 6 ; def foo 5 end set! foo 6 end # Special function definition shorthand (not yet supported) -#def max(a, b) -# if (a > b) a b; -#end +def max(a, b) + if (a > b) a b; +end # If statement if conditional diff --git a/source/lexer.scm b/source/lexer.scm index da5cbdc..4297207 100644 --- a/source/lexer.scm +++ b/source/lexer.scm @@ -15,7 +15,7 @@ ((eof-object? ch) ch) ; Whitespace - ((dlang/whitespace? in) + ((chobj-whitespace? ch) (dlang/whitespace in)) ; Comment @@ -23,8 +23,8 @@ (dlang/comment in)) ; Number - ((or (and (chobj-char=? ch #\-) (dlang/integer? (buf-lookahead! in 2))) - (char-numeric? (chobj-char ch))) + ((or (and (chobj-char=? ch #\-) (chobj-numeric? (buf-lookahead! in 2))) + (chobj-numeric? ch)) (dlang/number in)) ; Character @@ -59,8 +59,7 @@ (dlang/tokenize in)) (define (dlang/whitespace? in) - (and (not (eof-object? (buf-lookahead! in 1))) - (char-whitespace? (chobj-char (buf-lookahead! in 1))))) + (chobj-whitespace? (buf-lookahead! in 1))) (define (dlang/comment in) (char-match in #\#) @@ -102,13 +101,11 @@ (define (dlang/exponent in) (string-append (string - (if (and (not (eof-object? (buf-lookahead! in 1))) - (char=? #\e (chobj-char (buf-lookahead! in 1)))) - (char-match in #\e) - (char-match in #\E))) - (if (and (not (eof-object? (buf-lookahead! in 1))) - (char=? #\- (chobj-char (buf-lookahead! in 1)))) - (string (chobj-char (buf-consume! in))) "") + (if (chobj-char=? (buf-lookahead! in 1) #\e) + (char-match in #\e) + (char-match in #\E))) + (if (chobj-char=? (buf-lookahead! in 1) #\-) + (string (chobj-char (buf-consume! in))) "") (dlang/integer in))) (define (dlang/character in) @@ -134,8 +131,8 @@ (define (dlang/string-char? in) (define ch (buf-lookahead! in 1)) (and (not (eof-object? ch)) - (not (char=? #\newline (chobj-char ch))) - (not (char=? #\" (chobj-char ch))))) + (not (chobj-char=? ch #\newline)) + (not (chobj-char=? ch #\")))) (define (dlang/symbol in) (define location (buf-posdata in)) @@ -155,7 +152,7 @@ (define (dlang/id-char? in) (define ch (buf-lookahead! in 1)) (and (not (eof-object? ch)) - (not (char-whitespace? (chobj-char ch))) + (not (chobj-whitespace? ch)) (case (chobj-char ch) ((#\( #\) #\; #\, #\' #\" #\$ #\#) #f) (else #t)))) diff --git a/source/parse-utils.scm b/source/parse-utils.scm index 320823c..0beea80 100644 --- a/source/parse-utils.scm +++ b/source/parse-utils.scm @@ -47,6 +47,14 @@ (and (not (eof-object? obj)) (char=? (chobj-char obj) ch))) +(define (chobj-numeric? ch) + (and (not (eof-object? ch)) + (char-numeric? (chobj-char ch)))) + +(define (chobj-whitespace? ch) + (and (not (eof-object? ch)) + (char-whitespace? (chobj-char ch)))) + (define (charport-read chprt) (define ch (read-char (charport-port chprt))) (cond @@ -75,7 +83,7 @@ (if (eof-object? actual) (abort (string-append "Expected '" (string expect) "', received EOF instead")) - (if (equal? expect (chobj-char actual)) + (if (chobj-char=? actual expect) (buf-consume! buf) (abort (string-append -- 2.54.0