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

add first print functions for cherry values

parent add85075
......@@ -111,15 +111,16 @@ struct org_cherry_value* org_cherry_primitive_add(struct org_cherry_pair* pai
// Symboltables
// ----------------------------------------------------------------------------
struct org_cherry_symboltable;
struct org_cherry_symbollist;
struct org_cherry_symboltable* org_cherry_symboltable(void);
struct org_cherry_value* org_cherry_symbollist_get(struct org_cherry_symboltable* table, cy_byte_t* name);
struct org_cherry_symbollist* org_cherry_symbollist(void);
struct org_cherry_value* org_cherry_symbollist_get(struct org_cherry_symbollist* table, cy_byte_t* name);
extern struct org_cherry_symboltable* org_cherry_global_symboltable;
extern struct org_cherry_symbollist* org_cherry_global_symbollist;
extern struct org_cherry_value* org_cherry_symbol_emptylist;
extern struct org_cherry_value* org_cherry_symbol_true;
extern struct org_cherry_value* org_cherry_symbol_false;
extern struct org_cherry_value* org_cherry_symbol_quote;
extern struct org_cherry_value* org_cherry_symbol_define;
extern struct org_cherry_value* org_cherry_symbol_lambda;
extern struct org_cherry_value* org_cherry_symbol_if;
......@@ -137,6 +138,13 @@ extern struct org_cherry_value* org_cherry_symbol_else;
#define IS_VARIABLE(value) \
IS_SYMBOL(value)
// ----------------------------------------------------------------------------
// Print
// ----------------------------------------------------------------------------
void org_cherry_print(FILE* out, struct org_cherry_value* value);
// ----------------------------------------------------------------------------
// Default
// ----------------------------------------------------------------------------
......
......@@ -35,9 +35,9 @@ struct RbNode {
struct RbNode* right;
};
struct org_cherry_symboltable {
struct org_cherry_symbollist {
struct RbNode* root;
struct org_cherry_symboltable* parent;
struct org_cherry_symbollist* parent;
};
......@@ -141,7 +141,7 @@ rbnode_trinode_restructering(struct RbNode* node)
static void
rbnode_remedy_double_red(struct org_cherry_symboltable* tree, struct RbNode* node_z)
rbnode_remedy_double_red(struct org_cherry_symbollist* tree, struct RbNode* node_z)
{
assert(tree != 0 && node_z != 0);
......@@ -177,7 +177,7 @@ rbnode_remedy_double_red(struct org_cherry_symboltable* tree, struct RbNode* nod
static void
rbnode_remedey_double_black(struct org_cherry_symboltable* tree, struct RbNode* node_x, struct RbNode* node_r)
rbnode_remedey_double_black(struct org_cherry_symbollist* tree, struct RbNode* node_x, struct RbNode* node_r)
{
assert(node_x != 0);
......@@ -243,10 +243,10 @@ rbnode_remedey_double_black(struct org_cherry_symboltable* tree, struct RbNode*
}
struct org_cherry_symboltable*
org_cherry_symboltable(void)
struct org_cherry_symbollist*
org_cherry_symbollist(void)
{
struct org_cherry_symboltable* s = GC_MALLOC(sizeof(struct org_cherry_symboltable));
struct org_cherry_symbollist* s = GC_MALLOC(sizeof(struct org_cherry_symbollist));
s->root = 0;
s->parent = 0;
return s;
......@@ -288,7 +288,7 @@ string_compare(cy_byte_t* A, cy_byte_t* B)
struct org_cherry_value*
org_cherry_symbollist_get(struct org_cherry_symboltable* tree, cy_byte_t* name)
org_cherry_symbollist_get(struct org_cherry_symbollist* tree, cy_byte_t* name)
{
assert(tree != 0 && name != 0);
......
......@@ -25,11 +25,18 @@
// globals
// ----------------------------------------------------------------------------
struct org_cherry_symboltable* org_cherry_global_symboltable;
struct org_cherry_symbollist* org_cherry_global_symbollist;
struct org_cherry_value* org_cherry_symbol_emptylist;
struct org_cherry_value* org_cherry_symbol_true;
struct org_cherry_value* org_cherry_symbol_false;
struct org_cherry_value* org_cherry_symbol_define;
struct org_cherry_value* org_cherry_symbol_lambda;
struct org_cherry_value* org_cherry_symbol_cond;
struct org_cherry_value* org_cherry_symbol_if;
struct org_cherry_value* org_cherry_symbol_else;
struct org_cherry_value* org_cherry_symbol_quote;
// ----------------------------------------------------------------------------
// cherry object constructors
......@@ -150,7 +157,7 @@ org_cherry_primitive_add(struct org_cherry_pair* pair)
void
org_cherry_initialize(struct org_cherry_pair* arguments)
{
org_cherry_global_symboltable = org_cherry_symboltable();
org_cherry_global_symbollist = org_cherry_symbollist();
org_cherry_symbol_true = org_cherry_value_alloc();
org_cherry_symbol_true->meta.type = CY_BOOLEAN;
......@@ -163,9 +170,100 @@ org_cherry_initialize(struct org_cherry_pair* arguments)
org_cherry_symbol_emptylist = org_cherry_value_alloc();
org_cherry_symbol_emptylist->meta.type = CY_EMPTYLIST;
org_cherry_symbol_define = org_cherry_symbol("quote");
org_cherry_symbol_define = org_cherry_symbol("define");
org_cherry_symbol_lambda = org_cherry_symbol("lambda");
org_cherry_symbol_if = org_cherry_symbol("if");
org_cherry_symbol_cond = org_cherry_symbol("cond");
org_cherry_symbol_else = org_cherry_symbol("else");
}
// ----------------------------------------------------------------------------
// Print
// ----------------------------------------------------------------------------
static void
print_pair(FILE* out, struct org_cherry_pair* value)
{
org_cherry_value* head = HEAD(value);
org_cherry_value* tail = TAIL(value);
org_cherry_print(out, head);
if(IS_PAIR(tail)) {
fprintf(out, " ");
print_pair(out, tail);
} else if(IS_NULL(tail)) {
return;
} else {
fprintf(out, " . ");
org_cherry_print(out, tail);
}
}
void
org_cherry_print(FILE* out, struct org_cherry_pair* value)
{
cy_byte_t* p;
switch(value->meta.type) {
case CY_EMPTYLIST:
fprintf(out, "()");
break;
case CY_BOOLEAN:
fprintf(out, (value->boolean_value) ? "true" : "false");
break;
case CY_SYMBOL:
fprintf(out, "%s", value->symbol_value);
break;
case CY_FIXNUM:
fprintf(out, "%ld", value->fixnum_value);
break;
case CY_FLOAT:
fprintf(out, "%lf", value->float_value);
break;
case CY_CHAR:
fprintf(out, "\\");
switch(value->char_value) {
case '\n':
fprintf(out, "newline");
break
case ' ':
fprintf(out, "space");
break;
default:
fprintf(out, "%c", value->char_value);
}
break;
case CY_STRING:
p = value->string_value;
fprintf(out, "\"");
while(*p != '\0') {
switch(*p) {
case '\n':
fprintf(out, "\\n");
break;
case '\\':
fprintf(out, "\\");
break;
case '\"':
fprintf(out, "\\\"");
break;
default:
fprintf(out, "%c", *p);
}
p++;
}
fprintf(out, "\"");
break;
case CY_PAIR:
fprintf(out, "(");
print_pair(value);
fprintf(out, ")");
break;
default:
fprintf(stderr, "cannot write unknown type\n");
break;
}
}
Supports Markdown
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