home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: alt.sources
- Path: sparky!uunet!wupost!csus.edu!netcom.com!thinman
- From: thinman@netcom.com (Technically Sweet)
- Subject: Re: COOL: C Object-Oriented Library: part 0 of 4
- Message-ID: <1992Dec28.195232.22423@netcom.com>
- Organization: International Foundation for Internal Freedom
- References: <1992Dec23.191426.10202@netcom.com> <1992Dec28.191137.13827@netcom.com>
- Date: Mon, 28 Dec 1992 19:52:32 GMT
- Lines: 425
-
- OK, here's the fix: substitute this lookup.c for the one in the
- shars. The tests now run to completion on a Sparc.
-
- ---------------- lookup.c ------------ snippy snippy ------------
-
- /*
- * COOL: C Object-Oriented Library
- *
- * Object and method string lookups
- *
- * Copyright 1991 by Lance Norskog
- *
- * June 1992: implement overloaded methods
- * Dec. 1992: fix bugs in overloaded methods
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation. This software is provided "as is" without express or
- * implied warranty.
- */
-
- #include <stdio.h>
- #include <setjmp.h>
- #ifdef USG
- #include <string.h>
- #else
- #include <strings.h>
- #endif
- #include "cool.h"
- #include "coolint.h"
-
- /*
- * Look up an object in the object table.
- * Return error if not found.
- *
- * Internal use only!
- */
-
-
- object_t
- cool_getobject(object)
- object_t object;
- {
- cint i;
-
- if (ISOBJECT(object))
- if (GOODGEN(object))
- return object;
- else cool_raise("BadGeneration", CODEMARK);
- else {
-
- /* yeah yeah yeah. This should use ob_next for a live list. */
- for(i = 0; i < MAXOBJECTS; i++)
- if (cool_objtab[i].ob_name &&
- cool_streq(cool_objtab[i].ob_name,
- (char *) object))
- break;
- if (i == MAXOBJECTS)
- return OBJECTERR;
- return (object_t) MKOBJECT(i, cool_objtab[i].ob_generation);
- }
- }
-
- /*
- * Look up an object in the object table.
- * Raise exception if not found.
- */
-
- object_t
- cool_object(object)
- object_t object;
- {
- object_t obj;
-
- obj = cool_getobject(object);
- if (obj == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- return obj;
- }
-
- int
- cool_isobject(object)
- object_t object;
- {
- object_t obj;
-
- obj = cool_getobject(object);
- return (obj != OBJECTERR);
- }
-
- /*
- * Get a text name of an object.
- */
-
- char *
- cool_nameof(object)
- object_t object;
- {
- object_t obj;
-
- obj = cool_getobject(object);
- if (obj == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- return cool_objtab[OBJECTOF(obj)].ob_name;
- }
-
- /*
- * Get a class object of an object.
- */
-
- object_t
- cool_classof(object)
- object_t object;
- {
- object_t ob;
- objtab_t *obj;
- int o, p;
-
- ob = cool_getobject(object);
- if (ob == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- obj = &cool_objtab[OBJECTOF(ob)];
- return cool_objtab[OBJECTOF(ob)].ob_private[MYMETHODS]->class;
- }
-
- /*
- * Get a component object of an object.
- */
-
- object_t
- cool_componentof(object, name)
- object_t object;
- char *name;
- {
- char *obname, fullname[128];
- object_t compob;
-
- obname = cool_nameof(object);
- sprintf(fullname, "%s.%s", obname, name);
- compob = cool_getobject((object_t) fullname);
- if (compob == OBJECTERR)
- cool_raise("ComponentDoesNotExist", fullname);
- return compob;
- }
-
- /*
- * Look up a method in a particular method table.
- * Internal use only!
- */
-
- method_t
- cool_getmethod(meth, mt)
- method_t meth;
- methtab_t *mt;
- {
- cint i, m, match;
- sint matches;
-
- if (ISMETHOD(meth))
- return meth;
- if (mt == NULL)
- return METHODERR;
-
- for(matches = match = m = 0; m < MAXMETHODS; m++, mt++)
- if (mt->m_name && metheq(mt, (char *) meth)) {
- match = m;
- matches++;
- }
- /* hack! make C keep working the old way for "Create"
- if (matches == 1)
- return (method_t) match;
- */
-
- /* return last match */
- return (matches == 0) ? METHODERR : (method_t) match;
- }
-
- /* walk methods - each time return next method with same name */
- method_t
- cool_walkmethods(object, name, mp, retp, nargsp, argsp)
- object_t object;
- char *name;
- method_t *mp;
- char **retp;
- int *nargsp;
- char ***argsp;
- {
- object_t ob;
- objtab_t *obj;
- method_t meth;
- methtab_t *mt;
- int m, p;
-
- ob = cool_getobject(object);
- if (ob == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- obj = &cool_objtab[OBJECTOF(ob)];
-
- if (*mp == (method_t) -1) {
- m = 0;
- p = MYMETHODS;
- } else {
- /* Search starting after given parent/method */
- m = METHODOF(*mp);
- p = PARENTOF(*mp);
- if (++m == MAXMETHODS) {
- p--;
- m = 0;
- }
- }
- for(; p >= 0; p--, m = 0) {
- if (! (mt = obj->ob_methtab[p]))
- continue;
- mt = &mt[m];
-
- for(; m < MAXMETHODS; m++, mt++)
- if (cool_streq(mt->m_name, name)) {
- *mp = MKMETHOD(p, m);
- *retp = mt->m_rettype;
- *argsp = mt->m_argtypes;
- *nargsp = mt->m_nargs;
- return 1;
- }
- }
-
- /* no more of this name */
- return 0;
- }
-
- method_t
- cool_method(object, method)
- object_t object;
- method_t method;
- {
- object_t ob;
- objtab_t *obj;
- method_t meth;
- methtab_t *mt;
- int p;
-
- ob = cool_getobject(object);
- if (ob == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- obj = &cool_objtab[OBJECTOF(ob)];
- /* Search object's methods, then parents' methods */
- for(p = MYMETHODS; p >= 0; p--) {
- if (mt = obj->ob_methtab[p])
- if ((meth = cool_getmethod(method, mt)) != METHODERR)
- return MKMETHOD(p, meth);
- }
- cool_raise("MethodDoesNotExist", method);
- }
-
-
- int
- cool_hasmethod(object, method)
- object_t object;
- method_t method;
- {
-
- object_t ob;
- objtab_t *obj;
- method_t meth;
- methtab_t *mt;
- int p;
-
- ob = cool_getobject(object);
- if (ob == OBJECTERR)
- cool_raise("ObjectDoesNotExist", object);
- obj = &cool_objtab[OBJECTOF(ob)];
- /* Search object's methods, then parents' methods */
- for(p = MYMETHODS; p >= 0; p--) {
- if (mt = obj->ob_methtab[p])
- if ((meth = cool_getmethod(method, mt)) != METHODERR)
- return 1;
- }
- return 0;
- }
-
- static unsigned long argval[9];
- static char scmtype[9];
- static char cooltype[9];
- static char **ctype;
-
- /* table of names corresponding to argument types */
- char *ctypenames[] = {
- 0,
- "", /* end of argument list */
- "integer",
- "bool",
- "char",
- "string",
- "object",
- "msg",
- "double",
- "ivect", /* vector of integers */
- "dvect", /* vector of doubles */
- "*", /* anything */
- /* VR add-ons */
- "point", /* 3 or 4 doubles */
- "homog", /* homogeneous doubles matrix (4x4) */
- (char *) 0
- };
-
- /* digest an argument list. Return number of arguments found, -1 if no list */
- int
- methdigest(mt, s)
- methtab_t *mt;
- char *s;
- {
- int i, j;
- char *rt, *at;
-
- mt->m_name = s;
-
- /* add optional return type and arg type list */
- /* chop up copy of full string into words, add to list */
- /* syntax: method[=[return]][:[argtype[,argtype]*]] */
- /* XXX doesn't catch case where same arg list and different return */
- mt->m_rettype = (char *) 0;
- for(i = 0; i < MAXARGS; i++)
- mt->m_argtypes[i] = (char *) 0;
- rt = index(mt->m_name, '=');
- at = index(mt->m_name, ':');
- if (rt && at && (rt > at) || index(mt->m_name, ' '))
- cool_raise("MethodTypeSyntax", NULL);
- if (at)
- *at++ = '\0';
- if (rt) {
- *rt++ = '\0';
- mt->m_rettype = rt;
- /* parse out argument type */
- for(j = 1; ctypenames[j]; j++)
- if (cool_streq(rt, ctypenames[j]))
- mt->m_rettype = (char *) j;
- if (mt->m_rettype == CANY)
- cool_raise("MethodReturns*", NULL);
- }
- if (at) {
- if (index(at, ':') || index(at, '='))
- cool_raise("MethodTypeSyntax", NULL);
- mt->m_argtypes[0] = at;
- for(i = 1; at = index(at, ','); i++) {
- *at++ = '\0';
- mt->m_argtypes[i] = at;
- }
- for(i = 0; mt->m_argtypes[i]; i++) {
- /* parse out argument type */
- for(j = 1; ctypenames[j]; j++)
- if (cool_streq(mt->m_argtypes[i],ctypenames[j])) {
- mt->m_argtypes[i] = (char *) j;
- break;
- }
- }
- /* name: no arguments. VOID is valid return but not arg. */
- if (mt->m_argtypes[0] == CVOID) {
- mt->m_argtypes[0] = 0;
- return 0;
- } else
- return i;
- }
- return -1;
- }
-
- metheq(mt, s)
- methtab_t *mt;
- char *s;
- {
- int i, nargs;
- methtab_t tmp;
- char strbuf[256];
-
- strcpy(strbuf, s);
- tmp.m_nargs = methdigest(&tmp, strbuf);
-
- if (! cool_streq(mt->m_name, tmp.m_name))
- return 0;
- if (mt->m_rettype && tmp.m_rettype &&
- (mt->m_rettype != tmp.m_rettype))
- return 0;
- /* if argument list specified, it must match given method */
- if ((tmp.m_nargs >= 0) && (mt->m_nargs != tmp.m_nargs))
- return 0;
- if (mt->m_argtypes[0] && tmp.m_argtypes[0])
- if (mt->m_argtypes[0] != tmp.m_argtypes[0])
- return 0;
- for(i = 1; i < MAXARGS && (mt->m_argtypes[i] || tmp.m_argtypes[i]);i++)
- if (mt->m_argtypes[i] != tmp.m_argtypes[i])
- return 0;
- return 1;
- }
-
- /* junk */
-
- #ifdef JUNK
- metheq(s1, s2)
- char *s1, *s2;
- {
- for(; *s1 || *s2; s1++, s2++) {
- if (*s1 == *s2)
- continue;
- /* if one ends and other doesn't */
- if ((*s1 == '=' || *s1 == ':' || *s1 == ',') && !*s2)
- return 1;
- if ((*s2 == '=' || *s2 == ':' || *s2 == ',') && !*s1)
- return 1;
- /* skip optional return type if arglist */
- if (*s1 == '=' && *s2 == ':')
- while(*s1 != ':' && *s1)
- s1++;
- if (*s2 == '=' && *s1 == ':')
- while(*s2 != ':' && *s2)
- s2++;
- }
- return *s1 == *s2;
- }
- #endif
-
- --
-
- Lance Norskog
-
- Data is not information is not knowledge is not wisdom.
-