]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
minor refactoring of parser context and helper functions
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 21 May 2018 20:06:15 +0000 (16:06 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 21 May 2018 20:06:15 +0000 (16:06 -0400)
source/main.c
source/parser.c
source/sclpl.h

index 18b7d1d6ccb49201c012bd1d411478fe286d1f5d..c5f156310b7924ce50309f921b381da8ac93ff71 100644 (file)
@@ -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)) {
index c0d8c348f917e7d83da6c28162dab36d3f8e9da4..205a5f0262563e6b87878cadcb6f73cf998743e0 100644 (file)
@@ -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, "<file>:%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, "<file>:%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;
-}
index 6c44a90eb3a23d9552599c0e2f0a2c13f351c205..b734428fcca1345180251d7114cb29250e37be04 100644 (file)
@@ -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);