]> git.mdlowis.com Git - archive/dlang-scm.git/commitdiff
refactored lexer
authorMike D. Lowis <mike@mdlowis.com>
Sun, 30 Sep 2012 18:04:21 +0000 (14:04 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Sun, 30 Sep 2012 18:04:21 +0000 (14:04 -0400)
example.dl
source/lexer.scm
source/parse-utils.scm

index 2bda939aaacd8509ab253f3ca96c1aeedcacb76c..83d4b36ffd5e06d2039bbc0f0ce0f2355037bc1b 100644 (file)
@@ -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
index da5cbdc2a18d2fb9580a87d6b30b51358b5eec41..4297207bb37791bf2336f8b6ed337bfed70f2f69 100644 (file)
@@ -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 #\#)
 (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)
 (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))
 (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))))
index 320823ca7333847a9cac4c914d7485ce65f15642..0beea80d6bfcce74d7ac421703d6c6bdd57f1c37 100644 (file)
   (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