]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
call new lexer from parser
authorMichael D. Lowis <mike@mdlowis.com>
Sun, 17 Mar 2019 02:31:50 +0000 (22:31 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sun, 17 Mar 2019 02:31:50 +0000 (22:31 -0400)
source/main.c
source/parser.c
source/pprint.c
source/sclpl.h

index beb1f9474a4602d644dfcc1635a3970b364fd78a..22bb48d9ee443528dddd6c8453ec511c0c3886f8 100644 (file)
@@ -1,60 +1,33 @@
 #include <sclpl.h>
 
 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();
index 8f7dbc31fce7dcd5824887c57374a23bd4dee805..6b37ac12ccc6b4b1cc4662ff562eeefae0ab143e 100644 (file)
@@ -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);
 }
 
index f39eed0738b46a3eace691f549f1757a55e5fd52..26c02abf929d64e1b04195c43dc96f67b663d870 100644 (file)
@@ -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;
     }
index 156324c409459b8786d8d032ba83730646c00f74..ab1506f4872d7cf6f967f499bc68b905970d28cf 100644 (file)
@@ -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);