Skip to content

Commit 40f826c

Browse files
committed
Correct mutability for methods
1 parent 3668d89 commit 40f826c

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

examples/017_struct_methods.tea

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Point {
2727
}
2828

2929
// Method that modifies the point (moves it)
30-
fn move_by(dx: f32, dy: f32) {
30+
fn mut move_by(dx: f32, dy: f32) {
3131
self.x = self.x + dx;
3232
self.y = self.y + dy;
3333
}
@@ -73,7 +73,7 @@ impl Rectangle {
7373
}
7474

7575
// Scale the rectangle
76-
fn scale(factor: f32) {
76+
fn mut scale(factor: f32) {
7777
self.width = self.width * factor;
7878
self.height = self.height * factor;
7979
}
@@ -82,7 +82,7 @@ impl Rectangle {
8282
// Implement methods for Counter
8383
impl Counter {
8484
// Increment counter
85-
fn increment() {
85+
fn mut increment() {
8686
if self.value < self.max_value {
8787
self.value = self.value + 1;
8888
}
@@ -96,7 +96,7 @@ impl Counter {
9696
}
9797

9898
// Reset counter
99-
fn reset() {
99+
fn mut reset() {
100100
self.value = 0;
101101
}
102102

include/tea_scope.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ typedef struct {
88
tea_list_entry_t fns;
99
tea_list_entry_t nfns;
1010
tea_list_entry_t structs;
11-
1211
tea_list_entry_t vars;
1312
} tea_ctx_t;
1413

@@ -37,4 +36,4 @@ tea_var_t *tea_scope_find_local(const tea_scope_t *scp, const char *name);
3736
tea_var_t *tea_scope_find(const tea_scope_t *scp, const char *name);
3837

3938
bool tea_decl_var(tea_ctx_t *ctx, tea_scope_t *scp, const char *name,
40-
unsigned int flags, const char *type, const tea_node_t *init);
39+
unsigned int flags, const char *type, const tea_node_t *initial_value);

src/tea_fn.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,15 @@ tea_val_t tea_eval_fn_call(tea_ctx_t *ctx, tea_scope_t *scp,
264264
return tea_val_undef();
265265
}
266266

267-
// declare 'self' for the scope
268-
tea_decl_var(ctx, &inner_scope, "self", 0, NULL, object_node);
269-
270267
function_name = field_token->buf;
271268
function = tea_ctx_find_fn(&struct_declaration->fns, function_name);
272269

270+
if (function) {
271+
// declare 'self' for the scope
272+
tea_decl_var(ctx, &inner_scope, "self",
273+
function->mut ? TEA_VAR_MUT : 0,
274+
NULL, object_node);
275+
}
273276
} else {
274277
const tea_tok_t *token = node->tok;
275278
if (token) {
@@ -377,6 +380,10 @@ tea_val_t tea_eval_fn_call(tea_ctx_t *ctx, tea_scope_t *scp,
377380
return return_context.ret_val;
378381
}
379382

383+
if (!result) {
384+
exit(-1);
385+
}
386+
380387
return tea_val_undef();
381388
}
382389

0 commit comments

Comments
 (0)