radish/include/lexer.h

49 lines
1.1 KiB
C
Raw Permalink Normal View History

2025-01-06 03:00:01 +01:00
#ifndef LEXER_H
#define LEXER_H
#define DEFAULT_TOKENS_SIZE 512
typedef enum {
// Single-character tokens
2025-01-06 04:53:15 +01:00
TOKEN_LEFT_PAREN, TOKEN_RIGHT_PAREN, TOKEN_LEFT_BRACE, TOKEN_RIGHT_BRACE,
TOKEN_COMMA, TOKEN_DOT, TOKEN_MINUS, TOKEN_PLUS, TOKEN_SEMICOLON,
TOKEN_SLASH, TOKEN_STAR,
2025-01-06 03:00:01 +01:00
// One or two character tokens
2025-01-06 04:53:15 +01:00
TOKEN_BANG, TOKEN_BANG_EQUAL, TOKEN_EQUAL, TOKEN_EQUAL_EQUAL, TOKEN_GREATER,
TOKEN_GREATER_EQUAL, TOKEN_LESS, TOKEN_LESS_EQUAL,
2025-01-06 03:00:01 +01:00
// Literals
2025-01-06 04:53:15 +01:00
TOKEN_IDENTIFIER, TOKEN_STRING, TOKEN_NUMBER,
2025-01-06 03:00:01 +01:00
// Keywords
2025-01-06 04:53:15 +01:00
TOKEN_AND, TOKEN_CLASS, TOKEN_ELSE, TOKEN_FALSE, TOKEN_FUN, TOKEN_FOR,
TOKEN_IF, TOKEN_NIL, TOKEN_OR, TOKEN_PRINT, TOKEN_RETURN, TOKEN_SUPER,
TOKEN_THIS, TOKEN_TRUE, TOKEN_VAR, TOKEN_WHILE,
2025-01-06 03:00:01 +01:00
2025-01-06 04:53:15 +01:00
TOKEN_EOF
2025-01-06 03:00:01 +01:00
} token_type_t;
typedef struct {
char *keyword;
token_type_t token_type;
} keyword_map;
typedef struct {
token_type_t type;
char *value;
int line;
} token_t;
typedef struct {
token_t *tokens;
int length;
int capacity;
} array_t;
array_t *tokenize(char *filename);
void print_tokens(token_t *tokens);
void free_array(array_t *array);
#endif