]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
added function and type to support lexing multiple files
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 16 Mar 2019 03:43:16 +0000 (23:43 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 16 Mar 2019 03:43:16 +0000 (23:43 -0400)
source/lexer.l
source/main.c
source/parser.c
source/sclpl.h

index 4958cb40fca892e0ee8715caf09196a8a863807b..994adcdf67e093459fb5089a35d6c860cff9cbe0 100644 (file)
@@ -1,5 +1,7 @@
 %{
 #include <sclpl.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 
 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();
index 02638c53a1663bfbe65a60a702d18c2459093325..5bb8c9388010d290855c18202675d912ba769284 100644 (file)
@@ -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;
 }
index 4fe469769b356875e3fddb172e5cecb283f2c6c8..8f7dbc31fce7dcd5824887c57374a23bd4dee805 100644 (file)
@@ -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))
index a9429afd923340bc33d543f785988a685df63596..d70e568af3f8d4e67eb2b14db1e84240c1c44ff4 100644 (file)
@@ -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);