1
0
Fork 0

fix pair object

This commit is contained in:
Alex Kotov 2023-05-04 02:49:55 +04:00
parent 10d6c9ffde
commit 4cfe141b30
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 17 additions and 14 deletions

19
main.c
View File

@ -283,7 +283,7 @@ struct Object *parse()
struct Object *expr()
{
if (!tokens_top()) error("no tokens");
assert(tokens_top());
switch (tokens_top()->type) {
case TOKEN_OPEN: return parens();
@ -327,7 +327,8 @@ struct Object *parens_part()
b = parens_part();
}
return Object_new_pair(a, b);
struct Object *object = Object_new_pair(a, b);
return object;
}
/********
@ -341,17 +342,17 @@ struct Object *eval(struct Object *const program)
return NULL;
}
if (!program->a || program->a->type != TYPE_ATOM) {
if (!program->pair.a || program->pair.a->type != TYPE_ATOM) {
error("eval expects atom");
return NULL;
}
if (strcmp(program->a->s, "sum") != 0) {
if (strcmp(program->pair.a->s, "sum") != 0) {
error("unknown func");
return NULL;
}
return func_sum(program->b);
return func_sum(program->pair.b);
}
struct Object *func_sum(struct Object *const numbers)
@ -362,16 +363,16 @@ struct Object *func_sum(struct Object *const numbers)
if (numbers->type == TYPE_NUMBER) {
object->i = numbers->i;
} else if (numbers->type == TYPE_PAIR) {
if (numbers->a->type != TYPE_NUMBER) {
if (numbers->pair.a->type != TYPE_NUMBER) {
error("type error");
return NULL;
}
object->i = numbers->a->i;
object->i = numbers->pair.a->i;
struct Object *b = eval(numbers->b);
struct Object *b = eval(numbers->pair.b);
if (numbers->b->type != TYPE_NUMBER) {
if (numbers->pair.b->type != TYPE_NUMBER) {
error("type error");
return NULL;
}

View File

@ -18,8 +18,8 @@ static struct Object *new(const enum Type type)
struct Object *Object_new_pair(struct Object *const a, struct Object *const b)
{
struct Object *const object = new(TYPE_PAIR);
object->a = a;
object->b = b;
object->pair.a = a;
object->pair.b = b;
return object;
}
@ -64,8 +64,8 @@ void Object_print(struct Object *const self, const unsigned indent)
switch (self->type) {
case TYPE_PAIR:
printf("\n");
Object_print(self->a, indent + 1);
Object_print(self->b, indent + 1);
Object_print(self->pair.a, indent + 1);
Object_print(self->pair.b, indent + 1);
break;
case TYPE_ATOM:
case TYPE_STRING:

View File

@ -9,7 +9,9 @@ struct Object {
enum Type type;
union {
// For PAIR
struct Object *a, *b;
struct {
struct Object *a, *b;
} pair;
// For ATOM, STRING
char *s;
// For NUMBER