shithub: mc

Download patch

ref: 6b0e53468c9078efde8c5daaede91ea70bbd6e1d
parent: a6c7b6c46c1c4e9d8b4f246cdbc82ac5916cfd71
author: Ori Bernstein <[email protected]>
date: Sat Jun 16 10:06:54 EDT 2012

Work towards getting usefiles going.

--- a/8/main.c
+++ b/8/main.c
@@ -13,14 +13,18 @@
 #include "opt.h"
 #include "asm.h"
 
+/* FIXME: move into one place...? */
 Node *file;
-static char *outfile;
 int debug;
+char *outfile;
+char **incpaths;
+size_t nincpaths;
 
 static void usage(char *prog)
 {
     printf("%s [-h] [-o outfile] inputs\n", prog);
     printf("\t-h\tPrint this help\n");
+    printf("\t-I path\tAdd 'path' to use search path\n");
     printf("\t-d\tPrint debug dumps\n");
     printf("\t-o\tOutput to outfile\n");
 }
@@ -31,15 +35,21 @@
     int i;
     Stab *globls;
 
-    while ((opt = getopt(argc, argv, "dho:")) != -1) {
+    while ((opt = getopt(argc, argv, "dho:I:")) != -1) {
         switch (opt) {
             case 'o':
                 outfile = optarg;
                 break;
             case 'h':
+		usage(argv[0]);
+		exit(0);
+		break;
             case 'd':
                 debug++;
                 break;
+	    case 'I':
+		lappend(&incpaths, &nincpaths, optarg);
+		break;
             default:
                 usage(argv[0]);
                 exit(0);
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -87,8 +87,7 @@
 
     /* uses only allowed at top level. Do we want to keep it this way? */
     for (i = 0; i < n->file.nuses; i++)
-        fprintf(stderr, "INTERNAL: implement use loading\n");
-        /* readuse(n->file.uses[i], n->file.globls); */
+        readuse(n->file.uses[i], n->file.globls);
 }
 
 static void settype(Node *n, Type *t)
@@ -143,7 +142,7 @@
     char *s;
     switch (n->type) {
         case Nexpr:     s = opstr(exprop(n)); 	break;
-        case Ndecl:     s = declstr(n); 	break;
+        case Ndecl:     s = declname(n); 	break;
         case Nname:     s = namestr(n); 	break;
         default:        s = nodestr(n->type); 	break;
     }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -261,6 +261,7 @@
 void  die(char *msg, ...);
 void  fatal(int line, char *fmt, ...);
 char *strdupn(char *s, size_t len);
+char *strjoin(char *u, char *v);
 void *memdup(void *mem, size_t len);
 
 /* parsing etc */
@@ -374,3 +375,6 @@
 
 /* Options to control the compilation */
 extern int debug;
+extern char *outfile;
+extern char **incpaths;
+extern size_t nincpaths;
--- a/parse/use.c
+++ b/parse/use.c
@@ -11,13 +11,38 @@
 
 #include "parse.h"
 
+int loaduse(FILE *fd, Stab *st)
+{
+    return 1;
+}
+
 void readuse(Node *use, Stab *st)
 {
-    die("Unimplmented use loading");
+    size_t i;
+    FILE *fd;
+    char *p;
+
+    /* local (quoted) uses are always relative to the cwd */
+    fd = NULL;
+    if (use->use.islocal) {
+	fd = fopen(use->use.name, "r");
+    /* nonlocal (barename) uses are always searched on the include path */
+    } else {
+	for (i = 0; i < nincpaths; i++) {
+	    p = strjoin(incpaths[i], use->use.name);
+	    fd = fopen(p, "r");
+	    if (fd) {
+		free(p);
+		break;
+	    }
+	}
+    }
+
+    if (loaduse(fd, st))
+	die("Could not load usefile %s", use->use.name);
 }
 
 void writeuse(Node *file, FILE *out)
 {
-    die("Unimplemented use writing");
 }
 
--- a/parse/util.c
+++ b/parse/util.c
@@ -86,6 +86,18 @@
     return ret;
 }
 
+char *strjoin(char *u, char *v)
+{
+    size_t n;
+    char *s;
+
+    n = strlen(u) + strlen(v) + 1;
+    s = xalloc(n);
+    strcpy(s, u);
+    strcat(s, v);
+    return s;
+}
+
 void *memdup(void *mem, size_t len)
 {
     void *ret;
--- a/util/Makefile
+++ b/util/Makefile
@@ -1,5 +1,5 @@
 BIN=muse
-OBJ=main.o
+OBJ=muse.o
 
 DEPS=../parse/libparse.a
 
--- a/util/main.c
+++ /dev/null
@@ -1,79 +1,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <ctype.h>
-#include <string.h>
-#include <assert.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "parse.h"
-
-Node *file;
-static char *outfile;
-int debug;
-
-static void usage(char *prog)
-{
-    printf("%s [-h] [-o outfile] inputs\n", prog);
-    printf("\t-h\tPrint this help\n");
-    printf("\t-d\tPrint debug dumps\n");
-    printf("\t-o\tOutput to outfile\n");
-}
-
-int main(int argc, char **argv)
-{
-    int opt;
-    int i;
-    Stab *globls;
-    Node *rdback;
-    FILE *tmp;
-
-    while ((opt = getopt(argc, argv, "dho:")) != -1) {
-        switch (opt) {
-            case 'o':
-                outfile = optarg;
-                break;
-            case 'h':
-            case 'd':
-                debug++;
-                break;
-            default:
-                usage(argv[0]);
-                exit(0);
-                break;
-        }
-    }
-
-    for (i = optind; i < argc; i++) {
-        globls = mkstab();
-        tyinit(globls);
-        tokinit(argv[i]);
-        file = mkfile(argv[i]);
-        file->file.exports = mkstab();
-        file->file.globls = globls;
-        yyparse();
-
-        if (debug) {
-            /* test storing tree to file */
-            tmp = fopen("a.pkl", "w");
-            pickle(file, tmp);
-            fclose(tmp);
-
-            /* and reading it back */
-            tmp = fopen("a.pkl", "r");
-            rdback = unpickle(tmp);
-            dump(rdback, stdout);
-            fclose(tmp);
-
-            /* before we do anything to the parse */
-            dump(file, stdout);
-        }
-        infer(file);
-        die("FIXME: IMPLEMENT ME!");
-    }
-
-    return 0;
-}
--- /dev/null
+++ b/util/muse.c
@@ -1,0 +1,83 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <ctype.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "parse.h"
+
+/* FIXME: move into one place...? */
+Node *file;
+char *outfile;
+int debug;
+char **incpaths;
+size_t nincpaths;
+
+static void usage(char *prog)
+{
+    printf("%s [-h] [-o outfile] inputs\n", prog);
+    printf("\t-h\tPrint this help\n");
+    printf("\t-I path\tAdd 'path' to use search path\n");
+    printf("\t-d\tPrint debug dumps\n");
+    printf("\t-o\tOutput to outfile\n");
+}
+
+int main(int argc, char **argv)
+{
+    int opt;
+    int i;
+    Stab *globls;
+    Node *rdback;
+    FILE *tmp;
+
+    while ((opt = getopt(argc, argv, "dho:")) != -1) {
+        switch (opt) {
+            case 'o':
+                outfile = optarg;
+                break;
+            case 'h':
+            case 'd':
+                debug++;
+                break;
+            default:
+                usage(argv[0]);
+                exit(0);
+                break;
+        }
+    }
+
+    for (i = optind; i < argc; i++) {
+        globls = mkstab();
+        tyinit(globls);
+        tokinit(argv[i]);
+        file = mkfile(argv[i]);
+        file->file.exports = mkstab();
+        file->file.globls = globls;
+        yyparse();
+
+        if (debug) {
+            /* test storing tree to file */
+            tmp = fopen("a.pkl", "w");
+            pickle(file, tmp);
+            fclose(tmp);
+
+            /* and reading it back */
+            tmp = fopen("a.pkl", "r");
+            rdback = unpickle(tmp);
+            dump(rdback, stdout);
+            fclose(tmp);
+
+            /* before we do anything to the parse */
+            dump(file, stdout);
+        }
+        infer(file);
+        die("FIXME: IMPLEMENT ME!");
+    }
+
+    return 0;
+}