4 * Copyright (C) 2007-2011 David Lutterkort
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <dlutter@redhat.com>
31 #include <sys/types.h>
38 #include "transform.h"
41 /* Extension of source files */
42 #define AUG_EXT ".aug"
44 #define LNS_TYPE_CHECK(ctx) ((ctx)->aug->flags & AUG_TYPE_CHECK)
46 static const char *const builtin_module = "Builtin";
48 static const struct type string_type = { .ref = UINT_MAX, .tag = T_STRING };
49 static const struct type regexp_type = { .ref = UINT_MAX, .tag = T_REGEXP };
50 static const struct type lens_type = { .ref = UINT_MAX, .tag = T_LENS };
51 static const struct type tree_type = { .ref = UINT_MAX, .tag = T_TREE };
52 static const struct type filter_type = { .ref = UINT_MAX, .tag = T_FILTER };
53 static const struct type transform_type =
54 { .ref = UINT_MAX, .tag = T_TRANSFORM };
55 static const struct type unit_type = { .ref = UINT_MAX, .tag = T_UNIT };
57 const struct type *const t_string = &string_type;
58 const struct type *const t_regexp = ®exp_type;
59 const struct type *const t_lens = &lens_type;
60 const struct type *const t_tree = &tree_type;
61 const struct type *const t_filter = &filter_type;
62 const struct type *const t_transform = &transform_type;
63 const struct type *const t_unit = &unit_type;
65 static const char *const type_names[] = {
66 "string", "regexp", "lens", "tree", "filter",
67 "transform", "function", "unit", NULL
70 /* The anonymous identifier which we will never bind */
71 static const char const anon_ident[] = "_";
73 static void print_value(FILE *out, struct value *v);
75 /* The evaluation context with all loaded modules and the bindings for the
76 * module we are working on in LOCAL
79 const char *name; /* The module we are working on */
81 struct binding *local;
84 static void format_error(struct info *info, aug_errcode_t code,
85 const char *format, va_list ap) {
86 struct error *error = info->error;
87 char *si = NULL, *sf = NULL, *sd = NULL;
91 /* Only syntax errors are cumulative */
92 if (code != AUG_ESYNTAX)
95 si = format_info(info);
96 r = vasprintf(&sf, format, ap);
99 if (error->details != NULL) {
100 r = xasprintf(&sd, "%s\n%s%s", error->details,
101 (si == NULL) ? "(no location)" : si,
102 (sf == NULL) ? "(no details)" : sf);
104 r = xasprintf(&sd, "%s%s",
105 (si == NULL) ? "(no location)" : si,
106 (sf == NULL) ? "(no details)" : sf);
109 free(error->details);
116 void syntax_error(struct info *info, const char *format, ...) {
117 struct error *error = info->error;
120 if (error->code != AUG_NOERROR && error->code != AUG_ESYNTAX)
123 va_start(ap, format);
124 format_error(info, AUG_ESYNTAX, format, ap);
128 void fatal_error(struct info *info, const char *format, ...) {
129 struct error *error = info->error;
132 if (error->code == AUG_EINTERNAL)
135 va_start(ap, format);
136 format_error(info, AUG_EINTERNAL, format, ap);
140 static void free_param(struct param *param) {
143 assert(param->ref == 0);
144 unref(param->info, info);
145 unref(param->name, string);
146 unref(param->type, type);
150 void free_term(struct term *term) {
153 assert(term->ref == 0);
157 free(term->autoload);
158 unref(term->decls, term);
162 unref(term->exp, term);
170 unref(term->left, term);
171 unref(term->right, term);
174 unref(term->value, value);
177 unref(term->ident, string);
180 unref(term->brexp, term);
183 unref(term->param, param);
184 unref(term->body, term);
187 unref(term->rexp, term);
190 unref(term->test, term);
191 unref(term->result, term);
197 unref(term->next, term);
198 unref(term->info, info);
199 unref(term->type, type);
203 static void free_binding(struct binding *binding) {
206 assert(binding->ref == 0);
207 unref(binding->next, binding);
208 unref(binding->ident, string);
209 unref(binding->type, type);
210 unref(binding->value, value);
214 void free_module(struct module *module) {
217 assert(module->ref == 0);
219 unref(module->next, module);
220 unref(module->bindings, binding);
221 unref(module->autoload, transform);
225 void free_type(struct type *type) {
228 assert(type->ref == 0);
230 if (type->tag == T_ARROW) {
231 unref(type->dom, type);
232 unref(type->img, type);
237 static void free_exn(struct exn *exn) {
241 unref(exn->info, info);
243 for (int i=0; i < exn->nlines; i++) {
250 void free_value(struct value *v) {
257 unref(v->string, string);
260 unref(v->regexp, regexp);
263 unref(v->lens, lens);
266 free_tree(v->origin);
269 unref(v->filter, filter);
272 unref(v->transform, transform);
276 unref(v->native->type, type);
280 unref(v->func, term);
281 unref(v->bindings, binding);
291 unref(v->info, info);
296 * Creation of (some) terms. Others are in parser.y
297 * Refernce counted arguments are now owned by the returned object, i.e.
298 * the make_* functions do not increment the count.
299 * Returned objects have a referece count of 1.
301 struct term *make_term(enum term_tag tag, struct info *info) {
303 if (make_ref(term) < 0) {
312 struct term *make_param(char *name, struct type *type, struct info *info) {
313 struct term *term = make_term(A_FUNC, info);
316 make_ref_err(term->param);
317 term->param->info = ref(term->info);
318 make_ref_err(term->param->name);
319 term->param->name->str = name;
320 term->param->type = type;
327 struct value *make_value(enum value_tag tag, struct info *info) {
328 struct value *value = NULL;
329 if (make_ref(value) < 0) {
338 struct value *make_unit(struct info *info) {
339 return make_value(V_UNIT, info);
342 struct term *make_app_term(struct term *lambda, struct term *arg,
344 struct term *app = make_term(A_APP, info);
355 struct term *make_app_ident(char *id, struct term *arg, struct info *info) {
356 struct term *ident = make_term(A_IDENT, ref(info));
357 ident->ident = make_string(id);
358 if (ident->ident == NULL) {
364 return make_app_term(ident, arg, info);
367 struct term *build_func(struct term *params, struct term *exp) {
368 assert(params->tag == A_FUNC);
369 if (params->next != NULL)
370 exp = build_func(params->next, exp);
377 /* Ownership is taken as needed */
378 static struct value *make_closure(struct term *func, struct binding *bnds) {
379 struct value *v = NULL;
380 if (make_ref(v) == 0) {
382 v->info = ref(func->info);
384 v->bindings = ref(bnds);
389 struct value *make_exn_value(struct info *info,
390 const char *format, ...) {
396 va_start(ap, format);
397 r = vasprintf(&message, format, ap);
402 v = make_value(V_EXN, ref(info));
405 v->exn->message = message;
410 void exn_add_lines(struct value *v, int nlines, ...) {
411 assert(v->tag == V_EXN);
414 if (REALLOC_N(v->exn->lines, v->exn->nlines + nlines) == -1)
416 va_start(ap, nlines);
417 for (int i=0; i < nlines; i++) {
418 char *line = va_arg(ap, char *);
419 v->exn->lines[v->exn->nlines + i] = line;
422 v->exn->nlines += nlines;
425 void exn_printf_line(struct value *exn, const char *format, ...) {
430 va_start(ap, format);
431 r = vasprintf(&line, format, ap);
434 exn_add_lines(exn, 1, line);
437 struct value *exn_error(void) {
438 static const struct exn exn = {
439 .info = NULL, .seen = 1, .error = 1,
440 .message = (char *) "Error during evaluation",
441 .nlines = 0, .lines = NULL };
442 static const struct value value = {
443 .ref = REF_MAX, /* Protect against being freed */
444 .info = NULL, .tag = V_EXN,
445 { .exn = (struct exn *) &exn } };
446 return (struct value *) &value;
452 static int load_module(struct augeas *aug, const char *name);
453 static char *module_basename(const char *modname);
455 struct module *module_create(const char *name) {
456 struct module *module;
458 module->name = strdup(name);
462 static struct module *module_find(struct module *module, const char *name) {
463 list_for_each(e, module) {
464 if (STRCASEEQ(e->name, name))
470 static struct binding *bnd_lookup(struct binding *bindings, const char *name) {
471 list_for_each(b, bindings) {
472 if (STREQ(b->ident->str, name))
478 static char *modname_of_qname(const char *qname) {
479 char *dot = strchr(qname, '.');
483 return strndup(qname, dot - qname);
486 static int lookup_internal(struct augeas *aug, const char *ctx_modname,
487 const char *name, struct binding **bnd) {
488 char *modname = modname_of_qname(name);
492 if (modname == NULL) {
493 struct module *builtin =
494 module_find(aug->modules, builtin_module);
495 assert(builtin != NULL);
496 *bnd = bnd_lookup(builtin->bindings, name);
501 list_for_each(module, aug->modules) {
502 if (STRCASEEQ(module->name, modname)) {
503 *bnd = bnd_lookup(module->bindings, name + strlen(modname) + 1);
508 /* Try to load the module */
509 if (streqv(modname, ctx_modname)) {
513 int loaded = load_module(aug, modname) == 0;
521 struct lens *lens_lookup(struct augeas *aug, const char *qname) {
522 struct binding *bnd = NULL;
524 if (lookup_internal(aug, NULL, qname, &bnd) < 0)
526 if (bnd == NULL || bnd->value->tag != V_LENS)
528 return bnd->value->lens;
531 static struct binding *ctx_lookup_bnd(struct info *info,
532 struct ctx *ctx, const char *name) {
533 struct binding *b = NULL;
534 int nlen = strlen(ctx->name);
536 if (STREQLEN(ctx->name, name, nlen) && name[nlen] == '.')
539 b = bnd_lookup(ctx->local, name);
543 if (ctx->aug != NULL) {
545 r = lookup_internal(ctx->aug, ctx->name, name, &b);
548 char *modname = modname_of_qname(name);
549 syntax_error(info, "Could not load module %s for %s",
557 static struct value *ctx_lookup(struct info *info,
558 struct ctx *ctx, struct string *ident) {
559 struct binding *b = ctx_lookup_bnd(info, ctx, ident->str);
560 return b == NULL ? NULL : b->value;
563 static struct type *ctx_lookup_type(struct info *info,
564 struct ctx *ctx, struct string *ident) {
565 struct binding *b = ctx_lookup_bnd(info, ctx, ident->str);
566 return b == NULL ? NULL : b->type;
569 /* Takes ownership as needed */
570 static struct binding *bind_type(struct binding **bnds,
571 const char *name, struct type *type) {
572 struct binding *binding;
574 if (STREQ(name, anon_ident))
577 make_ref(binding->ident);
578 binding->ident->str = strdup(name);
579 binding->type = ref(type);
580 list_cons(*bnds, binding);
585 /* Takes ownership as needed */
586 static void bind_param(struct binding **bnds, struct param *param,
590 b->ident = ref(param->name);
591 b->type = ref(param->type);
597 static void unbind_param(struct binding **bnds, ATTRIBUTE_UNUSED struct param *param) {
598 struct binding *b = *bnds;
599 assert(b->ident == param->name);
600 assert(b->next != *bnds);
605 /* Takes ownership of VALUE */
606 static void bind(struct binding **bnds,
607 const char *name, struct type *type, struct value *value) {
608 struct binding *b = NULL;
610 if (STRNEQ(name, anon_ident)) {
611 b = bind_type(bnds, name, type);
612 b->value = ref(value);
617 * Some debug printing
620 static char *type_string(struct type *t);
622 static void dump_bindings(struct binding *bnds) {
623 list_for_each(b, bnds) {
624 char *st = type_string(b->type);
625 fprintf(stderr, " %s: %s", b->ident->str, st);
626 fprintf(stderr, " = ");
627 print_value(stderr, b->value);
633 static void dump_module(struct module *module) {
636 fprintf(stderr, "Module %s\n:", module->name);
637 dump_bindings(module->bindings);
638 dump_module(module->next);
642 static void dump_ctx(struct ctx *ctx) {
643 fprintf(stderr, "Context: %s\n", ctx->name);
644 dump_bindings(ctx->local);
645 if (ctx->aug != NULL) {
646 list_for_each(m, ctx->aug->modules)
654 static void print_tree(FILE *out, int indent, struct tree *tree) {
656 fprintf(out, "(null tree)\n");
659 list_for_each(t, tree) {
660 for (int i=0; i < indent; i++) fputc(' ', out);
662 if (t->label != NULL)
663 fprintf(out, "\"%s\"", t->label);
664 if (t->value != NULL)
665 fprintf(out, " = \"%s\"", t->value);
666 if (t->children != NULL) {
668 print_tree(out, indent + 2, t->children);
669 for (int i=0; i < indent; i++) fputc(' ', out);
677 static void print_value(FILE *out, struct value *v) {
679 fprintf(out, "<null>");
685 fprintf(out, "\"%s\"", v->string->str);
688 fprintf(out, "/%s/", v->regexp->pattern->str);
691 fprintf(out, "<lens:");
692 print_info(out, v->lens->info);
696 print_tree(out, 0, v->origin);
699 fprintf(out, "<filter:");
700 list_for_each(f, v->filter) {
701 fprintf(out, "%c%s%c", f->include ? '+' : '-', f->glob->str,
702 (f->next != NULL) ? ':' : '>');
706 fprintf(out, "<transform:");
707 print_info(out, v->transform->lens->info);
711 fprintf(out, "<native:");
712 print_info(out, v->info);
716 fprintf(out, "<closure:");
717 print_info(out, v->func->info);
721 if (! v->exn->seen) {
722 print_info(out, v->exn->info);
723 fprintf(out, "exception: %s\n", v->exn->message);
724 for (int i=0; i < v->exn->nlines; i++) {
725 fprintf(out, " %s\n", v->exn->lines[i]);
739 static int value_equal(struct value *v1, struct value *v2) {
740 if (v1 == NULL && v2 == NULL)
742 if (v1 == NULL || v2 == NULL)
744 if (v1->tag != v2->tag)
748 return STREQ(v1->string->str, v2->string->str);
751 // FIXME: Should probably build FA's and compare them
752 return STREQ(v1->regexp->pattern->str, v2->regexp->pattern->str);
755 return v1->lens == v2->lens;
758 return tree_equal(v1->origin->children, v2->origin->children);
761 return v1->filter == v2->filter;
764 return v1->transform == v2->transform;
767 return v1->native == v2->native;
770 return v1->func == v2->func && v1->bindings == v2->bindings;
782 struct type *make_arrow_type(struct type *dom, struct type *img) {
786 type->dom = ref(dom);
787 type->img = ref(img);
791 struct type *make_base_type(enum type_tag tag) {
793 return (struct type *) t_string;
794 else if (tag == T_REGEXP)
795 return (struct type *) t_regexp;
796 else if (tag == T_LENS)
797 return (struct type *) t_lens;
798 else if (tag == T_TREE)
799 return (struct type *) t_tree;
800 else if (tag == T_FILTER)
801 return (struct type *) t_filter;
802 else if (tag == T_TRANSFORM)
803 return (struct type *) t_transform;
804 else if (tag == T_UNIT)
805 return (struct type *) t_unit;
810 static const char *type_name(struct type *t) {
811 for (int i = 0; type_names[i] != NULL; i++)
813 return type_names[i];
818 static char *type_string(struct type *t) {
819 if (t->tag == T_ARROW) {
822 char *sd = type_string(t->dom);
823 char *si = type_string(t->img);
824 if (t->dom->tag == T_ARROW)
825 r = asprintf(&s, "(%s) -> %s", sd, si);
827 r = asprintf(&s, "%s -> %s", sd, si);
830 return (r == -1) ? NULL : s;
832 return strdup(type_name(t));
836 /* Decide whether T1 is a subtype of T2. The only subtype relations are
837 * T_STRING <: T_REGEXP and the usual subtyping of functions based on
838 * comparing domains/images
840 * Return 1 if T1 is a subtype of T2, 0 otherwise
842 static int subtype(struct type *t1, struct type *t2) {
845 /* We only promote T_STRING => T_REGEXP, no automatic conversion
846 of strings/regexps to lenses (yet) */
847 if (t1->tag == T_STRING)
848 return (t2->tag == T_STRING || t2->tag == T_REGEXP);
849 if (t1->tag == T_ARROW && t2->tag == T_ARROW) {
850 return subtype(t2->dom, t1->dom)
851 && subtype(t1->img, t2->img);
853 return t1->tag == t2->tag;
856 static int type_equal(struct type *t1, struct type *t2) {
857 return (t1 == t2) || (subtype(t1, t2) && subtype(t2, t1));
860 /* Return a type T with subtype(T, T1) && subtype(T, T2) */
861 static struct type *type_meet(struct type *t1, struct type *t2);
863 /* Return a type T with subtype(T1, T) && subtype(T2, T) */
864 static struct type *type_join(struct type *t1, struct type *t2) {
865 if (t1->tag == T_STRING) {
866 if (t2->tag == T_STRING)
868 else if (t2->tag == T_REGEXP)
870 } else if (t1->tag == T_REGEXP) {
871 if (t2->tag == T_STRING || t2->tag == T_REGEXP)
873 } else if (t1->tag == T_ARROW) {
874 if (t2->tag != T_ARROW)
876 struct type *dom = type_meet(t1->dom, t2->dom);
877 struct type *img = type_join(t1->img, t2->img);
878 if (dom == NULL || img == NULL) {
883 return make_arrow_type(dom, img);
884 } else if (type_equal(t1, t2)) {
890 /* Return a type T with subtype(T, T1) && subtype(T, T2) */
891 static struct type *type_meet(struct type *t1, struct type *t2) {
892 if (t1->tag == T_STRING) {
893 if (t2->tag == T_STRING || t2->tag == T_REGEXP)
895 } else if (t1->tag == T_REGEXP) {
896 if (t2->tag == T_STRING || t2->tag == T_REGEXP)
898 } else if (t1->tag == T_ARROW) {
899 if (t2->tag != T_ARROW)
901 struct type *dom = type_join(t1->dom, t2->dom);
902 struct type *img = type_meet(t1->img, t2->img);
903 if (dom == NULL || img == NULL) {
908 return make_arrow_type(dom, img);
909 } else if (type_equal(t1, t2)) {
915 static struct type *value_type(struct value *v) {
918 return make_base_type(T_STRING);
920 return make_base_type(T_REGEXP);
922 return make_base_type(T_LENS);
924 return make_base_type(T_TREE);
926 return make_base_type(T_FILTER);
928 return make_base_type(T_TRANSFORM);
930 return make_base_type(T_UNIT);
932 return ref(v->native->type);
934 return ref(v->func->type);
935 case V_EXN: /* Fail on exceptions */
942 /* Coerce V to the type T. Currently, only T_STRING can be coerced to
943 * T_REGEXP. Returns a value that is owned by the caller. Trying to perform
944 * an impossible coercion is a fatal error. Receives ownership of V.
946 static struct value *coerce(struct value *v, struct type *t) {
947 struct type *vt = value_type(v);
948 if (type_equal(vt, t)) {
952 if (vt->tag == T_STRING && t->tag == T_REGEXP) {
953 struct value *rxp = make_value(V_REGEXP, ref(v->info));
954 rxp->regexp = make_regexp_literal(v->info, v->string->str);
959 return make_exn_value(v->info, "Type %s can not be coerced to %s",
960 type_name(vt), type_name(t));
963 /* Return one of the expected types (passed as ...).
964 Does not give ownership of the returned type */
965 static struct type *expect_types_arr(struct info *info,
967 int ntypes, struct type *allowed[]) {
968 struct type *result = NULL;
970 for (int i=0; i < ntypes; i++) {
971 if (subtype(act, allowed[i])) {
976 if (result == NULL) {
978 for (int i=0; i < ntypes; i++) {
979 len += strlen(type_name(allowed[i]));
981 len += (ntypes - 1) * 4 + 1;
983 CALLOC(allowed_names, len);
984 for (int i=0; i < ntypes; i++) {
986 strcat(allowed_names, (i == ntypes - 1) ? ", or " : ", ");
987 strcat(allowed_names, type_name(allowed[i]));
989 char *act_str = type_string(act);
990 syntax_error(info, "type error: expected %s but found %s",
991 allowed_names, act_str);
998 static struct type *expect_types(struct info *info,
999 struct type *act, int ntypes, ...) {
1001 struct type *allowed[ntypes];
1003 va_start(ap, ntypes);
1004 for (int i=0; i < ntypes; i++)
1005 allowed[i] = va_arg(ap, struct type *);
1007 return expect_types_arr(info, act, ntypes, allowed);
1010 static struct value *apply(struct term *app, struct ctx *ctx);
1012 typedef struct value *(*impl0)(struct info *);
1013 typedef struct value *(*impl1)(struct info *, struct value *);
1014 typedef struct value *(*impl2)(struct info *, struct value *, struct value *);
1015 typedef struct value *(*impl3)(struct info *, struct value *, struct value *,
1017 typedef struct value *(*impl4)(struct info *, struct value *, struct value *,
1018 struct value *, struct value *);
1019 typedef struct value *(*impl5)(struct info *, struct value *, struct value *,
1020 struct value *, struct value *, struct value *);
1022 static struct value *native_call(struct info *info,
1023 struct native *func, struct ctx *ctx) {
1024 struct value *argv[func->argc];
1025 struct binding *b = ctx->local;
1026 struct value *result;
1028 for (int i = func->argc - 1; i >= 0; i--) {
1033 switch(func->argc) {
1035 result = ((impl0) *func->impl)(info);
1038 result = ((impl1) *func->impl)(info, argv[0]);
1041 result = ((impl2) *func->impl)(info, argv[0], argv[1]);
1044 result = ((impl3) *func->impl)(info, argv[0], argv[1], argv[2]);
1047 result = ((impl4) *func->impl)(info, argv[0], argv[1], argv[2], argv[3]);
1050 result = ((impl5) *func->impl)(info, argv[0], argv[1], argv[2], argv[3],
1062 static void type_error1(struct info *info, const char *msg, struct type *type) {
1063 char *s = type_string(type);
1064 syntax_error(info, "Type error: ");
1065 syntax_error(info, msg, s);
1069 static void type_error2(struct info *info, const char *msg,
1070 struct type *type1, struct type *type2) {
1071 char *s1 = type_string(type1);
1072 char *s2 = type_string(type2);
1073 syntax_error(info, "Type error: ");
1074 syntax_error(info, msg, s1, s2);
1079 static void type_error_binop(struct info *info, const char *opname,
1080 struct type *type1, struct type *type2) {
1081 char *s1 = type_string(type1);
1082 char *s2 = type_string(type2);
1083 syntax_error(info, "Type error: ");
1084 syntax_error(info, "%s of %s and %s is not possible", opname, s1, s2);
1089 static int check_exp(struct term *term, struct ctx *ctx);
1091 static struct type *require_exp_type(struct term *term, struct ctx *ctx,
1092 int ntypes, struct type *allowed[]) {
1095 if (term->type == NULL) {
1096 r = check_exp(term, ctx);
1101 return expect_types_arr(term->info, term->type, ntypes, allowed);
1104 static int check_compose(struct term *term, struct ctx *ctx) {
1105 struct type *tl = NULL, *tr = NULL;
1107 if (! check_exp(term->left, ctx))
1109 tl = term->left->type;
1111 if (tl->tag == T_ARROW) {
1112 /* Composition of functions f: a -> b and g: c -> d is defined as
1113 (f . g) x = g (f x) and is type correct if b <: c yielding a
1114 function with type a -> d */
1115 if (! check_exp(term->right, ctx))
1117 tr = term->right->type;
1118 if (tr->tag != T_ARROW)
1120 if (! subtype(tl->img, tr->dom))
1122 term->type = make_arrow_type(tl->dom, tr->img);
1123 } else if (tl->tag == T_UNIT) {
1124 if (! check_exp(term->right, ctx))
1126 term->type = ref(term->right->type);
1132 type_error_binop(term->info,
1133 "composition", term->left->type, term->right->type);
1137 static int check_binop(const char *opname, struct term *term,
1138 struct ctx *ctx, int ntypes, ...) {
1140 struct type *allowed[ntypes];
1141 struct type *tl = NULL, *tr = NULL;
1143 va_start(ap, ntypes);
1144 for (int i=0; i < ntypes; i++)
1145 allowed[i] = va_arg(ap, struct type *);
1148 tl = require_exp_type(term->left, ctx, ntypes, allowed);
1152 tr = require_exp_type(term->right, ctx, ntypes, allowed);
1156 term->type = type_join(tl, tr);
1157 if (term->type == NULL)
1161 type_error_binop(term->info, opname, term->left->type, term->right->type);
1165 static int check_value(struct value *v) {
1168 if (v->tag == V_REGEXP) {
1169 if (regexp_check(v->regexp, &msg) == -1) {
1170 syntax_error(v->info, "Invalid regular expression: %s", msg);
1177 /* Return 1 if TERM passes, 0 otherwise */
1178 static int check_exp(struct term *term, struct ctx *ctx) {
1180 assert(term->type == NULL || term->tag == A_VALUE || term->ref > 1);
1181 if (term->type != NULL && term->tag != A_VALUE)
1184 switch (term->tag) {
1186 result = check_binop("union", term, ctx, 2, t_regexp, t_lens);
1189 result = check_binop("minus", term, ctx, 1, t_regexp);
1192 result = check_compose(term, ctx);
1195 result = check_binop("concatenation", term, ctx,
1196 4, t_string, t_regexp, t_lens, t_filter);
1200 result = check_exp(term->right, ctx);
1202 struct term *func = term->left;
1203 assert(func->tag == A_FUNC);
1204 assert(func->param->type == NULL);
1205 func->param->type = ref(term->right->type);
1207 result = check_exp(func, ctx);
1210 term->type = ref(func->type->img);
1216 result = check_exp(term->left, ctx) & check_exp(term->right, ctx);
1218 if (term->left->type->tag != T_ARROW) {
1219 type_error1(term->info,
1220 "expected function in application but found %s",
1226 result = expect_types(term->info,
1228 1, term->left->type->dom) != NULL;
1230 type_error_binop(term->info, "application",
1231 term->left->type, term->right->type);
1236 term->type = ref(term->left->type->img);
1239 result = check_value(term->value);
1243 struct type *t = ctx_lookup_type(term->info, ctx, term->ident);
1245 syntax_error(term->info, "Undefined variable %s",
1249 term->type = ref(t);
1254 result = check_exp(term->brexp, ctx);
1256 term->type = ref(expect_types(term->info, term->brexp->type,
1258 if (term->type == NULL) {
1259 type_error1(term->info,
1260 "[..] is only defined for lenses, not for %s",
1268 bind_param(&ctx->local, term->param, NULL);
1269 result = check_exp(term->body, ctx);
1272 make_arrow_type(term->param->type, term->body->type);
1274 unbind_param(&ctx->local, term->param);
1278 result = check_exp(term->exp, ctx);
1280 term->type = ref(expect_types(term->info, term->exp->type, 2,
1282 if (term->type == NULL) {
1283 type_error1(term->info,
1284 "Incompatible types: repetition is only defined"
1285 " for regexp and lens, not for %s",
1295 assert(!result || term->type != NULL);
1299 static int check_decl(struct term *term, struct ctx *ctx) {
1300 assert(term->tag == A_BIND || term->tag == A_TEST);
1302 if (term->tag == A_BIND) {
1303 if (!check_exp(term->exp, ctx))
1305 term->type = ref(term->exp->type);
1307 if (bnd_lookup(ctx->local, term->bname) != NULL) {
1308 syntax_error(term->info,
1309 "the name %s is already defined", term->bname);
1312 bind_type(&ctx->local, term->bname, term->type);
1313 } else if (term->tag == A_TEST) {
1314 if (!check_exp(term->test, ctx))
1316 if (term->result != NULL) {
1317 if (!check_exp(term->result, ctx))
1319 if (! type_equal(term->test->type, term->result->type)) {
1320 type_error2(term->info,
1321 "expected test result of type %s but got %s",
1322 term->result->type, term->test->type);
1326 if (expect_types(term->info, term->test->type, 2,
1327 t_string, t_tree) == NULL)
1330 term->type = ref(term->test->type);
1337 static int typecheck(struct term *term, struct augeas *aug) {
1341 const char *basenam;
1343 assert(term->tag == A_MODULE);
1345 /* Check that the module name is consistent with the filename */
1346 fname = module_basename(term->mname);
1348 basenam = strrchr(term->info->filename->str, SEP);
1349 if (basenam == NULL)
1350 basenam = term->info->filename->str;
1353 if (STRNEQ(fname, basenam)) {
1354 syntax_error(term->info,
1355 "The module %s must be in a file named %s",
1356 term->mname, fname);
1364 ctx.name = term->mname;
1365 list_for_each(dcl, term->decls) {
1366 ok &= check_decl(dcl, &ctx);
1368 unref(ctx.local, binding);
1372 static struct value *compile_exp(struct info *, struct term *, struct ctx *);
1374 static struct value *compile_union(struct term *exp, struct ctx *ctx) {
1375 struct value *v1 = compile_exp(exp->info, exp->left, ctx);
1378 struct value *v2 = compile_exp(exp->info, exp->right, ctx);
1384 struct type *t = exp->type;
1385 struct info *info = exp->info;
1386 struct value *v = NULL;
1397 if (t->tag == T_REGEXP) {
1398 v = make_value(V_REGEXP, ref(info));
1399 v->regexp = regexp_union(info, v1->regexp, v2->regexp);
1400 } else if (t->tag == T_LENS) {
1401 struct lens *l1 = v1->lens;
1402 struct lens *l2 = v2->lens;
1403 v = lns_make_union(ref(info), ref(l1), ref(l2), LNS_TYPE_CHECK(ctx));
1405 fatal_error(info, "Tried to union a %s and a %s to yield a %s",
1406 type_name(exp->left->type), type_name(exp->right->type),
1414 static struct value *compile_minus(struct term *exp, struct ctx *ctx) {
1415 struct value *v1 = compile_exp(exp->info, exp->left, ctx);
1418 struct value *v2 = compile_exp(exp->info, exp->right, ctx);
1424 struct type *t = exp->type;
1425 struct info *info = exp->info;
1430 if (t->tag == T_REGEXP) {
1431 struct regexp *re1 = v1->regexp;
1432 struct regexp *re2 = v2->regexp;
1433 struct regexp *re = regexp_minus(info, re1, re2);
1435 v = make_exn_value(ref(info),
1436 "Regular expression subtraction 'r1 - r2' failed");
1437 exn_printf_line(v, "r1: /%s/", re1->pattern->str);
1438 exn_printf_line(v, "r2: /%s/", re2->pattern->str);
1440 v = make_value(V_REGEXP, ref(info));
1444 fatal_error(info, "Tried to subtract a %s and a %s to yield a %s",
1445 type_name(exp->left->type), type_name(exp->right->type),
1453 static struct value *compile_compose(struct term *exp, struct ctx *ctx) {
1454 struct info *info = exp->info;
1457 if (exp->left->type->tag == T_ARROW) {
1458 // FIXME: This is really crufty, and should be desugared in the
1459 // parser so that we don't have to do all this manual type
1460 // computation. Should we write function compostion as
1461 // concatenation instead of using a separate syntax ?
1463 /* Build lambda x: exp->right (exp->left x) as a closure */
1464 char *var = strdup("@0");
1465 struct term *param = make_param(var, ref(exp->left->type->dom),
1467 param->type = ref(exp->left->type);
1468 struct term *ident = make_term(A_IDENT, ref(info));
1469 ident->ident = ref(param->param->name);
1470 ident->type = ref(param->type);
1471 struct term *app = make_app_term(ref(exp->left), ident, ref(info));
1472 app->type = ref(app->left->type->img);
1473 app = make_app_term(ref(exp->right), app, ref(info));
1474 app->type = ref(app->left->type->img);
1476 struct term *func = build_func(param, app);
1478 if (!type_equal(func->type, exp->type)) {
1479 char *f = type_string(func->type);
1480 char *e = type_string(exp->type);
1482 "Composition has type %s but should have type %s", f, e);
1488 v = make_closure(func, ctx->local);
1491 v = compile_exp(exp->info, exp->left, ctx);
1493 v = compile_exp(exp->info, exp->right, ctx);
1498 static struct value *compile_concat(struct term *exp, struct ctx *ctx) {
1499 struct value *v1 = compile_exp(exp->info, exp->left, ctx);
1502 struct value *v2 = compile_exp(exp->info, exp->right, ctx);
1508 struct type *t = exp->type;
1509 struct info *info = exp->info;
1514 if (t->tag == T_STRING) {
1515 const char *s1 = v1->string->str;
1516 const char *s2 = v2->string->str;
1517 v = make_value(V_STRING, ref(info));
1518 make_ref(v->string);
1519 CALLOC(v->string->str, strlen(s1) + strlen(s2) + 1);
1520 char *s = v->string->str;
1523 } else if (t->tag == T_REGEXP) {
1524 v = make_value(V_REGEXP, ref(info));
1525 v->regexp = regexp_concat(info, v1->regexp, v2->regexp);
1526 } else if (t->tag == T_FILTER) {
1527 struct filter *f1 = v1->filter;
1528 struct filter *f2 = v2->filter;
1529 v = make_value(V_FILTER, ref(info));
1530 if (v2->ref == 1 && f2->ref == 1) {
1531 list_append(f2, ref(f1));
1532 v->filter = ref(f2);
1533 } else if (v1->ref == 1 && f1->ref == 1) {
1534 list_append(f1, ref(f2));
1535 v->filter = ref(f1);
1537 struct filter *cf1, *cf2;
1538 cf1 = make_filter(ref(f1->glob), f1->include);
1539 cf2 = make_filter(ref(f2->glob), f2->include);
1540 cf1->next = ref(f1->next);
1541 cf2->next = ref(f2->next);
1542 list_append(cf1, cf2);
1545 } else if (t->tag == T_LENS) {
1546 struct lens *l1 = v1->lens;
1547 struct lens *l2 = v2->lens;
1548 v = lns_make_concat(ref(info), ref(l1), ref(l2), LNS_TYPE_CHECK(ctx));
1550 fatal_error(info, "Tried to concat a %s and a %s to yield a %s",
1551 type_name(exp->left->type), type_name(exp->right->type),
1559 static struct value *apply(struct term *app, struct ctx *ctx) {
1560 struct value *f = compile_exp(app->info, app->left, ctx);
1561 struct value *result = NULL;
1567 struct value *arg = compile_exp(app->info, app->right, ctx);
1573 assert(f->tag == V_CLOS);
1575 lctx.aug = ctx->aug;
1576 lctx.local = ref(f->bindings);
1577 lctx.name = ctx->name;
1579 arg = coerce(arg, f->func->param->type);
1583 bind_param(&lctx.local, f->func->param, arg);
1584 result = compile_exp(app->info, f->func->body, &lctx);
1585 unref(result->info, info);
1586 result->info = ref(app->info);
1587 unbind_param(&lctx.local, f->func->param);
1590 unref(lctx.local, binding);
1596 static struct value *compile_bracket(struct term *exp, struct ctx *ctx) {
1597 struct value *arg = compile_exp(exp->info, exp->brexp, ctx);
1600 assert(arg->tag == V_LENS);
1602 struct value *v = lns_make_subtree(ref(exp->info), ref(arg->lens));
1608 static struct value *compile_rep(struct term *rep, struct ctx *ctx) {
1609 struct value *arg = compile_exp(rep->info, rep->rexp, ctx);
1610 struct value *v = NULL;
1615 arg = coerce(arg, rep->type);
1616 if (rep->type->tag == T_REGEXP) {
1618 if (rep->quant == Q_STAR) {
1620 } else if (rep->quant == Q_PLUS) {
1622 } else if (rep->quant == Q_MAYBE) {
1628 v = make_value(V_REGEXP, ref(rep->info));
1629 v->regexp = regexp_iter(rep->info, arg->regexp, min, max);
1630 } else if (rep->type->tag == T_LENS) {
1631 int c = LNS_TYPE_CHECK(ctx);
1632 if (rep->quant == Q_STAR) {
1633 v = lns_make_star(ref(rep->info), ref(arg->lens), c);
1634 } else if (rep->quant == Q_PLUS) {
1635 v = lns_make_plus(ref(rep->info), ref(arg->lens), c);
1636 } else if (rep->quant == Q_MAYBE) {
1637 v = lns_make_maybe(ref(rep->info), ref(arg->lens), c);
1642 fatal_error(rep->info, "Tried to repeat a %s to yield a %s",
1643 type_name(rep->rexp->type), type_name(rep->type));
1649 static struct value *compile_exp(struct info *info,
1650 struct term *exp, struct ctx *ctx) {
1651 struct value *v = NULL;
1655 v = compile_compose(exp, ctx);
1658 v = compile_union(exp, ctx);
1661 v = compile_minus(exp, ctx);
1664 v = compile_concat(exp, ctx);
1667 v = apply(exp, ctx);
1670 if (exp->value->tag == V_NATIVE) {
1671 v = native_call(info, exp->value->native, ctx);
1673 v = ref(exp->value);
1677 v = ref(ctx_lookup(exp->info, ctx, exp->ident));
1680 v = compile_bracket(exp, ctx);
1683 v = make_closure(exp, ctx->local);
1686 v = compile_rep(exp, ctx);
1696 static int compile_test(struct term *term, struct ctx *ctx) {
1697 struct value *actual = compile_exp(term->info, term->test, ctx);
1698 struct value *expect = NULL;
1701 if (term->tr_tag == TR_EXN) {
1703 printf("Test run should have produced exception, but produced\n");
1704 print_value(stdout, actual);
1710 print_info(stdout, term->info);
1711 printf("exception thrown in test\n");
1712 print_value(stdout, actual);
1715 } else if (term->tr_tag == TR_CHECK) {
1716 expect = compile_exp(term->info, term->result, ctx);
1719 if (! value_equal(actual, expect)) {
1720 printf("Test failure:");
1721 print_info(stdout, term->info);
1723 printf(" Expected:\n");
1724 print_value(stdout, expect);
1726 printf(" Actual:\n");
1727 print_value(stdout, actual);
1732 printf("Test result: ");
1733 print_info(stdout, term->info);
1735 if (actual->tag == V_TREE) {
1736 print_tree(stdout, 2, actual->origin->children);
1738 print_value(stdout, actual);
1744 reset_error(term->info->error);
1745 unref(actual, value);
1746 unref(expect, value);
1750 static int compile_decl(struct term *term, struct ctx *ctx) {
1751 if (term->tag == A_BIND) {
1754 struct value *v = compile_exp(term->info, term->exp, ctx);
1755 bind(&ctx->local, term->bname, term->type, v);
1757 if (EXN(v) && !v->exn->seen) {
1758 struct error *error = term->info->error;
1759 struct memstream ms;
1761 init_memstream(&ms);
1763 syntax_error(term->info, "Failed to compile %s",
1765 fprintf(ms.stream, "%s\n", error->details);
1766 print_value(ms.stream, v);
1767 close_memstream(&ms);
1770 free(error->details);
1771 error->details = ms.buf;
1776 } else if (term->tag == A_TEST) {
1777 return compile_test(term, ctx);
1783 static struct module *compile(struct term *term, struct augeas *aug) {
1785 struct transform *autoload = NULL;
1786 assert(term->tag == A_MODULE);
1790 ctx.name = term->mname;
1791 list_for_each(dcl, term->decls) {
1792 if (!compile_decl(dcl, &ctx))
1796 if (term->autoload != NULL) {
1797 struct binding *bnd = bnd_lookup(ctx.local, term->autoload);
1799 syntax_error(term->info, "Undefined transform in autoload %s",
1803 if (expect_types(term->info, bnd->type, 1, t_transform) == NULL)
1805 autoload = bnd->value->transform;
1807 struct module *module = module_create(term->mname);
1808 module->bindings = ctx.local;
1809 module->autoload = ref(autoload);
1812 unref(ctx.local, binding);
1817 * Defining native functions
1819 static struct info *
1820 make_native_info(struct error *error, const char *fname, int line) {
1822 if (make_ref(info) < 0)
1824 info->first_line = info->last_line = line;
1825 info->first_column = info->last_column = 0;
1826 info->error = error;
1827 if (make_ref(info->filename) < 0)
1829 info->filename->str = strdup(fname);
1836 int define_native_intl(const char *file, int line,
1837 struct error *error,
1838 struct module *module, const char *name,
1839 int argc, void *impl, ...) {
1840 assert(argc > 0); /* We have no unit type */
1844 struct term *params = NULL, *body = NULL, *func = NULL;
1846 struct value *v = NULL;
1847 struct info *info = NULL;
1850 info = make_native_info(error, file, line);
1855 for (int i=0; i < argc; i++) {
1858 tag = va_arg(ap, enum type_tag);
1859 type = make_base_type(tag);
1860 snprintf(ident, 10, "@%d", i);
1861 pterm = make_param(strdup(ident), type, ref(info));
1862 list_append(params, pterm);
1864 tag = va_arg(ap, enum type_tag);
1867 type = make_base_type(tag);
1876 if (ALLOC(v->native) < 0)
1878 v->native->argc = argc;
1879 v->native->type = type;
1880 v->native->impl = impl;
1885 body->info = ref(info);
1886 body->type = ref(type);
1887 body->tag = A_VALUE;
1891 func = build_func(params, body);
1897 ctx.local = ref(module->bindings);
1898 ctx.name = module->name;
1899 if (! check_exp(func, &ctx)) {
1900 fatal_error(info, "Typechecking native %s failed",
1904 v = make_closure(func, ctx.local);
1906 unref(module->bindings, binding);
1909 bind(&ctx.local, name, func->type, v);
1912 unref(module->bindings, binding);
1914 module->bindings = ctx.local;
1924 /* Defined in parser.y */
1925 int augl_parse_file(struct augeas *aug, const char *name, struct term **term);
1927 static char *module_basename(const char *modname) {
1930 if (asprintf(&fname, "%s" AUG_EXT, modname) == -1)
1932 for (int i=0; i < strlen(modname); i++)
1933 fname[i] = tolower(fname[i]);
1937 static char *module_filename(struct augeas *aug, const char *modname) {
1939 char *filename = NULL;
1940 char *name = module_basename(modname);
1942 while ((dir = argz_next(aug->modpathz, aug->nmodpath, dir)) != NULL) {
1943 int len = strlen(name) + strlen(dir) + 2;
1946 if (REALLOC_N(filename, len) == -1)
1948 sprintf(filename, "%s/%s", dir, name);
1949 if (stat(filename, &st) == 0)
1959 int load_module_file(struct augeas *aug, const char *filename) {
1960 struct term *term = NULL;
1963 augl_parse_file(aug, filename, &term);
1966 if (! typecheck(term, aug))
1969 struct module *module = compile(term, aug);
1970 ERR_THROW(module == NULL, aug, AUG_ESYNTAX,
1971 "Failed to load %s", filename);
1973 list_append(aug->modules, module);
1976 // FIXME: This leads to a bad free of a string used in a del lens
1977 // To reproduce run lenses/tests/test_yum.aug
1982 static int load_module(struct augeas *aug, const char *name) {
1983 char *filename = NULL;
1985 if (module_find(aug->modules, name) != NULL)
1988 if ((filename = module_filename(aug, name)) == NULL)
1991 if (load_module_file(aug, filename) == -1)
2002 int interpreter_init(struct augeas *aug) {
2005 aug->modules = builtin_init(aug->error);
2007 if (aug->flags & AUG_NO_MODL_AUTOLOAD)
2010 // For now, we just load every file on the search path
2011 const char *dir = NULL;
2013 int gl_flags = GLOB_NOSORT;
2015 MEMZERO(&globbuf, 1);
2017 while ((dir = argz_next(aug->modpathz, aug->nmodpath, dir)) != NULL) {
2019 r = asprintf(&globpat, "%s/*.aug", dir);
2020 ERR_NOMEM(r < 0, aug);
2022 r = glob(globpat, gl_flags, NULL, &globbuf);
2023 if (r != 0 && r != GLOB_NOMATCH) {
2024 /* This really has to be an allocation failure; glob is not
2025 * supposed to return GLOB_ABORTED here */
2026 aug_errcode_t code =
2027 r == GLOB_NOSPACE ? AUG_ENOMEM : AUG_EINTERNAL;
2028 ERR_REPORT(aug, code, "glob failure for %s", globpat);
2032 gl_flags |= GLOB_APPEND;
2036 for (int i=0; i < globbuf.gl_pathc; i++) {
2038 p = strrchr(globbuf.gl_pathv[i], SEP);
2040 p = globbuf.gl_pathv[i];
2044 name = strndup(p, q - p);
2045 name[0] = toupper(name[0]);
2046 if (load_module(aug, name) == -1)
2059 * indent-tabs-mode: nil