From 57f73fa7c9aca8f2e6a06141282b646e6183df5a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 17 Mar 2019 22:25:01 -0400 Subject: [PATCH] updated parser to record requires and provides --- Makefile | 5 +---- source/codegen.c | 9 ++------- source/main.c | 6 ++---- source/parser.c | 19 +++++++++---------- source/pkg.c | 8 ++++++++ source/sclpl.h | 15 ++++++++------- 6 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 63881f1..8301555 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ CPPFLAGS = CFLAGS += -O0 -g ${INCS} ${CPPFLAGS} LDFLAGS += ${LIBS} ARFLAGS = rcs +MAKEFLAGS = -j #------------------------------------------------------------------------------ # Build Targets and Rules @@ -22,7 +23,6 @@ OBJS = source/main.o \ source/pkg.o \ source/pprint.o \ source/parser.o \ - source/lexer.o \ source/lex.o \ source/ast.o \ source/types.o \ @@ -45,9 +45,6 @@ tests: $(BIN) @echo "Parsing example file..." ./sclpl -Aast < example.src -.l.c: - ${LEX} -o $@ $< - .c.o: ${CC} ${CFLAGS} -c -o $@ $< diff --git a/source/codegen.c b/source/codegen.c index daa1a3b..a625f09 100644 --- a/source/codegen.c +++ b/source/codegen.c @@ -4,14 +4,9 @@ void codegen_init(Parser* p) { sym_add(&(p->syms), SF_TYPEDEF, "void", VoidType()); sym_add(&(p->syms), SF_TYPEDEF, "bool", UIntType(1u)); sym_add(&(p->syms), SF_TYPEDEF, "byte", UIntType(8u)); -// sym_add(&(p->syms), SF_TYPEDEF, "ushort", UIntType(16u)); -// sym_add(&(p->syms), SF_TYPEDEF, "short", IntType(16u)); -// sym_add(&(p->syms), SF_TYPEDEF, "uint", UIntType(32u)); - sym_add(&(p->syms), SF_TYPEDEF, "int", IntType(32u)); -// sym_add(&(p->syms), SF_TYPEDEF, "ulong", UIntType(64u)); -// sym_add(&(p->syms), SF_TYPEDEF, "long", IntType(64u)); + sym_add(&(p->syms), SF_TYPEDEF, "int", IntType(64u)); + sym_add(&(p->syms), SF_TYPEDEF, "uint", UIntType(64u)); sym_add(&(p->syms), SF_TYPEDEF, "float", FloatType(32u)); -// sym_add(&(p->syms), SF_TYPEDEF, "double", FloatType(64u)); sym_add(&(p->syms), SF_TYPEDEF, "string", ArrayOf(sym_get(&(p->syms), "byte")->type, -1)); } diff --git a/source/main.c b/source/main.c index 22bb48d..2640324 100644 --- a/source/main.c +++ b/source/main.c @@ -18,8 +18,7 @@ static int emit_tokens(Parser* ctx, int argc, char **argv) { static int emit_ast(Parser* ctx, int argc, char **argv) { AST* tree = NULL; - while(NULL != (tree = toplevel(ctx))) - pprint_tree(stdout, tree, 0); + toplevel(ctx); return 0; } @@ -48,12 +47,11 @@ int main(int argc, char **argv) { case 'A': Artifact = EOPTARG(usage()); break; default: usage(); } OPTEND; - + /* initialize the parser */ 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(&ctx, argc, argv); diff --git a/source/parser.c b/source/parser.c index 6b37ac1..ba77231 100644 --- a/source/parser.c +++ b/source/parser.c @@ -56,34 +56,33 @@ static void expect(Parser* p, TokType type) { } static Tok* expect_val(Parser* p, TokType type) { - Tok* tok = NULL; + static Tok token = {0}; + /* perform the match */ if (matches(p, type)) { - tok = calloc(1, sizeof(Tok)); - *tok = *(peek(p)); + token = *(peek(p)); p->tok.type = T_NONE; } else { error(p, "Unexpected token"); } - return tok; + return &token; } /* Grammar Definition *****************************************************************************/ -AST* toplevel(Parser* p) { +void toplevel(Parser* p) { if (matches(p, T_REQUIRES)) require_list(p); if (matches(p, T_PROVIDES)) provide_list(p); definition_list(p); - return NULL; } static void require_list(Parser* p) { accept(p, T_REQUIRES); expect(p, '('); while (!matches(p, ')')) { - expect(p, T_STRING); - pkg_add_require(&(p->pkg), "required/module"); + Tok* tok = expect_val(p, T_STRING); + pkg_add_require(&(p->pkg), tok->text); } expect(p, ')'); } @@ -92,8 +91,8 @@ static void provide_list(Parser* p) { accept(p, T_PROVIDES); expect(p, '('); while (!matches(p, ')')) { - expect(p, T_ID); - pkg_add_provide(&(p->pkg), "provided/symbol"); + Tok* tok = expect_val(p, T_ID); + pkg_add_provide(&(p->pkg), tok->text); } expect(p, ')'); } diff --git a/source/pkg.c b/source/pkg.c index 482b81a..5972480 100644 --- a/source/pkg.c +++ b/source/pkg.c @@ -2,10 +2,18 @@ void pkg_add_require(Package* p, char* req) { + Require* rq = malloc(sizeof(Require)); + rq->path = strdup(req); + rq->next = p->requires; + p->requires = rq; } void pkg_add_provide(Package* p, char* exp) { + Provide* pr = malloc(sizeof(Provide)); + pr->name = strdup(exp); + pr->next = p->provides; + p->provides = pr; } void pkg_add_definition(Package* p, AST* ast) diff --git a/source/sclpl.h b/source/sclpl.h index ab1506f..28ed79c 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -166,16 +166,17 @@ typedef struct Provide { char* name; } Provide; -typedef struct File { - struct File* next; - char* name; - SymTable* syms; -} File; +typedef struct Definition { + struct Provide* next; + AST* ast; +} Definition; typedef struct { char* name; SymTable* syms; - File* files; + Require* requires; + Provide* provides; + Definition* definitions; } Package; void pkg_add_require(Package* p, char* req); @@ -209,7 +210,7 @@ typedef struct { void lexfile(Parser* ctx, char* path); void lex(Parser* ctx); void gettoken(Parser* ctx); -AST* toplevel(Parser* p); +void toplevel(Parser* p); void codegen_init(Parser* p); /* Option Parsing -- 2.52.0