Skip to content

Commit 5c43ffd

Browse files
committed
Implement token linked list
1 parent 9a5fe11 commit 5c43ffd

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

parse.c

+39-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <ctype.h>
2121
#include <stdbool.h>
2222
#include <stdio.h>
23+
#include <stdlib.h>
2324
#include <strings.h>
2425
#include "parse.h"
2526
#include "utils.h"
@@ -110,13 +111,15 @@ static void print_token(struct Token *token) {
110111
}
111112

112113
void parse(char *code) {
113-
while (true) {
114-
struct Token token = token_get(code, &code);
115-
if (!code) break;
116-
if (token.type != TOK_WHITESPACE) print_token(&token);
117-
if (token.type == TOK_UNKNOWN) die("!!! Unknown token encountered !!!");
118-
}
119-
return;
114+
struct TokenList token_list = token_get_list(code);
115+
if (!token_list.length) return;
116+
struct TokenListNode *token_list_node = token_list.head;
117+
if (token_list.dirty) fputs("!!! WARNING: Unknown token(s) encountered !!!\n", stderr);
118+
do {
119+
struct Token *token = token_list_node->token;
120+
if (token->type != TOK_WHITESPACE) print_token(token);
121+
token_list_node = token_list_node->next;
122+
} while (token_list_node);
120123
}
121124

122125
struct Token token_get(char *code, char **next) {
@@ -244,6 +247,35 @@ struct Token token_get(char *code, char **next) {
244247
return token;
245248
}
246249

250+
struct TokenList token_get_list(char *code) {
251+
struct TokenList list = {.length = 0, .dirty = false};
252+
253+
while (code) {
254+
struct Token *token = malloc(sizeof(struct Token));
255+
if (!token) goto end;
256+
257+
*token = token_get(code, &code);
258+
if (token->type == TOK_UNKNOWN) list.dirty = true;
259+
260+
struct TokenListNode *new_node = malloc(sizeof(struct TokenListNode));
261+
if (!new_node) goto end;
262+
if (list.tail) {
263+
list.tail->next = new_node;
264+
new_node->prev = list.tail;
265+
list.tail = new_node;
266+
} else {
267+
list.head = new_node;
268+
list.tail = new_node;
269+
new_node->prev = NULL;
270+
}
271+
new_node->token = token;
272+
new_node->next = NULL;
273+
list.length += 1;
274+
}
275+
276+
end: return list;
277+
};
278+
247279
size_t scan_string(char *str, bool (cmpfunc)(char)) {
248280
size_t len = 0;
249281
while (true) {

parse.h

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef PARSE_H
2121
#define PARSE_H
2222

23+
#include <stdbool.h>
2324
#include <stddef.h>
2425

2526
enum TokenType {
@@ -44,8 +45,22 @@ struct Token {
4445
size_t data_len;
4546
};
4647

48+
struct TokenList {
49+
size_t length;
50+
struct TokenListNode *head;
51+
struct TokenListNode *tail;
52+
bool dirty;
53+
};
54+
55+
struct TokenListNode {
56+
struct Token *token;
57+
struct TokenListNode *prev;
58+
struct TokenListNode *next;
59+
};
60+
4761
void parse(char *code);
4862
struct Token token_get(char *code, char **next);
63+
struct TokenList token_get_list(char *code);
4964
size_t scan_string(char *str, bool (cmpfunc)(char));
5065

5166
bool char_is_whitespace(char chr);

0 commit comments

Comments
 (0)