Skip to content

Commit

Permalink
Add 'def' builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
uasouz committed Mar 9, 2020
1 parent c9e30ad commit 87381b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
20 changes: 20 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ HzValue* builtin_pow(HzEnv* env,HzValue* value){
return builtin_op(env,value,"^");
}

HzValue* builtin_def(HzEnv* env,HzValue* value){
LASSERT(value,value->cell[0]->type == HZVAL_QEXPR,"Function 'def' passed incorrect type.");

HzValue* symbols = value->cell[0];

for(int i =0;i< symbols->count;i++){
LASSERT(value,symbols->cell[i]->type == HZVAL_SYM,"Function 'def' cannot define non-symbol");
}

LASSERT(value,symbols->count == value->count-1,"Function 'def cannot define incorrect "
"number of values to symbols");

for(int i=0;i<symbols->count;i++){
hzenv_put(env,symbols->cell[i],value->cell[i+1]);
}

hzval_del(value);
return hzval_sexpression();
}

/*deprecated(?)*/
HzValue* builtin(HzEnv* env,HzValue* value,char* func){
if (strcmp("list", func) == 0) { return builtin_list(env,value); }
Expand Down
7 changes: 3 additions & 4 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ HzValue* builtin_eval(HzEnv* env,HzValue* value);
HzValue* builtin_join(HzEnv* env,HzValue* value);
HzValue* builtin_cons(HzEnv* env,HzValue* value);

HzValue* builtin_add(HzEnv* env,HzValue* value);
HzValue* builtin_def(HzEnv* env,HzValue* value);

//Math Function wrappers
HzValue* builtin_add(HzEnv* env,HzValue* value);
HzValue* builtin_sub(HzEnv* env,HzValue* value);

HzValue* builtin_mul(HzEnv* env,HzValue* value);

HzValue* builtin_div(HzEnv* env,HzValue* value);

HzValue* builtin_pow(HzEnv* env,HzValue* value);

#endif
1 change: 1 addition & 0 deletions hzval.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void hzenv_add_builtins(HzEnv* env){
hzenv_add_function(env, "eval", builtin_eval);
hzenv_add_function(env, "join", builtin_join);
hzenv_add_function(env, "cons", builtin_cons);
hzenv_add_function(env, "def", builtin_def);

/* Mathematical Functions */
hzenv_add_function(env, "+", builtin_add);
Expand Down
1 change: 1 addition & 0 deletions hzval.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ HzValue* hzval_eval(HzEnv*env,HzValue* value);

/*Environment */
HzEnv* hzenv_new(void);
void hzenv_put(HzEnv* env,HzValue* key,HzValue* value);
void hzenv_del(HzEnv* env);
void hzenv_add_builtins(HzEnv* env);
#endif

0 comments on commit 87381b3

Please sign in to comment.