Skip to content

Commit ccf0139

Browse files
committed
Add some comments to piet pseudocode file
1 parent 42e6e2e commit ccf0139

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

contents/euclidean_algorithm/code/piet/euclidian_algorithm.piet

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
112
function gcd(a, b)
213
while a ≠ b
314
if a > b
@@ -6,9 +17,12 @@ function gcd(a, b)
617
b := b − a;
718
return a;
819

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
1226
push 1 1AA
1327
duplicate 11AA
1428
subtract 0AA
@@ -20,11 +34,10 @@ subtract -2 1/0 A
2034
multiply -2/0 A
2135
push 1 1 -2/0 A
2236
add -1/1 A
23-
multiply A
37+
multiply A // A should now be an absolute value
2438

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
2841
push 1 1BBA
2942
duplicate 11BBA
3043
subtract 0BBA
@@ -36,9 +49,9 @@ subtract -2 1/0 BA
3649
multiply -2/0 BA
3750
push 1 1 -2/0 BA
3851
add -1/1 BA
39-
multiply BA
52+
multiply BA // B should now be an absolute value
4053

41-
// Start of loop
54+
// Start of the main loop while a ≠ b
4255
duplicate BBA
4356
push 3 3BBA
4457
push 2 23BBA
@@ -50,9 +63,9 @@ roll ABBA
5063
subtract 0/x BA
5164
not 1/0 BA // 1 if a = b and 0 if a ≠ b
5265
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.
5467

55-
// Go left if a ≠ b
68+
// Go left if a ≠ b (DP changed one clockwise)
5669
duplicate BBA
5770
push 3 3BBA
5871
push 2 23BBA
@@ -65,9 +78,9 @@ pointer BA
6578
push 1 12ABBA
6679
roll BABA
6780
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.
6982

70-
// If A > B
83+
// If A > B (DP has changed 1 clockwise)
7184
duplicate BBA
7285
push 3 3BBA
7386
push 1 13BBA
@@ -78,8 +91,7 @@ pointer BA
7891
roll BA
7992
// Go back to start of loop
8093

81-
// If B > A
82-
// b = b - a
94+
// If B > A (DP stayed the same)
8395
push 2 2BA
8496
push 1 12BA
8597
roll AB
@@ -92,17 +104,26 @@ pointer BA
92104

93105
// Go down if a = b (end of while loop)
94106
pop A
95-
out(number) -
107+
out(number) - // Print out A when done.
96108

97109
---------------------------------------------------------------------------
98110

111+
-------------------
112+
MODULO
113+
-------------------
114+
115+
Pseudo code:
116+
99117
function gcd(a, b)
100118
while b ≠ 0
101119
t := b;
102120
b := a mod b;
103121
a := t;
104122
return a;
105123

124+
Piet code:
125+
126+
COMMAND STATE OF STACK
106127
in(number) A
107128
in(number) BA
108129

@@ -122,5 +143,4 @@ pointer BA
122143

123144
// Go right if b = 0
124145
pop A
125-
out(number) -
126-
146+
out(number) - // Print out A when done.

0 commit comments

Comments
 (0)