#lang planet dyoo/whalesong
(require "semantics.rkt"
(for-syntax racket/base))
(provide greater-than
less-than
plus
minus
period
comma
brackets
(rename-out [my-module-begin #%module-begin]))
(define-syntax (my-module-begin stx)
(syntax-case stx ()
[(_ body ...)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(#%plain-module-begin
(define-values (current-data current-ptr) (new-state))
(define (run)
(begin body ... (void)))
(run))))]))
(define-syntax (greater-than stx)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax-case stx ()
[(_)
(quasisyntax/loc stx
(increment-ptr current-data current-ptr
(srcloc '#,(syntax-source stx)
'#,(syntax-line stx)
'#,(syntax-column stx)
'#,(syntax-position stx)
'#,(syntax-span stx))))])))
(define-syntax (less-than stx)
(syntax-case stx ()
[(_)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(quasisyntax/loc stx
(decrement-ptr current-data current-ptr
(srcloc '#,(syntax-source stx)
'#,(syntax-line stx)
'#,(syntax-column stx)
'#,(syntax-position stx)
'#,(syntax-span stx)))))]))
(define-syntax (plus stx)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(increment-byte current-data current-ptr))))
(define-syntax (minus stx)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(decrement-byte current-data current-ptr))))
(define-syntax (period stx)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(write-byte-to-stdout current-data current-ptr))))
(define-syntax (comma stx)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(read-byte-from-stdin current-data current-ptr))))
(define-syntax (brackets stx)
(syntax-case stx ()
[(_ body ...)
(with-syntax ([current-data (datum->syntax stx 'current-data)]
[current-ptr (datum->syntax stx 'current-ptr)])
(syntax/loc stx
(loop current-data current-ptr body ...)))]))