cxmach.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; nyacc/lang/c99/cxmach.scm - constant expression grammar
  2. ;; Copyright (C) 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 cxmach)
  18. #:export (c99cx-spec c99cx-mach gen-c99cx-files)
  19. #:use-module (nyacc lalr)
  20. #:use-module (nyacc parse)
  21. #:use-module (nyacc lex)
  22. #:use-module (nyacc util)
  23. #:use-module (nyacc lang util)
  24. #:use-module (rnrs arithmetic bitwise)
  25. #:use-module ((srfi srfi-43) #:select (vector-map vector-for-each))
  26. #:use-module (system foreign))
  27. (define c99cx-spec
  28. (lalr-spec
  29. (notice (string-append "Copyright (C) 2018 Matthew R. Wette" license-lgpl3+))
  30. (expect 0)
  31. (start constant-expression)
  32. (grammar
  33. (primary-expression
  34. (identifier ($$ `(p-expr ,$1)))
  35. (constant ($$ `(p-expr ,$1)))
  36. (string-literal ($$ `(p-expr ,(tl->list $1))))
  37. ("(" constant-expression ")" ($$ $2)))
  38. (postfix-expression
  39. (primary-expression)
  40. (postfix-expression "[" constant-expression "]" ($$ `(array-ref ,$3 ,$1)))
  41. (postfix-expression "." identifier ($$ `(d-sel ,$3 ,$1)))
  42. (postfix-expression "->" identifier ($$ `(i-sel ,$3 ,$1)))
  43. (postfix-expression "++" ($$ `(post-inc ,$1)))
  44. (postfix-expression "--" ($$ `(post-dec ,$1))))
  45. (unary-expression
  46. (postfix-expression) ; S 6.5.3
  47. ("++" unary-expression ($$ `(pre-inc ,$2)))
  48. ("--" unary-expression ($$ `(pre-dec ,$2)))
  49. (unary-operator cast-expression ($$ (list $1 $2)))
  50. ("sizeof" unary-expression ($$ `(sizeof-expr ,$2)))
  51. ;;("sizeof" "(" type-name ")" ($$ `(sizeof-type ,$3)))
  52. )
  53. (unary-operator ("&" ($$ 'ref-to)) ("*" ($$ 'de-ref))
  54. ("+" ($$ 'pos)) ("-" ($$ 'neg))
  55. ("~" ($$ 'bitwise-not)) ("!" ($$ 'not)))
  56. (cast-expression
  57. (unary-expression)
  58. ;;("(" type-name ")" cast-expression ($$ `(cast ,$2 ,$4)))
  59. )
  60. (multiplicative-expression
  61. (cast-expression)
  62. (multiplicative-expression "*" cast-expression ($$ `(mul ,$1 ,$3)))
  63. (multiplicative-expression "/" cast-expression ($$ `(div ,$1 ,$3)))
  64. (multiplicative-expression "%" cast-expression ($$ `(mod ,$1 ,$3))))
  65. (additive-expression
  66. (multiplicative-expression)
  67. (additive-expression "+" multiplicative-expression ($$ `(add ,$1 ,$3)))
  68. (additive-expression "-" multiplicative-expression ($$ `(sub ,$1 ,$3))))
  69. (shift-expression
  70. (additive-expression)
  71. (shift-expression "<<" additive-expression ($$ `(lshift ,$1 ,$3)))
  72. (shift-expression ">>" additive-expression ($$ `(rshift ,$1 ,$3))))
  73. (relational-expression
  74. (shift-expression)
  75. (relational-expression "<" shift-expression ($$ `(lt ,$1 ,$3)))
  76. (relational-expression ">" shift-expression ($$ `(gt ,$1 ,$3)))
  77. (relational-expression "<=" shift-expression ($$ `(le ,$1 ,$3)))
  78. (relational-expression ">=" shift-expression ($$ `(ge ,$1 ,$3))))
  79. (equality-expression
  80. (relational-expression)
  81. (equality-expression "==" relational-expression ($$ `(eq ,$1 ,$3)))
  82. (equality-expression "!=" relational-expression ($$ `(ne ,$1 ,$3))))
  83. (bitwise-and-expression
  84. (equality-expression)
  85. (bitwise-and-expression
  86. "&" equality-expression ($$ `(bitwise-and ,$1 ,$3))))
  87. (bitwise-xor-expression
  88. (bitwise-and-expression)
  89. (bitwise-xor-expression
  90. "^" bitwise-and-expression ($$ `(bitwise-xor ,$1 ,$3))))
  91. (bitwise-or-expression
  92. (bitwise-xor-expression)
  93. (bitwise-or-expression
  94. "|" bitwise-xor-expression ($$ `(bitwise-or ,$1 ,$3))))
  95. (logical-and-expression
  96. (bitwise-or-expression)
  97. (logical-and-expression
  98. "&&" bitwise-or-expression ($$ `(and ,$1 ,$3))))
  99. (logical-or-expression
  100. (logical-and-expression)
  101. (logical-or-expression
  102. "||" logical-and-expression ($$ `(or ,$1 ,$3))))
  103. (conditional-expression
  104. (logical-or-expression)
  105. (logical-or-expression
  106. "?" constant-expression
  107. ":" conditional-expression ($$ `(cond-expr ,$1 ,$3 ,$5))))
  108. (constant-expression
  109. (conditional-expression))
  110. ;;
  111. (identifier
  112. ($ident ($$ `(ident ,$1))))
  113. (constant
  114. ($fixed ($$ `(fixed ,$1))) ; integer literal
  115. ($float ($$ `(float ,$1))) ; floating literal
  116. ($chlit ($$ `(char ,$1))) ; char literal
  117. ($chlit/L ($$ `(char (@ (type "wchar_t")) ,$1)))
  118. ($chlit/u ($$ `(char (@ (type "char16_t")) ,$1)))
  119. ($chlit/U ($$ `(char (@ (type "char32_t")) ,$1))))
  120. (string-literal
  121. ($string ($$ (make-tl 'string $1))) ; string-constant
  122. (string-literal $string ($$ (tl-append $1 $2)))))))
  123. (define c99cx-mach
  124. (compact-machine
  125. (hashify-machine
  126. (make-lalr-machine c99cx-spec))))
  127. ;;; =====================================
  128. ;; @item gen-c99cx-files [dir] => #t
  129. ;; Update or generate the files @quot{cppact.scm} and @quot{cpptab.scm}.
  130. ;; If there are no changes to existing files, no update occurs.
  131. (define* (gen-c99cx-files #:optional (path "."))
  132. (define (mdir file) (mach-dir path file))
  133. (write-lalr-actions c99cx-mach (mdir "c99cx-act.scm.new") #:prefix "c99cx-")
  134. (write-lalr-tables c99cx-mach (mdir "c99cx-tab.scm.new") #:prefix "c99cx-")
  135. (let ((a (move-if-changed (mdir "c99cx-act.scm.new") (mdir "c99cx-act.scm")))
  136. (b (move-if-changed (mdir "c99cx-tab.scm.new") (mdir "c99cx-tab.scm"))))
  137. (or a b)))
  138. ;; --- last line ---