diff --git a/lang/main.c b/lang/main.c index 1fd46c9..2ad9fea 100644 --- a/lang/main.c +++ b/lang/main.c @@ -39,11 +39,34 @@ abort(); } +struct object; +typedef struct object *(object_call)(struct object*, struct object*); + typedef struct { + int symbol; + object_call *call; +} call_mapping; + +#define CALLS_MAX 32 + +typedef struct object { int value; atomic_int refcount; + call_mapping calls[CALLS_MAX]; + int calls_count; } object; +object *create_object(int value); + +object *add_objects(object *objA, object *objB) { + if(objB == NULL) { + printf("ERROR: objA is null\n"); + abort(); + } + int new_value = objA->value + objB->value; + return create_object(new_value); +} + object *create_object(int value) { object *obj = malloc(sizeof(object)); if(!obj) { @@ -52,6 +75,10 @@ } obj->value = value; obj->refcount = 1; + call_mapping *add_call = &obj->calls[0]; + add_call->symbol = find_symbol("Add"); + add_call->call = add_objects; + obj->calls_count = 1; return obj; } @@ -67,24 +94,15 @@ *objptr = NULL; } -object *add_objects(object *objA, object *objB) { - if(objA == NULL) { - printf("ERROR: objA is null\n"); - abort(); - } - if(objB == NULL) { - printf("ERROR: objA is null\n"); - abort(); - } - int new_value = objA->value + objB->value; - return create_object(new_value); -} - object *dispatch_call(object *obj, int sym, object *arg1) { - if(sym == find_symbol("Add")) { - return add_objects(obj, arg1); + for(int i = 0; i < obj->calls_count; ++i) { + call_mapping *curr_call = &obj->calls[i]; + int symbol = curr_call->symbol; + if(symbol == sym) { + return curr_call->call(obj, arg1); + } } - printf("ERROR: invalid call\n"); + printf("ERROR: no call found to dispatch\n"); abort(); }