txlyre 1 天之前
父節點
當前提交
42286abedd
共有 1 個文件被更改,包括 39 次插入4 次删除
  1. 39 4
      qic.c

+ 39 - 4
qic.c

@@ -4378,7 +4378,7 @@ void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, tabl
   compile_node(gbuf, buf, ctx, ltab, lstk, sstk, lbl, n);
 }
 
-char *compile(char *source) {
+char *compile(char *source, list_t *required) {
   list_t *ctx = list_new();
   table_t *ltab = table_new();
   stack_t *lstk = stack_new();
@@ -4390,6 +4390,15 @@ char *compile(char *source) {
   buffer_t *buf = buffer_new();
 
   require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, "std");
+
+  if (required && required->length)
+    for (size_t i = 0; i < required->length; i++)
+      if (require_once(gbuf, buf, ctx, ltab, lstk, sstk, lbl, required->data[i]) < 0) {
+        fprintf(stderr, "fatal: '%s' is not a valid file path or a builtin library name\n", (char *)required->data[i]);
+
+        exit(1);
+      }
+
   compile_into(source, gbuf, buf, ctx, ltab, lstk, sstk, lbl);
 
   buffer_t *rbuf = buffer_new();
@@ -4415,7 +4424,7 @@ char *compile(char *source) {
   return buffer_read(rbuf);
 }
 
-char *compile_file(char *filename, FILE *fd) {
+char *compile_file(char *filename, FILE *fd, list_t *required) {
   buffer_t *buf = buffer_new();
 
   for (;;) {
@@ -4435,7 +4444,7 @@ char *compile_file(char *filename, FILE *fd) {
 
   list_push(FILES, pair);
 
-  char *out = compile(source);
+  char *out = compile(source, required);
 
   list_pop(FILES);
 
@@ -4450,7 +4459,33 @@ int main(int argc, char **argv) {
 
   genmathlib();
 
-  char *out = compile_file("<stdin>", stdin);
+  char *out;
+
+  if (argc < 2)
+    out = compile_file("<stdin>", stdin, NULL);
+  else {
+    char *main = argv[1];
+    list_t *required = NULL;
+
+    if (argc > 2)
+      required = list_new();
+
+    for (size_t i = 2; i < argc; i++)
+      list_push(required, argv[i]);
+
+    FILE *fd = fopen(main, "rb");
+    if (!fd) {
+      fprintf(stderr, "fatal: unable to open file: '%s'\n", main);
+
+      return 1;
+    }
+
+    list_push(REQUIRED, main);
+
+    out = compile_file(main, fd, required);
+
+    fclose(fd);
+  }
 
   fwrite(out, sizeof(char), strlen(out), stdout);