From 12fb91b467dda5371cd1dc169c0607d554a25321 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 21 Mar 2026 09:56:51 -0400 Subject: [PATCH] frontend: parser: Add parser groundwork Signed-off-by: Ian Moffett --- core/quip.c | 5 ++++ frontend/parser.c | 58 +++++++++++++++++++++++++++++++++++++++ include/frontend/parser.h | 25 +++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 frontend/parser.c create mode 100644 include/frontend/parser.h diff --git a/core/quip.c b/core/quip.c index e6e66cc..e062c96 100644 --- a/core/quip.c +++ b/core/quip.c @@ -12,6 +12,7 @@ #include #include "common/knobs.h" #include "common/state.h" +#include "frontend/parser.h" int main(int argc, char **argv) @@ -28,6 +29,10 @@ main(int argc, char **argv) return -1; } + if (parser_parse(&state) < 0) { + return -1; + } + quip_state_destroy(&state); return 0; } diff --git a/frontend/parser.c b/frontend/parser.c new file mode 100644 index 0000000..040ae01 --- /dev/null +++ b/frontend/parser.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + * + * Abstract: + * This file implements the parser. + * Author: + * Ian M. Moffett + */ + +#include +#include "frontend/parser.h" +#include "frontend/lexer.h" +#include "frontend/token.h" + +/* Symbolic token */ +#define symtok(tokstr) \ + "[" tokstr "]" + +/* Quoted string */ +#define qtok(tokstr) \ + "'" tokstr "'" + +/* Convert token to string */ +#define tokstr1(tt) \ + toktab[(tt)] + +/* Convert token to string */ +#define tokstr(tok) \ + tokstr1((tok)->type) + +/* + * Table used to convert token constants into human + * readable strings for debugging + */ +static const char *toktab[] = { + [TT_NONE] = symtok("none"), + [TT_NAME] = symtok("name"), + [TT_NEWLINE] = symtok("newline"), + [TT_COLON] = qtok(":"), + [TT_COLONDUB] = qtok("::") +}; + +int +parser_parse(struct quip_state *state) +{ + struct token token; + + if (state == NULL) { + return -1; + } + + while (lexer_scan(state, &token) == 0) { + printf("got token %s\n", tokstr(&token)); + } + + return 0; +} diff --git a/include/frontend/parser.h b/include/frontend/parser.h new file mode 100644 index 0000000..8b2245e --- /dev/null +++ b/include/frontend/parser.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + * + * Abstract: + * This file implements the parser. + * Author: + * Ian M. Moffett + */ + +#ifndef FRONTEND_PARSER_H +#define FRONTEND_PARSER_H 1 + +#include "common/state.h" + +/* + * Begin parsing the source file + * + * @state: Quip state machine + * + * Returns zero on success + */ +int parser_parse(struct quip_state *state); + +#endif /* !FRONTEND_PARSER_H */