diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-27 13:42:50 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-27 13:42:50 -0400 |
commit | b22ad26be0f96df1a626742a6f6ebd1bfefcf56d (patch) | |
tree | 9deb66255f8ea7755fd2942814f9e2f9b501d197 | |
parent | f726c7b6628b44f55b9047e6423afa588f2ff4d5 (diff) |
parameters are immutable again
-rw-r--r-- | cgen.c | 2 | ||||
-rw-r--r-- | decls_cgen.c | 2 | ||||
-rw-r--r-- | parse.c | 2 | ||||
-rw-r--r-- | test.toc | 2 | ||||
-rw-r--r-- | types.c | 24 |
5 files changed, 25 insertions, 7 deletions
@@ -292,8 +292,6 @@ static bool cgen_fn_header(CGenerator *g, FnExpr *f, Location where) { cgen_write(g, " "); bool ptr = cgen_uses_ptr(&d->type); if (ptr) { - fprint_type(stdout, &d->type); - puts(""); IdentDecl *idecl = ident_decl(*i); assert(idecl); idecl->flags |= IDECL_FLAG_CGEN_PTR; diff --git a/decls_cgen.c b/decls_cgen.c index 58d8667..58383f7 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -57,12 +57,14 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { case EXPR_FN: e->fn.c.name = NULL; e->fn.c.id = g->ident_counter++; + fn_enter(&e->fn, 0); if (!cgen_fn_header(g, &e->fn, e->where)) return false; cgen_write(g, ";"); cgen_nl(g); if (!cgen_decls_block(g, &e->fn.body)) return false; + fn_exit(&e->fn); break; case EXPR_TYPE: case EXPR_DIRECT: @@ -723,6 +723,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { default: unrecognized: tokr_err(t, "Unrecognized expression."); + t->token = end + 1; return false; } t->token = end; @@ -1282,7 +1283,6 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { return true; } tokr_err(t, "Unrecognized expression."); - t->token = end + 1; return false; } } @@ -16,5 +16,7 @@ putf @= fn(x: float) { Foo @= int; main @= fn() { + f := fn(x: int) { p := &x; *p = 5; }; + // f(3); // x : Foo; }; @@ -99,6 +99,10 @@ static bool expr_arr_must_mut(Expression *e) { err_print(e->where, "Cannot modify a constant array."); return false; } + if (d->flags & DECL_FLAG_PARAM) { + err_print(e->where, "Parameters are immutable."); + return false; + } } return true; case EXPR_CAST: case EXPR_CALL: @@ -887,10 +891,12 @@ static bool types_expr(Typer *tr, Expression *e) { } } break; case EXPR_BINARY_OP: { - Type *lhs_type = &e->binary.lhs->type; - Type *rhs_type = &e->binary.rhs->type; - if (!types_expr(tr, e->binary.lhs) - || !types_expr(tr, e->binary.rhs)) + Expression *lhs = e->binary.lhs; + Expression *rhs = e->binary.rhs; + Type *lhs_type = &lhs->type; + Type *rhs_type = &rhs->type; + if (!types_expr(tr, lhs) + || !types_expr(tr, rhs)) return false; if (lhs_type->kind == TYPE_UNKNOWN || rhs_type->kind == TYPE_UNKNOWN) { return true; @@ -900,6 +906,16 @@ static bool types_expr(Typer *tr, Expression *e) { if (!expr_must_lval(e->binary.lhs)) { return false; } + { + if (lhs->kind == EXPR_IDENT) { + Declaration *d = ident_decl(lhs->ident)->decl; + if (d->flags & DECL_FLAG_PARAM) { + err_print(e->where, "Parameters are immutable."); + return false; + } + } + } + /* fallthrough */ case BINARY_ADD: case BINARY_SUB: |