DEADSOFTWARE

Добавлены строки в отладочную информацию класса (быстрохак)
[dsw-obn.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
6 #include "oberon-internals.h"
7 #include "generator.h"
9 struct string_stack
10 {
11 char * string;
12 struct string_stack * next;
13 };
15 static oberon_context_t * ctx;
16 static oberon_module_t * mod;
17 static const char * module;
18 static const char * out_path;
19 static struct string_stack * path_list;
21 static void
22 add_to_stack(struct string_stack ** stack, char * string)
23 {
24 struct string_stack * p = malloc(sizeof *p);
25 p -> string = string;
26 p -> next = *stack;
27 *stack = p;
28 }
30 static FILE *
31 open_file(struct string_stack * path, const char * ext, const char * name, const char * mode, oberon_scanner_t * res)
32 {
33 char fname[256];
34 memset(fname, 0, 256);
36 FILE * fp = NULL;
37 while(fp == NULL && path != NULL)
38 {
39 snprintf(fname, 256, "%s/%s.%s", path -> string, name, ext);
40 fp = fopen(fname, mode);
41 path = path -> next;
42 }
44 if(fp == NULL)
45 {
46 snprintf(fname, 256, "%s.%s", name, ext);
47 fp = fopen(fname, mode);
48 }
50 size_t len = strlen(fname);
51 res -> source = malloc(len + 1);
52 memcpy(res -> source, fname, len + 1);
54 return fp;
55 }
57 static oberon_scanner_t *
58 import_module(const char * name)
59 {
60 oberon_scanner_t * res = malloc(sizeof *res);
61 memset(res, 0, sizeof *res);
63 FILE * fp;
64 fp = open_file(path_list, "obn", name, "r", res);
65 if(fp == NULL)
66 {
67 return NULL;
68 }
70 size_t len;
72 fseek(fp, 0, SEEK_END);
73 len = ftell(fp);
74 fseek(fp, 0, SEEK_SET);
76 res -> code = calloc(1, len + 1);
77 fread(res -> code, len, 1, fp);
79 return res;
80 }
82 static void
83 init(int argc, char ** argv)
84 {
85 int i = 1;
86 out_path = ".";
88 while(argc > 2)
89 {
90 if(argc > 2 && strcmp(argv[i], "-d") == 0)
91 {
92 out_path = argv[i + 1];
93 argc -= 2;
94 i += 2;
95 }
96 else if(argc > 2 && strcmp(argv[i], "-I") == 0)
97 {
98 add_to_stack(&path_list, argv[i + 1]);
99 argc -= 2;
100 i += 2;
102 else
104 break;
108 if(argc != 2 || argv[i][0] == '-')
110 printf("usage: obn [-d out_dir] module\n");
111 exit(1);
114 module = argv[i];
117 int
118 main(int argc, char ** argv)
120 oberon_scanner_t * s;
122 init(argc, argv);
124 s = import_module(module);
125 if(s == NULL)
127 printf("can't open module source %s\n", module);
128 return 0;
131 ctx = oberon_create_context(import_module);
132 oberon_set_out_directory(ctx, out_path);
134 mod = oberon_compile_module(ctx, s);
136 oberon_destroy_context(ctx);
138 return 0;