nyacc: fixed C99 CPP to deal with numbers correctly

This commit is contained in:
Matt Wette 2017-02-22 08:23:27 -08:00 committed by Jan Nieuwenhuizen
parent 62154122b3
commit 2c6a6dc47d
8 changed files with 215 additions and 168 deletions

View File

@ -5,7 +5,9 @@ are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is, notice and this notice are preserved. This file is offered as-is,
without any warranty. without any warranty.
BUG-004 <= next bug id BUG-005 <= next bug id
BUG-004 Idea: use guile prompts to implement rollback parser
BUG-003 If using phony prec token then it ends up in match table. BUG-003 If using phony prec token then it ends up in match table.
For example, "then" appears in the match table for lang/c99. For example, "then" appears in the match table for lang/c99.

View File

@ -1,3 +1,12 @@
2017-02-22 Matt Wette <mwette@alumni.caltech.edu>
* lang/c99/body.scm: added 'skip state so that if skipping #if
then no CPP if or elif arguments are evaluated
* lang/c99/cpp.scm: parse 0L numbers, convert c numbers (e.g.,
123L) to scheme so that string->number works. I need to update
cnumstr->snumstr in nyacc/lex.scm.
2017-02-16 Matt Wette <mwette@alumni.caltech.edu> 2017-02-16 Matt Wette <mwette@alumni.caltech.edu>
* lang/c99/cpp.scm (rtokl->string): now handled symb ## arg ## symb * lang/c99/cpp.scm (rtokl->string): now handled symb ## arg ## symb

View File

@ -312,22 +312,29 @@
(find-file-in-dirl file (cpi-incs info))) (find-file-in-dirl file (cpi-incs info)))
(define (eval-cpp-stmt-1/code stmt) (define (eval-cpp-stmt-1/code stmt)
;; eval control flow: states are {skip-look, keep, skip-done} ;; eval control flow states: {skip-look, keep, skip-done, skip}
(case (car stmt) (case (car stmt)
((if) ((if)
(let ((val (eval-cpp-cond-text (cadr stmt)))) (case (car ppxs)
(if (not val) (p-err "unresolved: ~S" (cadr stmt))) ((skip-look skip-done skip) ;; don't eval if excluded
(if (eq? 'keep (car ppxs)) (set! ppxs (cons 'skip ppxs)))
(if (zero? val) (else
(set! ppxs (cons 'skip-look ppxs)) (let ((val (eval-cpp-cond-text (cadr stmt))))
(set! ppxs (cons 'keep ppxs))) (if (not val) (p-err "unresolved: ~S" (cadr stmt)))
(set! ppxs (cons 'skip-done ppxs))))) (if (eq? 'keep (car ppxs))
(if (zero? val)
(set! ppxs (cons 'skip-look ppxs))
(set! ppxs (cons 'keep ppxs)))
(set! ppxs (cons 'skip-done ppxs)))))))
((elif) ((elif)
(let ((val (eval-cpp-cond-text (cadr stmt)))) (case (car ppxs)
(if (not val) (p-err "unresolved: ~S" (cadr stmt))) ((skip) #t) ;; don't eval if excluded
(case (car ppxs) (else
((skip-look) (if (not (zero? val)) (set-car! ppxs 'keep))) (let ((val (eval-cpp-cond-text (cadr stmt))))
((keep) (set-car! ppxs 'skip-done))))) (if (not val) (p-err "unresolved: ~S" (cadr stmt)))
(case (car ppxs)
((skip-look) (if (not (zero? val)) (set-car! ppxs 'keep)))
((keep) (set-car! ppxs 'skip-done)))))))
((else) ((else)
(case (car ppxs) (case (car ppxs)
((skip-look) (set-car! ppxs 'keep)) ((skip-look) (set-car! ppxs 'keep))
@ -444,14 +451,10 @@
;; Loop between reading tokens and skipping tokens via CPP logic. ;; Loop between reading tokens and skipping tokens via CPP logic.
(let iter ((pair (read-token))) (let iter ((pair (read-token)))
(case (car ppxs) (case (car ppxs)
((keep) ((keep) pair)
;;(simple-format #t "lx=>~S\n" pair) ((skip-done skip-look skip)
pair)
((skip-done skip-look)
(iter (read-token))) (iter (read-token)))
((skip1-pop) (else (error "coding error"))))
(set! ppxs (cdr ppxs))
(iter (read-token)))))
))))) )))))
;; --- last line --- ;; --- last line ---

View File

@ -199,7 +199,7 @@
(eval-expr (eval-expr
(lambda (tree) (lambda (tree)
(case (car tree) (case (car tree)
((fixed) (string->number (tx1 tree))) ((fixed) (string->number (cnumstr->scm (tx1 tree))))
((char) (char->integer (tx1 tree))) ((char) (char->integer (tx1 tree)))
((defined) (if (assoc-ref dict (tx1 tree)) 1 0)) ((defined) (if (assoc-ref dict (tx1 tree)) 1 0))
((pre-inc post-inc) (1+ (ev1 tree))) ((pre-inc post-inc) (1+ (ev1 tree)))
@ -300,26 +300,21 @@
;; We just scanned "defined", now need to scan the arg to inhibit expansion. ;; We just scanned "defined", now need to scan the arg to inhibit expansion.
;; For example, we have scanned "defined"; we now scan "(FOO)" or "FOO", and ;; For example, we have scanned "defined"; we now scan "(FOO)" or "FOO", and
;; return "defined(FOO)". We use ec (end-char) as terminal char: ;; return "defined(FOO)" or "defined FOO".
;; #\) if starts with #( or #\nul if other.
(define (scan-defined-arg) (define (scan-defined-arg)
(let* ((ch (skip-il-ws (read-char))) (let* ((ch (skip-il-ws (read-char))) (no-ec (not (char=? ch #\())))
(ec (if (char=? ch #\() #\) #\null))) (let iter ((chl (list ch)) (ch (read-char)))
(let iter ((chl '(#\())
(ec ec)
(ch (if (char=? ec #\)) (skip-il-ws (read-char)) ch)))
(cond (cond
((eof-object? ch) ((eof-object? ch)
(if (char=? ec #\null) (if no-ec
(string-append "defined" (list->string (reverse (cons #\) chl)))) (string-append "defined " (list->string (reverse chl)))
(cpp-err "illegal argument to `defined'"))) (cpp-err "illegal argument to `defined'")))
((char-set-contains? c:ir ch) ((char-set-contains? c:ir ch)
(iter (cons ch chl) ec (read-char))) (iter (cons ch chl) (read-char)))
((char=? ec #\)) (no-ec
(if (char=? #\) (skip-il-ws ch)) (unread-char ch)
(string-append "defined" (list->string (reverse (cons #\) chl)))) (string-append "defined " (list->string (reverse chl))))
(cpp-err "garbage in argument to `defined'"))) ((char=? #\) (skip-il-ws ch))
((char=? ec #\null) ;; past identifier
(string-append "defined" (list->string (reverse (cons #\) chl))))) (string-append "defined" (list->string (reverse (cons #\) chl)))))
(else (else
(cpp-err "illegal argument to `defined'")))))) (cpp-err "illegal argument to `defined'"))))))

View File

@ -96,6 +96,7 @@
($fixed ($$ `(fixed ,$1))) ; integer-constant ($fixed ($$ `(fixed ,$1))) ; integer-constant
($chlit ($$ `(char ,$1))) ; char-constant ($chlit ($$ `(char ,$1))) ; char-constant
("defined" "(" $ident ")" ($$ `(defined ,$3))) ("defined" "(" $ident ")" ($$ `(defined ,$3)))
("defined" $ident ($$ `(defined ,$2)))
("(" expression-list ")" ($$ $2))) ("(" expression-list ")" ($$ $2)))
(expression-list (expression-list
(conditional-expression) (conditional-expression)

View File

@ -101,6 +101,8 @@
(lambda ($1 . $rest) `(char ,$1)) (lambda ($1 . $rest) `(char ,$1))
;; primary-expression => "defined" "(" '$ident ")" ;; primary-expression => "defined" "(" '$ident ")"
(lambda ($4 $3 $2 $1 . $rest) `(defined ,$3)) (lambda ($4 $3 $2 $1 . $rest) `(defined ,$3))
;; primary-expression => "defined" '$ident
(lambda ($2 $1 . $rest) `(defined ,$2))
;; primary-expression => "(" expression-list ")" ;; primary-expression => "(" expression-list ")"
(lambda ($3 $2 $1 . $rest) $2) (lambda ($3 $2 $1 . $rest) $2)
;; expression-list => conditional-expression ;; expression-list => conditional-expression

View File

@ -8,7 +8,7 @@
(define len-v (define len-v
#(1 1 5 1 3 1 3 1 3 1 3 1 3 1 3 3 1 3 3 3 3 1 3 3 1 3 3 1 3 3 3 1 2 2 2 2 #(1 1 5 1 3 1 3 1 3 1 3 1 3 1 3 3 1 3 3 3 3 1 3 3 1 3 3 1 3 3 3 1 2 2 2 2
2 2 1 2 2 1 1 1 4 3 1 3)) 2 2 1 2 2 1 1 1 4 2 3 1 3))
(define pat-v (define pat-v
#(((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . #(((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 .
@ -17,87 +17,88 @@
. 24) (50 . 25)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) . 24) (50 . 25)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7)
(9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 .
15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22) 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22)
(48 . 23) (49 . 24) (50 . 54) (36 . 55)) ((3 . 53)) ((-1 . -43)) ((-1 . (48 . 23) (49 . 24) (50 . 55) (36 . 56)) ((3 . 53) (7 . 54)) ((-1 . -43))
-42)) ((-1 . -41)) ((-1 . -38)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) ( ((-1 . -42)) ((-1 . -41)) ((-1 . -38)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7
37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) ( . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38
39 . 52)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8 . 13) (39 . 52)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7)
) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 51)) ((3 . 1) (4 (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 51)) ((3
. 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (
16 . 11) (15 . 12) (38 . 13) (39 . 50)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (
7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38
. 13) (39 . 49)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7
) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 48)) ((3
. 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11
. 10) (16 . 11) (15 . 12) (38 . 13) (39 . 47)) ((9 . 45) (8 . 46) (-1 . . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 50)) ((3 . 1) (4 . 2) (5 . 3) (
-31)) ((-1 . -27)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -24)) ((16 . 40) ( 6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15
15 . 41) (-1 . -21)) ((18 . 38) (17 . 39) (-1 . -16)) ((22 . 34) (21 . 35) . 12) (38 . 13) (39 . 49)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 .
(20 . 36) (19 . 37) (-1 . -13)) ((24 . 32) (23 . 33) (-1 . -11)) ((25 .
31) (-1 . -9)) ((26 . 30) (-1 . -7)) ((27 . 29) (-1 . -5)) ((28 . 28) (-1
. -3)) ((31 . 26) (29 . 27) (2 . -1) (1 . -1) (35 . -1)) ((35 . 0)) ((3 .
1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11
. 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17
) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22) (48 . 23) (49 . 77)) (
(3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9)
(11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42
. 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22) (48 . 76)) ((3 . 1
) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 .
10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17)
(43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 75)) ((3 . 1) (4 . 2) (5 . 3
) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (
15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44
. 19) (45 . 20) (46 . 74)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 .
6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 .
14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 73)) ((3 . 1) 48)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10
(4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 47)) ((9 . 45) (8 . 46
) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) ( ) (-1 . -31)) ((-1 . -27)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -24)) ((16
43 . 18) (44 . 72)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 40) (15 . 41) (-1 . -21)) ((18 . 38) (17 . 39) (-1 . -16)) ((22 . 34) (
. 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) ( 21 . 35) (20 . 36) (19 . 37) (-1 . -13)) ((24 . 32) (23 . 33) (-1 . -11))
40 . 15) (41 . 16) (42 . 17) (43 . 71)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) ( ((25 . 31) (-1 . -9)) ((26 . 30) (-1 . -7)) ((27 . 29) (-1 . -5)) ((28 .
7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 28) (-1 . -3)) ((31 . 26) (29 . 27) (2 . -1) (1 . -1) (35 . -1)) ((35 . 0)
. 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 70)) ((3 . 1) (4 . 2) ) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 .
(5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (
. 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 69)) ((3 . 1 42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22) (48 . 23) (49
) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . . 78)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (
10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 68)) 10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 .
16) (42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 22) (48 . 77))
((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9 ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9
) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) ( ) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (
42 . 67)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8 42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 76)) ((3 . 1) (4 .
) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16
. 16) (42 . 66)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 .
) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 18) (44 . 19) (45 . 20) (46 . 75)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5
. 15) (41 . 65)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) ) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13
(9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . ) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 74)) (
15) (41 . 64)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) ( (3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9)
9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 63 (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42
)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 17) (43 . 18) (44 . 73)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 .
. 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 62)) ((3 . 1)
(4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 .
10) (16 . 11) (15 . 12) (38 . 13) (39 . 61)) ((3 . 1) (4 . 2) (5 . 3) (6
. 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 .
12) (38 . 13) (39 . 60)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6)
(8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 59
)) ((-1 . -39)) ((-1 . -40)) ((-1 . -32)) ((-1 . -33)) ((-1 . -34)) ((-1
. -35)) ((-1 . -36)) ((-1 . -37)) ((7 . 58)) ((2 . -46) (1 . -46)) ((2 .
56) (1 . 57)) ((-1 . -45)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 .
6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 .
14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) 14) (40 . 15) (41 . 16) (42 . 17) (43 . 72)) ((3 . 1) (4 . 2) (5 . 3) (6
(47 . 22) (48 . 23) (49 . 24) (50 . 80)) ((2 . 79)) ((-1 . -30)) ((-1 . . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 .
-29)) ((-1 . -28)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -26)) ((14 . 42) ( 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 71)) ((3 . 1)
13 . 43) (12 . 44) (-1 . -25)) ((16 . 40) (15 . 41) (-1 . -23)) ((16 . 40) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10
(15 . 41) (-1 . -22)) ((18 . 38) (17 . 39) (-1 . -20)) ((18 . 38) (17 . ) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 70)) (
39) (-1 . -19)) ((18 . 38) (17 . 39) (-1 . -18)) ((18 . 38) (17 . 39) (-1 (3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9)
. -17)) ((22 . 34) (21 . 35) (20 . 36) (19 . 37) (-1 . -15)) ((22 . 34) ( (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42
21 . 35) (20 . 36) (19 . 37) (-1 . -14)) ((24 . 32) (23 . 33) (-1 . -12)) . 69)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (
((25 . 31) (-1 . -10)) ((26 . 30) (-1 . -8)) ((27 . 29) (-1 . -6)) ((28 . 10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 .
28) (-1 . -4)) ((30 . 78) (29 . 27)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 16) (42 . 68)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (
. 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 15
. 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 20 ) (41 . 16) (42 . 67)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (
) (46 . 21) (47 . 22) (48 . 23) (49 . 24) (50 . 81)) ((-1 . -44)) ((2 . 8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14)
-47) (1 . -47)) ((2 . -2) (1 . -2) (35 . -2)))) (40 . 15) (41 . 66)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8
. 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (
40 . 15) (41 . 65)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8
. 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (
40 . 64)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8
) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 14) (40 . 63)) ((
3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (
11 . 10) (16 . 11) (15 . 12) (38 . 13) (39 . 62)) ((3 . 1) (4 . 2) (5 . 3)
(6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15
. 12) (38 . 13) (39 . 61)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4) (7 . 5) (37
. 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (38 . 13) (39
. 60)) ((-1 . -39)) ((-1 . -40)) ((-1 . -32)) ((-1 . -33)) ((-1 . -34)) ((
-1 . -35)) ((-1 . -36)) ((-1 . -37)) ((7 . 59)) ((-1 . -45)) ((2 . -47) (1
. -47)) ((2 . 57) (1 . 58)) ((-1 . -46)) ((3 . 1) (4 . 2) (5 . 3) (6 . 4)
(7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (15 . 12) (
38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45
. 20) (46 . 21) (47 . 22) (48 . 23) (49 . 24) (50 . 81)) ((2 . 80)) ((-1
. -30)) ((-1 . -29)) ((-1 . -28)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -26
)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -25)) ((16 . 40) (15 . 41) (-1 .
-23)) ((16 . 40) (15 . 41) (-1 . -22)) ((18 . 38) (17 . 39) (-1 . -20)) ((
18 . 38) (17 . 39) (-1 . -19)) ((18 . 38) (17 . 39) (-1 . -18)) ((18 . 38)
(17 . 39) (-1 . -17)) ((22 . 34) (21 . 35) (20 . 36) (19 . 37) (-1 . -15)
) ((22 . 34) (21 . 35) (20 . 36) (19 . 37) (-1 . -14)) ((24 . 32) (23 . 33
) (-1 . -12)) ((25 . 31) (-1 . -10)) ((26 . 30) (-1 . -8)) ((27 . 29) (-1
. -6)) ((28 . 28) (-1 . -4)) ((30 . 79) (29 . 27)) ((3 . 1) (4 . 2) (5 . 3
) (6 . 4) (7 . 5) (37 . 6) (8 . 7) (9 . 8) (10 . 9) (11 . 10) (16 . 11) (
15 . 12) (38 . 13) (39 . 14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44
. 19) (45 . 20) (46 . 21) (47 . 22) (48 . 23) (49 . 24) (50 . 82)) ((-1 .
-44)) ((2 . -48) (1 . -48)) ((2 . -2) (1 . -2) (35 . -2))))
(define rto-v (define rto-v
#(#f 50 50 49 49 48 48 47 47 46 46 45 45 44 44 44 43 43 43 43 43 42 42 42 #(#f 50 50 49 49 48 48 47 47 46 46 45 45 44 44 44 43 43 43 43 43 42 42 42
41 41 41 40 40 40 40 39 39 39 39 39 39 39 38 38 38 37 37 37 37 37 36 36)) 41 41 41 40 40 40 40 39 39 39 39 39 39 39 38 38 38 37 37 37 37 37 37 36 36
))
(define mtab (define mtab
'(("," . 1) (")" . 2) ("(" . 3) ("defined" . 4) ($chlit . 5) ($fixed . 6) '(("," . 1) (")" . 2) ("(" . 3) ("defined" . 4) ($chlit . 5) ($fixed . 6)

View File

@ -1,6 +1,6 @@
;;; nyacc/lex.scm ;;; nyacc/lex.scm
;;; ;;;
;;; Copyright (C) 2015,2016 - Matthew R.Wette ;;; Copyright (C) 2015-2017 - Matthew R.Wette
;;; ;;;
;;; This library is free software; you can redistribute it and/or modify it ;;; This library is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as published by ;;; under the terms of the GNU Lesser General Public License as published by
@ -47,6 +47,7 @@
read-c-num read-c-num
read-oct read-hex read-oct read-hex
like-c-ident? like-c-ident?
cnumstr->scm
filter-mt remove-mt map-mt make-ident-like-p filter-mt remove-mt map-mt make-ident-like-p
c:ws c:if c:ir) c:ws c:if c:ir)
#:use-module ((srfi srfi-1) #:select (remove append-reverse)) #:use-module ((srfi srfi-1) #:select (remove append-reverse))
@ -87,6 +88,9 @@
(define c:ir (string->char-set digit c:if)) ; ident, rest chars (define c:ir (string->char-set digit c:if)) ; ident, rest chars
(define c:nx (string->char-set "eEdD")) ; number exponent (define c:nx (string->char-set "eEdD")) ; number exponent
(define c:hx (string->char-set "abcdefABCDEF")) (define c:hx (string->char-set "abcdefABCDEF"))
(define c:sx (string->char-set "lLuU")) ; suffix
(define (lsr chl) (list->string (reverse chl))) ; used often
;; @deffn eval-reader reader string => result ;; @deffn eval-reader reader string => result
;; For test and debug, this procedure will evaluate a reader on a string. ;; For test and debug, this procedure will evaluate a reader on a string.
@ -131,11 +135,11 @@
(cond (cond
((eof-object? ch) ((eof-object? ch)
(if (null? chl) #f (if (null? chl) #f
(list->string (reverse chl)))) (lsr chl)))
((char-set-contains? cs-rest ch) ((char-set-contains? cs-rest ch)
(iter (cons ch chl) (read-char))) (iter (cons ch chl) (read-char)))
(else (unread-char ch) (else (unread-char ch)
(list->string (reverse chl))))) (lsr chl))))
#f))) #f)))
;; @deffn read-c-ident ch => #f|string ;; @deffn read-c-ident ch => #f|string
@ -164,7 +168,7 @@
(if (eq? c1 #\newline) (if (eq? c1 #\newline)
(iter cl (read-char)) (iter cl (read-char))
(iter (cons* c1 cl) (read-char))))) (iter (cons* c1 cl) (read-char)))))
((eq? ch delim) (cons '$string (list->string (reverse cl)))) ((eq? ch delim) (cons '$string (lsr cl)))
(else (iter (cons ch cl) (read-char))))) (else (iter (cons ch cl) (read-char)))))
#f))) #f)))
@ -230,7 +234,7 @@
(cons (integer->char (read-oct ch)) cl) (cons (integer->char (read-oct ch)) cl)
(cons c1 cl)))) (cons c1 cl))))
(read-char)))) (read-char))))
((eq? ch #\") (cons '$string (list->string (reverse cl)))) ((eq? ch #\") (cons '$string (lsr cl)))
(else (iter (cons ch cl) (read-char))))))) (else (iter (cons ch cl) (read-char)))))))
;; @deffn make-chlit-reader ;; @deffn make-chlit-reader
@ -261,64 +265,94 @@
(else (error "bad escape sequence"))))) (else (error "bad escape sequence")))))
(cons '$chlit (string c1)))))) (cons '$chlit (string c1))))))
(define (fix-dot l) (if (char=? #\. (car l)) (cons #\0 l) l))
;; @deffn make-num-reader => (proc ch) => #f|($fixed . "1")|($float . "1.0") ;; @deffn make-num-reader => (proc ch) => #f|($fixed . "1")|($float . "1.0")
;; Reads C numbers.
;; This routine will clean by adding "0" before or after dot. ;; This routine will clean by adding "0" before or after dot.
;; TODO: add arg to specify alternate syntaxes (e.g. "0x123")
;; may want to replace "eEdD" w/ "e" ;; may want to replace "eEdD" w/ "e"
;; integer decimal(#t/#f) fraction exponent looking-at ;; integer decimal(#t/#f) fraction exponent looking-at
;; i, f and e are lists of characters ;; i, f and e are lists of characters
(define (make-num-reader) (define (make-num-reader)
;; 0: start; 1: p-i; 2: p-f; 3: p-e-sign; 4: p-e-d; 5: packup ;; 0: start; 1: p-i; 2: p-f; 3: p-e-sign; 4: p-e-d; 5: packup
;; Removed support for leading '.' to be a number. ;; Removed support for leading '.' to be a number.
(let ((fix-dot (lambda (l) (if (char=? #\. (car l)) (cons #\0 l) l)))) (lambda (ch1)
(lambda (ch1) ;; chl: char list; ty: '$fixed or '$float; st: state; ch: input char
;; chl: char list; ty: '$fixed or '$float; st: state; ch: input char (let iter ((chl '()) (ty #f) (st 0) (ch ch1))
(let iter ((chl '()) (ty #f) (st 0) (ch ch1)) (case st
(case st ((0)
((0) (cond
(cond ((eof-object? ch) (iter chl ty 5 ch))
((eof-object? ch) (iter chl ty 5 ch)) ((char=? #\0 ch) (iter (cons ch chl) '$fixed 10 (read-char)))
((char=? #\0 ch) (iter (cons ch chl) '$fixed 10 (read-char))) ((char-numeric? ch) (iter chl '$fixed 1 ch))
((char-numeric? ch) (iter chl '$fixed 1 ch)) (else #f)))
(else #f))) ((10) ;; allow x after 0
((10) ;; allow x after 0 (cond
(cond ((eof-object? ch) (iter chl ty 5 ch))
((eof-object? ch) (iter chl ty 5 ch)) ((char=? #\x ch) (iter (cons ch chl) ty 1 (read-char)))
((char=? #\x ch) (iter (cons ch chl) ty 1 (read-char))) (else (iter chl ty 1 ch))))
(else (iter chl ty 1 ch)))) ((1)
((1) (cond
(cond ((eof-object? ch) (iter chl ty 5 ch))
((eof-object? ch) (iter chl ty 5 ch)) ((char-numeric? ch) (iter (cons ch chl) ty 1 (read-char)))
((char-numeric? ch) (iter (cons ch chl) ty 1 (read-char))) ((char=? #\. ch) (iter (cons #\. chl) '$float 2 (read-char)))
((char=? #\. ch) (iter (cons #\. chl) '$float 2 (read-char))) ((char-set-contains? c:hx ch)
((char-set-contains? c:hx ch) (iter (cons ch chl) ty 1 (read-char)))
(iter (cons ch chl) ty 1 (read-char))) ((char-set-contains? c:sx ch)
((char-set-contains? c:if ch) (error "reading number st=1")) (iter (cons ch chl) ty 11 (read-char)))
(else (iter chl '$fixed 5 ch)))) ((char-set-contains? c:if ch) (error "lex/num-reader st=1"))
((2) (else (iter chl '$fixed 5 ch))))
(cond ((11) ;; got l L u or U, look for l or L
((eof-object? ch) (iter chl ty 5 ch)) (cond
((char-numeric? ch) (iter (cons ch chl) ty 2 (read-char))) ((eof-object? ch) (cons '$fixed (lsr chl)))
((char-set-contains? c:nx ch) ((char=? #\L ch) (cons '$fixed (lsr (cons ch chl))))
(iter (cons ch (fix-dot chl)) ty 3 (read-char))) ((char=? #\l ch) (cons '$fixed (lsr (cons ch chl))))
((char-set-contains? c:if ch) (error "reading number st=2")) (else (iter chl '$fixed 5 ch))))
(else (iter (fix-dot chl) ty 5 ch)))) ((2)
((3) (cond
(cond ((eof-object? ch) (iter chl ty 5 ch))
((eof-object? ch) (iter chl ty 5 ch)) ((char-numeric? ch) (iter (cons ch chl) ty 2 (read-char)))
((or (char=? #\+ ch) (char=? #\- ch)) ((char-set-contains? c:nx ch)
(iter (cons ch chl) ty 4 (read-char))) (iter (cons ch (fix-dot chl)) ty 3 (read-char)))
((char-numeric? ch) (iter chl ty 4 ch)) ((char-set-contains? c:if ch) (error "lex/num-reader st=2"))
(else (error "syntax3")))) (else (iter (fix-dot chl) ty 5 ch))))
((4) ((3)
(cond (cond
((eof-object? ch) (iter chl ty 5 ch)) ((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 4 (read-char))) ((or (char=? #\+ ch) (char=? #\- ch))
((char-set-contains? c:if ch) (error "reading number st=4")) (iter (cons ch chl) ty 4 (read-char)))
(else (iter chl ty 5 ch)))) ((char-numeric? ch) (iter chl ty 4 ch))
((5) (else (error "syntax3"))))
(unless (eof-object? ch) (unread-char ch)) ((4)
(cons ty (list->string (reverse chl))))))))) (cond
((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 4 (read-char)))
((char-set-contains? c:if ch) (error "lex/num-reader st=4"))
(else (iter chl ty 5 ch))))
((5)
(unless (eof-object? ch) (unread-char ch))
(cons ty (lsr chl)))))))
;; @deffn cnumstr->scm C99-str => scm-str
;; Convert C number-string (e.g, @code{0x123LL}) to Scheme numbers-string
;; (e.g., @code{#x123}).
(define (cnumstr->scm str)
(define (2- n) (1- (1- n)))
(let* ((nd (string-length str)))
(define (trim-rt st) ;; trim LlUu from right
(if (char-set-contains? c:sx (string-ref str (1- nd)))
(if (char-set-contains? c:sx (string-ref str (2- nd)))
(substring str st (2- nd))
(substring str st (1- nd)))
(substring str st nd)))
(if (< nd 2) str
(if (char=? #\0 (string-ref str 0))
(if (char=? #\x (string-ref str 1))
(string-append "#x" (trim-rt 2))
(if (char-numeric? (string-ref str 1))
(string-append "#o" (trim-rt 1))
(trim-rt 0)))
(trim-rt 0)))))
;; @deffn read-c-num ch => #f|string ;; @deffn read-c-num ch => #f|string
;; Reader for unsigned numbers as used in C (or close to it). ;; Reader for unsigned numbers as used in C (or close to it).
@ -369,12 +403,12 @@
((assq-ref node (car cl)) => ;; accept or shift next character ((assq-ref node (car cl)) => ;; accept or shift next character
(lambda (n) (lambda (n)
(if (eq? (caar n) 'else) ; if only else, accept, else read on (if (eq? (caar n) 'else) ; if only else, accept, else read on
(cons (cdar n) (list->string (reverse cl))) (cons (cdar n) (lsr cl))
(iter (cons (read-char) cl) n)))) (iter (cons (read-char) cl) n))))
((assq-ref node 'else) => ; else exists, accept ((assq-ref node 'else) => ; else exists, accept
(lambda (tok) (lambda (tok)
(unread-char (car cl)) (unread-char (car cl))
(cons tok (list->string (reverse (cdr cl)))))) (cons tok (lsr (cdr cl)))))
(else ;; reject (else ;; reject
(let pushback ((cl cl)) (let pushback ((cl cl))
(unless (null? (cdr cl)) (unless (null? (cdr cl))