1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* $Header: /local/src/CVS/nickle/struct.c,v 1.23 2004/02/27 03:50:16 keithp Exp $ */
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
#include "nickle.h"
Value
StructMemRef (Value sv, Atom name)
{
ENTER ();
Struct *s = &sv->structs;
StructType *st = s->type;
int i;
for (i = 0; i < st->nelements; i++)
if (StructTypeAtoms(st)[i] == name)
RETURN (NewRef (s->values, i));
RETURN (0);
}
Value
StructMemValue (Value sv, Atom name)
{
ENTER ();
Struct *s = &sv->structs;
StructType *st = s->type;
int i;
for (i = 0; i < st->nelements; i++)
if (StructTypeAtoms(st)[i] == name)
RETURN (BoxValue (s->values, i));
RETURN (0);
}
Type *
StructMemType (StructType *st, Atom name)
{
int i;
for (i = 0; i < st->nelements; i++)
if (StructTypeAtoms(st)[i] == name)
return (BoxTypesElements(st->types)[i]);
return (0);
}
static Bool
StructPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
Struct *s = &av->structs;
StructType *st = s->type;
int i;
Bool pretty = format == 'v' || format == 'g';
if (pretty)
FileOutput (f, '{');
for (i = 0; i < st->nelements; i++)
{
FilePuts (f, AtomName (StructTypeAtoms(st)[i]));
FilePuts (f, " = ");
if (!Print (f, BoxValueGet (s->values, i), format, base, width, prec, fill))
return False;
if (i < st->nelements - 1)
{
if (pretty)
FileOutput (f, ',');
FileOutput (f, ' ');
}
}
if (pretty)
FileOutput (f, '}');
return True;
}
static void
StructMark (void *object)
{
Struct *s = object;
MemReference (s->type);
MemReference (s->values);
}
static Value
StructEqual (Value a, Value b, int expandOk)
{
int i;
StructType *at = a->structs.type;
if (at->nelements != b->structs.type->nelements)
return FalseVal;
for (i = 0; i < at->nelements; i++)
{
if (False (Equal (BoxValue (a->structs.values, i),
StructMemValue (b, StructTypeAtoms(at)[i]))))
return FalseVal;
}
return TrueVal;
}
static HashValue
StructHash (Value a)
{
Struct *s = &a->structs;
StructType *at = s->type;
HashValue h = 0;
int i;
for (i = 0; i < at->nelements; i++)
h = h ^ ValueInt (ValueHash (BoxValue (a->structs.values, i)));
return h;
}
ValueRep StructRep = {
{ StructMark, 0, "StructRep" }, /* base */
rep_struct, /* tag */
{ /* binary */
0,
0,
0,
0,
0,
0,
0,
StructEqual,
0,
0,
},
{ /* unary */
0,
0,
0,
},
0,
0,
StructPrint,
0,
StructHash,
};
Value
NewStruct (StructType *type, Bool constant)
{
ENTER ();
Value ret;
ret = ALLOCATE (&StructRep.data, sizeof (Struct));
ret->structs.type = type;
ret->structs.values = 0;
ret->structs.values = NewTypedBox (False, type->types);
RETURN (ret);
}
static void
StructTypeMark (void *object)
{
StructType *st = object;
MemReference (st->types);
}
DataType StructTypeType = { StructTypeMark, 0, "StructTypeType" };
StructType *
NewStructType (int nelements)
{
ENTER ();
StructType *st;
int i;
Atom *atoms;
st = ALLOCATE (&StructTypeType, sizeof (StructType) +
nelements * sizeof (Atom));
st->nelements = nelements;
st->types = NewBoxTypes (nelements);
atoms = StructTypeAtoms(st);
for (i = 0; i < nelements; i++)
atoms[i] = 0;
RETURN (st);
}
Value Elementless;
StructType *ElementlessType;
int
StructInit (void)
{
ENTER ();
ElementlessType = NewStructType (0);
MemAddRoot (ElementlessType);
Elementless = NewStruct (ElementlessType, True);
MemAddRoot (Elementless);
EXIT ();
return 1;
}
Type *
BuildStructType (int nelements, ...)
{
ENTER ();
StructType *st;
int i;
char *name;
Type *type;
va_list ap;
st = NewStructType (nelements);
va_start (ap, nelements);
for (i = 0; i < nelements; i++)
{
type = va_arg (ap, Type *);
name = va_arg (ap, char *);
AddBoxType (&st->types, type);
StructTypeAtoms (st)[i] = AtomId (name);
}
va_end (ap);
RETURN (NewTypeStruct (st));
}
|