From 7b665385c967277557b31b44df8ac8fcf547e247 Mon Sep 17 00:00:00 2001 From: Matt Wette Date: Sun, 1 Jan 2017 19:40:27 -0800 Subject: [PATCH] nyacc: have something working for in-place include --- module/nyacc/ChangeLog | 16 +++++++++---- module/nyacc/lang/c99/body.scm | 40 ++++++++++++++++++-------------- module/nyacc/lang/c99/pprint.scm | 3 +++ module/nyacc/lang/util.scm | 11 --------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/module/nyacc/ChangeLog b/module/nyacc/ChangeLog index e92c8360..9d4cd244 100644 --- a/module/nyacc/ChangeLog +++ b/module/nyacc/ChangeLog @@ -1,23 +1,29 @@ -2016-11-25 Matt Wette +2017-01-01 Matt Wette + + * lang/c99/body.scm (gen-c-lexer): in code mode now expands includes + + * lang/c99/pprint.scm (pretty-print-c99): added enum-dev w/o name + +2016-11-25 Matt Wette * added support for ellipsis to lang/c99/cpp.scm -2016-11-24 Matt Wette +2016-11-24 Matt Wette * added (ellipsis) to lang/c99/pprint.scm -2016-04-09 Matt Wette +2016-04-09 Matt Wette * bison.scm: new file providing make-lalr-machin/bison. It is similar to make-lalr-machine but uses external bison program instead of the default from-scratch lalr code. -2016-03-04 Matt Wette +2016-03-04 Matt Wette * lalr.scm: changed grammar parser to interpret $string as terminal. This saves typing a quote in front. -Copyright (C) 2015,2016 Matthew R. Wette +Copyright (C) 2015-2017 Matthew R. Wette Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright diff --git a/module/nyacc/lang/c99/body.scm b/module/nyacc/lang/c99/body.scm index f96574b6..596adfa2 100644 --- a/module/nyacc/lang/c99/body.scm +++ b/module/nyacc/lang/c99/body.scm @@ -307,26 +307,29 @@ (define (exec-cpp line) ;; Parse the line into a CPP stmt, execute it, and return it. - (let* ((stmt (read-cpp-stmt line)) - (perr (lambda (file) - (throw 'parse-error "file not found: ~S" file)))) + (let* ((stmt (read-cpp-stmt line))) (case (car stmt) ((include) (let* ((parg (cadr stmt)) (leng (string-length parg)) (file (substring parg 1 (1- leng))) + (path (find-file-in-dirl file (cpi-incs info))) (tynd (assoc-ref (cpi-tynd info) file))) - (if tynd - (for-each add-typename tynd) - (let* ((pth (find-file-in-dirl file (cpi-incs info))) - (tree (if pth ; path exists - (or (with-input-from-file pth run-parse) - (throw 'parse-error "~A" pth)) - (perr file)))) - (simple-format #t "INCLUDE top?=~S\n" (at-top?)) - (for-each add-define (xp1 tree)) ; add def's - ;; Attach tree onto "include" statement. - (if (pair? tree) (set! stmt (append stmt (list tree)))) - )))) + (cond + (tynd (for-each add-typename tynd)) ; in dot-h dict + ((and #t (eqv? mode 'code)) ; include flat + (if (not path) (throw 'parse-error "not found: ~S" file)) + (push-input (open-input-file path)) + (set! stmt #f)) + (else ; include as tree + (if (not path) (throw 'parse-error "not found: ~A" path)) + (let* ((tree (with-input-from-file path run-parse))) + (if (not tree) (throw 'parse-error "~A" path)) + ;;(simple-format #t "INCLUDE top?=~S\n" (at-top?)) + (for-each add-define (xp1 tree)) ; add def's + ;; Attach tree onto "include" statement. + (if (pair? tree) + (set! stmt (append stmt (list tree))) + stmt)))))) ((define) (add-define stmt)) ((undef) @@ -383,7 +386,7 @@ stmt) (else (error "unhandled cpp stmt"))) - (cons 'cpp-stmt stmt))) + (if stmt (cons 'cpp-stmt stmt) '()))) ;; Composition of @code{read-cpp-line} and @code{exec-cpp}. (define (read-cpp ch) @@ -399,7 +402,10 @@ (bol (cond ((read-comm ch bol) => assc-$) - ((read-cpp ch) => assc-$) + ((read-cpp ch) => + (lambda (res) + ;; not pair means expand include file, so loop again + (if (pair? res) (assc-$ res) (iter (read-char))))) (else (set! bol #f) (iter ch)))) ((read-ident ch) => (lambda (name) diff --git a/module/nyacc/lang/c99/pprint.scm b/module/nyacc/lang/c99/pprint.scm index f2f6a4f3..7a210825 100644 --- a/module/nyacc/lang/c99/pprint.scm +++ b/module/nyacc/lang/c99/pprint.scm @@ -336,6 +336,9 @@ ((enum-def (ident ,name) (enum-def-list . ,edl)) (sf "enum ~A " name) (ppx `(enum-def-list . ,edl))) + ((enum-def (enum-def-list . ,edl)) + (sf "enum ") (ppx `(enum-def-list . ,edl))) + ((enum-def-list . ,defns) (sf "{\n") (push-il) (for-each ppx defns) diff --git a/module/nyacc/lang/util.scm b/module/nyacc/lang/util.scm index be0fa01a..cd240379 100644 --- a/module/nyacc/lang/util.scm +++ b/module/nyacc/lang/util.scm @@ -63,17 +63,6 @@ the file COPYING included with the this distribution.") (set-current-input-port (car ipstk)) (fluid-set! *input-stack* (cdr ipstk)))))) -;; It may be possible to reimplement with closures, using soft-ports. -;; (push-string-input ... - -#| -(define (push-string-input str) -(let* ((prev (current-input-port)) - (port (make-soft-port ...)) - ) -#f)) -|# - ;; === tl ============================== ;; @section Tagged Lists