Skip to content

Commit b4dd8f2

Browse files
type strictness for token_t
1 parent cad1963 commit b4dd8f2

File tree

9 files changed

+139
-142
lines changed

9 files changed

+139
-142
lines changed

include/basic/console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void print_statement(struct basic_ctx* ctx);
165165
* @param ctx The BASIC context.
166166
* @param tok The token for the colour.
167167
*/
168-
void colour_statement(struct basic_ctx* ctx, int tok);
168+
void colour_statement(struct basic_ctx* ctx, enum token_t tok);
169169

170170
/**
171171
* @brief Set the text background colour.

include/basic/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ void statement(struct basic_ctx* ctx);
397397
* @param ctx The BASIC context.
398398
* @return True if the token matches, false otherwise.
399399
*/
400-
bool accept(int token, struct basic_ctx* ctx);
400+
bool accept(enum token_t token, struct basic_ctx* ctx);
401401

402402
/**
403403
* @brief Parse function definitions in the BASIC program.

include/basic/tokenizer.h

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -48,117 +48,117 @@
4848
* enum, which is built and used within basic.c for tokenization.
4949
*/
5050
#define TOKEN(T) \
51-
T(NO_TOKEN) \
52-
T(ERROR) \
53-
T(ENDOFINPUT) \
54-
T(NUMBER) \
55-
T(HEXNUMBER) \
56-
T(STRING) \
57-
T(VARIABLE) \
58-
T(LET) \
59-
T(PRINT) \
60-
T(IF) \
61-
T(THEN) \
62-
T(ELSE) \
63-
T(CHAIN) \
64-
T(FOR) \
65-
T(STEP) \
66-
T(TO) \
67-
T(NEXT) \
68-
T(CURSOR) \
69-
T(GOTO) \
70-
T(GOSUB) \
71-
T(RETURN) \
72-
T(CALL) \
73-
T(INPUT) \
74-
T(COLOUR) \
75-
T(COLOR) \
76-
T(BACKGROUND) \
77-
T(EVAL) \
78-
T(CLOSE) \
79-
T(DEF) \
80-
T(PROC) \
81-
T(ENDPROC) \
82-
T(FN) \
83-
T(END) \
84-
T(REM) \
85-
T(COMMA) \
86-
T(SEMICOLON) \
87-
T(PLUS) \
88-
T(MINUS) \
89-
T(AND) \
90-
T(OR) \
91-
T(NOT) \
92-
T(EOR) \
93-
T(ASTERISK) \
94-
T(SLASH) \
95-
T(MOD) \
96-
T(OPENBRACKET) \
97-
T(CLOSEBRACKET) \
98-
T(LESSTHAN) \
99-
T(GREATERTHAN) \
100-
T(EQUALS) \
101-
T(NEWLINE) \
102-
T(AMPERSAND) \
103-
T(TILDE) \
104-
T(GLOBAL) \
105-
T(SOCKREAD) \
106-
T(SOCKWRITE) \
107-
T(CONNECT) \
108-
T(SOCKCLOSE) \
109-
T(CLS) \
110-
T(GCOL) \
111-
T(LINE) \
112-
T(TRIANGLE) \
113-
T(RECTANGLE) \
114-
T(CIRCLE) \
115-
T(POINT) \
116-
T(DATA) \
117-
T(RESTORE) \
118-
T(WRITE) \
119-
T(MKDIR) \
120-
T(RMDIR) \
121-
T(DELETE) \
122-
T(REPEAT) \
123-
T(UNTIL) \
124-
T(DIM) \
125-
T(REDIM) \
126-
T(PUSH) \
127-
T(POKE) \
128-
T(POKEW) \
129-
T(POKED) \
130-
T(POKEQ) \
131-
T(POP) \
132-
T(LOCAL) \
133-
T(CHDIR) \
134-
T(LIBRARY) \
135-
T(YIELD) \
136-
T(SETVARI) \
137-
T(SETVARR) \
138-
T(SETVARS) \
139-
T(SPRITELOAD) \
140-
T(SPRITEFREE) \
141-
T(PLOT) \
142-
T(AUTOFLIP) \
143-
T(FLIP) \
144-
T(KEYMAP) \
145-
T(MOUNT) \
146-
T(SETTIMEZONE) \
147-
T(ENDIF) \
148-
T(PLOTQUAD) \
149-
T(ON) \
150-
T(OFF) \
151-
T(WHILE) \
152-
T(ENDWHILE) \
153-
T(SLEEP) \
154-
T(CONTINUE) \
155-
T(UDPBIND) \
156-
T(UDPUNBIND) \
157-
T(UDPWRITE) \
158-
T(OUTPORT) \
159-
T(OUTPORTW) \
160-
T(OUTPORTD) \
161-
T(KGET)
51+
T(NO_TOKEN) /* 0 */ \
52+
T(ERROR) /* 1 */ \
53+
T(ENDOFINPUT) /* 2 */ \
54+
T(NUMBER) /* 3 */ \
55+
T(HEXNUMBER) /* 4 */ \
56+
T(STRING) /* 5 */ \
57+
T(VARIABLE) /* 6 */ \
58+
T(LET) /* 7 */ \
59+
T(PRINT) /* 8 */ \
60+
T(IF) /* 9 */ \
61+
T(THEN) /* 10 */ \
62+
T(ELSE) /* 11 */ \
63+
T(CHAIN) /* 12 */ \
64+
T(FOR) /* 13 */ \
65+
T(STEP) /* 14 */ \
66+
T(TO) /* 15 */ \
67+
T(NEXT) /* 16 */ \
68+
T(CURSOR) /* 17 */ \
69+
T(GOTO) /* 18 */ \
70+
T(GOSUB) /* 19 */ \
71+
T(RETURN) /* 20 */ \
72+
T(CALL) /* 21 */ \
73+
T(INPUT) /* 22 */ \
74+
T(COLOUR) /* 23 */ \
75+
T(COLOR) /* 24 */ \
76+
T(BACKGROUND) /* 25 */ \
77+
T(EVAL) /* 26 */ \
78+
T(CLOSE) /* 27 */ \
79+
T(DEF) /* 28 */ \
80+
T(PROC) /* 29 */ \
81+
T(ENDPROC) /* 30 */ \
82+
T(FN) /* 31 */ \
83+
T(END) /* 32 */ \
84+
T(REM) /* 33 */ \
85+
T(COMMA) /* 34 */ \
86+
T(SEMICOLON) /* 35 */ \
87+
T(PLUS) /* 36 */ \
88+
T(MINUS) /* 37 */ \
89+
T(AND) /* 38 */ \
90+
T(OR) /* 39 */ \
91+
T(NOT) /* 40 */ \
92+
T(EOR) /* 41 */ \
93+
T(ASTERISK) /* 42 */ \
94+
T(SLASH) /* 43 */ \
95+
T(MOD) /* 44 */ \
96+
T(OPENBRACKET) /* 45 */ \
97+
T(CLOSEBRACKET) /* 46 */ \
98+
T(LESSTHAN) /* 47 */ \
99+
T(GREATERTHAN) /* 48 */ \
100+
T(EQUALS) /* 49 */ \
101+
T(NEWLINE) /* 50 */ \
102+
T(AMPERSAND) /* 51 */ \
103+
T(TILDE) /* 52 */ \
104+
T(GLOBAL) /* 53 */ \
105+
T(SOCKREAD) /* 54 */ \
106+
T(SOCKWRITE) /* 55 */ \
107+
T(CONNECT) /* 56 */ \
108+
T(SOCKCLOSE) /* 57 */ \
109+
T(CLS) /* 58 */ \
110+
T(GCOL) /* 59 */ \
111+
T(LINE) /* 60 */ \
112+
T(TRIANGLE) /* 61 */ \
113+
T(RECTANGLE) /* 62 */ \
114+
T(CIRCLE) /* 63 */ \
115+
T(POINT) /* 64 */ \
116+
T(DATA) /* 65 */ \
117+
T(RESTORE) /* 66 */ \
118+
T(WRITE) /* 67 */ \
119+
T(MKDIR) /* 68 */ \
120+
T(RMDIR) /* 69 */ \
121+
T(DELETE) /* 70 */ \
122+
T(REPEAT) /* 71 */ \
123+
T(UNTIL) /* 72 */ \
124+
T(DIM) /* 73 */ \
125+
T(REDIM) /* 74 */ \
126+
T(PUSH) /* 75 */ \
127+
T(POKE) /* 76 */ \
128+
T(POKEW) /* 77 */ \
129+
T(POKED) /* 78 */ \
130+
T(POKEQ) /* 79 */ \
131+
T(POP) /* 80 */ \
132+
T(LOCAL) /* 81 */ \
133+
T(CHDIR) /* 82 */ \
134+
T(LIBRARY) /* 83 */ \
135+
T(YIELD) /* 84 */ \
136+
T(SETVARI) /* 85 */ \
137+
T(SETVARR) /* 86 */ \
138+
T(SETVARS) /* 87 */ \
139+
T(SPRITELOAD) /* 88 */ \
140+
T(SPRITEFREE) /* 89 */ \
141+
T(PLOT) /* 90 */ \
142+
T(AUTOFLIP) /* 91 */ \
143+
T(FLIP) /* 92 */ \
144+
T(KEYMAP) /* 93 */ \
145+
T(MOUNT) /* 94 */ \
146+
T(SETTIMEZONE) /* 95 */ \
147+
T(ENDIF) /* 96 */ \
148+
T(PLOTQUAD) /* 97 */ \
149+
T(ON) /* 98 */ \
150+
T(OFF) /* 99 */ \
151+
T(WHILE) /* 100 */ \
152+
T(ENDWHILE) /* 101 */ \
153+
T(SLEEP) /* 102 */ \
154+
T(CONTINUE) /* 103 */ \
155+
T(UDPBIND) /* 104 */ \
156+
T(UDPUNBIND) /* 105 */ \
157+
T(UDPWRITE) /* 106 */ \
158+
T(OUTPORT) /* 107 */ \
159+
T(OUTPORTW) /* 108 */ \
160+
T(OUTPORTD) /* 109 */ \
161+
T(KGET) /* 110 */ \
162162

163163
GENERATE_ENUM_LIST(TOKEN, token_t)
164164

@@ -183,7 +183,7 @@ void tokenizer_next(struct basic_ctx* ctx);
183183
* @param ctx context
184184
* @return int token
185185
*/
186-
int tokenizer_token(struct basic_ctx* ctx);
186+
enum token_t tokenizer_token(struct basic_ctx* ctx);
187187

188188
/**
189189
* @brief Get integer number as next token
@@ -193,7 +193,7 @@ int tokenizer_token(struct basic_ctx* ctx);
193193
* @param token token (NUMBER or HEXNUMBER)
194194
* @return int64_t number read from program
195195
*/
196-
int64_t tokenizer_num(struct basic_ctx* ctx, int token);
196+
int64_t tokenizer_num(struct basic_ctx* ctx, enum token_t token);
197197

198198
/**
199199
* @brief Get real number as next token
@@ -203,7 +203,7 @@ int64_t tokenizer_num(struct basic_ctx* ctx, int token);
203203
* @param token token (NUMBER)
204204
* @param f number read from program
205205
*/
206-
void tokenizer_fnum(struct basic_ctx* ctx, int token, double* f);
206+
void tokenizer_fnum(struct basic_ctx* ctx, enum token_t token, double* f);
207207

208208
/**
209209
* @brief Get a variable name as next token
@@ -232,7 +232,7 @@ bool tokenizer_string(char *dest, int len, struct basic_ctx* ctx);
232232
* @param ctx context
233233
* @return int true if the program has finished
234234
*/
235-
int tokenizer_finished(struct basic_ctx* ctx);
235+
bool tokenizer_finished(struct basic_ctx* ctx);
236236

237237
/**
238238
* @brief display an error to the terminal and end the program

src/basic/console.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void print_statement(struct basic_ctx* ctx)
172172
}
173173
}
174174

175-
void colour_statement(struct basic_ctx* ctx, int tok)
175+
void colour_statement(struct basic_ctx* ctx, enum token_t tok)
176176
{
177177
accept_or_return(tok, ctx);
178178
setforeground(expr(ctx));

src/basic/flow_control.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ void if_statement(struct basic_ctx* ctx)
102102

103103
int depth = 0;
104104
while (!tokenizer_finished(ctx)) {
105-
int t = tokenizer_token(ctx);
105+
enum token_t t = tokenizer_token(ctx);
106106

107107
/* Detect nested multiline IF: IF ... THEN NEWLINE */
108108
if (t == IF) {
109109

110110
/* Advance until THEN or end-of-line/input */
111111
for (;;) {
112112
tokenizer_next(ctx);
113-
int tt = tokenizer_token(ctx);
113+
enum token_t tt = tokenizer_token(ctx);
114114
if (tt == THEN) {
115115
tokenizer_next(ctx);
116116
tt = tokenizer_token(ctx);
@@ -325,12 +325,12 @@ void end_statement(struct basic_ctx* ctx)
325325
ctx->ended = true;
326326
}
327327

328-
static bool seek_to_matching_token(struct basic_ctx* ctx, int open_tok, int close_tok, const char* name) {
328+
static bool seek_to_matching_token(struct basic_ctx* ctx, enum token_t open_tok, enum token_t close_tok, const char* name) {
329329
int depth = 1;
330330
int64_t line = 0;
331331

332332
for (;;) {
333-
int tok = tokenizer_token(ctx);
333+
enum token_t tok = tokenizer_token(ctx);
334334

335335
if (tok == ENDOFINPUT) {
336336
tokenizer_error_printf(ctx, "Unclosed %s", name);
@@ -408,7 +408,7 @@ void continue_statement(struct basic_ctx* ctx)
408408
{
409409
accept_or_return(CONTINUE, ctx);
410410

411-
int kind = tokenizer_token(ctx);
411+
enum token_t kind = tokenizer_token(ctx);
412412
if (kind != WHILE && kind != FOR && kind != REPEAT) {
413413
tokenizer_error_print(ctx, "CONTINUE must be followed by WHILE, FOR, or REPEAT");
414414
return;

src/basic/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ void basic_destroy(struct basic_ctx *ctx) {
521521
kfree_null(&ctx);
522522
}
523523

524-
bool accept(int token, struct basic_ctx *ctx) {
524+
bool accept(enum token_t token, struct basic_ctx *ctx) {
525525
if (token != tokenizer_token(ctx)) {
526526
GENERATE_ENUM_STRING_NAMES(TOKEN, token_names)
527527
tokenizer_error_printf(ctx, "Expected %s got %s", token_names[token], token_names[tokenizer_token(ctx)]);

src/basic/statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern bool debug;
44

55
void statement(struct basic_ctx* ctx)
66
{
7-
int token = tokenizer_token(ctx);
7+
enum token_t token = tokenizer_token(ctx);
88

99
basic_debug("line %ld statement(%d)\n", ctx->current_linenum, token);
1010

0 commit comments

Comments
 (0)