summaryrefslogtreecommitdiff
path: root/04a/in03
blob: e4771f3711c3de837db01e9134e1bd866563a690 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
I=8S
A=d3
?I!A:usage_error
; open input file
	J=S
	; argv[1] is at *(rsp+16)
	J+=d16
	J=8J
	I=d0
	syscall x2
	J=A
	?J<0:input_file_error
; open output file
	J=S
	; argv[2] is at *(rsp+24)
	J+=d24
	J=8J
	I=x241
	D=x1ed
	syscall x2
	J=A
	?J<0:output_file_error
; initialize :definitions_end
J=:definitions_end
D=:definitions
8J=D

:read_line
; use rbp to store line pointer
R=:line
:read_line_loop
	; read 1 byte into rbp
	J=d3
	I=R
	D=d1
	syscall x0
	D=A
	?D=0:eof
	
	; check if the character was a newline:
	C=1R
	D=xa
	?C=D:read_line_loop_end
	R+=d1
	!:read_line_loop
:read_line_loop_end

; check if line = "#define " up to a terminator of ' '.
C=x20
I=:#define
J=:line
call :string=
D=A
?D!0:handle_#define

; handle a normal line
; R will store a pointer to the current character
R=:line
:process_line_loop
	C=1R
	B=C
	call :isident
	?A!0:process_ident
		; if *R is not an identifier character, just output it to the file.
		J=d4
		B=C
		call :fputc
		; check if we reached the end of the line
		C=1R
		D=xa
		?C=A:read_line
		; increment R, keep looping
		R+=d1
		!:process_line_loop
	:process_ident
		; if *R is an ident char. look up this identifier in :definitions.
		; use C to keep pointer to definition
		C=:definitions
		:lookup_loop
			D=1C
			; check if we reached end of definition table
			?D=0:lookup_loop_end
			; check if this entry matches our identifier
			I=R
			J=C
			call :ident=
			?A!0:perform_substitution
			; if not, skip over this entry
			:skip_definition_loop
				D=1C
				I=xa
				C+=d1
				?I!D:skip_definition_loop
			!:lookup_loop
		:lookup_loop_end
		; this identifier doesn't match anything in the definitions table; just write it.
		; first, figure out how long it is
		J=R
		:length_loop
			C=1J
			B=C
			call :isident
			?A=0:length_loop_end
			J+=d1
			!:length_loop
		:length_loop_end
		; now write it.
		I=R
		R=J
		J-=I
		D=J
		J=d4
		syscall x1
		; keep looping
		!:process_line_loop

:perform_substitution
	; right now, I is a pointer to the end of the identifier in :line,
	; and J is a pointer to the end of the identifier in :definitions.
	
	; advance :line pointer for next loop iteration
	R=I
	
	J+=d1
	; J now points to the definition. let's write it
	I=J
	:definition_end_loop
		C=1I
		D=xa
		?C=D:definition_end_loop_end
		I+=d1
		!:definition_end_loop
	:definition_end_loop_end
	D=I
	D-=J
	I=J
	J=d4
	syscall x1
	; process the rest of this line
	!:process_line_loop
	
:eof
	J=d0
	syscall x3c

; can the character in rbx appear in an identifier?
:isident
	A='0
	?B<A:return_0
	; note: 58 = '9' + 1
	A=d58
	?B<A:return_1
	A='A
	?B<A:return_0
	; note: 91 = 'z' + 1
	A=d91
	?B<A:return_1
	A='z
	?B>A:return_0
	; 96 = 'a' - 1
	A=d96
	?B>A:return_1
	A='_
	?B=A:return_1
	!:return_0
	

:handle_#define
	J=:definitions_end
	J=8J
	; start copy from after "#define"
	I=:line
	I+=d8
	
	:#define_copy_loop
		D=1I
		1J=D
		I+=d1
		J+=d1
		A=xa
		?D=A:#define_copy_loop_end
		!:#define_copy_loop
	:#define_copy_loop_end
	
	; update end of definitions
	D=:definitions_end
	8D=J
	; emit newline so we don't screw up line numbers
	J=d4
	I=:newline
	D=d1
	syscall x1
	
	!:read_line

:newline
	xa
	

:usage_error
	B=:usage_error_message
	call :error
	
:usage_error_message
	str Please provide an input and an output file.
	xa
	x0

:input_file_error
	B=:input_file_error_message
	!:error

:input_file_error_message
	str Couldn't open input file.
	xa
	x0

:output_file_error
	B=:output_file_error_message
	!:error

:output_file_error_message
	str Couldn't open output file.
	xa
	x0

:error
	J=B
	call :strlen
	D=A
	I=J
	J=d2
	syscall x1
	J=d1
	syscall x3c
	
:strlen
	I=B
	D=B
	:strlen_loop
	C=1I
	?C=0:strlen_ret
	I+=d1
	!:strlen_loop
	:strlen_ret
	I-=D
	A=I
	return

:#define
	str #define
	x20
	x0

; check if strings in rdi and rsi are equal, up to terminator in rcx
:string=
	D=1I
	A=1J
	?D!A:return_0
	?D=C:return_1
	I+=d1
	J+=d1
	!:string=

; check if strings in rdi and rsi are equal, up to the first non-identifier character
:ident=
	D=1I
	B=D
	call :isident
	; I ended
	?A=0:ident=_I_end
	
	D=1J
	B=D
	call :isident
	; J ended, but I didn't
	?A=0:return_0
	
	; we haven't reached the end of either
	D=1I
	A=1J
	?D!A:return_0
	I+=d1
	J+=d1
	!:ident=
:ident=_I_end
	D=1J
	B=D
	call :isident
	; check if J also ended
	?A=0:return_1
	; J didn't end
	!:return_0
	
:return_0
	A=d0
	return
:return_1
	A=d1
	return

; write the character in rbx to the file in rdi.
:fputc
	C=B
	I=S
	I-=d1
	1I=C
	D=d1
	syscall x1
	return
	

align
:definitions_end
	reserve d8
:line
	reserve d1000
:definitions
	reserve d200000
	
; we shouldn't end the file with a reserve; we don't handle that properly	
x00