]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
updated parser to record requires and provides
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 18 Mar 2019 02:25:01 +0000 (22:25 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 18 Mar 2019 02:25:01 +0000 (22:25 -0400)
Makefile
source/codegen.c
source/main.c
source/parser.c
source/pkg.c
source/sclpl.h

index 63881f1ac6898481499d92bd28f9788a1d345382..8301555994764677c3d6e81b35a2c859754a0b1f 100644 (file)
--- 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 $@ $<
 
index daa1a3b49ca3ed8894ff6c3f186080d457fe83a4..a625f0937698151e5e1a65b9596582fc9caefaf6 100644 (file)
@@ -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));
 }
index 22bb48d9ee443528dddd6c8453ec511c0c3886f8..2640324b113752a18abe3afe2a8013ae17b867ee 100644 (file)
@@ -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);
index 6b37ac12ccc6b4b1cc4662ff562eeefae0ab143e..ba772319eee56efc50e1b1c1e7c4c0a1e2244271 100644 (file)
@@ -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, ')');
 }
index 482b81a4ce2e35c400cda0c0ea68c7106f59440c..597248098fa06401126ffe720f7a17a77956e2e0 100644 (file)
@@ -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)
index ab1506f4872d7cf6f967f499bc68b905970d28cf..28ed79c1f2f4e7ff0edfb338d2fd6b9bfd9707b2 100644 (file)
@@ -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