6 #include "../include/oberon.h"
11 struct string_stack
* next
;
14 static oberon_context_t
* ctx
;
15 static oberon_module_t
* mod
;
16 static const char * module
;
17 static const char * out_path
;
18 static struct string_stack
* path_list
;
21 add_to_stack(struct string_stack
** stack
, char * string
)
23 struct string_stack
* p
= malloc(sizeof *p
);
30 open_file(struct string_stack
* path
, const char * ext
, const char * name
, const char * mode
)
33 while(fp
== NULL
&& path
!= NULL
)
36 snprintf(fname
, 256, "%s/%s.%s", path
-> string
, name
, ext
);
37 fp
= fopen(fname
, mode
);
44 import_module(const char * name
)
47 fp
= open_file(path_list
, "obn", name
, "r");
56 fseek(fp
, 0, SEEK_END
);
58 fseek(fp
, 0, SEEK_SET
);
60 source
= calloc(1, len
+ 1);
61 fread(source
, len
, 1, fp
);
67 init(int argc
, char ** argv
)
70 add_to_stack(&path_list
, ".");
75 if(argc
> 2 && strcmp(argv
[i
], "-d") == 0)
77 out_path
= argv
[i
+ 1];
81 else if(argc
> 2 && strcmp(argv
[i
], "-I") == 0)
83 add_to_stack(&path_list
, argv
[i
+ 1]);
93 if(argc
!= 2 || argv
[i
][0] == '-')
95 printf("usage: obn [-d out_dir] module\n");
103 main(int argc
, char ** argv
)
109 code
= import_module(module
);
112 printf("can't open module source %s\n", module
);
116 ctx
= oberon_create_context(import_module
);
117 oberon_set_out_directory(ctx
, out_path
);
119 mod
= oberon_compile_module(ctx
, code
);
121 oberon_destroy_context(ctx
);