55#include <ctype.h>
66#include "stringpool.h"
77
8+ #include "gc.h"
9+
810void create_file_lexer (lexing_context * ctx , const char * filename ) {
911 ctx -> filename = filename ;
1012 ctx -> lineno = 1 ;
@@ -54,12 +56,12 @@ static void advance_until_newline(lexing_context *ctx) {
5456
5557// Read a string literal, ending on a close-quote
5658const char * read_string (lexing_context * ctx ) {
57- char literal [MAX_LITERAL_SIZE ];
59+ char buffer [MAX_LITERAL_SIZE ];
5860 int position = 0 ;
5961 while (true) {
6062 int c = get_char (ctx );
6163 if (c == '\"' ) {
62- literal [position ] = '\0' ;
64+ buffer [position ] = '\0' ;
6365 break ;
6466 }
6567
@@ -81,10 +83,12 @@ const char *read_string(lexing_context *ctx) {
8183 }
8284 }
8385
84- literal [position ] = c ;
86+ buffer [position ] = c ;
8587 ++ position ;
8688 }
87- return strdup (literal );
89+ char * literal = GC_MALLOC_ATOMIC (strlen (buffer ) + 1 );
90+ strcpy (literal , buffer );
91+ return literal ;
8892}
8993
9094bool is_validsymbolchar (char c ) {
@@ -137,7 +141,7 @@ bool is_letter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
137141bool is_alphanumeric (char c ) { return is_letter (c ) || is_digit (c ); }
138142
139143void unget_token (lexing_context * ctx , token_t * token ) {
140- token_t * new_token = malloc (sizeof (token_t ));
144+ token_t * new_token = GC_MALLOC (sizeof (token_t ));
141145 memcpy (new_token , token , sizeof (token_t ));
142146 new_token -> next = ctx -> stack ;
143147 ctx -> stack = new_token ;
@@ -149,7 +153,7 @@ void get_next_token(lexing_context *ctx, token_t *token) {
149153 token_t * next_tok = ctx -> stack ;
150154 ctx -> stack = next_tok -> next ;
151155 memcpy (token , next_tok , sizeof (token_t ));
152- free (next_tok );
156+ GC_FREE (next_tok );
153157 return ;
154158 }
155159
0 commit comments