"semantics.rkt"
#lang racket
(require racket/unsafe/ops)
(provide (all-defined-out))
(define-struct (exn:fail:out-of-bounds exn:fail)
(srcloc)
#:property prop:exn:srclocs
(lambda (a-struct)
(list (exn:fail:out-of-bounds-srcloc a-struct))))
(define-syntax-rule (new-state)
(values (make-vector 30000 0)
0))
(define-syntax-rule (increment-ptr data ptr loc)
(begin
(set! ptr (unsafe-fx+ ptr 1))
(when (unsafe-fx>= ptr (unsafe-vector-length data))
(raise (make-exn:fail:out-of-bounds
"out of bounds"
(current-continuation-marks)
loc)))))
(define-syntax-rule (decrement-ptr data ptr loc)
(begin
(set! ptr (unsafe-fx- ptr 1))
(when (unsafe-fx< ptr 0)
(raise (make-exn:fail:out-of-bounds
"out of bounds"
(current-continuation-marks)
loc)))))
(define-syntax-rule (increment-byte data ptr)
(unsafe-vector-set! data ptr
(unsafe-fxmodulo
(unsafe-fx+
(unsafe-vector-ref data ptr) 1)
256)))
(define-syntax-rule (decrement-byte data ptr)
(unsafe-vector-set! data ptr
(unsafe-fxmodulo
(unsafe-fx-
(unsafe-vector-ref data ptr) 1)
256)))
(define-syntax-rule (write-byte-to-stdout data ptr)
(write-byte (unsafe-vector-ref data ptr)
(current-output-port)))
(define-syntax-rule (read-byte-from-stdin data ptr)
(unsafe-vector-set! data ptr
(let ([a-value (read-byte
(current-input-port))])
(if (eof-object? a-value)
0
a-value))))
(define-syntax-rule (loop data ptr body ...)
(let loop ()
(unless (unsafe-fx= (unsafe-vector-ref data ptr)
0)
body ...
(loop))))