gpy_object_t * gpy_obj_list_new (gpy_typedef_t * type, gpy_object_t ** args) { gpy_object_t * retval = NULL_OBJECT; bool check = gpy_args_check_fmt (args, "i,V."); gpy_assert (check); int len = gpy_args_lit_parse_int (args [0]); gpy_object_t ** vec = gpy_args_lit_parse_vec (args [1]); struct gpy_object_list * self = (struct gpy_object_list *) gpy_malloc (sizeof (struct gpy_object_list)); int size = gpy_threshold_alloc (len); self->vector = (gpy_object_t **) gpy_calloc (size, sizeof (gpy_object_t *)); self->length = len; self->size = size; int i; for (i = 0; i < len; ++i) self->vector[i] = vec [i]; retval = gpy_create_object_state (type, self); return retval; }
gpy_object_t * gpy_create_object_state (gpy_typedef_t * type, void * self) { gpy_object_state_t * state = (gpy_object_state_t *) gpy_malloc (sizeof(gpy_object_state_t)); state->identifier = strdup (type->identifier); state->ref_count = 0; state->state = self; state->definition = type; gpy_object_t * retval = (gpy_object_t *) gpy_malloc (sizeof(gpy_object_t)); retval->T = TYPE_OBJECT_STATE; retval->o.object_state = state; return retval; }
gpy_object_t * gpy_obj_integer_new (gpy_typedef_t * type, gpy_object_t * args) { gpy_object_t * retval = NULL_OBJECT; bool check = gpy_args_check_fmt (args, "i."); gpy_assert (check); int val = gpy_args_lit_parse_int (&args[0]); struct gpy_obj_integer_t * self = (struct gpy_obj_integer_t *) gpy_malloc (sizeof (struct gpy_obj_integer_t)); self->Int = val; retval = gpy_create_object_state (type, self); return retval; }
gpy_object_t * gpy_obj_string_new (gpy_typedef_t * type, gpy_object_t ** args) { gpy_object_t * retval = NULL_OBJECT; bool check = gpy_args_check_fmt (args, "s."); gpy_assert (check); char * string = gpy_args_lit_parse_string (args [0]); struct gpy_object_string * self = (struct gpy_object_string *) gpy_malloc (sizeof (struct gpy_object_string)); self->string = gpy_strdup (string); retval = gpy_create_object_state (type, self); return retval; }
static gpy_object_t * gpy_obj_list_add (gpy_object_t * o1, gpy_object_t * o2) { gpy_assert (o1->T == TYPE_OBJECT_STATE); gpy_assert (o2->T == TYPE_OBJECT_STATE); gpy_object_state_t * x = o1->o.object_state; gpy_object_state_t * y = o2->o.object_state; if (strcmp (x->identifier, "List") != 0 || strcmp (y->identifier, "List") != 0) { fatal ("invalid object types for '+': <%s> and <%s>\n", x->identifier, y->identifier); } struct gpy_object_list * l1 = (struct gpy_object_list *) x->state; struct gpy_object_list * l2 = (struct gpy_object_list *) y->state; int n = l1->length + l2->length; int ns = gpy_threshold_alloc (n + 1); gpy_object_t ** elems = (gpy_object_t **) gpy_calloc (ns, sizeof (gpy_object_t *)); int i, z; for (i = 0; i < l1->length; ++i) elems [i] = l1->vector [i]; for (z = 0; z < l2->length; ++z) { elems [i] = l2->vector [z]; i++; } elems [n] = NULL; struct gpy_object_list * self = (struct gpy_object_list *) gpy_malloc (sizeof (struct gpy_object_list)); self->vector = elems; self->length = n; self->size = ns; gpy_object_t * retval = gpy_create_object_state (__gpy_list_type_node, self); gpy_assert (retval->T == TYPE_OBJECT_STATE); return retval; }
/* args = code addr/nargs */ gpy_object_t * gpy_object_func_new (gpy_typedef_t * type, gpy_object_t * args) { gpy_object_t * retval = NULL_OBJECT; bool check = gpy_args_check_fmt (args, "s,p,i."); gpy_assert (check); char * id = gpy_args_lit_parse_string (&args[0]); unsigned char * code_addr = gpy_args_lit_parse_pointer (&args[1]); int nargs = gpy_args_lit_parse_int (&args[2]); struct gpy_object_func_t * self = gpy_malloc (type->state_size); self->identifier = id; self->code = (GPY_CFUNC) code_addr; self->nargs = nargs; retval = gpy_create_object_decl (type, self); return retval; }
void gpy_wrap_builtins (gpy_typedef_t * const type, size_t len) { struct gpy_builtinAttribs_t * builtins = type->builtins; struct gpy_builtinAttribs_t atm; if (len > 1) { gpy_object_t ** folded = (gpy_object_t **) gpy_calloc (len -1, sizeof (gpy_object_t *)); int idx; for (idx = 0; builtins[idx].identifier != NULL; ++idx) { atm = builtins [idx]; gpy_object_t * builtin = NULL_OBJECT; gpy_object_t ** args = (gpy_object_t **) gpy_calloc (4, sizeof(gpy_object_t*)); gpy_literal_t i; i.type = TYPE_STRING; i.literal.string = (char *)atm.identifier; gpy_literal_t p; p.type = TYPE_ADDR; p.literal.addr = (unsigned char *) atm.addr; gpy_literal_t n; n.type = TYPE_INTEGER; n.literal.integer = atm.nargs; gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &i }; gpy_object_t a2 = { .T = TYPE_OBJECT_LIT, .o.literal = &p }; gpy_object_t a3 = { .T = TYPE_OBJECT_LIT, .o.literal = &n }; gpy_object_t a4 = { .T = TYPE_NULL, .o.literal = NULL }; args[0] = &a1; args[1] = &a2; args[2] = &a3; args[3] = &a4; gpy_typedef_t * def = __gpy_func_type_node; builtin = def->tp_new (def, args); gpy_free (args); gpy_assert (builtin->T == TYPE_OBJECT_DECL); folded [idx] = builtin; } /* Now to append/create the attribute access table .. */ struct gpy_object_attrib_t ** members = (struct gpy_object_attrib_t **) gpy_calloc (len, sizeof (struct gpy_object_attrib_t *)); for (idx = 0; idx < len - 1; ++idx) { atm = builtins [idx]; struct gpy_object_attrib_t * fattr = (struct gpy_object_attrib_t *) gpy_malloc (sizeof (struct gpy_object_attrib_t)); fattr->identifier = strdup (atm.identifier); fattr->T = GPY_CATTR; fattr->offset = 0; fattr->addr = folded [idx]; members[idx] = fattr; } // sentinal members[idx] = NULL; type->members_defintion = members; gpy_free (folded); } }