-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingobjectoriented.rkt
155 lines (120 loc) · 3.95 KB
/
turingobjectoriented.rkt
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#lang racket
(require "abstractie-leds.rkt")
(require racket/format)
(provide (all-defined-out))
(define list-pin-leds (map (lambda (x) (+ 100 x)) '(0 1 2 3 4 11 12 13 14 15)))
(define (turing name state-init state-term blank rules)
(let ((leds (mk-leds list-pin-leds)))
(define state state-init)
(define head 50000)
(define bottom 50000)
(define top 50000)
(define steps 0)
(define tape (make-vector 100000 blank))
(define (init)
(set! state state-init)
(set! head 50000)
(set! bottom 50000)
(set! top 50000)
(set! steps 0)
(set! tape (make-vector 100000 blank)))
(define (show)
(display " steps: ") (display (~a steps #:align 'right #:width 2 #:pad-string " "))
(display " state: ") (display (~a state #:align 'left #:width 5 #:pad-string " "))
(display " tape: ")
(do ((pos bottom (+ pos 1))) ((> pos top))
(display (if (= pos head) "[" " "))
(display (vector-ref tape pos))
(display (if (= pos head) "]" " ")))
(newline))
(define (write!)
(let ((head-symbol (vector-ref tape head)))
(cond ((eq? head-symbol blank)
(vector-set! tape head 1))
((eq? head-symbol 1)
(vector-set! tape head blank)))))
(define (set-head! symbol)
(vector-set! tape head symbol))
(define (move action)
(case action
((left)
(set! head (- head 1))
(cond
((< head bottom)
(set! bottom head)
(set-head! blank))))
((right)
(set! head (+ head 1))
(cond
((> head top)
(set! top head)
(set-head! blank))))))
(define (step! output)
(cond (output
(show)))
(let try
((rules rules))
(unless
(null? rules)
(let* ((rule (car rules))
(state-before (vector-ref rule 0))
(symbol-before (vector-ref rule 1)))
(if (and (eq? state-before state)
(eq? symbol-before (vector-ref tape head)))
(let ((symbol-after (vector-ref rule 2))
(action (vector-ref rule 3))
(state-after (vector-ref rule 4)))
(set-head! symbol-after)
(move action)
(set! state state-after))
(try (cdr rules))))))
(set! steps (+ steps 1)))
(define (run)
(let step-loop ()
(if (eq? state-term state) ;Wat als state op blank state staat?
(show)
(begin
(step! #t)
(step-loop))))
(newline) (newline))
(define (reset symbols)
(init)
(do ()((null? symbols))
(vector-set! tape top (car symbols))
(set! symbols (cdr symbols))
(set! top (+ top 1)))
(vector-set! tape top blank))
(define (led-value symbol)
(if (eq? symbol blank)
'off
'on))
(define head-offset 4)
(define (list-leds)
(define lijst-symbolen '())
(do ([pos (- head head-offset) (+ pos 1)])
((= (length lijst-symbolen) 10)
(reverse lijst-symbolen))
(set! lijst-symbolen (cons (led-value
(vector-ref tape pos))
lijst-symbolen))))
(define (dispatch msg . argument)
(when (not (null? argument))
(set! argument (car argument)))
(case msg
('reset (reset argument)
(show))
('show (show))
('write! (write!)
(show))
('left (move 'left)
(show))
('right (move 'right)
(show))
('step
(step! #t)
(show))
('run (run)))
((leds 'change-leds!) (list-leds))
(when (eq? msg 'finished?)
(eq? state-term state)))
dispatch))