From: Michael D. Lowis Date: Mon, 21 May 2018 20:06:15 +0000 (-0400) Subject: minor refactoring of parser context and helper functions X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ccb4a74258052bfb241efc5059b9f56906408fda;p=proto%2Fsclpl.git minor refactoring of parser context and helper functions --- diff --git a/source/main.c b/source/main.c index 18b7d1d..c5f1563 100644 --- a/source/main.c +++ b/source/main.c @@ -7,9 +7,9 @@ char* Artifact = "ast"; *****************************************************************************/ static int emit_tokens(void) { Tok token = { 0 }; - Parser* ctx = parser_new(NULL, stdin); + Parser ctx = { .input = stdin }; while (1) { - gettoken(ctx, &token); + gettoken(&ctx, &token); if (token.type == T_END_FILE) break; else @@ -20,8 +20,8 @@ static int emit_tokens(void) { static int emit_ast(void) { AST* tree = NULL; - Parser* ctx = parser_new(NULL, stdin); - while(NULL != (tree = toplevel(ctx))) + Parser ctx = { .input = stdin }; + while(NULL != (tree = toplevel(&ctx))) pprint_tree(stdout, tree, 0); return 0; } @@ -49,7 +49,6 @@ int main(int argc, char **argv) { } OPTEND; /* Execute the main compiler process */ - if (0 == strcmp("tok", Artifact)) { return emit_tokens(); } else if (0 == strcmp("ast", Artifact)) { diff --git a/source/parser.c b/source/parser.c index c0d8c34..205a5f0 100644 --- a/source/parser.c +++ b/source/parser.c @@ -15,14 +15,49 @@ static AST* if_stmnt(Parser* p); static AST* token_to_tree(Tok* tok); static AST* func_app(Parser* p, AST* fn); -// Parsing Routines -static void fetch(Parser* parser); -static Tok* peek(Parser* parser); -static void error(Parser* parser, const char* text); -static bool match(Parser* parser, TokType type); -static bool accept(Parser* parser, TokType type); -static void expect(Parser* parser, TokType type); -static Tok* expect_val(Parser* parser, TokType type); +/* Parsing Routines + *****************************************************************************/ +static Tok* peek(Parser* p) { + if (T_NONE == p->tok.type) + gettoken(p, &(p->tok)); + return &(p->tok); +} + +static void error(Parser* parser, const char* text) { + Tok* tok = peek(parser); + fprintf(stderr, ":%zu:%zu:Error: %s\n", tok->line, tok->col, text); + exit(1); +} + +static bool match(Parser* parser, TokType type) { + return (peek(parser)->type == type); +} + +static bool accept(Parser* parser, TokType type) { + if (peek(parser)->type == type) { + parser->tok.type = T_NONE; + return true; + } + return false; +} + +static void expect(Parser* parser, TokType type) { + if (!accept(parser, type)) + error(parser, "Unexpected token"); +} + +static Tok* expect_val(Parser* parser, TokType type) { + Tok* tok = NULL; + if (peek(parser)->type == type) { + tok = calloc(1, sizeof(Tok)); + *tok = *(peek(parser)); + parser->tok.type = T_NONE; + } else { + error(parser, "Unexpected token"); + } + return tok; +} + /* Grammar Definition *****************************************************************************/ @@ -202,54 +237,3 @@ static AST* func_app(Parser* p, AST* fn) { expect(p,T_RPAR); return app; } - -/* Parsing Routines - *****************************************************************************/ -Parser* parser_new(char* prompt, FILE* input) { - Parser* parser = emalloc(sizeof(Parser)); - memset(parser, 0, sizeof(Parser)); - parser->input = input; - parser->prompt = prompt; - return parser; -} - -static Tok* peek(Parser* p) { - if (T_NONE == p->tok.type) - gettoken(p, &(p->tok)); - return &(p->tok); -} - -static void error(Parser* parser, const char* text) { - Tok* tok = peek(parser); - fprintf(stderr, ":%zu:%zu:Error: %s\n", tok->line, tok->col, text); - exit(1); -} - -static bool match(Parser* parser, TokType type) { - return (peek(parser)->type == type); -} - -static bool accept(Parser* parser, TokType type) { - if (peek(parser)->type == type) { - parser->tok.type = T_NONE; - return true; - } - return false; -} - -static void expect(Parser* parser, TokType type) { - if (!accept(parser, type)) - error(parser, "Unexpected token"); -} - -static Tok* expect_val(Parser* parser, TokType type) { - Tok* tok = NULL; - if (peek(parser)->type == type) { - tok = calloc(1, sizeof(Tok)); - *tok = *(peek(parser)); - parser->tok.type = T_NONE; - } else { - error(parser, "Unexpected token"); - } - return tok; -} diff --git a/source/sclpl.h b/source/sclpl.h index 6c44a90..b734428 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -204,17 +204,12 @@ void pprint_tree(FILE* file, AST* tree, int depth); /* Lexer and Parser Types *****************************************************************************/ typedef struct { - char* line; - size_t index; - size_t lineno; FILE* input; - char* prompt; Tok tok; } Parser; // Lexer routines void gettoken(Parser* ctx, Tok* tok); -void fetchline(Parser* ctx); // Parser routines Parser* parser_new(char* p_prompt, FILE* input);