From a4c9a32c3f8a8fc15743356865a206d9e54a7767 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 20 May 2018 14:48:54 -0400 Subject: [PATCH] stripped down grammar. ignoring broken specs for now --- source/lexer.l | 12 +++--------- source/main.c | 11 ++++++++--- source/parser.c | 31 +++++++++++-------------------- source/sclpl.h | 10 +++++----- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/source/lexer.l b/source/lexer.l index d590817..c69e6bf 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -124,16 +124,10 @@ false { %% -Tok* gettoken(Parser* ctx) -{ - Tok* tok = NULL; - int type = yylex(); - if (type != T_END_FILE) { - tok = emalloc(sizeof(Tok)); - tok->type = type; +void gettoken(Parser* ctx, Tok* tok) { + tok->type = yylex(); + if (tok->type != T_END_FILE) memcpy(&(tok->value), &Value, sizeof(Value)); - } - return tok; } void fetchline(Parser* ctx) diff --git a/source/main.c b/source/main.c index 0a22d40..534ebd2 100644 --- a/source/main.c +++ b/source/main.c @@ -6,10 +6,15 @@ char* Artifact = "bin"; /* Driver Modes *****************************************************************************/ static int emit_tokens(void) { - Tok* token = NULL; + Tok token = { 0 }; Parser* ctx = parser_new(NULL, stdin); - while(NULL != (token = gettoken(ctx))) - pprint_token(stdout, token, true); + while (1) { + gettoken(ctx, &token); + if (token.type == T_END_FILE) + break; + else + pprint_token(stdout, &token, true); + } return 0; } diff --git a/source/parser.c b/source/parser.c index 5b036eb..bb86726 100644 --- a/source/parser.c +++ b/source/parser.c @@ -39,12 +39,10 @@ AST* toplevel(Parser* p) { AST* ret = NULL; if (!match(p, T_END_FILE)) { - if (accept(p, T_REQUIRE)) - ret = require(p); - else if (accept(p, T_DEF)) + if (accept(p, T_DEF)) ret = definition(p); else - error(p, "expressions are not allowed at the toplevel"); + error(p, "only definitions are allowed at the toplevel"); } return ret; } @@ -208,27 +206,22 @@ static void type_annotation(Parser* p) Parser* parser_new(char* prompt, FILE* input) { Parser* parser = emalloc(sizeof(Parser)); - parser->line = NULL; - parser->index = 0; - parser->lineno = 0; + memset(parser, 0, sizeof(Parser)); parser->input = input; parser->prompt = prompt; - parser->tok = NULL; return parser; } static void fetch(Parser* parser) { - parser->tok = gettoken(parser); - if (NULL == parser->tok) - parser->tok = &tok_eof; + gettoken(parser, &(parser->tok)); } static Tok* peek(Parser* parser) { - if (NULL == parser->tok) + if (T_NONE == parser->tok.type) fetch(parser); - return parser->tok; + return &(parser->tok); } static bool parser_eof(Parser* parser) @@ -238,8 +231,8 @@ static bool parser_eof(Parser* parser) static void parser_resume(Parser* parser) { - if ((NULL != parser->tok) && (&tok_eof != parser->tok)) - parser->tok = NULL; + if ((T_NONE != parser->tok.type) && (T_END_FILE != parser->tok.type)) + parser->tok.type = T_NONE; /* We ignore the rest of the current line and attempt to start parsing * again on the next line */ fetchline(parser); @@ -260,11 +253,9 @@ static bool match(Parser* parser, TokType type) static Tok* accept(Parser* parser, TokType type) { Tok* tok = peek(parser); - if (tok->type == type) { - parser->tok = NULL; - return tok; - } - return NULL; + if (tok->type == type) + parser->tok.type = T_NONE; + return tok; } static Tok* expect(Parser* parser, TokType type) diff --git a/source/sclpl.h b/source/sclpl.h index 7edd081..6c44a90 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -40,9 +40,9 @@ void vec_set(vec_t* vec, size_t index, void* data); /* Token Types *****************************************************************************/ typedef enum { - T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, T_LBRACE, T_RBRACE, T_LBRACK, - T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE, T_END, T_COLON, T_AMP, - T_REQUIRE, T_DEF, T_IF, T_FN, T_THEN, T_ELSE, T_END_FILE + T_NONE, T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, T_LBRACE, T_RBRACE, + T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE, T_END, + T_COLON, T_AMP, T_REQUIRE, T_DEF, T_IF, T_FN, T_THEN, T_ELSE, T_END_FILE } TokType; typedef struct { @@ -209,11 +209,11 @@ typedef struct { size_t lineno; FILE* input; char* prompt; - Tok* tok; + Tok tok; } Parser; // Lexer routines -Tok* gettoken(Parser* ctx); +void gettoken(Parser* ctx, Tok* tok); void fetchline(Parser* ctx); // Parser routines -- 2.54.0