DEADSOFTWARE

Добавлен счёт строк
[dsw-obn.git] / src / main.c
index 754d90aa7f216863d6f6337c0c3d41bd3def2afc..9d471e6baa33ed44cddced222fba5c25d81869c8 100644 (file)
@@ -3,7 +3,8 @@
 #include <string.h>
 #include <assert.h>
 
-#include "../include/oberon.h"
+#include "oberon-internals.h"
+#include "generator.h"
 
 struct string_stack
 {
@@ -27,47 +28,61 @@ add_to_stack(struct string_stack ** stack, char * string)
 }
 
 static FILE *
-open_file(struct string_stack * path, const char * ext, const char * name, const char * mode)
+open_file(struct string_stack * path, const char * ext, const char * name, const char * mode, oberon_scanner_t * res)
 {
+       char fname[256];
+       memset(fname, 0, 256);
+
        FILE * fp = NULL;
        while(fp == NULL && path != NULL)
        {
-               char fname[256];
                snprintf(fname, 256, "%s/%s.%s", path -> string, name, ext);
                fp = fopen(fname, mode);
                path = path -> next;
        }
+
+       if(fp == NULL)
+       {
+               snprintf(fname, 256, "%s.%s", name, ext);
+               fp = fopen(fname, mode);
+       }
+
+       size_t len = strlen(fname);
+       res -> source = malloc(len + 1);
+       memcpy(res -> source, fname, len);
+
        return fp;
 }
 
-static const char *
+static oberon_scanner_t *
 import_module(const char * name)
 {
+       oberon_scanner_t * res = malloc(sizeof *res);
+       memset(res, 0, sizeof *res);
+
        FILE * fp;
-       fp = open_file(path_list, "obn", name, "r");
+       fp = open_file(path_list, "obn", name, "r", res);
        if(fp == NULL)
        {
                return NULL;
        }
 
-       char * source;
        size_t len;
 
        fseek(fp, 0, SEEK_END);
        len = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
-       source = calloc(1, len + 1);
-       fread(source, len, 1, fp);
+       res -> code = calloc(1, len + 1);
+       fread(res -> code, len, 1, fp);
 
-       return source;
+       return res;
 }
 
 static void
 init(int argc, char ** argv)
 {
        int i = 1;
-       add_to_stack(&path_list, ".");
        out_path = ".";
 
        while(argc > 2)
@@ -102,12 +117,12 @@ init(int argc, char ** argv)
 int
 main(int argc, char ** argv)
 {
-       const char * code;
+       oberon_scanner_t * s;
 
        init(argc, argv);
 
-       code = import_module(module);
-       if(code == NULL)
+       s = import_module(module);
+       if(s == NULL)
        {
                printf("can't open module source %s\n", module);
                return 0;
@@ -116,7 +131,7 @@ main(int argc, char ** argv)
        ctx = oberon_create_context(import_module);
        oberon_set_out_directory(ctx, out_path);
 
-       mod = oberon_compile_module(ctx, code);
+       mod = oberon_compile_module(ctx, s);
 
        oberon_destroy_context(ctx);