Commit 487f0053 authored by Chris Müller's avatar Chris Müller
Browse files

Add named let as loop to cherry.

parent 5b68b0f2
...@@ -186,6 +186,7 @@ extern struct cherry_value* cherry_symbol_define; ...@@ -186,6 +186,7 @@ extern struct cherry_value* cherry_symbol_define;
extern struct cherry_value* cherry_symbol_let; extern struct cherry_value* cherry_symbol_let;
extern struct cherry_value* cherry_symbol_lambda; extern struct cherry_value* cherry_symbol_lambda;
extern struct cherry_value* cherry_symbol_if; extern struct cherry_value* cherry_symbol_if;
extern struct cherry_value* cherry_symbol_loop;
extern struct cherry_value* cherry_symbol_begin; extern struct cherry_value* cherry_symbol_begin;
extern struct cherry_value* cherry_symbol_try; extern struct cherry_value* cherry_symbol_try;
...@@ -251,6 +252,9 @@ struct cherry_value* cherry_system_exit(struct cherry_environment* env, struc ...@@ -251,6 +252,9 @@ struct cherry_value* cherry_system_exit(struct cherry_environment* env, struc
#define IS_LAMBDA(value) \ #define IS_LAMBDA(value) \
IS_TAGGED(value, cherry_symbol_lambda) IS_TAGGED(value, cherry_symbol_lambda)
#define IS_LOOP(value) \
IS_TAGGED(value, cherry_symbol_loop)
#define IS_BEGIN(value) \ #define IS_BEGIN(value) \
IS_TAGGED(value, cherry_symbol_begin) IS_TAGGED(value, cherry_symbol_begin)
......
...@@ -689,7 +689,7 @@ cherry_read_pair(struct cherry_context* context) ...@@ -689,7 +689,7 @@ cherry_read_pair(struct cherry_context* context)
head = cherry_read(context); head = cherry_read(context);
tail = cherry_read_pair(context); tail = cherry_read_pair(context);
return (struct cherry_value*) cherry_list_cons(head, tail); return cherry_list_cons(head, tail);
} }
static struct cherry_value* static struct cherry_value*
...@@ -754,6 +754,32 @@ cherry_transform_if(struct cherry_value* value) ...@@ -754,6 +754,32 @@ cherry_transform_if(struct cherry_value* value)
} }
static struct cherry_value*
cherry_transform_loop(struct cherry_value* value)
{
struct cherry_value* name = HEAD(HEAD(value));
struct cherry_value* args = cherry_list_reverse(TAIL(HEAD(value)));
struct cherry_value* body = TAIL(value);
struct cherry_value* params = cherry_emptylist;
struct cherry_value* values = cherry_emptylist;
while(!IS_NULL(args)) {
struct cherry_value* duo = HEAD(args);
params = cherry_list_cons(HEAD(duo), params);
values = cherry_list_cons(HEAD(TAIL(duo)), values);
args = TAIL(args);
}
struct cherry_value* lambda = cherry_list_cons(cherry_symbol_lambda, cherry_list_cons(params, body));
struct cherry_value* let = cherry_list_cons(cherry_symbol_let, cherry_list_cons(name, cherry_list_cons(lambda, cherry_emptylist)));
struct cherry_value* app = cherry_list_cons(name, values);
return cherry_list_cons(cherry_symbol_begin, cherry_list_cons(let, cherry_list_cons(app, cherry_emptylist)));
}
static struct cherry_value* static struct cherry_value*
cherry_transform(struct cherry_value* value) cherry_transform(struct cherry_value* value)
{ {
...@@ -763,7 +789,9 @@ cherry_transform(struct cherry_value* value) ...@@ -763,7 +789,9 @@ cherry_transform(struct cherry_value* value)
return cherry_transform_define(TAIL(value)); return cherry_transform_define(TAIL(value));
else if(IS_IF(value)) else if(IS_IF(value))
return cherry_transform_if(value); return cherry_transform_if(value);
else else if(IS_LOOP(value)) {
return cherry_transform_loop(TAIL(value));
} else
return value; return value;
} }
...@@ -829,5 +857,6 @@ cherry_read(struct cherry_context* context) ...@@ -829,5 +857,6 @@ cherry_read(struct cherry_context* context)
return 0; return 0;
RETURN_VALUE: RETURN_VALUE:
return cherry_transform(value); return cherry_transform(value);
} }
...@@ -35,6 +35,7 @@ struct cherry_value* cherry_symbol_let; ...@@ -35,6 +35,7 @@ struct cherry_value* cherry_symbol_let;
struct cherry_value* cherry_symbol_lambda; struct cherry_value* cherry_symbol_lambda;
struct cherry_value* cherry_symbol_if; struct cherry_value* cherry_symbol_if;
struct cherry_value* cherry_symbol_quote; struct cherry_value* cherry_symbol_quote;
struct cherry_value* cherry_symbol_loop;
struct cherry_value* cherry_symbol_try; struct cherry_value* cherry_symbol_try;
struct cherry_value* cherry_symbol_begin; struct cherry_value* cherry_symbol_begin;
...@@ -66,6 +67,7 @@ cherry_initialize(struct cherry_value* arguments) ...@@ -66,6 +67,7 @@ cherry_initialize(struct cherry_value* arguments)
cherry_symbol_let = cherry_symbol("let"); cherry_symbol_let = cherry_symbol("let");
cherry_symbol_lambda = cherry_symbol("lambda"); cherry_symbol_lambda = cherry_symbol("lambda");
cherry_symbol_if = cherry_symbol("if"); cherry_symbol_if = cherry_symbol("if");
cherry_symbol_loop = cherry_symbol("loop");
cherry_symbol_begin = cherry_symbol("begin"); cherry_symbol_begin = cherry_symbol("begin");
cherry_symbol_catch = cherry_symbol("catch"); cherry_symbol_catch = cherry_symbol("catch");
cherry_symbol_try = cherry_symbol("try"); cherry_symbol_try = cherry_symbol("try");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment