Skip to content

Commit 272a05e

Browse files
authored
Merge pull request #52 from quark-programming/feat-mult-imp
multiple import directories
2 parents b2e412e + 6b1ba13 commit 272a05e

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
#include "parser/keywords.h"
1010
#include "compiler/righthand/declaration/identifier.h"
1111

12-
#define QUARK_VERSION "0.4.0b"
12+
#define QUARK_VERSION "0.4.1"
1313
#define QUARK_STABILITY "untested"
1414

15-
typedef Vector(char*) CStringVector;
16-
1715
FunctionDeclaration* entry_declaration() {
1816
FunctionType* function_type = (void*) new_type((Type) { NodeFunctionType });
1917
Type* const int_type = new_type((Type) { .External = { NodeExternal, .data = String("int") } });
@@ -66,7 +64,7 @@ int main(int argc, char** argv) {
6664
output_file = clarg();
6765
break;
6866
case 'l':
69-
global_library_path = clarg();
67+
push(&global_library_paths, clarg());
7068
break;
7169
case 'i':
7270
push(&include_paths, clarg());
@@ -75,6 +73,8 @@ int main(int argc, char** argv) {
7573
}
7674
}
7775

76+
push(&global_library_paths, ".");
77+
7878
push(&include_paths, "stdint.h");
7979
push(&include_paths, "stdio.h");
8080
push(&include_paths, "string.h");

src/parser/statement/keywords.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,46 @@
77
#include "../type/clash_types.h"
88
#include "../type/types.h"
99

10-
char* global_library_path = ".";
10+
CStringVector global_library_paths = { 0 };
1111

1212
// TODO: (organizational) move some of these functions out of this file
1313

1414
Node* keyword_import(const Token token, Parser* parser) {
15-
String import_path = strf(0, global_library_path);
15+
String sub_path = { 0 };
1616
Trace full_trace = token.trace;
1717

1818
do {
1919
const Trace section = expect(parser->tokenizer, TokenIdentifier).trace;
20-
strf(&import_path, "/%.*s", PRINT(section.source));
20+
strf(&sub_path, "/%.*s", PRINT(section.source));
2121
full_trace = stretch(full_trace, section);
2222
} while(try(parser->tokenizer, TokenDoubleColon, NULL));
2323
expect(parser->tokenizer, ';');
2424

25-
strf(&import_path, ".qk");
26-
push(&import_path, '\0');
25+
strf(&sub_path, ".qk");
2726

28-
char* input_content = fs_readfile(import_path.data);
29-
if(!input_content) {
30-
push(parser->tokenizer->messages,
31-
REPORT_ERR(full_trace, strf(0, "unable to open or read '%.*s'", PRINT(import_path))));
32-
return new_node((Node) { NodeNone });
33-
}
27+
String import_path = { 0 };
28+
for(size_t i = 0; i < global_library_paths.size; i++) {
29+
strf(&import_path, "%s%.*s%c", global_library_paths.data[i], PRINT(sub_path), 0);
30+
char* input_content = fs_readfile(import_path.data);
31+
32+
if(!input_content) {
33+
import_path.size = 0;
34+
continue;
35+
}
3436

35-
Tokenizer import_tokenizer = new_tokenizer(import_path.data, input_content, parser->tokenizer->messages);
36-
Tokenizer* const tokenizer = parser->tokenizer;
37-
parser->tokenizer = &import_tokenizer;
37+
Tokenizer import_tokenizer = new_tokenizer(import_path.data, input_content, parser->tokenizer->messages);
38+
Tokenizer* const tokenizer = parser->tokenizer;
39+
parser->tokenizer = &import_tokenizer;
3840

39-
Scope* scope = new_scope(NULL);
40-
scope->children = collect_until(parser, &statement, 0, 0);
41-
parser->tokenizer = tokenizer;
42-
return (void*) scope;
41+
Scope* scope = new_scope(NULL);
42+
scope->children = collect_until(parser, &statement, 0, 0);
43+
parser->tokenizer = tokenizer;
44+
return (void*) scope;
45+
}
46+
47+
push(parser->tokenizer->messages,
48+
REPORT_ERR(full_trace, strf(0, "unable to open or read '%.*s'", PRINT(import_path))));
49+
return new_node((Node) { NodeNone });
4350
}
4451

4552
Node* keyword_return(const Token token, Parser* parser) {

src/parser/statement/statement.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ NodeVector collect_until(Parser* parser, Node* (*supplier)(Parser*), char separa
88

99
Node* statement(Parser* parser);
1010

11-
extern char* global_library_path;
11+
typedef Vector(char*) CStringVector;
12+
13+
extern CStringVector global_library_paths;
1214

1315
#endif

0 commit comments

Comments
 (0)