summaryrefslogtreecommitdiff
path: root/infer.c
diff options
context:
space:
mode:
Diffstat (limited to 'infer.c')
-rw-r--r--infer.c39
1 files changed, 26 insertions, 13 deletions
diff --git a/infer.c b/infer.c
index ac32f81..d4d4513 100644
--- a/infer.c
+++ b/infer.c
@@ -1,16 +1,29 @@
-/* infers */
-static bool infer_expr(Typer *tr, Expression *e, Declaration *decls,
- Expression *exprs) {
- e->kind = EXPR_VAL;
- Value *val = &e->val;
- val->type = malloc(sizeof *val->type);
- memset(val->type, 0, sizeof *val->type);
- val->type->kind = TYPE_BUILTIN;
- val->type->builtin = BUILTIN_I64;
- val->type->flags = TYPE_IS_RESOLVED;
- memset(&e->type, 0, sizeof e->type);
- e->type.kind = TYPE_TYPE;
- e->type.flags = TYPE_IS_RESOLVED;
+/* infers the expression of decls[idx], according to the types of the other decls */
+static bool infer_expr(Typer *tr, size_t idx, Declaration *decls) {
+ Declaration *decl = decls + idx;
+ if (decl->flags & DECL_HAS_EXPR) return true; /* already did it */
+
+ decl->expr.kind = EXPR_VAL;
+ decl->expr.type.flags = TYPE_IS_RESOLVED;
+ decl->expr.type.kind = TYPE_TYPE;
+ decl->expr.val.type = calloc(1,sizeof (Type));
+ decl->expr.val.type->kind = TYPE_BUILTIN;
+ decl->expr.val.type->builtin = BUILTIN_I64;
+ decl->expr.val.type->flags = TYPE_IS_RESOLVED;
+
+ decl->type = decl->expr.type;
+ decl->flags |= DECL_FOUND_TYPE;
+ decl->flags |= DECL_HAS_EXPR;
+ return true;
+}
+
+/* infers ALL of the expressions in the declarations */
+static bool infer_exprs_in_decls(Typer *tr, Declaration *decls) {
+ for (size_t idx = 0; idx < arr_len(decls); ++idx) {
+ if (decls[idx].flags & DECL_INFER)
+ if (!infer_expr(tr, idx, decls))
+ return false;
+ }
return true;
}