Posted by Anonymous Tue 27th Feb 2007 23:26 - Syntax is CSS - 32 views
Download | New Post | Modify | Hide line numbers
  1.  
  2. (define-syntax state
  3.   (syntax-rules (else)
  4.     ((_ (next-state test-case then-case)
  5.         ...
  6.         (else
  7.          else-case))
  8.      (lambda (message)
  9.        (case message
  10.          ((next-state)
  11.           (if test-case
  12.               then-case
  13.               else-case))
  14.          ...
  15.          (else
  16.           else-case))))
  17.     ((_ (next-state test-case then-case)
  18.         ...)
  19.      (state
  20.        (next-state test-case then-case)
  21.        ...
  22.        (else
  23.         #f)))))
  24.  
  25. (define-syntax states
  26.   (syntax-rules (else)
  27.     ((_ (state-name
  28.           (next-state test-case then-case)
  29.           ...)
  30.         ...)
  31.      (begin
  32.        (define state-name
  33.          (state
  34.            (next-state test-case then-case)
  35.            ...))
  36.        ...))
  37.     ((_ (state-name
  38.           (next-state test-case then-case)
  39.           ...
  40.           (else
  41.            else-case))
  42.         ...)
  43.      (begin
  44.        (define state-name
  45.          (state
  46.            (next-state test-case then-case)
  47.            ...
  48.            (else
  49.             else-case)))
  50.        ...))))
  51.  
  52.  
  53. ;;; Test the new programming structures from above.
  54.  
  55.  
  56. (define available-power #t)
  57.  
  58. (states
  59.   (standby
  60.     (power available-power (begin (display "IDLE\n") idle))
  61.     (else
  62.      (begin (display "STANDBY\n") standby)))
  63.  
  64.   (idle
  65.     (play #t (begin (display "PLAYING\n") playing))
  66.     (power #t (begin (display "STANDBY\n") standby))
  67.     (else
  68.      (begin (display "IDLE\n") idle)))
  69.  
  70.   (playing
  71.     (stop #t (begin (display "IDLE\n") idle))
  72.     (power #t (begin (display "STANDBY\n") standby))
  73.     (else
  74.      (begin (display "PLAYING\n") playing))))
  75.  
  76.  
  77. (define S standby)
  78.  
  79. (define S (S 'play))
  80. (define S (S 'power))
  81. (define S (S 'power))
  82. (define S ((S 'power) 'play))
  83. (define S (S 'stop))
  84. (define S (S 'play))
  85. (define S (S 'power))
  86. (define S (S 'power))

PermaLink to this entry https://pastebin.co.uk/11095
Posted by Anonymous Tue 27th Feb 2007 23:26 - Syntax is CSS - 32 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0