1
+ This file describes the path that the Piet program takes in a more human readable form.
2
+ Two functions are implemented:
3
+ - GCD with subtraction
4
+ - GCD with the modulo operator
5
+
6
+ -------------------
7
+ SUBTRACTION
8
+ -------------------
9
+
10
+ Pseudo code:
11
+
1
12
function gcd(a, b)
2
13
while a ≠ b
3
14
if a > b
@@ -6,9 +17,12 @@ function gcd(a, b)
6
17
b := b − a;
7
18
return a;
8
19
9
- in(number) A
10
- // absolute of A
11
- duplicate AA
20
+
21
+ Piet code:
22
+
23
+ COMMAND STATE OF STACK
24
+ in(number) A // Take A as an input
25
+ duplicate AA // Start to take the absolute value of A
12
26
push 1 1AA
13
27
duplicate 11AA
14
28
subtract 0AA
@@ -20,11 +34,10 @@ subtract -2 1/0 A
20
34
multiply -2/0 A
21
35
push 1 1 -2/0 A
22
36
add -1/1 A
23
- multiply A
37
+ multiply A // A should now be an absolute value
24
38
25
- in(number) BA
26
- // absolute of B
27
- duplicate BBA
39
+ in(number) BA // Take B as an input
40
+ duplicate BBA // Start to take the absolute value of B
28
41
push 1 1BBA
29
42
duplicate 11BBA
30
43
subtract 0BBA
@@ -36,9 +49,9 @@ subtract -2 1/0 BA
36
49
multiply -2/0 BA
37
50
push 1 1 -2/0 BA
38
51
add -1/1 BA
39
- multiply BA
52
+ multiply BA // B should now be an absolute value
40
53
41
- // Start of loop
54
+ // Start of the main loop while a ≠ b
42
55
duplicate BBA
43
56
push 3 3BBA
44
57
push 2 23BBA
@@ -50,9 +63,9 @@ roll ABBA
50
63
subtract 0/x BA
51
64
not 1/0 BA // 1 if a = b and 0 if a ≠ b
52
65
not 0/1 BA // 1 if a ≠ b and 0 if a = b
53
- pointer BA
66
+ pointer BA // If a ≠ b, the DP should change one clockwise, otherwise, go straight ahead.
54
67
55
- // Go left if a ≠ b
68
+ // Go left if a ≠ b (DP changed one clockwise)
56
69
duplicate BBA
57
70
push 3 3BBA
58
71
push 2 23BBA
@@ -65,9 +78,9 @@ pointer BA
65
78
push 1 12ABBA
66
79
roll BABA
67
80
greater 0/1 BA // A > B; 1 if true; 0 if false
68
- pointer BA // maybe a switch here instead?
81
+ pointer BA // If A > B, DP goes one clockwise, otherwise, DP stays the same.
69
82
70
- // If A > B
83
+ // If A > B (DP has changed 1 clockwise)
71
84
duplicate BBA
72
85
push 3 3BBA
73
86
push 1 13BBA
@@ -78,8 +91,7 @@ pointer BA
78
91
roll BA
79
92
// Go back to start of loop
80
93
81
- // If B > A
82
- // b = b - a
94
+ // If B > A (DP stayed the same)
83
95
push 2 2BA
84
96
push 1 12BA
85
97
roll AB
@@ -92,17 +104,26 @@ pointer BA
92
104
93
105
// Go down if a = b (end of while loop)
94
106
pop A
95
- out(number) -
107
+ out(number) - // Print out A when done.
96
108
97
109
---------------------------------------------------------------------------
98
110
111
+ -------------------
112
+ MODULO
113
+ -------------------
114
+
115
+ Pseudo code:
116
+
99
117
function gcd(a, b)
100
118
while b ≠ 0
101
119
t := b;
102
120
b := a mod b;
103
121
a := t;
104
122
return a;
105
123
124
+ Piet code:
125
+
126
+ COMMAND STATE OF STACK
106
127
in(number) A
107
128
in(number) BA
108
129
@@ -122,5 +143,4 @@ pointer BA
122
143
123
144
// Go right if b = 0
124
145
pop A
125
- out(number) -
126
-
146
+ out(number) - // Print out A when done.
0 commit comments