-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.fs
144 lines (120 loc) · 2.7 KB
/
main.fs
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
require input-util.fs
: read-parameter-position ( index -- p )
+ read intcode swap read -rot drop drop ;
: read-parameter-immediate ( index -- p )
+ read -rot drop drop ;
: read-parameters ( addr index opcode -- p1 p2 )
100 /
3dup 3dup
10 mod
case
0 of 2dup 1 read-parameter-position endof
1 of 2dup 1 read-parameter-immediate endof
endcase
>r
10 / 10 mod
case
0 of 2dup 2 read-parameter-position endof
1 of 2dup 2 read-parameter-immediate endof
endcase
r> ;
: write-result ( addr index value -- )
-rot
3 + read intcode
-rot write ;
: add ( count addr index opcode -- count addr index )
read-parameters
+ >r \ result
3dup drop \ TODO: Read A
r> write-result
drop 4 + ;
: mul ( count addr index opcode -- count addr index )
read-parameters
* >r \ result
3dup drop \ TODO: Read A
r> write-result
drop 4 + ;
: non-zero ( count addr index opcode -- count addr index )
read-parameters
0 <> if
-rot drop drop
else
drop drop 3 +
endif ;
: is-zero ( count addr index opcode -- count addr index )
read-parameters
0 = if
-rot drop drop
else
drop drop 3 +
endif ;
: less ( count addr index opcode -- count addr index+4 )
3dup read-parameters swap
< if
1 >r
else
0 >r
endif
drop
3 + read
intcode swap
r> swap write
drop 4 + ;
: equals ( count addr index opcode -- count addr index+4 )
3dup read-parameters swap
= if
1 >r
else
0 >r
endif
drop
3 + read
intcode swap
r> swap write
drop 4 + ;
: input ( count addr index opcode -- count addr index+2 )
drop
2dup
1 + read intcode swap
s\" Input: " type
std-input
s\" \n" type
swap write
2 + ;
: output ( count addr index opcode -- count addr index+2 )
100 /
3dup
10 mod
case
0 of 2dup 1 read-parameter-position . endof
1 of 2dup 1 read-parameter-immediate . endof
endcase
s\" \n" type
drop 2 + ;
: read-opcode ( count addr index -- count addr index opcode )
2dup read dup 100 mod
case
1 of add endof
2 of mul endof
3 of input endof
4 of output endof
5 of non-zero endof
6 of is-zero endof
7 of less endof
8 of equals endof
endcase ;
: run ( count addr -- )
s\" \n\n" type
0
begin
2dup read 99 <> while
read-opcode
repeat drop drop drop ;
: run-print ( count addr -- )
s\" \n\n" type
0
begin
print
2dup read 99 <> while
read-opcode
repeat drop drop drop ;