cppmach.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;; lang/c99/cppmach.scm - CPP expression grammar
  2. ;; Copyright (C) 2015,2016,2018 Matthew R. Wette
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public License
  15. ;; along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (define-module (nyacc lang c99 cppmach)
  18. #:export (cpp-spec cpp-mach gen-cpp-files)
  19. #:use-module (nyacc lalr)
  20. #:use-module (nyacc parse)
  21. #:use-module (nyacc lex)
  22. #:use-module (nyacc lang util)
  23. #:use-module ((srfi srfi-43) #:select (vector-map))
  24. #:use-module (rnrs arithmetic bitwise))
  25. (define cpp-spec
  26. (lalr-spec
  27. (notice (string-append "Copyright (C) 2016,2017 Matthew R. Wette"
  28. license-lgpl3+))
  29. (expect 0)
  30. (start conditional-expression)
  31. (grammar
  32. (conditional-expression
  33. (logical-or-expression)
  34. (logical-or-expression "?" logical-or-expression ":" conditional-expression
  35. ($$ `(cond-expr ,$1 ,$3 ,$5))))
  36. (logical-or-expression
  37. (logical-and-expression)
  38. (logical-or-expression "||" logical-and-expression ($$ `(or ,$1 ,$3))))
  39. (logical-and-expression
  40. (bitwise-or-expression)
  41. (logical-and-expression "&&" bitwise-or-expression ($$ `(and ,$1 ,$3))))
  42. (bitwise-or-expression
  43. (bitwise-xor-expression)
  44. (bitwise-or-expression "|" bitwise-xor-expression
  45. ($$ `(bitwise-or ,$1 ,$3))))
  46. (bitwise-xor-expression
  47. (bitwise-and-expression)
  48. (bitwise-xor-expression "^" bitwise-and-expression
  49. ($$ `(bitwise-xor ,$1 ,$3))))
  50. (bitwise-and-expression
  51. (equality-expression)
  52. (bitwise-and-expression "&" equality-expression
  53. ($$ `(bitwise-and ,$1 ,$3))))
  54. (equality-expression
  55. (relational-expression)
  56. (equality-expression "==" relational-expression ($$ `(eq ,$1 ,$3)))
  57. (equality-expression "!=" relational-expression ($$ `(ne ,$1 ,$3))))
  58. (relational-expression
  59. (shift-expression)
  60. (relational-expression "<" shift-expression ($$ `(lt ,$1 ,$3)))
  61. (relational-expression "<=" shift-expression ($$ `(le ,$1 ,$3)))
  62. (relational-expression ">" shift-expression ($$ `(gt ,$1 ,$3)))
  63. (relational-expression ">=" shift-expression ($$ `(ge ,$1 ,$3))))
  64. (shift-expression
  65. (additive-expression)
  66. (shift-expression "<<" additive-expression ($$ `(lshift ,$1 ,$3)))
  67. (shift-expression ">>" additive-expression ($$ `(rshift ,$1 ,$3))))
  68. (additive-expression
  69. (multiplicative-expression)
  70. (additive-expression "+" multiplicative-expression ($$ `(add ,$1 ,$3)))
  71. (additive-expression "-" multiplicative-expression ($$ `(sub ,$1 ,$3))))
  72. (multiplicative-expression
  73. (unary-expression)
  74. (multiplicative-expression "*" unary-expression ($$ `(mul ,$1 ,$3)))
  75. (multiplicative-expression "/" unary-expression ($$ `(div ,$1 ,$3)))
  76. (multiplicative-expression "%" unary-expression ($$ `(mod ,$1 ,$3))))
  77. (unary-expression
  78. (postfix-expression)
  79. ("-" unary-expression ($$ `(neg ,$2)))
  80. ("+" unary-expression ($$ `(pos ,$2)))
  81. ("!" unary-expression ($$ `(not ,$2)))
  82. ("~" unary-expression ($$ `(bitwise-not ,$2)))
  83. ("++" unary-expression ($$ `(pre-inc ,$2)))
  84. ("--" unary-expression ($$ `(pre-dec ,$2))))
  85. (postfix-expression
  86. (primary-expression)
  87. (postfix-expression "++" ($$ `(post-inc ,$1)))
  88. (postfix-expression "--" ($$ `(post-dec ,$1))))
  89. (primary-expression
  90. ($ident ($$ `(ident ,$1)))
  91. ($fixed ($$ `(fixed ,$1))) ; integer literal
  92. ($chlit ($$ `(char ,$1))) ; char literal
  93. ($chlit/L ($$ `(char (@ (type "wchar_t")) ,$1)))
  94. ($chlit/u ($$ `(char (@ (type "char16_t")) ,$1)))
  95. ($chlit/U ($$ `(char (@ (type "char32_t")) ,$1)))
  96. ("defined" "(" $ident ")" ($$ `(defined ,$3)))
  97. ("defined" $ident ($$ `(defined ,$2)))
  98. ("__has_include__" "(" $string ")" ($$ `(has-include ,$3)))
  99. ("__has_include_next__" "(" $string ")" ($$ `(has-include-next ,$3)))
  100. ("(" expression-list ")" ($$ $2)))
  101. (expression-list
  102. (conditional-expression)
  103. (expression-list "," conditional-expression ($$ $3)))
  104. )))
  105. (define cpp-mach
  106. (compact-machine
  107. (hashify-machine
  108. (make-lalr-machine cpp-spec))))
  109. ;;; =====================================
  110. ;; @item gen-cpp-files [dir] => #t
  111. ;; Update or generate the files @quot{cppact.scm} and @quot{cpptab.scm}.
  112. ;; If there are no changes to existing files, no update occurs.
  113. (define (gen-cpp-files . rest)
  114. (define (lang-dir path)
  115. (if (pair? rest) (string-append (car rest) "/" path) path))
  116. (define (xtra-dir path)
  117. (lang-dir (string-append "mach.d/" path)))
  118. (write-lalr-actions cpp-mach (xtra-dir "cpp-act.scm.new") #:prefix "cpp-")
  119. (write-lalr-tables cpp-mach (xtra-dir "cpp-tab.scm.new") #:prefix "cpp-")
  120. (let ((a (move-if-changed (xtra-dir "cpp-act.scm.new")
  121. (xtra-dir "cpp-act.scm")))
  122. (b (move-if-changed (xtra-dir "cpp-tab.scm.new")
  123. (xtra-dir "cpp-tab.scm"))))
  124. (or a b)))
  125. ;; --- last line ---