%{
#include <sclpl.h>
+#include <fcntl.h>
+#include <sys/stat.h>
static union {
char* text;
%%
+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();
/* 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)
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);
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;
}
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;
}
/* 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))
/* 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);