char.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 (hoot primitives)
  39. (%+ +)
  40. (%- -)
  41. (%string? string?)
  42. (%vector-ref vector-ref)
  43. (%< <) (%<= <=) (%= =) (%>= >=) (%> >))
  44. (hoot bitwise)
  45. (hoot match)
  46. (hoot bitvectors)
  47. (hoot errors))
  48. (define (char->integer x) (%char->integer x))
  49. (define (integer->char x) (%integer->char x))
  50. (define (char? x) (%char? x))
  51. (define-syntax-rule (define-comparison-expansion name cmp)
  52. (define name
  53. (case-lambda
  54. ((a b) (cmp a b))
  55. ((a b . c)
  56. (let lp ((res (cmp a b)) (a b) (c c))
  57. (match c
  58. (() res)
  59. ((b . c)
  60. (lp (and (cmp a b) res) b c))))))))
  61. (define-syntax-rule (define-char-comparison-expansion name cmp)
  62. (define-comparison-expansion name
  63. (lambda (a b) (cmp (char->integer a) (char->integer b)))))
  64. (define-char-comparison-expansion char<? <)
  65. (define-char-comparison-expansion char<=? <=)
  66. (define-char-comparison-expansion char=? =)
  67. (define-char-comparison-expansion char>=? >=)
  68. (define-char-comparison-expansion char>? >)
  69. ;; generated (scheme char) procedures:
  70. ;; char-upcase
  71. ;; char-downcase
  72. ;; char-upper-case?
  73. ;; char-lower-case?
  74. ;; char-alphabetic?
  75. ;; char-numeric?
  76. ;; char-whitespace?
  77. (include-from-path "hoot/char-prelude")
  78. (define (string-upcase str)
  79. (check-type str string? 'string-upcase)
  80. (%inline-wasm
  81. '(func (param $str (ref string))
  82. (result (ref eq))
  83. (struct.new $string
  84. (i32.const 0)
  85. (call $string-upcase (local.get $str))))
  86. str))
  87. (define (string-downcase str)
  88. (check-type str string? 'string-downcase)
  89. (%inline-wasm
  90. '(func (param $str (ref string))
  91. (result (ref eq))
  92. (struct.new $string
  93. (i32.const 0)
  94. (call $string-downcase (local.get $str))))
  95. str)))