-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdbl.scm
73 lines (56 loc) · 1.62 KB
/
pdbl.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;; Interpreter for P", the language for simulation of a Turing machine with
;;; left-infinite tape developed by Corrado Böhm in 1964.
;;;
;; Copyright 2017 Matthew Lavin
;;
;; This program utilizes the lalr-scm parser generation library, which is free
;; software licensed under the GNU LGPL. Development and testing is done using
;; GNU Guile, so interoperability cannot be guaranteed.
;;
; (load "lalr-scm/lalr.scm")
(use-modules (srfi srfi-42)) ; Access to list-ec function
(define *tape* #()) ; Tape operated on by the machine
(define *position* 0) ; Current position of tape head
(define *base* 0) ; Largest digit in notation
(define (setup tape pos base)
(set! *tape* tape)
(set! *position* pos)
(set! *base* base))
(define (set-pos! pos)
(set! *position* pos))
(define (write out)
(vector-set! *tape* *position* out))
(define (read)
(vector-ref *tape* *position*))
(define (neq a b)
(not (= a b)))
(define (Alpha)
(eq? (read) '_))
; Write c_(i+1) to square, then move head left iff possible. 0 is equivalent to
; (+ *base* 1) is equivalent to _, hence over- and under-flow is intentional.
(define (Lambda)
(if (Alpha)
(write 1)
(if (eq? (read) *base*)
(write 0)
(write (+ (read) 1))))
(if (neq *position* 0)
(set! *position* (- *position* 1))))
(define (R)
(set! *position* (+ *position* 1)))
(define (ntimes func)
(list-ec (: i *base*)
(func)))
(define (While func)
(while (neq (read) 0)
(func)))
; Some example input to work with
(define tape1
(list->vector '(_ _ 0 1 1 1 1 0 _ _ _)))
(setup tape1 2 2)
(define (r)
(Lambda)
(R))
(define (L)
(ntimes r)
(Lambda))