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 "../include/oberon.h"
8 static oberon_context_t * ctx;
9 static oberon_module_t * mod;
10 static const char * module;
11 static const char * out_path;
13 static const char *
14 import_module(const char * name)
15 {
16 assert(name);
18 FILE * fp;
19 char fname[256];
20 snprintf(fname, 256, "%s.obn", name);
21 fp = fopen(fname, "r");
22 if(fp == NULL)
23 {
24 printf("can't open file %s\n", fname);
25 exit(1);
26 return NULL;
27 }
29 char * source;
30 size_t len;
32 fseek(fp, 0, SEEK_END);
33 len = ftell(fp);
34 fseek(fp, 0, SEEK_SET);
36 source = calloc(1, len + 1);
37 fread(source, len, 1, fp);
39 return source;
40 }
42 static void
43 init(int argc, char ** argv)
44 {
45 int i = 1;
46 out_path = ".";
48 if(argc > 2 && strcmp(argv[i], "-d") == 0)
49 {
50 out_path = argv[i + 1];
51 argc -= 2;
52 i += 2;
53 }
55 if(argc != 2 || argv[i][0] == '-')
56 {
57 printf("usage: obn [-d out_dir] module\n");
58 exit(1);
59 }
61 module = argv[i];
62 }
64 int
65 main(int argc, char ** argv)
66 {
67 const char * code;
69 init(argc, argv);
71 code = import_module(module);
73 ctx = oberon_create_context(import_module);
74 oberon_set_out_directory(ctx, out_path);
76 mod = oberon_compile_module(ctx, code);
78 oberon_destroy_context(ctx);
80 return 0;
81 }