set.scm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright (C) 2008 Kragen Javier Sitaker
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>
  18. ;; Setup output file
  19. (set-current-output-port (open-output-file "test/results/test059.answer"))
  20. (define (newline) (display #\newline))
  21. ;;; Tests of set!
  22. ;; There are three ways variables are stored at the moment: globally,
  23. ;; on the stack, and in the heap.
  24. (define global "global")
  25. (define (printglobal) (display global) (newline))
  26. (printglobal)
  27. ;; We'd like to ensure that the stack effect of set! (all three kinds)
  28. ;; is correct. It should return exactly one value, not zero or two.
  29. ;; But we can't care about what its actual value is, because it's
  30. ;; probably something different in the reference Scheme
  31. ;; implementation.
  32. ;; Executing it in what you'd call void context in C doesn't tell us
  33. ;; anything; it's unlikely we'll crash the program by popping off too
  34. ;; many stack items, and leaving extra stack items there is safe. So
  35. ;; we have to pass it as an argument to something. But it has to be
  36. ;; in a position where we're using the stuff underneath it; at the
  37. ;; moment, that means it needs to be the first argument.
  38. (display (cdr (cons (set! global "global variable") "and ")))
  39. (printglobal)
  40. (set! global "global variable gets set!")
  41. (printglobal)
  42. (define (printlocal local)
  43. (display (cdr (cons (set! local "local variable always the same")
  44. "the right stuff: ")))
  45. (display local)
  46. (newline))
  47. (printlocal "a")
  48. (printlocal "b")
  49. (define heap-printer
  50. (let ((val "original heap var"))
  51. (lambda (cmd arg)
  52. (case cmd
  53. ((set!) (display (cdr (cons (set! val arg) "set-"))))
  54. ((print) (begin (display val) (newline)))
  55. (else (error "bad cmd" cmd))))))
  56. (heap-printer 'print 0)
  57. (heap-printer 'set! "heap var")
  58. (heap-printer 'print 0)
  59. (heap-printer 'set! "heap var changes!")
  60. (heap-printer 'print 0)
  61. (exit 0)