preprocess.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (define-module (mescc preprocess)
  21. #:use-module (ice-9 optargs)
  22. #:use-module (system base pmatch)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (nyacc lang c99 parser)
  26. #:use-module (nyacc lang c99 parser)
  27. #:use-module (nyacc version)
  28. #:use-module (mes guile)
  29. #:export (c99-input->ast))
  30. (when (getenv "MESC_DEBUG")
  31. (format (current-error-port) "*nyacc-version*=~a\n" *nyacc-version*))
  32. ;; list of which rules you want progress reported
  33. (define need-progress
  34. (or (assoc-ref
  35. '(("0.85.3" (1 2 3))
  36. ("0.86.0" (1 2 3)))
  37. *nyacc-version*)
  38. '(1 2 3)))
  39. (define (progress o)
  40. (when (and o (getenv "NYACC_DEBUG"))
  41. (display " :" (current-error-port))
  42. (display o (current-error-port))
  43. (display "\n" (current-error-port))))
  44. (define (insert-progress-monitors act-v len-v)
  45. (let ((n (vector-length act-v)))
  46. (let loop ((ix 0))
  47. (when (< ix n)
  48. (if (memq ix need-progress)
  49. (vector-set!
  50. act-v ix
  51. (lambda args
  52. (progress (list-ref args (1- (vector-ref len-v ix))))
  53. (apply (vector-ref act-v ix) args))))
  54. (loop (1+ ix))))))
  55. (cond-expand
  56. (guile
  57. (insert-progress-monitors (@@ (nyacc lang c99 parser) c99-act-v)
  58. (@@ (nyacc lang c99 parser) c99-len-v)))
  59. (mes
  60. (insert-progress-monitors c99-act-v c99-len-v)))
  61. (define (logf port string . rest)
  62. (apply format (cons* port string rest))
  63. (force-output port)
  64. #t)
  65. (define (stderr string . rest)
  66. (apply logf (cons* (current-error-port) string rest)))
  67. (define mes? (pair? (current-module)))
  68. (define* (c99-input->full-ast #:key (prefix "") (defines '()) (includes '()))
  69. (let ((sys-include (if (equal? prefix "") "include" (string-append prefix "/share/include"))))
  70. (parse-c99
  71. #:inc-dirs (append includes (cons* sys-include "include" "lib" (or (and=> (getenv "C_INCLUDE_PATH") (cut string-split <> #\:)) '())))
  72. #:cpp-defs `(
  73. "NULL=0"
  74. "__linux__=1"
  75. "_POSIX_SOURCE=0"
  76. "WITH_GLIBC=0"
  77. "__STDC__=1"
  78. "__MESC__=1"
  79. ,(if mes? "__MESC_MES__=1" "__MESC_MES__=0")
  80. ,@defines)
  81. #:mode 'code)))
  82. (define* (c99-input->ast #:key (prefix "") (defines '()) (includes '()))
  83. (stderr "parsing: input\n")
  84. ((compose ast-strip-const ast-strip-comment) (c99-input->full-ast #:prefix prefix #:defines defines #:includes includes)))
  85. (define (ast-strip-comment o)
  86. (pmatch o
  87. ((comment . ,comment) #f)
  88. (((comment . ,comment) . ,t) (filter-map ast-strip-comment t))
  89. (((comment . ,comment) . ,cdr) cdr)
  90. ((,car . (comment . ,comment)) car)
  91. ((,h . ,t) (if (list? o) (filter-map ast-strip-comment o)
  92. (cons (ast-strip-comment h) (ast-strip-comment t))))
  93. (_ o)))
  94. (define (ast-strip-const o)
  95. (pmatch o
  96. ((type-qual ,qual) (if (equal? qual "const") #f o))
  97. ((pointer (type-qual-list (type-qual ,qual)) . ,rest)
  98. (if (equal? qual "const") `(pointer ,@rest) o))
  99. ((decl-spec-list (type-qual ,qual))
  100. (if (equal? qual "const") #f
  101. `(decl-spec-list (type-qual ,qual))))
  102. ((decl-spec-list (type-qual ,qual) . ,rest)
  103. (if (equal? qual "const") `(decl-spec-list ,@rest)
  104. `(decl-spec-list (type-qual ,qual) ,@(map ast-strip-const rest))))
  105. ((decl-spec-list (type-qual-list (type-qual ,qual)) . ,rest)
  106. (if (equal? qual "const") `(decl-spec-list ,@rest)
  107. `(decl-spec-list (type-qual-list (type-qual ,qual)) ,@(map ast-strip-const rest))))
  108. ((,h . ,t) (if (list? o) (filter-map ast-strip-const o)
  109. (cons (ast-strip-const h) (ast-strip-const t))))
  110. (_ o)))