From e47f9e8a4c67ac3c61e911926e1c7837b9fa9081 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 16 Mar 2019 22:31:50 -0400 Subject: [PATCH] call new lexer from parser --- source/main.c | 58 +++++++++++++++---------------------------------- source/parser.c | 2 +- source/pprint.c | 1 + source/sclpl.h | 1 + 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/source/main.c b/source/main.c index beb1f94..22bb48d 100644 --- a/source/main.c +++ b/source/main.c @@ -1,60 +1,33 @@ #include char* ARGV0; -char* Artifact = "tok"; +char* Artifact = "bin"; /* Driver Modes *****************************************************************************/ -static int emit_tokens(int argc, char **argv) { - Parser ctx = {0}; - for (; argc; argc--,argv++) - lexfile(&ctx, *argv); +static int emit_tokens(Parser* ctx, int argc, char **argv) { while (1) { - lex(&ctx); - if (ctx.tok.type == T_END_FILE) + lex(ctx); + if (ctx->tok.type == T_END_FILE) break; else - pprint_token(stdout, &(ctx.tok), true); + pprint_token(stdout, &(ctx->tok), true); } return 0; } -static int emit_ast(int argc, char **argv) { +static int emit_ast(Parser* ctx, int argc, char **argv) { AST* tree = NULL; - Parser ctx = {0}; - codegen_init(&ctx); - while(NULL != (tree = toplevel(&ctx))) + while(NULL != (tree = toplevel(ctx))) pprint_tree(stdout, tree, 0); return 0; } -static int emit_binary(int argc, char **argv) { - if (!argc) { - fprintf(stderr, "%s: error: no input files\n", ARGV0); - exit(1); - } - Parser ctx = {0}; - codegen_init(&ctx); - for (; argc; argc--,argv++) { -// ctx.input = fopen(*argv,"r"); - toplevel(&ctx); -// fclose(ctx.input); - } +static int emit_binary(Parser* ctx, int argc, char **argv) { return 0; } -static int emit_library(int argc, char **argv) { - if (!argc) { - fprintf(stderr, "%s: error: no input files\n", ARGV0); - exit(1); - } - Parser ctx = {0}; - codegen_init(&ctx); - for (; argc; argc--,argv++) { -// ctx.input = fopen(*argv,"r"); -// toplevel(&ctx); -// fclose(ctx.input); - } +static int emit_library(Parser* ctx, int argc, char **argv) { return 0; } @@ -76,15 +49,20 @@ int main(int argc, char **argv) { default: usage(); } OPTEND; + Parser ctx = {0}; + codegen_init(&ctx); + for (; argc; argc--,argv++) + lexfile(&ctx, *argv); + /* Execute the main compiler process */ if (0 == strcmp("tok", Artifact)) { - return emit_tokens(argc, argv); + return emit_tokens(&ctx, argc, argv); } else if (0 == strcmp("ast", Artifact)) { - return emit_ast(argc, argv); + return emit_ast(&ctx, argc, argv); } else if (0 == strcmp("bin", Artifact)) { - return emit_binary(argc, argv); + return emit_binary(&ctx, argc, argv); } else if (0 == strcmp("lib", Artifact)) { - return emit_library(argc, argv); + return emit_library(&ctx, argc, argv); } else { fprintf(stderr, "Unknown artifact type: '%s'\n\n", Artifact); usage(); diff --git a/source/parser.c b/source/parser.c index 8f7dbc3..6b37ac1 100644 --- a/source/parser.c +++ b/source/parser.c @@ -23,7 +23,7 @@ static AST* token_to_tree(Parser* p, Tok* tok); *****************************************************************************/ static Tok* peek(Parser* p) { if (T_NONE == p->tok.type) - gettoken(p); + lex(p); return &(p->tok); } diff --git a/source/pprint.c b/source/pprint.c index f39eed0..26c02ab 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -127,6 +127,7 @@ static void pprint_literal(FILE* file, AST* tree, int depth) void pprint_tree(FILE* file, AST* tree, int depth) { + puts("tree"); if (tree == NULL) { return; } diff --git a/source/sclpl.h b/source/sclpl.h index 156324c..ab1506f 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -207,6 +207,7 @@ typedef struct { } Parser; void lexfile(Parser* ctx, char* path); +void lex(Parser* ctx); void gettoken(Parser* ctx); AST* toplevel(Parser* p); void codegen_init(Parser* p); -- 2.52.0