Skip to content

Commit 4abf2c8

Browse files
WIP: try implement preprocessor directives
1 parent f8905eb commit 4abf2c8

File tree

4 files changed

+100
-10
lines changed

4 files changed

+100
-10
lines changed

compiler/debug.corth

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ macro NO-TOKEN 0x22000 endmacro // log-item: reason
250250
macro UNXPCT-TYPE 0x22001 endmacro // ptr: address
251251
macro UNXPCT-KEYWORD 0x22002 endmacro // ptr: address
252252
macro REC-NAMESPACE 0x22003 endmacro //
253+
macro NOT-A-PREPROC 0x22004 endmacro // ptr: address
253254
// TODO: Add ELSE-NO-IF
254255
// TODO: Add DO-NO-WHILE
255256
// TODO: Add END-NO-START
@@ -367,6 +368,10 @@ in let log-item log-stream in
367368
else type LOG-TYPE:REC-NAMESPACE = if
368369
"error: tried to start a namespace scope inside another one\n" log-stream fputs
369370

371+
else type LOG-TYPE:NOT-A-PREPROC = if
372+
"error: not a preprocessor directive\n" log-stream fputs
373+
arg1 log-stream fput-here
374+
370375
// Compilation errors
371376
else type LOG-TYPE:STACK-NOT-MATCH = if
372377
"error: stack does not match the expected stack\n" log-stream fputs
@@ -470,6 +475,6 @@ in let log-item log-stream in
470475
else
471476
"unknown\n" log-stream fputs
472477
end end end end end end end end end end end end end end end end end end end end end end
473-
end end end end end end end end end end end end end end end end end end end end end
478+
end end end end end end end end end end end end end end end end end end end end end end
474479
end
475480
end

compiler/enums.corth

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ macro OUTPUT-STREAM-SIZE 0x100000 endmacro
2020
// Type IDs
2121
namespace TOKEN-TYPE
2222
macro UNKNOWN 0 endmacro
23-
macro NAME 1 endmacro
24-
macro INTEGER 2 endmacro
25-
macro KEYWORD 3 endmacro
26-
macro INTRINSIC 4 endmacro
27-
macro STRING 5 endmacro
28-
macro INT-TYPE 6 endmacro
23+
macro PREPROC 1 endmacro
24+
macro NAME 2 endmacro
25+
macro INTEGER 3 endmacro
26+
macro KEYWORD 4 endmacro
27+
macro INTRINSIC 5 endmacro
28+
macro STRING 6 endmacro
29+
macro INT-TYPE 7 endmacro
2930
endnamespace
3031

3132
// Type strings
3233
namespace TOKEN-TYPE-NAME
3334
macro UNKNOWN "<unknown>" endmacro
35+
macro PREPROC "preproc" endmacro
3436
macro NAME "name" endmacro
3537
macro INTEGER "integer" endmacro
3638
macro KEYWORD "keyword" endmacro
@@ -39,6 +41,20 @@ macro STRING "string" endmacro
3941
macro INT-TYPE "int-type" endmacro
4042
endnamespace
4143

44+
// Preprocessor directive IDs
45+
namespace PREPROC
46+
macro UNKNOWN 0 endmacro
47+
macro END 1 endmacro
48+
macro IFDEF 2 endmacro
49+
endnamespace
50+
51+
// Preprocessor directive strings
52+
namespace PREPROC-NAME
53+
macro UNKNOWN "<unknown>" endmacro
54+
macro END "end" endmacro
55+
macro IFDEF "ifdef" endmacro
56+
endnamespace
57+
4258
// Keyword IDs
4359
namespace KEYWORD
4460
macro UNKNOWN 0 endmacro

compiler/lexer.corth

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ proc create-name-token
206206
int ptr int ptr
207207

208208
// Checks if the name is a keyword or an intrinsic, and creates a new token.
209-
// After the keyword and intrinsic checks, macros are checked for any match. If there is any macro with the same name, the macro tokens are loaded to a buffer.
210209
in let file-name file start-position buffer buffer-end in
211210
// Get full token.
212211
file buffer buffer-end parse-next-token dup isn-null if
@@ -290,6 +289,57 @@ in let file-name file start-position buffer buffer-end in
290289
end end
291290

292291

292+
proc create-preprocessor-token
293+
// ptr: file-name file-desc: file-desc
294+
ptr file-desc
295+
// int: start-position
296+
int
297+
// ptr: buffer ptr: buffer-end
298+
ptr ptr ->
299+
// int: token-type ptr: address [int|ptr]: arg ptr: log-item
300+
int ptr int ptr
301+
302+
// Checks if the name is a preprocessor and creates a new preprocessor token.
303+
// After the keyword and intrinsic checks, macros are checked for any match. If there is any macro with the same name, the macro tokens are loaded to a buffer.
304+
in let file-name file start-position buffer buffer-end in
305+
// Get full token.
306+
file buffer buffer-end parse-next-token dup isn-null if
307+
let log-item in TOKEN-TYPE:UNKNOWN NULLPTR 0 log-item end return
308+
end drop
309+
310+
buffer-end @64 buffer - let buffer-size in
311+
// Check if the token is a preprocessor directive.
312+
buffer buffer-size PREPROC-NAME:END memcmp8-wl is-zero if PREPROC:END
313+
else buffer buffer-size PREPROC-NAME:IFDEF memcmp8-wl is-zero if PREPROC:IFDEF
314+
else
315+
// FUCKING UNREACHABLE CODE EXCEPTIONS!!!
316+
true if
317+
// Generate the address of the token.
318+
file-name NULLPTR start-position file ftell generate-address let address in
319+
address is-null if
320+
TOKEN-TYPE:UNKNOWN NULLPTR 0 LOG-TYPE:MALLOC-FAILED generate-log0 return
321+
end
322+
323+
// Return error as the token string was not a preprocessor directive.
324+
TOKEN-TYPE:UNKNOWN NULLPTR 0 address LOG-TYPE:NOT-A-PREPROC generate-log1 return
325+
end
326+
end PREPROC:UNKNOWN
327+
end end
328+
329+
let token-arg in
330+
// Return token type, address and argument.
331+
file-name NULLPTR start-position file ftell generate-address let address in
332+
address is-null if
333+
TOKEN-TYPE:UNKNOWN NULLPTR 0 LOG-TYPE:MALLOC-FAILED generate-log0 return
334+
end
335+
336+
TOKEN-TYPE:PREPROC address token-arg NULLPTR
337+
end
338+
end
339+
end
340+
end end
341+
342+
293343
proc get-next-token
294344
// ptr: file-name file-desc: file-desc
295345
ptr file-desc
@@ -336,7 +386,26 @@ in
336386
buffer 1 file fgets drop
337387
buffer-end @inc64
338388

339-
buffer @8 is-ddigit if
389+
buffer @8 '#' = if
390+
// -------- Preprocessor directive type --------
391+
392+
// Get the full token.
393+
file buffer buffer-end parse-next-token dup isn-null if
394+
let log-item in
395+
TOKEN-TYPE:UNKNOWN NULLPTR 0 log-item false
396+
end return
397+
end drop
398+
399+
// Generate the address object.
400+
file-name NULLPTR start-position file ftell generate-address let address in
401+
address is-null if TOKEN-TYPE:UNKNOWN NULLPTR 0 LOG-TYPE:MALLOC-FAILED generate-log0 false return end
402+
403+
// Create the preprocessor token.
404+
// NOTE: Remove the first character (#) by shifting the buffer pointer once.
405+
file-name file start-position buffer inc buffer-end create-preprocessor-token dup isn-null if false return end drop
406+
end
407+
408+
else buffer @8 is-ddigit if
340409
// -------- Integer type --------
341410

342411
// Get full token.
@@ -498,7 +567,7 @@ in
498567
else
499568
// -------- Name and keyword types --------
500569
file-name file start-position buffer buffer-end create-name-token dup isn-null if false return end drop
501-
end end end end end
570+
end end end end end end
502571
end
503572
end
504573
end

corth

6.73 KB
Binary file not shown.

0 commit comments

Comments
 (0)