Skip to content

Commit a9ebc57

Browse files
author
mmolle
committed
* limpy is halfway usable now...
1 parent 381182b commit a9ebc57

File tree

8 files changed

+321
-48
lines changed

8 files changed

+321
-48
lines changed

builtinenv.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ struct { char *name; limo_builtin f; } builtin_array[] = {
3535
{ "SLEEP", builtin_sleep },
3636
{ "STRING-CONCAT", builtin_string_concat },
3737
{ "MAKE-SYM", builtin_make_sym },
38-
{ "GET_ANNOTATION", builtin_get_annotation }
38+
{ "GET-ANNOTATION", builtin_get_annotation },
39+
{ "MAKE-DICT", builtin_make_dict },
40+
{ "DICT-GET", builtin_dict_get },
41+
{ "DICT-SET", builtin_dict_set },
42+
{ "DICT-UNSET", builtin_dict_unset },
43+
{ "DICT-TO-LIST", builtin_dict_to_list },
44+
{ "DICT-HAS-KEY", builtin_dict_has_key },
3945
};
4046

4147
limo_data *make_globalenv(int argc, char **argv)

dict.c

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,19 @@ limo_data **dict_get_place(limo_data *dict, limo_data *key)
113113
limo_error("dict_get_place(): this should not happen!");
114114
}
115115

116-
limo_data *dict_remove(limo_data *dict, limo_data *key)
116+
void dict_remove(limo_data *dict, limo_data *key)
117117
{
118-
limo_data **place = dict_get_place(dict, key);
119-
(*place) = NULL;
120-
dict->data.d_dict->used--;
118+
limo_data **place;
121119

122-
// dict_resize MUST be done, or else items, which should have been stored in the same bucket as *key
123-
// are unfindable after removal of this. SERIOUSLY hard to find bug!
124-
dict_resize(dict);
120+
place = dict_get_place(dict, key);
121+
if (place !=NULL) {
122+
(*place) = NULL;
123+
dict->data.d_dict->used--;
124+
125+
// dict_resize MUST be done, or else items, which should have been stored in the same bucket as *key
126+
// are unfindable after removal of this. SERIOUSLY hard to find bug!
127+
dict_resize(dict);
128+
}
125129
}
126130

127131
limo_data *dict_to_list(limo_data *dict)
@@ -135,3 +139,103 @@ limo_data *dict_to_list(limo_data *dict)
135139

136140
return res;
137141
}
142+
143+
////////// BUILTINS ////////////
144+
145+
BUILTIN(builtin_make_dict)
146+
{
147+
return make_dict();
148+
}
149+
150+
BUILTIN(builtin_dict_get)
151+
{
152+
limo_data *dict;
153+
limo_data *key;
154+
limo_data **res;
155+
156+
if (list_length(arglist) != 3)
157+
limo_error("(dict-get DICT KEY)");
158+
159+
dict = eval(FIRST_ARG, env);
160+
if (dict->type != limo_TYPE_DICT)
161+
limo_error("(dict-get DICT KEY)");
162+
163+
key = eval(SECOND_ARG, env);
164+
res = dict_get_place(dict, key);
165+
if (res == NULL)
166+
throw(make_cons(make_string("Could not find key"), key));
167+
return *res;
168+
}
169+
170+
BUILTIN(builtin_dict_set)
171+
{
172+
limo_data *dict;
173+
limo_data *key;
174+
limo_data *value;
175+
176+
if (list_length(arglist) != 4)
177+
limo_error("(dict-set DICT KEY VALUE)");
178+
179+
dict = eval(FIRST_ARG, env);
180+
if (dict->type != limo_TYPE_DICT)
181+
limo_error("(dict-set DICT KEY VALUE)");
182+
183+
key = eval(SECOND_ARG, env);
184+
value = eval(THIRD_ARG, env);
185+
186+
dict_put(dict, key, value);
187+
return make_nil();
188+
}
189+
190+
191+
BUILTIN(builtin_dict_unset)
192+
{
193+
limo_data *dict;
194+
limo_data *key;
195+
196+
if (list_length(arglist) != 3)
197+
limo_error("(dict-unset DICT KEY)");
198+
199+
dict = eval(FIRST_ARG, env);
200+
if (dict->type != limo_TYPE_DICT)
201+
limo_error("(dict-unset DICT KEY)");
202+
203+
key = eval(SECOND_ARG, env);
204+
dict_remove(dict, key);
205+
return make_nil();
206+
}
207+
208+
BUILTIN(builtin_dict_has_key)
209+
{
210+
limo_data *dict;
211+
limo_data *key;
212+
limo_data **res;
213+
214+
if (list_length(arglist) != 3)
215+
limo_error("(dict-has-key DICT KEY)");
216+
217+
dict = eval(FIRST_ARG, env);
218+
if (dict->type != limo_TYPE_DICT)
219+
limo_error("(dict-has-key DICT KEY)");
220+
221+
key = eval(SECOND_ARG, env);
222+
res = dict_get_place(dict, key);
223+
if (res == NULL)
224+
return make_nil();
225+
else
226+
return sym_true;
227+
}
228+
229+
BUILTIN(builtin_dict_to_list)
230+
{
231+
limo_data *dict;
232+
233+
if (list_length(arglist) != 2)
234+
limo_error("(dict-unset DICT KEY)");
235+
236+
dict = eval(FIRST_ARG, env);
237+
if (dict->type != limo_TYPE_DICT)
238+
limo_error("(dict-unset DICT KEY)");
239+
240+
return dict_to_list(dict);
241+
}

limo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ int main(int argc, char **argv)
7676
if (sigsetjmp(*ljbuf, 1)) {
7777
printf("\nUNHANDLED EXCEPTION CAUGHT\n");
7878
if (exception) {
79+
rs = limo_rs_make_readline();
7980
print_stacktrace(var_lookup(globalenv, sym_stacktrace));
8081
stacktrace = make_nil();
8182
writer(exception);

limo.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ limo_dict *make_dict_size(int minused);
146146
void dict_resize(limo_data *dict);
147147
void dict_put(limo_data *dict, limo_data *key, limo_data *value);
148148
limo_data **dict_get_place(limo_data *dict, limo_data *key);
149-
limo_data *dict_remove(limo_data *dict, limo_data *key);
149+
void dict_remove(limo_data *dict, limo_data *key);
150150
limo_data *dict_to_list(limo_data *dict);
151151

152152
limo_data *var_lookup(limo_data *env, limo_data *name);
@@ -195,6 +195,12 @@ BUILTIN(builtin_sleep);
195195
BUILTIN(builtin_string_concat);
196196
BUILTIN(builtin_make_sym);
197197
BUILTIN(builtin_get_annotation);
198+
BUILTIN(builtin_make_dict);
199+
BUILTIN(builtin_dict_get);
200+
BUILTIN(builtin_dict_set);
201+
BUILTIN(builtin_dict_unset);
202+
BUILTIN(builtin_dict_to_list);
203+
BUILTIN(builtin_dict_has_key);
198204

199205
limo_data *real_eval(limo_data *form, limo_data *env);
200206
limo_data *eval(limo_data *form, limo_data *env);

0 commit comments

Comments
 (0)