PageRenderTime 37ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/utils.ss

http://github.com/yinwang0/ydiff
Scheme | 214 lines | 140 code | 51 blank | 23 comment | 0 complexity | 43e28fc3ef6d703814df93ec56fcbba4 MD5 | raw file
Possible License(s): GPL-3.0
  1. ;; ydiff - a language-aware tool for comparing programs
  2. ;; Copyright (C) 2011 Yin Wang (yinwang0@gmail.com)
  3. ;; This program is free software: you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;-------------------------------------------------------------
  14. ; utilities
  15. ;-------------------------------------------------------------
  16. (define-syntax letv
  17. (syntax-rules ()
  18. [(_ () body ...)
  19. (begin body ...)]
  20. [(_ ([(e1 e2* ...) e3] bd* ...) body ...)
  21. (let-values ([(e1 e2* ...) e3])
  22. (letv (bd* ...) body ...))]
  23. [(_ ([e1 e2] bd* ...) body ...)
  24. (let ([e1 e2])
  25. (letv (bd* ...) body ...))]))
  26. (define-syntax first-val
  27. (syntax-rules ()
  28. [(_ e)
  29. (letv ([(x y) e]) x)]))
  30. (define-syntax second-val
  31. (syntax-rules ()
  32. [(_ e)
  33. (letv ([(x y) e]) y)]))
  34. (define *debug* #f)
  35. (define-syntax peek
  36. (syntax-rules ()
  37. [(_ name args ...)
  38. (if *debug*
  39. (begin
  40. (printf "~s: ~s = ~s~n" name 'args args)
  41. ...)
  42. (void))]))
  43. ;; utility for error reporting
  44. (define fatal
  45. (lambda (who . args)
  46. (printf "~s: " who)
  47. (for-each display args)
  48. (display "\n")
  49. (error who "")))
  50. ; foldl of Racket has a bug!
  51. ; (foldl (lambda (x y) x) 0 '(1 2 3 4))
  52. ; => 4
  53. ; Don't use it!
  54. (define foldl2
  55. (lambda (f x ls)
  56. (cond
  57. [(null? ls) x]
  58. [else
  59. (foldl2 f (f x (car ls)) (cdr ls))])))
  60. ; (foldl2 + 0 '(1 2 3 4 ))
  61. (define orf
  62. (lambda (a b)
  63. (or a b)))
  64. (define char->string
  65. (lambda (c)
  66. (list->string (list c))))
  67. (define read-file
  68. (lambda (filename)
  69. (let ([port (open-input-file filename #:mode 'text)])
  70. (let loop ([line (read-line port)]
  71. [all ""])
  72. (cond
  73. [(eof-object? line) all]
  74. [else
  75. (loop (read-line port)
  76. (string-append all line "\n"))])))))
  77. (define new-progress
  78. (lambda (size)
  79. (let ([counter 0])
  80. (lambda (x)
  81. (cond
  82. [(string? x)
  83. (display x)
  84. (display "\n")
  85. (flush-output)]
  86. [(= 0 (remainder counter size))
  87. (set! counter (+ x counter))
  88. (display ".")
  89. (flush-output)]
  90. [else
  91. (set! counter (+ x counter))])))))
  92. ;;----------------- multi dimensional eq hash --------------------
  93. (define hash-put!
  94. (lambda (hash key1 key2 v)
  95. (cond
  96. [(hash-has-key? hash key2)
  97. (let ([inner (hash-ref hash key2)])
  98. (hash-set! inner key1 v))]
  99. [else
  100. (let ([inner (make-hasheq)])
  101. (hash-set! inner key1 v)
  102. (hash-set! hash key2 inner))])))
  103. (define hash-get
  104. (lambda (hash key1 key2)
  105. (cond
  106. [(hash-has-key? hash key2)
  107. (let ([inner (hash-ref hash key2)])
  108. (cond
  109. [(hash-has-key? inner key1)
  110. (hash-ref inner key1)]
  111. [else #f]))]
  112. [else #f])))
  113. (define hash-put2!
  114. (lambda (hash key1 key2 v)
  115. (cond
  116. [(hash-has-key? hash key2)
  117. (let ([inner (hash-ref hash key2)])
  118. (hash-set! inner key1 v))]
  119. [else
  120. (let ([inner (make-hash)])
  121. (hash-set! inner key1 v)
  122. (hash-set! hash key2 inner))])))
  123. (define hash-get2
  124. (lambda (hash key1 key2)
  125. (cond
  126. [(hash-has-key? hash key2)
  127. (let ([inner (hash-ref hash key2)])
  128. (cond
  129. [(hash-has-key? inner key1)
  130. (hash-ref inner key1)]
  131. [else #f]))]
  132. [else #f])))
  133. (define predand
  134. (lambda preds
  135. (lambda (x)
  136. (cond
  137. [(null? preds) #t]
  138. [((car preds) x)
  139. ((apply predand (cdr preds)) x)]
  140. [else #f]))))
  141. (define predor
  142. (lambda preds
  143. (lambda (x)
  144. (cond
  145. [(null? preds) #f]
  146. [((car preds) x) #t]
  147. [else
  148. ((apply predor (cdr preds)) x)]))))
  149. (define set-
  150. (lambda (s1 s2)
  151. (cond
  152. [(null? s1) '()]
  153. [(memq (car s1) s2)
  154. (set- (cdr s1) s2)]
  155. [else
  156. (cons (car s1) (set- (cdr s1) s2))])))
  157. (define string-join
  158. (lambda (ls sep)
  159. (cond
  160. [(null? ls) ""]
  161. [else
  162. (string-append (car ls) sep (string-join (cdr ls) sep))])))
  163. ; (string-join (list "a" "b" "c") ",")