#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;
}
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();