From 34fc6119714b999bae5aaf08697453dca500f705 Mon Sep 17 00:00:00 2001 From: "Ian M." Date: Sat, 3 Oct 2026 06:25:43 +0000 Subject: frontend: Add lexer+parser groundwork Signed-off-by: Ian M. --- core/lexer.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 core/lexer.c (limited to 'core/lexer.c') diff --git a/core/lexer.c b/core/lexer.c new file mode 100644 index 0000000..019724c --- /dev/null +++ b/core/lexer.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#include +#include +#include + +/* + * Returns true if the given char is a whitespace + * + * @c: Character to compare against a set of whitespace + */ +static inline bool +lexer_is_ws(char c) +{ + switch (c) { + case '\n': + case '\f': + case '\t': + case '\r': + case ' ': + return true; + } + + return false; +} + +/* + * Scan a single byte from the input source file + * + * @state: Assembler state + * @skip_ws: If true, skip whitespaces + * + * Returns the scanned byte on success, returns '\0' + * on failure. + */ +static char +lexer_scan_byte(struct atas_state *state, bool skip_ws) +{ + char c; + + if (state == NULL) { + return '\0'; + } + + c = fgetc(state->in_file); + while (lexer_is_ws(c) && skip_ws) { + if ((c = fgetc(state->in_file)) == EOF) { + return '\0'; + } + } + + return c; +} + +int +atas_lexer_scan(struct atas_state *state, struct atas_token *token) +{ + char c; + + if (state == NULL || token == NULL) { + return -1; + } + + c = lexer_scan_byte(state, true); + if (c == '\0') { + return -1; + } + + switch (c) { + case ',': + token->type = TT_COMMA; + token->c = c; + return 0; + } + + return -1; +} -- cgit v1.2.3