From: Michael D. Lowis Date: Sat, 16 Mar 2019 03:43:16 +0000 (-0400) Subject: added function and type to support lexing multiple files X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=44ab3315a814e2a26cb6079a5b8d4c252c1f9484;p=proto%2Fsclpl.git added function and type to support lexing multiple files --- diff --git a/source/lexer.l b/source/lexer.l index 4958cb4..994adcd 100644 --- a/source/lexer.l +++ b/source/lexer.l @@ -1,5 +1,7 @@ %{ #include +#include +#include static union { char* text; @@ -124,6 +126,27 @@ false { %% +static char* file_load(char* path) { + int fd = -1, nread = 0, length = 0; + struct stat sb = {0}; + char* contents = NULL; + if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0)) { + contents = calloc(sb.st_size + 1u, 1u); + while (sb.st_size && (nread = read(fd, contents+length, sb.st_size)) > 0) + length += nread, sb.st_size -= nread; + } + if (fd > 0) close(fd); + return contents; +} + +void lexfile(Parser* ctx, char* path) { + LexFile* file = calloc(sizeof(file), 1u); + file->path = strdup(path); + file->fbeg = file->fpos = file_load(path); + file->next = ctx->file; + ctx->file = file; +} + void gettoken(Parser* ctx) { ctx->tok.line = yylineno; ctx->tok.type = yylex(); diff --git a/source/main.c b/source/main.c index 02638c5..5bb8c93 100644 --- a/source/main.c +++ b/source/main.c @@ -6,7 +6,7 @@ char* Artifact = "bin"; /* Driver Modes *****************************************************************************/ static int emit_tokens(int argc, char **argv) { - Parser ctx = { .input = stdin }; + Parser ctx = {0}; while (1) { gettoken(&ctx); if (ctx.tok.type == T_END_FILE) @@ -19,7 +19,7 @@ static int emit_tokens(int argc, char **argv) { static int emit_ast(int argc, char **argv) { AST* tree = NULL; - Parser ctx = { .input = stdin }; + Parser ctx = {0}; codegen_init(&ctx); while(NULL != (tree = toplevel(&ctx))) pprint_tree(stdout, tree, 0); @@ -34,9 +34,9 @@ static int emit_binary(int argc, char **argv) { Parser ctx = {0}; codegen_init(&ctx); for (; argc; argc--,argv++) { - ctx.input = fopen(*argv,"r"); +// ctx.input = fopen(*argv,"r"); toplevel(&ctx); - fclose(ctx.input); +// fclose(ctx.input); } return 0; } @@ -49,9 +49,9 @@ static int emit_library(int argc, char **argv) { Parser ctx = {0}; codegen_init(&ctx); for (; argc; argc--,argv++) { - ctx.input = fopen(*argv,"r"); - toplevel(&ctx); - fclose(ctx.input); +// ctx.input = fopen(*argv,"r"); +// toplevel(&ctx); +// fclose(ctx.input); } return 0; } diff --git a/source/parser.c b/source/parser.c index 4fe4697..8f7dbc3 100644 --- a/source/parser.c +++ b/source/parser.c @@ -70,9 +70,6 @@ static Tok* expect_val(Parser* p, TokType type) { /* Grammar Definition *****************************************************************************/ AST* toplevel(Parser* p) { - extern FILE* yyin; - if (yyin != p->input) - yyin = p->input; if (matches(p, T_REQUIRES)) require_list(p); if (matches(p, T_PROVIDES)) diff --git a/source/sclpl.h b/source/sclpl.h index a9429af..d70e568 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -187,13 +187,21 @@ void pprint_tree(FILE* file, AST* tree, int depth); /* Lexer and Parser Types *****************************************************************************/ +typedef struct LexFile { + struct LexFile* next; + char* path; + char* fbeg; + char* fpos; +} LexFile; + typedef struct { - FILE* input; + LexFile* file; Tok tok; SymTable syms; Package pkg; } Parser; +void lexfile(Parser* ctx, char* path); void gettoken(Parser* ctx); AST* toplevel(Parser* p); void codegen_init(Parser* p);