Commit 6c88ec6f authored by Chris Müller's avatar Chris Müller
Browse files

Add cond macro to transform

parent 2cff7e4e
......@@ -175,6 +175,11 @@ extern struct value* symbol_lambda;
extern struct value* symbol_if;
extern struct value* symbol_loop;
extern struct value* symbol_begin;
extern struct value* symbol_cond;
extern struct value* symbol_match;
extern struct value* symbol_when;
extern struct value* symbol_else;
// ----------------------------------------------------------------------------
// Evaluation
......@@ -207,6 +212,9 @@ extern struct value* symbol_begin;
#define IS_IF(value) \
IS_TAGGED(value, symbol_if)
#define IS_COND(value) \
IS_TAGGED(value, symbol_cond)
#define IS_LAMBDA(value) \
IS_TAGGED(value, symbol_lambda)
......
......@@ -547,9 +547,9 @@ lex_symbol(struct context* context)
byte_t* sym = array_get(buffer, 0);
if (strcmp(sym, "True") == 0)
if (strcmp(sym, "true") == 0)
return TOK_TRUE;
else if(strcmp(sym, "False") == 0)
else if(strcmp(sym, "false") == 0)
return TOK_FALSE;
else
return TOK_SYMBOL;
......@@ -762,6 +762,35 @@ transform_loop(struct value* value)
return cons(symbol_begin, cons(let, cons(app, emptylist)));
}
static struct value*
transform_cond(struct value* clauses)
{
#define COND_CLAUSE(clauses) HEAD(clauses)
#define COND_REST(clauses) TAIL(clauses)
if(IS_NULL(clauses))
return False;
else {
if(HEAD(COND_CLAUSE(clauses)) == symbol_else) {
if(IS_NULL(COND_REST(clauses)))
return cons(symbol_begin, TAIL(COND_CLAUSE(clauses)));
else
failure(0, "else clauses is not the last statement in cond");
}
if(HEAD(COND_CLAUSE(clauses)) != symbol_when)
failure(0, "cond clauses is not starting with when");
else if(IS_NULL(TAIL(COND_CLAUSE(clauses))))
failure(0, "no predicate is given in cond clause");
else
return list4(symbol_if, HEAD(TAIL(COND_CLAUSE(clauses))),
cons(symbol_begin, TAIL(TAIL(COND_CLAUSE(clauses)))),
transform_cond(COND_REST(clauses)));
}
}
static struct value*
transform(struct value* value)
{
......@@ -769,8 +798,10 @@ transform(struct value* value)
return transform_define(TAIL(value));
else if(IS_IF(value))
return transform_if(value);
else if(IS_LOOP(value)) {
else if(IS_LOOP(value))
return transform_loop(TAIL(value));
else if (IS_COND(value)) {
return transform_cond(TAIL(value));
} else
return value;
}
......
......@@ -34,6 +34,10 @@ struct value* symbol_define;
struct value* symbol_let;
struct value* symbol_lambda;
struct value* symbol_if;
struct value* symbol_cond;
struct value* symbol_match;
struct value* symbol_when;
struct value* symbol_else;
struct value* symbol_quote;
struct value* symbol_loop;
struct value* symbol_begin;
......@@ -72,6 +76,10 @@ initialize(struct value* arguments)
symbol_if = symbol("if");
symbol_loop = symbol("loop");
symbol_begin = symbol("begin");
symbol_cond = symbol("cond");
symbol_match = symbol("match");
symbol_when = symbol("when");
symbol_else = symbol("else");
STDIN = port(stdin);
STDOUT = port(stdout);
......
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