Commit b7a148cb2e2738014599e49515bcbf2d685d9316

Thomas de Grivel 2024-07-16T21:10:50

bugfix --load and newlines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
diff --git a/ikc3/ikc3.c b/ikc3/ikc3.c
index bd5b005..6e9be7b 100644
--- a/ikc3/ikc3.c
+++ b/ikc3/ikc3.c
@@ -10,6 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <errno.h>
 #include <string.h>
 #include "../libkc3/kc3.h"
 #include "config.h"
@@ -41,60 +42,20 @@ sw buf_ignore_character (s_buf *buf)
   return csize;
 }
 
-sw ikc3_buf_ignore_spaces (s_buf *out, s_buf *in)
-{
-  character c;
-  sw csize;
-  sw r;
-  sw result = 0;
-  assert(in);
-  assert(out);
-  while ((r = buf_peek_character_utf8(in, &c)) > 0 &&
-         c < UCD_MAX &&
-         g_ucd[c].flags & (UCD_OTHER_CONTROL | UCD_SEPARATOR_SPACE)) {
-    csize = r;
-    if ((r = buf_ignore(in, csize)) != csize)
-      return -1;
-    result += csize;
-    if ((r = buf_flush(out)) < 0)
-      return -1;
-  }
-  if (r < 0)
-    return r;
-  return result;
-}
-
-sw buf_xfer_spaces (s_buf *out, s_buf *in)
-{
-  character c;
-  sw csize;
-  sw r;
-  sw size = 0;
-  assert(in);
-  assert(out);
-  while ((r = buf_peek_character_utf8(in, &c)) > 0 &&
-         c < UCD_MAX &&
-         g_ucd[c].flags & (UCD_OTHER_CONTROL | UCD_SEPARATOR_SPACE)) {
-    csize = r;
-    if ((r = buf_xfer(out, in, csize)) != csize)
-      return -1;
-    size += csize;
-    if ((r = buf_flush(out)) < 0)
-      return -1;
-  }
-  if (r < 0)
-    return r;
-  return size;
-}
-
 sw ikc3_run (void)
 {
   s_tag input;
   sw r;
   s_tag result;
-  while ((r = ikc3_buf_ignore_spaces(&g_kc3_env.out,
-                                     &g_kc3_env.in)) >= 0) {
-    if ((r = buf_parse_tag(&g_kc3_env.in, &input)) > 0) {
+  while (1) {
+    r = buf_ignore_spaces(&g_kc3_env.in);
+    if (r < 0)
+      return 0;
+    r = buf_parse_comments(&g_kc3_env.in);
+    if (r < 0)
+      return 0;
+    r = buf_parse_tag(&g_kc3_env.in, &input);
+    if (r > 0) {
       if (! eval_tag(&input, &result)) {
         tag_clean(&input);
         continue;
@@ -102,25 +63,33 @@ sw ikc3_run (void)
       if (buf_inspect_tag(&g_kc3_env.out, &result) < 0) {
 	tag_clean(&input);
 	tag_clean(&result);
-        break;
+        return 0;
       }
       tag_clean(&input);
       tag_clean(&result);
     }
+    if (r < 0)
+      return 0;
     if ((r = buf_write_1(&g_kc3_env.out, "\n")) < 0)
-      break;
+      return 0;
     if ((r = buf_flush(&g_kc3_env.out)) < 0)
-      break;
+      return 0;
     if (r < 0 ||
         (r == 0 &&
          (r = buf_ignore_character(&g_kc3_env.in)) <= 0))
-      break;
+      return 0;
   }
   return 0;
 }
 
 int main (int argc, char **argv)
 {
+  sw     e = 0;
+  s_env *env;
+  s_tag *file_dir;
+  s_tag  file_dir_save;
+  s_tag *file_path;
+  s_tag  file_path_save;
   FILE *fp = 0;
   char  in_data[BUF_SIZE];
   s_buf in_original;
@@ -129,13 +98,14 @@ int main (int argc, char **argv)
     return usage("ikc3");
   if (! kc3_init(NULL, &argc, &argv))
     return 1;
-  in_original = g_kc3_env.in;
-  buf_init(&g_kc3_env.in, false, sizeof(in_data), in_data);
+  env = &g_kc3_env;
+  in_original = env->in;
+  buf_init(&env->in, false, sizeof(in_data), in_data);
   while (argc) {
     if (! strcmp("--load", *argv) ||
         ! strcmp("-l", *argv)) {
       if (argc < 2) {
-        err_write_1(g_kc3_env.argv[0]);
+        err_write_1(env->argv[0]);
         err_write_1(": ");
         err_write_1(*argv);
         err_write_1(" without an argument\n");
@@ -144,12 +114,36 @@ int main (int argc, char **argv)
         goto clean;
       }
       fp = fopen(argv[1], "rb");
-      if (! buf_file_open_r(&g_kc3_env.in, fp)) {
+      if (! fp) {
+        e = errno;
+        err_write_1("ikc3: ");
+        err_write_1(argv[1]);
+        err_write_1(": ");
+        err_write_1(strerror(e));
+        err_write_1("\n");
+        assert(! "ikc3: fopen");
+        r = 1;
+        goto clean;
+      }
+      if (! buf_file_open_r(&env->in, fp)) {
+        r = -1;
+        goto clean;
+      }
+      file_dir = frame_get_w(&env->global_frame, &g_sym___DIR__);
+      file_dir_save = *file_dir;
+      file_path = frame_get_w(&env->global_frame, &g_sym___FILE__);
+      file_path_save = *file_path;
+      tag_init_str_1(file_path, NULL, argv[1]);
+      file_dir->type = TAG_STR;
+      if (! file_dirname(&file_path->data.str, &file_dir->data.str)) {
+        buf_file_close(&env->in);
         r = -1;
         goto clean;
       }
       r = ikc3_run();
-      buf_file_close(&g_kc3_env.in);
+      *file_dir = file_dir_save;
+      *file_path = file_path_save;
+      buf_file_close(&env->in);
       fclose(fp);
       if (r)
         goto clean;
@@ -164,18 +158,18 @@ int main (int argc, char **argv)
       break;
   }
 #if HAVE_WINEDITLINE
-  buf_wineditline_open_r(&g_kc3_env.in, "ikc3> ", ".ikc3_history");
+  buf_wineditline_open_r(&env->in, "ikc3> ", ".ikc3_history");
 #else
-  buf_linenoise_open_r(&g_kc3_env.in, "ikc3> ", ".ikc3_history");
+  buf_linenoise_open_r(&env->in, "ikc3> ", ".ikc3_history");
 #endif
   r = ikc3_run();
- clean:
 #if HAVE_WINEDITLINE
-  buf_wineditline_close(&g_kc3_env.in, ".ikc3_history");
+  buf_wineditline_close(&env->in, ".ikc3_history");
 #else
-  buf_linenoise_close(&g_kc3_env.in, ".ikc3_history");
+  buf_linenoise_close(&env->in, ".ikc3_history");
 #endif
-  g_kc3_env.in = in_original;
+ clean:
+  env->in = in_original;
   kc3_clean(NULL);
   return r;
 }
diff --git a/kc3s/kc3s.c b/kc3s/kc3s.c
index bac903d..aea9fbd 100644
--- a/kc3s/kc3s.c
+++ b/kc3s/kc3s.c
@@ -90,6 +90,11 @@ sw kc3s_run (void)
 
 int main (int argc, char **argv)
 {
+  s_env *env;
+  s_tag *file_dir;
+  s_tag  file_dir_save;
+  s_tag *file_path;
+  s_tag  file_path_save;
   FILE *fp;
   char  in_data[BUF_SIZE];
   s_buf in_original;
@@ -98,13 +103,14 @@ int main (int argc, char **argv)
     return usage(argv[0]);
   if (! kc3_init(NULL, &argc, &argv))
     return 1;
-  in_original = g_kc3_env.in;
-  buf_init(&g_kc3_env.in, false, sizeof(in_data), in_data);
+  env = &g_kc3_env;
+  in_original = env->in;
+  buf_init(&env->in, false, sizeof(in_data), in_data);
   while (argc) {
     if (! strcmp("--load", *argv) ||
         ! strcmp("-l", *argv)) {
       if (argc < 2) {
-        err_write_1(g_kc3_env.argv[0]);
+        err_write_1(env->argv[0]);
         err_write_1(": ");
         err_write_1(*argv);
         err_write_1(" without an argument\n");
@@ -113,12 +119,25 @@ int main (int argc, char **argv)
         goto clean;
       }
       fp = fopen(argv[1], "rb");
-      if (! buf_file_open_r(&g_kc3_env.in, fp)) {
+      if (! buf_file_open_r(&env->in, fp)) {
+        r = -1;
+        goto clean;
+      }
+      file_dir = frame_get_w(&env->global_frame, &g_sym___DIR__);
+      file_dir_save = *file_dir;
+      file_path = frame_get_w(&env->global_frame, &g_sym___FILE__);
+      file_path_save = *file_path;
+      tag_init_str_1(file_path, NULL, argv[1]);
+      file_dir->type = TAG_STR;
+      if (! file_dirname(&file_path->data.str, &file_dir->data.str)) {
+        buf_file_close(&env->in);
         r = -1;
         goto clean;
       }
       r = kc3s_run();
-      buf_file_close(&g_kc3_env.in);
+      *file_dir = file_dir_save;
+      *file_path = file_path_save;
+      buf_file_close(&env->in);
       fclose(fp);
       if (r)
         goto clean;
@@ -134,7 +153,7 @@ int main (int argc, char **argv)
   }
   r = kc3s_run();
  clean:
-  g_kc3_env.in = in_original;
+  env->in = in_original;
   kc3_clean(NULL);
   return r;
 }
diff --git a/lib/kc3/0.1/ekc3.kc3 b/lib/kc3/0.1/ekc3.kc3
index fd4d79e..59342b6 100644
--- a/lib/kc3/0.1/ekc3.kc3
+++ b/lib/kc3/0.1/ekc3.kc3
@@ -1,6 +1,6 @@
 defmodule EKC3 do
 
-  dlopen(__DIR__ + "libekc3.so")
+  dlopen(__DIR__ + "/libekc3.so")
 
   def render_file = cfn Str "ekc3_render_file" (Str)
 
diff --git a/libkc3/binding.c b/libkc3/binding.c
index 7dd7853..9599ab5 100644
--- a/libkc3/binding.c
+++ b/libkc3/binding.c
@@ -46,6 +46,18 @@ const s_tag * binding_get (const s_binding *binding, const s_sym *name)
   return NULL;
 }
 
+s_tag * binding_get_w (s_binding *binding, const s_sym *name)
+{
+  s_binding *b;
+  b = binding;
+  while (b) {
+    if (b->name == name)
+      return &b->value;
+    b = b->next;
+  }
+  return NULL;
+}
+
 s_binding * binding_init (s_binding *binding, const s_sym *name,
                           const s_tag *value, s_binding *next)
 {
diff --git a/libkc3/binding.h b/libkc3/binding.h
index ebb9268..118871a 100644
--- a/libkc3/binding.h
+++ b/libkc3/binding.h
@@ -15,22 +15,24 @@
 
 #include "types.h"
 
-/* stack-allocation compatible functions */
+/* Stack-allocation compatible functions, call binding_clean after
+   use. */
 void        binding_clean (s_binding *binding);
 s_binding * binding_init (s_binding *binding, const s_sym *name,
                           const s_tag *value, s_binding *next);
 
-/* constructors */
-s_binding * binding_new (const s_sym *name, const s_tag *value,
-                         s_binding *next);
-
-/* destructors */
+/* Heap-allocation functions, call binding_delete* after use. */
 s_binding * binding_delete (s_binding *binding);
 void        binding_delete_all (s_binding *binding);
+s_binding * binding_new (const s_sym *name, const s_tag *value,
+                         s_binding *next);
 
-/* observers */
+/* Observers. */
 const s_tag * binding_get (const s_binding *binding, const s_sym *name);
 const s_tag * binding_is_bound (const s_binding *binding,
                                 const s_sym *name);
 
+/* Operators. */
+s_tag * binding_get_w (s_binding *binding, const s_sym *name);
+
 #endif /* LIBC3_BINDING_H */
diff --git a/libkc3/buf.c3 b/libkc3/buf.c3
deleted file mode 100644
index f2b80bc..0000000
--- a/libkc3/buf.c3
+++ /dev/null
@@ -1,8 +0,0 @@
-defmodule Buf do
-  defstruct do
-    Str str
-    u64 rpos
-    u64 wpos
-    sw flush (Buf buf)
-    sw refill (Buf buf)
-    
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 1bf9782..004d338 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -1187,12 +1187,16 @@ sw buf_parse_comment (s_buf *buf)
     if ((r = buf_read_1(buf, "#")) <= 0)
       goto restore;
     result += r1 + r;
-    while ((r = buf_peek_character_utf8(buf, &c)) > 0 &&
-           c != '\n') {
+    while (1) {
+      r = buf_peek_character_utf8(buf, &c);
+      if (r <= 0)
+        break;
       csize = r;
       if ((r = buf_ignore(buf, csize)) < 0)
         goto clean;
       result += csize;
+      if (c == '\n')
+        break;
     }
     if (r < 0)
       goto clean;
diff --git a/libkc3/env.c b/libkc3/env.c
index 6896e84..9c2eaea 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -13,8 +13,8 @@
 #include <string.h>
 #include <unistd.h>
 #include "alloc.h"
-#include "assert.h"
 #include "array.h"
+#include "assert.h"
 #include "binding.h"
 #include "block.h"
 #include "buf.h"
@@ -23,7 +23,6 @@
 #include "buf_inspect.h"
 #include "buf_parse.h"
 #include "buf_save.h"
-#include "kc3_main.h"
 #include "call.h"
 #include "cfn.h"
 #include "compare.h"
@@ -43,21 +42,25 @@
 #include "fn_clause.h"
 #include "frame.h"
 #include "ident.h"
+#include "integer.h"
 #include "io.h"
+#include "kc3_main.h"
 #include "list.h"
 #include "map.h"
 #include "module.h"
 #include "str.h"
 #include "struct.h"
 #include "struct_type.h"
+#include "sym.h"
 #include "tag.h"
 #include "tag_init.h"
 #include "tuple.h"
-#include "integer.h"
 
 s_env g_kc3_env;
 
 static s_env * env_init_args (s_env *env, int *argc, char ***argv);
+static s_env * env_init_globals (s_env *env);
+static s_env * env_init_toplevel (s_env *env);
 
 bool env_call_get (s_env *env, s_call *call)
 {
@@ -1946,8 +1949,10 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
     return NULL;
   sym_init_g_sym();
   env->error_handler = NULL;
-  env->frame = frame_new(NULL); // toplevel
-  frame_init(&env->global_frame, NULL); // globals
+  if (! env_init_toplevel(env))
+    return NULL;
+  if (! env_init_globals(env))
+    return NULL;
   buf_init_alloc(&env->in, BUF_SIZE);
   buf_file_open_r(&env->in, stdin);
   buf_init_alloc(&env->out, BUF_SIZE);
@@ -1956,18 +1961,18 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
   buf_file_open_w(&env->err, stderr);
   facts_init(&env->facts);
   env->path = list_new_str_1
-    (NULL, ".", list_new_str_1
-     (NULL, "..", list_new_str_1
-      (NULL, "../Resources", list_new_str_1
-       (NULL, "../..", list_new_str_1
-        (NULL, "../../..", list_new_str_1
-         (NULL, "../../../..", list_new_str_1
-          (NULL, "../../../../..", list_new_str_1
-           (NULL, "../../../../../..", NULL))))))));
+    (NULL, "./", list_new_str_1
+     (NULL, "../", list_new_str_1
+      (NULL, "../Resources/", list_new_str_1
+       (NULL, "../../", list_new_str_1
+        (NULL, "../../../", list_new_str_1
+         (NULL, "../../../../", list_new_str_1
+          (NULL, "../../../../../", list_new_str_1
+           (NULL, "../../../../../../", NULL))))))));
   str_init_1(&path, NULL, "lib/kc3/0.1/");
   if (! file_search(&path, &g_sym_x, &env->module_path)) {
-    err_puts("env_init: module_path not found");
-    assert(! "env_init: module path not found");
+    err_puts("env_init: lib/kc3/0.1 not found in module path");
+    assert(! "env_init: lib/kc3/0.1 not found in module path");
     return NULL;
   }
   env->current_defmodule = &g_sym_KC3;
@@ -2012,6 +2017,30 @@ s_env * env_init_args (s_env *env, int *argc, char ***argv)
   return env;
 }
 
+s_env * env_init_globals (s_env *env)
+{
+  if (! frame_init(&env->global_frame, NULL))
+    return NULL;
+  if (! tag_init_str(&env->file_dir, NULL, env->argv0_dir.size,
+                     env->argv0_dir.ptr.pchar))
+    return NULL;
+  if (! frame_binding_new(&env->global_frame, &g_sym___DIR__,
+                          &env->file_dir))
+    return NULL;
+  if (! tag_init_str_1(&env->file_path, NULL, env->argv[0]))
+    return NULL;
+  if (! frame_binding_new(&env->global_frame, &g_sym___FILE__,
+                          &env->file_path))
+    return NULL;
+  return env;
+}
+
+s_env * env_init_toplevel (s_env *env)
+{
+  env->frame = frame_new(NULL);
+  return env;
+}
+
 s_tag * env_let (s_env *env, const s_tag *tag, const s_block *block,
                  s_tag *dest)
 {
@@ -2069,6 +2098,10 @@ s_tag * env_let (s_env *env, const s_tag *tag, const s_block *block,
 bool env_load (s_env *env, const s_str *path)
 {
   s_buf buf;
+  s_tag dir;
+  s_tag env_file_dir;
+  s_tag env_file_path;
+  s_tag path_tag;
   sw r;
   s_tag tag = {0};
   s_tag tmp;
@@ -2080,6 +2113,16 @@ bool env_load (s_env *env, const s_str *path)
     buf_clean(&buf);
     return false;
   }
+  dir.type = TAG_STR;
+  if (! file_dirname(path, &dir.data.str)) {
+    buf_clean(&buf);
+    return false;
+  }
+  tag_init_str(&path_tag, NULL, path->size, path->ptr.pchar);
+  env_file_dir = env->file_dir;
+  env_file_path = env->file_path;
+  env->file_dir = dir;
+  env->file_path = path_tag;
   while (1) {
     if ((r = buf_parse_tag(&buf, &tag)) < 0) {
       buf_getc_close(&buf);
@@ -2099,6 +2142,8 @@ bool env_load (s_env *env, const s_str *path)
   }
   buf_getc_close(&buf);
   buf_clean(&buf);
+  env->file_dir = env_file_dir;
+  env->file_path = env_file_path;
   return true;
 }
 
diff --git a/libkc3/file.c b/libkc3/file.c
index 8b490d5..d2348d0 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -118,7 +118,7 @@ s_str * file_dirname (const s_str *path, s_str *dest)
     return str_init(dest, NULL, 2, "./");
   if (! dirsep_pos)
     return str_init(dest, NULL, 1, "/");
-  return str_init_slice(dest, path, 0, dirsep_pos + 1);
+  return str_init_slice(dest, path, 0, dirsep_pos);
 }
 
 s_tag * file_mtime (const s_str *path, s_tag *dest)
diff --git a/libkc3/frame.c b/libkc3/frame.c
index a917c33..7107db8 100644
--- a/libkc3/frame.c
+++ b/libkc3/frame.c
@@ -69,6 +69,21 @@ const s_tag * frame_get (const s_frame *frame, const s_sym *sym)
   return NULL;
 }
 
+s_tag * frame_get_w (s_frame *frame, const s_sym *sym)
+{
+  s_frame *f;
+  s_tag *result;
+  assert(sym);
+  f = frame;
+  while (f) {
+    result = binding_get_w(f->bindings, sym);
+    if (result)
+      return result;
+    f = f->next;
+  }
+  return NULL;
+}
+
 s_frame * frame_init (s_frame *frame, s_frame *next)
 {
   s_frame tmp = {0};
diff --git a/libkc3/frame.h b/libkc3/frame.h
index 18f6774..9fa6925 100644
--- a/libkc3/frame.h
+++ b/libkc3/frame.h
@@ -15,22 +15,23 @@
 
 #include "types.h"
 
-/* stack-allocation compatible functions */
+/* Stack-allocation compatible functions, call frame_clean after use. */
 s_frame * frame_clean (s_frame *frame);
 s_frame * frame_init (s_frame *frame, s_frame *next);
 
-/* constructors */
+/* Constructors. */
 s_frame * frame_new (s_frame *next);
 
-/* destructors */
+/* Destructors. */
 s_frame * frame_delete (s_frame *frame);
 void      frame_delete_all (s_frame *frame);
 
-/* modifiers */
+/* Operators. */
 s_frame * frame_binding_new (s_frame *frame, const s_sym *name,
-                             const s_tag *value);
+                                 const s_tag *value);
+s_tag *   frame_get_w (s_frame *frame, const s_sym *sym);
 
-/* observers */
+/* Observers. */
 const s_tag * frame_get (const s_frame *frame, const s_sym *sym);
 
 #endif /* LIBKC3_FRAME_H */
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index a2c718c..019a96c 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -108,8 +108,16 @@ void ** kc3_dlopen (const s_str *path, void **dest)
 {
   assert(path);
   assert(dest);
-  printf("dlopen %p := %s\n", (void *) dest, path->ptr.pchar);
+  err_write_1("kc3_dlopen: ");
+  err_inspect_str(path);
+  err_write_1("\n");
   *dest = dlopen(path->ptr.pchar, RTLD_GLOBAL);
+  if (! *dest) {
+    err_write_1("kc3_dlopen: ");
+    err_inspect_str(path);
+    err_puts(": dlopen failed");
+    assert(! "kc3_dlopen: dlopen failed");
+  }
   return dest;
 }
 
diff --git a/libkc3/kc3.h b/libkc3/kc3.h
index 2994628..0d6242e 100644
--- a/libkc3/kc3.h
+++ b/libkc3/kc3.h
@@ -62,6 +62,7 @@
 #include "file.h"
 #include "fn.h"
 #include "fn_clause.h"
+#include "frame.h"
 #include "hash.h"
 #include "ident.h"
 #include "integer.h"
diff --git a/libkc3/sym.c b/libkc3/sym.c
index a93f893..fdecba8 100644
--- a/libkc3/sym.c
+++ b/libkc3/sym.c
@@ -22,6 +22,8 @@
 #include "sym.h"
 #include "tag_type.h"
 
+const s_sym g_sym___DIR__         = {{{NULL},  7, {"__DIR__"}}};
+const s_sym g_sym___FILE__        = {{{NULL},  8, {"__FILE__"}}};
 const s_sym g_sym__brackets       = {{{NULL},  2, {"[]"}}};
 const s_sym g_sym__equal          = {{{NULL},  1, {"="}}};
 const s_sym g_sym__paren          = {{{NULL},  2, {"()"}}};
@@ -315,6 +317,8 @@ const s_sym ** sym_init_copy (const s_sym **sym,
 
 void sym_init_g_sym (void)
 {
+  sym_register(&g_sym___DIR__, NULL);
+  sym_register(&g_sym___FILE__, NULL);
   sym_register(&g_sym__brackets, NULL);
   sym_register(&g_sym__equal, NULL);
   sym_register(&g_sym__paren, NULL);
diff --git a/libkc3/sym.h b/libkc3/sym.h
index 1c3374e..0c8d3c6 100644
--- a/libkc3/sym.h
+++ b/libkc3/sym.h
@@ -26,6 +26,8 @@
 
 #define SYM_MAX 1024
 
+extern const s_sym g_sym___DIR__;
+extern const s_sym g_sym___FILE__;
 extern const s_sym g_sym__brackets;
 extern const s_sym g_sym__equal;
 extern const s_sym g_sym__paren;
diff --git a/libkc3/types.h b/libkc3/types.h
index 11076e4..3c0910c 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -652,6 +652,8 @@ struct env {
   s_buf             err;
   s_error_handler  *error_handler;
   s_facts           facts;
+  s_tag             file_dir;
+  s_tag             file_path;
   s_frame          *frame;
   s_frame           global_frame;
   s_buf             in;
diff --git a/test/ikc3/comment.in b/test/ikc3/comment.in
index bddc50e..31884f7 100644
--- a/test/ikc3/comment.in
+++ b/test/ikc3/comment.in
@@ -12,4 +12,4 @@
 "c"
 # 9
 # 10
-# 11
\ No newline at end of file
+# 11
diff --git a/test/ikc3/globals.in b/test/ikc3/globals.in
new file mode 100644
index 0000000..c6e92bb
--- /dev/null
+++ b/test/ikc3/globals.in
@@ -0,0 +1,4 @@
+quote __DIR__
+__DIR__
+quote __FILE__
+__FILE__
diff --git a/test/ikc3/globals.out.expected b/test/ikc3/globals.out.expected
new file mode 100644
index 0000000..4bb1292
--- /dev/null
+++ b/test/ikc3/globals.out.expected
@@ -0,0 +1,4 @@
+__DIR__
+"./"
+__FILE__
+"globals.in"
diff --git a/test/ikc3/globals.ret.expected b/test/ikc3/globals.ret.expected
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/ikc3/globals.ret.expected
@@ -0,0 +1 @@
+0
diff --git a/test/ikc3_test b/test/ikc3_test
index 99fbef3..564a79c 100755
--- a/test/ikc3_test
+++ b/test/ikc3_test
@@ -40,7 +40,7 @@ fi
 
 for TARGET in $TARGETS; do
     RESULT=test_ok
-    $IKC3 < ${TARGET}.in > ${TARGET}.out 2>&1
+    $IKC3 --load ${TARGET}.in --quit > ${TARGET}.out 2>&1
     echo $? > ${TARGET}.ret
     if ! diff -abu ${TARGET}.out.expected ${TARGET}.out > ${TARGET}.diff
     then