Files
c2/v0/token.h
T
seeseemelk 49b9db5b75 Refactor token mapping: use keyword map for tokenization instead of strcmp
- Created KeywordMap structure with keyword-to-token mapping at top of token.c
- Added lookup_keyword() function to check if identifier is a keyword
- Replaced 3 strcmp calls (lines 99-101) with single lookup_keyword() call
- Removed token_to_string() function and its tests (3 tests removed)
- Single easy-to-read and modify keyword map serves both documentation and implementation
- Added new keywords by editing the keywords[] array at top of token.c

All 12 tests passing (removed token_to_string tests which are now unnecessary).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-24 09:44:19 +02:00

54 lines
976 B
C

/**
* Contains the interface for reading tokens from a file.
*/
#ifndef TOKEN_H
#define TOKEN_H
#include "buffer.h"
typedef enum {
// Keywords
TOKEN_MODULE,
TOKEN_IMPORT,
TOKEN_SEMICOLON,
// Symbols
TOKEN_PARENT_OPEN,
TOKEN_PARENT_CLOSE,
TOKEN_BRACKET_OPEN,
TOKEN_BRACKET_CLOSE,
TOKEN_COMMA,
// Primitives
TOKEN_VOID,
// Variable
TOKEN_IDENTIFIER,
} Token;
typedef struct TokenStream TokenStream;
/**
* Returns a TokenStream for a given buffer.
*
* When the tokenstream is closed, the underlying buffer is also closed.
*
* @param buffer The buffer to read from.
* @returns A handle to the TokenStream.
*/
TokenStream* tokenstream_open(Buffer* buffer);
/**
* Closes a TokenStream.
* @param ts The TokenStream to close.
*/
void tokenstream_close(TokenStream* ts);
/**
* Gets the next token from the TokenStream.
* @param ts The TokenStream to read from.
* @returns The next token read.
*/
Token tokenstream_next(TokenStream* ts);
#endif