char.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ;;; (hoot chars) library
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Basic parts of (scheme chars).
  18. ;;;
  19. ;;; Code:
  20. (library (hoot char)
  21. (export char->integer
  22. integer->char
  23. char?
  24. char<?
  25. char<=?
  26. char=?
  27. char>=?
  28. char>?
  29. char-upcase
  30. char-downcase
  31. char-alphabetic?
  32. char-lower-case?
  33. char-numeric?
  34. char-upper-case?
  35. char-whitespace?
  36. string-upcase
  37. string-downcase)
  38. (import (rename (only (hoot primitives)
  39. %+ %- %string? %vector-ref %< %<= %= %>= %>
  40. %char->integer %integer->char %char?)
  41. (%+ +)
  42. (%- -)
  43. (%string? string?)
  44. (%vector-ref vector-ref)
  45. (%< <) (%<= <=) (%= =) (%>= >=) (%> >))
  46. (hoot bitvectors)
  47. (hoot bitwise)
  48. (hoot errors)
  49. (hoot inline-wasm)
  50. (hoot match)
  51. (hoot syntax))
  52. (define (char->integer x) (%char->integer x))
  53. (define (integer->char x) (%integer->char x))
  54. (define (char? x) (%char? x))
  55. (define-syntax-rule (define-comparison-expansion name cmp)
  56. (define name
  57. (case-lambda
  58. ((a b) (cmp a b))
  59. ((a b . c)
  60. (let lp ((res (cmp a b)) (a b) (c c))
  61. (match c
  62. (() res)
  63. ((b . c)
  64. (lp (and (cmp a b) res) b c))))))))
  65. (define-syntax-rule (define-char-comparison-expansion name cmp)
  66. (define-comparison-expansion name
  67. (lambda (a b) (cmp (char->integer a) (char->integer b)))))
  68. (define-char-comparison-expansion char<? <)
  69. (define-char-comparison-expansion char<=? <=)
  70. (define-char-comparison-expansion char=? =)
  71. (define-char-comparison-expansion char>=? >=)
  72. (define-char-comparison-expansion char>? >)
  73. ;; generated (scheme char) procedures:
  74. ;; char-upcase
  75. ;; char-downcase
  76. ;; char-upper-case?
  77. ;; char-lower-case?
  78. ;; char-alphabetic?
  79. ;; char-numeric?
  80. ;; char-whitespace?
  81. (include-from-path "hoot/char-prelude")
  82. (define (string-upcase str)
  83. (check-type str string? 'string-upcase)
  84. (%inline-wasm
  85. '(func (param $str (ref string))
  86. (result (ref eq))
  87. (struct.new $string
  88. (i32.const 0)
  89. (call $string-upcase (local.get $str))))
  90. str))
  91. (define (string-downcase str)
  92. (check-type str string? 'string-downcase)
  93. (%inline-wasm
  94. '(func (param $str (ref string))
  95. (result (ref eq))
  96. (struct.new $string
  97. (i32.const 0)
  98. (call $string-downcase (local.get $str))))
  99. str)))