Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef struct arena_block {
typedef struct {
arena_block_t *head;
int total_bytes; /* Track total allocation for profiling */
int block_size; /* Default block size for new blocks */
} arena_t;

/* string-based hash map definitions */
Expand Down
11 changes: 8 additions & 3 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ arena_t *arena_init(int initial_capacity)
}
arena->head = arena_block_create(initial_capacity);
arena->total_bytes = initial_capacity;
/* Use the initial capacity as the default block size for future growth. */
arena->block_size = initial_capacity;
return arena;
}

Expand All @@ -166,9 +168,12 @@ void *arena_alloc(arena_t *arena, int size)
size = (size + PTR_SIZE - 1) & ~(PTR_SIZE - 1);

if (!arena->head || arena->head->offset + size > arena->head->capacity) {
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE, size) */
int new_capacity =
(size > DEFAULT_ARENA_SIZE ? size : DEFAULT_ARENA_SIZE);
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE,
* arena->block_size, size) */
int base =
(arena->block_size > DEFAULT_ARENA_SIZE ? arena->block_size
: DEFAULT_ARENA_SIZE);
int new_capacity = (size > base ? size : base);
arena_block_t *new_block = arena_block_create(new_capacity);
new_block->next = arena->head;
arena->head = new_block;
Expand Down
36 changes: 27 additions & 9 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,23 @@ bool is_numeric(char buffer[])

void skip_whitespace(void)
{
int pos = SOURCE->size;
while (true) {
if (is_linebreak(next_char)) {
SOURCE->size += 2;
next_char = SOURCE->elements[SOURCE->size];
/* Handle backslash-newline (line continuation) using local pos */
if (next_char == '\\' && SOURCE->elements[pos + 1] == '\n') {
pos += 2;
next_char = SOURCE->elements[pos];
continue;
}
if (is_whitespace(next_char) ||
(skip_newline && is_newline(next_char))) {
SOURCE->size++;
next_char = SOURCE->elements[SOURCE->size];
pos++;
next_char = SOURCE->elements[pos];
continue;
}
break;
}
SOURCE->size = pos;
}

char read_char(bool is_skip_space)
Expand Down Expand Up @@ -296,27 +299,42 @@ token_t lex_token_internal(bool aliasing)
/* C-style comments */
if (next_char == '*') {
/* in a comment, skip until end */
int pos = SOURCE->size;
do {
read_char(false);
/* advance one char */
pos++;
next_char = SOURCE->elements[pos];
if (next_char == '*') {
read_char(false);
/* look ahead */
pos++;
next_char = SOURCE->elements[pos];
if (next_char == '/') {
read_char(true);
/* consume closing '/', then commit and skip trailing
* whitespaces
*/
pos++;
next_char = SOURCE->elements[pos];
SOURCE->size = pos;
skip_whitespace();
return lex_token_internal(aliasing);
}
}
} while (next_char);

SOURCE->size = pos;
if (!next_char)
error("Unenclosed C-style comment");
return lex_token_internal(aliasing);
}

/* C++-style comments */
if (next_char == '/') {
int pos = SOURCE->size;
do {
read_char(false);
pos++;
next_char = SOURCE->elements[pos];
} while (next_char && !is_newline(next_char));
SOURCE->size = pos;
return lex_token_internal(aliasing);
}

Expand Down