Skip to content

Commit

Permalink
Add snek_id_store
Browse files Browse the repository at this point in the history
Replace the pattern:

	ref = snek_id_ref(id, true);
	if (ref)
		*ref = value

with the new snek_id_store function, saving a bit of space.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Dec 15, 2022
1 parent 272eca2 commit e80f0b5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
27 changes: 4 additions & 23 deletions snek-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,13 @@ snek_range_start(snek_offset_t ip)
}

/* Assign initial value (current - step) */

snek_poly_t *id_ref = snek_id_ref(id, true);
if(!id_ref)
return;
*id_ref = snek_float_to_poly(current - step);
(void) snek_id_store(id, snek_float_to_poly(current - step));

/* Save limit in tmp variable */
snek_poly_t *limit_ref = snek_id_ref(snek_for_tmp(for_depth, 0), true);
if (!limit_ref)
return;
*limit_ref = snek_float_to_poly(limit);
(void) snek_id_store(snek_for_tmp(for_depth, 0), snek_float_to_poly(limit));

/* Save step in tmp variable */
snek_poly_t *step_ref = snek_id_ref(snek_for_tmp(for_depth, 1), true);
if (!step_ref)
return;
*step_ref = snek_float_to_poly(step);

return;
(void) snek_id_store(snek_for_tmp(for_depth, 1), snek_float_to_poly(step));
}

/*
Expand Down Expand Up @@ -204,7 +192,6 @@ snek_in_step(snek_offset_t ip)
{
uint8_t for_depth; /* nesting depth of loop */
snek_id_t id; /* id of the 'for' variable */
snek_poly_t *ref; /* reference to the 'for' variable storage */

memcpy(&for_depth, &snek_code->code[ip + sizeof(snek_offset_t)], sizeof(uint8_t));

Expand Down Expand Up @@ -246,13 +233,7 @@ snek_in_step(snek_offset_t ip)
/* Update value */
memcpy(&id, &snek_code->code[ip + sizeof(snek_offset_t) + sizeof (uint8_t)], sizeof (snek_id_t));

snek_stack_push(value);
ref = snek_id_ref(id, true);
value = snek_stack_pop();
if (!ref)
return false;
*ref = value;
return true;
return snek_id_store(id, value);
}

/*
Expand Down
14 changes: 14 additions & 0 deletions snek-frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ snek_id_ref(snek_id_t id, bool insert)
return &v->value;
}

bool
snek_id_store(snek_id_t id, snek_poly_t value)
{
snek_poly_t *ref;

snek_stack_push(value);
ref = snek_id_ref(id, true);
value = snek_stack_pop();
if (!ref)
return false;
*ref = value;
return true;
}

bool
snek_id_is_local(snek_id_t id)
{
Expand Down
6 changes: 1 addition & 5 deletions snek-gram.ll
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ command : @{ snek_print_val = snek_interactive; }@ stat
snek_poly_t poly = snek_func_to_poly(func);
snek_id_t id = value_pop().id;

snek_stack_push(poly);
snek_poly_t *ref = snek_id_ref(id, true);
poly = snek_stack_pop();
if (ref)
*ref = poly;
snek_id_store(id, poly);
}@
| IMPORT NAME
;
Expand Down
3 changes: 3 additions & 0 deletions snek.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ snek_frame_pop(void);
snek_poly_t *
snek_id_ref(snek_id_t id, bool insert);

bool
snek_id_store(snek_id_t id, snek_poly_t value);

bool
snek_id_is_local(snek_id_t id);

Expand Down

0 comments on commit e80f0b5

Please sign in to comment.