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,
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.
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>
* 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)))
(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)
((if)
(let ((val (eval-cpp-cond-text (cadr stmt))))
(if (not val) (p-err "unresolved: ~S" (cadr stmt)))
(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)))))
(case (car ppxs)
((skip-look skip-done skip) ;; don't eval if excluded
(set! ppxs (cons 'skip ppxs)))
(else
(let ((val (eval-cpp-cond-text (cadr stmt))))
(if (not val) (p-err "unresolved: ~S" (cadr stmt)))
(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)
(let ((val (eval-cpp-cond-text (cadr stmt))))
(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)))))
(case (car ppxs)
((skip) #t) ;; don't eval if excluded
(else
(let ((val (eval-cpp-cond-text (cadr stmt))))
(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)
(case (car ppxs)
((skip-look) (set-car! ppxs 'keep))
@ -444,14 +451,10 @@
;; Loop between reading tokens and skipping tokens via CPP logic.
(let iter ((pair (read-token)))
(case (car ppxs)
((keep)
;;(simple-format #t "lx=>~S\n" pair)
pair)
((skip-done skip-look)
((keep) pair)
((skip-done skip-look skip)
(iter (read-token)))
((skip1-pop)
(set! ppxs (cdr ppxs))
(iter (read-token)))))
(else (error "coding error"))))
)))))
;; --- last line ---

View file

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

View file

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

View file

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

View file

@ -8,7 +8,7 @@
(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
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
#(((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)
(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 . 54) (36 . 55)) ((3 . 53)) ((-1 . -43)) ((-1 .
-42)) ((-1 . -41)) ((-1 . -38)) ((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 . 52)) ((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 . 51)) ((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 . 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
(48 . 23) (49 . 24) (50 . 55) (36 . 56)) ((3 . 53) (7 . 54)) ((-1 . -43))
((-1 . -42)) ((-1 . -41)) ((-1 . -38)) ((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 . 52)) ((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 . 51)) ((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 . 47)) ((9 . 45) (8 . 46) (-1 .
-31)) ((-1 . -27)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -24)) ((16 . 40) (
15 . 41) (-1 . -21)) ((18 . 38) (17 . 39) (-1 . -16)) ((22 . 34) (21 . 35)
(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 .
. 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 .
14) (40 . 15) (41 . 16) (42 . 17) (43 . 18) (44 . 19) (45 . 73)) ((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 . 72)) ((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 . 71)) ((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 . 70)) ((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 . 69)) ((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 . 68))
48)) ((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 . 47)) ((9 . 45) (8 . 46
) (-1 . -31)) ((-1 . -27)) ((14 . 42) (13 . 43) (12 . 44) (-1 . -24)) ((16
. 40) (15 . 41) (-1 . -21)) ((18 . 38) (17 . 39) (-1 . -16)) ((22 . 34) (
21 . 35) (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
. 78)) ((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 . 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 . 67)) ((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 . 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 .
15) (41 . 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 . 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 .
42 . 17) (43 . 18) (44 . 19) (45 . 20) (46 . 21) (47 . 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 . 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 . 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 . 14) (40 . 15) (41 . 16) (42
. 17) (43 . 18) (44 . 73)) ((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 . 80)) ((2 . 79)) ((-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 . 78) (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 . 81)) ((-1 . -44)) ((2 .
-47) (1 . -47)) ((2 . -2) (1 . -2) (35 . -2))))
14) (40 . 15) (41 . 16) (42 . 17) (43 . 72)) ((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 . 71)) ((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 . 70)) (
(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
. 69)) ((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 . 68)) ((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 . 67)) ((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 . 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
#(#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
'(("," . 1) (")" . 2) ("(" . 3) ("defined" . 4) ($chlit . 5) ($fixed . 6)

View file

@ -1,6 +1,6 @@
;;; 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
;;; under the terms of the GNU Lesser General Public License as published by
@ -47,6 +47,7 @@
read-c-num
read-oct read-hex
like-c-ident?
cnumstr->scm
filter-mt remove-mt map-mt make-ident-like-p
c:ws c:if c:ir)
#: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:nx (string->char-set "eEdD")) ; number exponent
(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
;; For test and debug, this procedure will evaluate a reader on a string.
@ -131,11 +135,11 @@
(cond
((eof-object? ch)
(if (null? chl) #f
(list->string (reverse chl))))
(lsr chl)))
((char-set-contains? cs-rest ch)
(iter (cons ch chl) (read-char)))
(else (unread-char ch)
(list->string (reverse chl)))))
(lsr chl))))
#f)))
;; @deffn read-c-ident ch => #f|string
@ -164,7 +168,7 @@
(if (eq? c1 #\newline)
(iter 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)))))
#f)))
@ -230,7 +234,7 @@
(cons (integer->char (read-oct ch)) cl)
(cons c1 cl))))
(read-char))))
((eq? ch #\") (cons '$string (list->string (reverse cl))))
((eq? ch #\") (cons '$string (lsr cl)))
(else (iter (cons ch cl) (read-char)))))))
;; @deffn make-chlit-reader
@ -261,65 +265,95 @@
(else (error "bad escape sequence")))))
(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")
;; Reads C numbers.
;; 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"
;; integer decimal(#t/#f) fraction exponent looking-at
;; i, f and e are lists of characters
(define (make-num-reader)
;; 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.
(let ((fix-dot (lambda (l) (if (char=? #\. (car l)) (cons #\0 l) l))))
(lambda (ch1)
;; chl: char list; ty: '$fixed or '$float; st: state; ch: input char
(let iter ((chl '()) (ty #f) (st 0) (ch ch1))
(case st
((0)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char=? #\0 ch) (iter (cons ch chl) '$fixed 10 (read-char)))
((char-numeric? ch) (iter chl '$fixed 1 ch))
(else #f)))
((10) ;; allow x after 0
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char=? #\x ch) (iter (cons ch chl) ty 1 (read-char)))
(else (iter chl ty 1 ch))))
((1)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 1 (read-char)))
((char=? #\. ch) (iter (cons #\. chl) '$float 2 (read-char)))
((char-set-contains? c:hx ch)
(iter (cons ch chl) ty 1 (read-char)))
((char-set-contains? c:if ch) (error "reading number st=1"))
(else (iter chl '$fixed 5 ch))))
((2)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 2 (read-char)))
((char-set-contains? c:nx ch)
(iter (cons ch (fix-dot chl)) ty 3 (read-char)))
((char-set-contains? c:if ch) (error "reading number st=2"))
(else (iter (fix-dot chl) ty 5 ch))))
((3)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((or (char=? #\+ ch) (char=? #\- ch))
(iter (cons ch chl) ty 4 (read-char)))
((char-numeric? ch) (iter chl ty 4 ch))
(else (error "syntax3"))))
((4)
(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 "reading number st=4"))
(else (iter chl ty 5 ch))))
((5)
(unless (eof-object? ch) (unread-char ch))
(cons ty (list->string (reverse chl)))))))))
(lambda (ch1)
;; chl: char list; ty: '$fixed or '$float; st: state; ch: input char
(let iter ((chl '()) (ty #f) (st 0) (ch ch1))
(case st
((0)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char=? #\0 ch) (iter (cons ch chl) '$fixed 10 (read-char)))
((char-numeric? ch) (iter chl '$fixed 1 ch))
(else #f)))
((10) ;; allow x after 0
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char=? #\x ch) (iter (cons ch chl) ty 1 (read-char)))
(else (iter chl ty 1 ch))))
((1)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 1 (read-char)))
((char=? #\. ch) (iter (cons #\. chl) '$float 2 (read-char)))
((char-set-contains? c:hx ch)
(iter (cons ch chl) ty 1 (read-char)))
((char-set-contains? c:sx ch)
(iter (cons ch chl) ty 11 (read-char)))
((char-set-contains? c:if ch) (error "lex/num-reader st=1"))
(else (iter chl '$fixed 5 ch))))
((11) ;; got l L u or U, look for l or L
(cond
((eof-object? ch) (cons '$fixed (lsr chl)))
((char=? #\L ch) (cons '$fixed (lsr (cons ch chl))))
((char=? #\l ch) (cons '$fixed (lsr (cons ch chl))))
(else (iter chl '$fixed 5 ch))))
((2)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((char-numeric? ch) (iter (cons ch chl) ty 2 (read-char)))
((char-set-contains? c:nx ch)
(iter (cons ch (fix-dot chl)) ty 3 (read-char)))
((char-set-contains? c:if ch) (error "lex/num-reader st=2"))
(else (iter (fix-dot chl) ty 5 ch))))
((3)
(cond
((eof-object? ch) (iter chl ty 5 ch))
((or (char=? #\+ ch) (char=? #\- ch))
(iter (cons ch chl) ty 4 (read-char)))
((char-numeric? ch) (iter chl ty 4 ch))
(else (error "syntax3"))))
((4)
(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
;; Reader for unsigned numbers as used in C (or close to it).
(define read-c-num (make-num-reader))
@ -369,12 +403,12 @@
((assq-ref node (car cl)) => ;; accept or shift next character
(lambda (n)
(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))))
((assq-ref node 'else) => ; else exists, accept
(lambda (tok)
(unread-char (car cl))
(cons tok (list->string (reverse (cdr cl))))))
(cons tok (lsr (cdr cl)))))
(else ;; reject
(let pushback ((cl cl))
(unless (null? (cdr cl))