-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.comment
56 lines (50 loc) · 3 KB
/
factorial.comment
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
Input a number, calculate the factorial
*** Remember, this file doesn't contain a valid Whitespace program. We use [s] = ' ' (Space), [t] = '\t' (Tab), and [n] = '\n' (Newline) ***
------
Preparing job
------
Push -1, for getting the opposite number at last: [s][s][t][t][n]
Push -1, for determine when to quit the loop: [s][s][t][t][n]
Push heap address 0: [s][s][s][s][n]
Accept a number input and store it to heap at 0x0: [t][n][t][t]
Push heap address 0: [s][s][s][s][n]
Get the number from heap at 0x0 (which we have just inputted): [t][t][t]
Duplicate stack top, for stack top need to be popped to check if it equals to 0: [s][n][s]
If the inputted number (which is now at stack top) is 0, jump to where we prepared for this case (label 0xfb): [n][t][s][t][t][t][t][t][s][t][t][n]
Duplicate stack top, for stack top need to be popped to check if it is negative: [s][n][s]
If the inputted number is negative, jump to where we prepared for this case (label 0xfa): [n][t][t][t][t][t][t][t][s][t][s][n]
------
Now we need to create the series: n, n-1, ..., 3, 2, 1, 0
------
Label: loop begins (0xff): [n][s][s][t][t][t][t][t][t][t][t][n]
Duplicate stack top, to create the minuend (the number that is going to be substracted): [s][n][s]
Push 1, to create the subtrahend (the number that the minuend need to subtract): [s][s][s][t][n]
Execute substraction: [t][s][s][t]
Duplicate stack top, for stack top need to be popped to check if it equals to 0: [s][n][s]
If the stack top is 0, jump to the end (0xfe) (if so, the series has been created): [n][t][s][t][t][t][t][t][t][t][s][n]
If not, back to the loop start unconditionally (0xff): [n][s][n][t][t][t][t][t][t][t][t][n]
Label: end (0xfe): [n][s][s][t][t][t][t][t][t][t][s][n]
Pop 0: [s][n][n]
------
Now we are going to multiply the numbers one by one, from back to beginning
------
Label: loop begins (0xfd): [n][s][s][t][t][t][t][t][t][s][t][n]
Multiply stack top with the one right under the top. When the loop comes to where it should end, it will be the correct result multiply with -1. Everytime, the two factors will be popped, and the product will be pushed: [t][s][s][n]
Duplicate stack top, for stack top need to be popped to check if it is negative: [s][n][s]
If so, end the loop (goto 0xfc): [n][t][t][t][t][t][t][t][t][s][s][n]
If else, continue to loop (goto 0xfd): [n][s][n][t][t][t][t][t][t][s][t][n]
Label: end of loop (0xfc): [n][s][s][t][t][t][t][t][t][s][s][n]
When the loop ends, we'll get the negative result, so we need to let it multiply with -1 (which we pushed in the preparing job): [t][s][s][n]
Output the result: [t][n][s][t]
End the program: [n][n][n]
------
Special cases
------
Label: inputted number equals to 0 (0xfb): [n][s][s][t][t][t][t][t][s][t][t][n]
Push the factorial of 0, which is 1: [s][s][s][t][n]
Output the result: [t][n][s][t]
End the program: [n][n][n]
Label: inputted number is less than 0 (0xfa): [n][s][s][t][t][t][t][t][s][t][s][n]
Negative integers doesn't have a factorial, push 0: [s][s][s][s][n]
Output the result: [t][n][s][t]
End the program: [n][n][n]