Skip to content

Commit

Permalink
Add SNEK_NO_DICT build option
Browse files Browse the repository at this point in the history
This option removes dictionaries from the language, saving
considerable space in ROM.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Feb 2, 2021
1 parent 67abfbd commit 6f265a2
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 14 deletions.
19 changes: 16 additions & 3 deletions snek-exec.c
Expand Up @@ -225,8 +225,10 @@ snek_in_step(snek_offset_t ip)
switch (snek_poly_type(array)) {
case snek_list:
list = snek_poly_to_list(array);
#ifndef SNEK_NO_DICT
if (snek_list_type(list) == snek_list_dict)
i *= 2;
#endif
if ((snek_offset_t) i < list->size)
value = snek_list_data(list)[(snek_offset_t) i];
break;
Expand Down Expand Up @@ -393,7 +395,11 @@ snek_binary(snek_poly_t a, snek_op_t op, snek_poly_t b, bool inplace)
switch (bt) {
case snek_list:
bl = snek_poly_to_list(b);
snek_offset_t o, step = snek_list_type(bl) == snek_list_dict ? 2 : 1;
snek_offset_t o, step =
#ifndef SNEK_NO_DICT
snek_list_type(bl) == snek_list_dict ? 2 :
#endif
1;
found = false;
for (o = 0; o < bl->size; o += step) {
if (snek_poly_cmp(a, snek_list_data(bl)[o], false) == 0) {
Expand Down Expand Up @@ -425,8 +431,11 @@ snek_binary(snek_poly_t a, snek_op_t op, snek_poly_t b, bool inplace)
al = snek_poly_to_list(a);
bl = snek_poly_to_list(b);

if (snek_list_type(al) == snek_list_type(bl) &&
snek_list_type(al) != snek_list_dict)
if (snek_list_type(al) == snek_list_type(bl)
#ifndef SNEK_NO_DICT
&& snek_list_type(al) != snek_list_dict
#endif
)
{
if (inplace && !snek_list_readonly(al))
al = snek_list_append(al, bl);
Expand All @@ -448,7 +457,9 @@ snek_binary(snek_poly_t a, snek_op_t op, snek_poly_t b, bool inplace)
switch (at) {
case snek_list:
al = snek_poly_to_list(a);
#ifndef SNEK_NO_DICT
if (snek_list_type(al) != snek_list_dict)
#endif
ret = snek_list_to_poly(snek_list_times(al, bo));
break;
case snek_string:
Expand Down Expand Up @@ -826,7 +837,9 @@ snek_exec(snek_code_t *code_in)
break;
case snek_op_list:
case snek_op_tuple:
#ifndef SNEK_NO_DICT
case snek_op_dict:
#endif
memcpy(&o, &snek_code->code[ip], sizeof(snek_offset_t));
ip += sizeof (snek_offset_t);
snek_a = snek_list_imm(o, op - snek_op_list);
Expand Down
4 changes: 4 additions & 0 deletions snek-gram.ll
Expand Up @@ -496,6 +496,7 @@ expr-prim : OP opt-tuple CP
return parse_return_syntax;
snek_code_add_op_offset(snek_op_list, num);
}@
{SNEK_DICT
| OC
@{
/* Zero dict-ents so far */
Expand All @@ -507,6 +508,7 @@ expr-prim : OP opt-tuple CP
snek_offset_t num = value_pop().offset;
snek_code_add_op_offset(snek_op_dict, num);
}@
}
| NAME
@{
snek_code_add_op_id(snek_op_id, snek_token_val.id);
Expand Down Expand Up @@ -611,6 +613,7 @@ actuals-p : COMMA actuals-end
actuals-end : expr actual-p actuals-p
|
;
{SNEK_DICT
opt-dict-ents : dict-ent dict-ents-p
|
;
Expand All @@ -631,3 +634,4 @@ dict-ent : expr
snek_code_set_push(snek_code_prev_insn());
}@
;
}
2 changes: 2 additions & 0 deletions snek-lex.c
Expand Up @@ -448,11 +448,13 @@ snek_lex(void)
RETURN(OS);
case ']':
return snek_lex_close(CS);
#ifndef SNEK_NO_DICT
case '{':
++snek_ignore_nl;
RETURN(OC);
case '}':
return snek_lex_close(CC);
#endif
case '+':
return check_equal(PLUS, snek_op_plus);
case '-':
Expand Down
15 changes: 13 additions & 2 deletions snek-list.c
Expand Up @@ -139,6 +139,7 @@ snek_list_times(snek_list_t *a, snek_soffset_t count)
return n;
}

#ifndef SNEK_NO_DICT
static bool
snek_mutable(snek_poly_t p)
{
Expand All @@ -154,13 +155,16 @@ snek_mutable(snek_poly_t p)
return true;
return false;
}
#endif

static snek_poly_t *
_snek_list_ref(snek_list_t *list, snek_poly_t p, bool report_error, bool add)
{
snek_offset_t o;
snek_poly_t *data = snek_list_data(list);

(void) add;
#ifndef SNEK_NO_DICT
if (snek_list_type(list) == snek_list_dict) {
snek_offset_t l = 0, r = list->size;
while (l < r) {
Expand All @@ -187,7 +191,9 @@ _snek_list_ref(snek_list_t *list, snek_poly_t p, bool report_error, bool add)
data[o] = p;
}
o++;
} else {
} else
#endif
{
snek_soffset_t so = snek_poly_get_soffset(p);
o = so;
if (so < 0)
Expand Down Expand Up @@ -226,10 +232,12 @@ snek_list_del(snek_poly_t lp, snek_poly_t p)
return;
snek_offset_t num = 1;
switch (snek_list_type(list)) {
#ifndef SNEK_NO_DICT
case snek_list_dict:
r--;
num = 2;
break;
#endif
case snek_list_tuple:
snek_error_value(lp);
return;
Expand Down Expand Up @@ -273,6 +281,7 @@ snek_list_imm(snek_offset_t size, snek_list_type_t type)
}

snek_poly_t *data = snek_list_data(list);
#ifndef SNEK_NO_DICT
if (type == snek_list_dict) {
list->size = 0;
snek_offset_t s = size;
Expand All @@ -285,7 +294,9 @@ snek_list_imm(snek_offset_t size, snek_list_type_t type)
*ref = value;
}
snek_stack_drop(size);
} else {
} else
#endif
{
while (size--)
data[size] = snek_stack_pop();
}
Expand Down
6 changes: 6 additions & 0 deletions snek-parse.c
Expand Up @@ -33,6 +33,12 @@ snek_token_val_t snek_token_val;
#define snek_no_slice 0
#endif

#ifdef SNEK_NO_DICT
#define snek_no_dict SNEK_NO_DICT
#else
#define snek_no_dict 0
#endif

#define GRAMMAR_TABLE
#ifdef PARSE_DEBUG
#define TOKEN_NAMES
Expand Down
2 changes: 2 additions & 0 deletions snek-poly.c
Expand Up @@ -126,8 +126,10 @@ snek_poly_len(snek_poly_t a)
case snek_list:
al = snek_poly_to_list(a);
len = al->size;
#ifndef SNEK_NO_DICT
if (snek_list_type(al) == snek_list_dict)
len /= 2;
#endif
return len;
default:
return 0;
Expand Down
19 changes: 14 additions & 5 deletions snek-print.c
Expand Up @@ -28,10 +28,12 @@ static inline char snek_list_open(snek_list_type_t type)
switch(type) {
case snek_list_list:
return '[';
case snek_list_tuple:
return '(';
default:
#ifndef SNEK_NO_DICT
return '{';
case snek_list_tuple:
#endif
return '(';
}
}

Expand All @@ -40,10 +42,12 @@ static inline char snek_list_close(snek_list_type_t type)
switch(type) {
case snek_list_list:
return ']';
case snek_list_tuple:
return ')';
default:
#ifndef SNEK_NO_DICT
return '}';
case snek_list_tuple:
#endif
return ')';
}
}

Expand Down Expand Up @@ -140,8 +144,13 @@ snek_poly_format(snek_buf_t *buf, snek_poly_t a, char format)
list = snek_stack_pop_list();
snek_stack_push_list(list);
snek_poly_format(buf, snek_list_data(list)[o], format);
#ifdef SNEK_NO_DICT
#define list_sep_char(type,o) ','
#else
#define list_sep_char(type,o) (((type) == snek_list_dict && !((o)&1)) ? ':' : ',')
#endif
if (o < size - 1 || (size == 1 && type == snek_list_tuple)) {
buf->put_c((type == snek_list_dict && !(o&1)) ? ':' : ',', closure);
buf->put_c(list_sep_char(type, o), closure);
buf->put_c(' ', closure);
}
}
Expand Down
5 changes: 4 additions & 1 deletion snek-string.c
Expand Up @@ -185,7 +185,10 @@ snek_string_interpolate(char *a, snek_poly_t poly)
is_list = false;
if (snek_poly_type(poly) == snek_list) {
snek_list_t *list = snek_poly_to_list(poly);
if (snek_list_type(list) != snek_list_dict) {
#ifndef SNEK_NO_DICT
if (snek_list_type(list) != snek_list_dict)
#endif
{
is_list = true;
size = list->size;
}
Expand Down
12 changes: 9 additions & 3 deletions snek.defs
Expand Up @@ -101,13 +101,21 @@ SNEK_BASE_CFLAGS = \

LOLA_FLAGS ?=


ifdef SNEK_NO_SLICE
SNEK_BASE_CFLAGS += -DSNEK_NO_SLICE
SNEK_LOLA_FLAGS += -DSNEK_NO_SLICE
else
SNEK_LOLA_FLAGS += -DSNEK_SLICE
endif

ifdef SNEK_NO_DICT
SNEK_BASE_CFLAGS += -DSNEK_NO_DICT
SNEK_LOLA_FLAGS += -DSNEK_NO_DICT
else
SNEK_LOLA_FLAGS += -DSNEK_DICT
endif

SNEK_LDFLAGS = -Wl,--gc-sections

SNEK_CFLAGS ?= $(SNEK_WARNINGS) $(SNEK_BASE_CFLAGS)
Expand All @@ -126,10 +134,8 @@ SNEK_SED = sed \

ifndef SNEK_NO_BUILD_TARGETS

LOLA_FLAGS =

snek-gram.h: $(SNEK_ROOT)/snek-gram.ll
lola $(LOLA_FLAGS) -o $@ $^
lola $(SNEK_LOLA_FLAGS) -o $@ $^

snek-builtin.h: $(SNEK_ROOT)/snek-builtin.py $(SNEK_BUILTINS)
python3 $^ -o $@
Expand Down
4 changes: 4 additions & 0 deletions snek.h
Expand Up @@ -133,7 +133,9 @@ typedef enum {
snek_op_string,
snek_op_list,
snek_op_tuple,
#ifndef SNEK_NO_DICT
snek_op_dict,
#endif
snek_op_id,

snek_op_not,
Expand Down Expand Up @@ -210,7 +212,9 @@ typedef struct snek_mem {
typedef enum {
snek_list_list,
snek_list_tuple,
#ifndef SNEK_NO_DICT
snek_list_dict
#endif
} __attribute__((packed)) snek_list_type_t;

typedef struct snek_list {
Expand Down

0 comments on commit 6f265a2

Please sign in to comment.