Something.

This commit is contained in:
Jacob Signorovitch 2024-10-31 16:44:17 -04:00
parent 2662f54c51
commit a1f210fee1
5 changed files with 13 additions and 6 deletions

View File

@ -38,6 +38,7 @@ typedef struct {
Lexer* lexer_init(char* src); Lexer* lexer_init(char* src);
// Destroy a lexer. // Destroy a lexer.
// Does not destroy `lexer->src`!
void lexer_destroy(Lexer* lexer); void lexer_destroy(Lexer* lexer);
// Convert text to tokens. // Convert text to tokens.

View File

@ -17,8 +17,9 @@ typedef struct {
Parser* parser_init(size_t tokenc, Token** tokenv); Parser* parser_init(size_t tokenc, Token** tokenv);
void parser_destroy(Parser* parser); void parser_destroy(Parser* parser);
// Increment parser->ctoken. // Increment `parser->ctoken`.
void parser_inc(Parser* parser); void parser_inc(Parser* parser);
// Step forward 1 token and add to stacks. // Step forward 1 token and add to stacks.
void parser_step(Parser* parser); void parser_step(Parser* parser);

View File

@ -6,15 +6,18 @@
#define STACK_MAX 64 #define STACK_MAX 64
typedef struct { typedef struct {
size_t i; size_t i; // Current index in the stack.
void* val[STACK_MAX]; void* val[STACK_MAX]; // The stack itself.
} Stack; } Stack;
Stack* stack_init(); Stack* stack_init();
// stack->i must be 0. // Destroy a stack.
// Note that `stack->i` must be `0`.
void stack_destroy(Stack* stack); void stack_destroy(Stack* stack);
// Push a value to the stack.
void stack_push(Stack* stack, void* val); void stack_push(Stack* stack, void* val);
// Pop a value from the stack.
void* stack_pop(Stack* stack); void* stack_pop(Stack* stack);
#endif #endif

View File

@ -24,8 +24,7 @@ Lexer* lexer_init(char* src) {
} }
void lexer_destroy(Lexer* lexer) { void lexer_destroy(Lexer* lexer) {
free(lexer->src); // Does not free lexer->src.
for (int i = 0; i < lexer->ntokens; i++) token_destroy(lexer->tokens[i]); for (int i = 0; i < lexer->ntokens; i++) token_destroy(lexer->tokens[i]);
} }

View File

@ -20,6 +20,9 @@ int main(int argc, char** argv) {
Lexer* lexer = lexer_init(cline->buf); Lexer* lexer = lexer_init(cline->buf);
lexer_lex(lexer); lexer_lex(lexer);
lexer_print(lexer); lexer_print(lexer);
} lexer_destroy(lexer);
}
dstr_destroy(cline);
} }
} }