-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdao_UserType.c
287 lines (264 loc) · 9.12 KB
/
dao_UserType.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include"daoStdtype.h"
#include"daoValue.h"
#include"daoProcess.h"
typedef struct DaoxUserType DaoxUserType;
/* bit integer */
struct DaoxUserType
{
DAO_CSTRUCT_COMMON;
dao_integer value;
};
DaoType *daox_type_user_type = NULL;
DAO_DLL DaoxUserType* DaoxUserType_New();
DAO_DLL void DaoxUserType_Delete( DaoxUserType *self );
DaoxUserType* DaoxUserType_New()
{
DaoxUserType *self = (DaoxUserType*) dao_calloc( 1, sizeof(DaoxUserType) );
DaoCstruct_Init( (DaoCstruct*) self, daox_type_user_type );
return self;
}
void DaoxUserType_Delete( DaoxUserType *self )
{
DaoCstruct_Free( (DaoCstruct*) self );
dao_free( self );
}
#if 0
static DaoxUserType* DaoProcess_PutUserPod( DaoProcess *proc, dao_integer value )
{
DaoxUserType buffer = {DAO_CSTRUCT, 0};
buffer.ctype = daox_type_user_pod_type;
buffer.value = value;
return (DaoxUserType*) DaoProcess_PutValue( proc, (DaoValue*) & buffer );
}
static void UT_New1( DaoProcess *proc, DaoValue *p[], int N )
{
DaoProcess_PutUserPod( proc, p[0]->xInteger.value );
}
#endif
static void UT_New1( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *self = DaoxUserType_New();
DaoProcess_PutValue( proc, (DaoValue*) self );
self->value = p[0]->xInteger.value;
}
static void UT_GETI( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *self = (DaoxUserType*) DaoValue_CastCstruct( p[0], daox_type_user_type );
}
static void UT_BinaryOper2( DaoProcess *proc, DaoValue *p[], int N, int oper )
{
DaoxUserType *C = DaoxUserType_New();
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoProcess_PutValue( proc, (DaoValue*) C );
}
static void UT_CompOper2( DaoProcess *proc, DaoValue *p[], int N, int oper )
{
DaoValue *C = NULL;
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
daoint D = 0;
if( C ) DaoProcess_PutValue( proc, C );
else DaoProcess_PutInteger( proc, D );
}
static void UT_ADD2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_ADD );
}
static void UT_SUB2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_SUB );
}
static void UT_MUL2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_MUL );
}
static void UT_DIV2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_DIV );
}
static void UT_MOD2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_MOD );
}
static void UT_POW2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BinaryOper2( proc, p, N, DVM_POW );
}
static void UT_AND2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_CompOper2( proc, p, N, DVM_AND );
}
static void UT_OR2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_CompOper2( proc, p, N, DVM_OR );
}
static void UT_LT2( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoProcess_PutInteger( proc, A->value < B->value );
}
static void UT_LE2( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoProcess_PutInteger( proc, A->value <= B->value );
}
static void UT_EQ2( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoProcess_PutInteger( proc, A->value == B->value );
}
static void UT_NE2( DaoProcess *proc, DaoValue *p[], int N )
{
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoProcess_PutInteger( proc, A->value != B->value );
}
static void UT_UnaryOper( DaoProcess *proc, DaoValue *p[], int N, int oper )
{
daoint ta;
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *C = DaoxUserType_New();
DaoProcess_PutValue( proc, (DaoValue*) C );
}
static void UT_MINUS( DaoProcess *proc, DaoValue *p[], int N )
{
UT_UnaryOper( proc, p, N, DVM_MINUS );
}
static void UT_NOT( DaoProcess *proc, DaoValue *p[], int N )
{
UT_UnaryOper( proc, p, N, DVM_NOT );
}
static void UT_TILDE( DaoProcess *proc, DaoValue *p[], int N )
{
UT_UnaryOper( proc, p, N, DVM_TILDE );
}
static void UT_BitOper2( DaoProcess *proc, DaoValue *p[], int N, int oper )
{
DaoxUserType *A = (DaoxUserType*) p[0];
DaoxUserType *B = (DaoxUserType*) p[1];
DaoxUserType *C = DaoxUserType_New();
DaoProcess_PutValue( proc, (DaoValue*) C );
}
static void UT_BITAND2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BitOper2( proc, p, N, DVM_BITAND );
}
static void UT_BITOR2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BitOper2( proc, p, N, DVM_BITOR );
}
static void UT_BITXOR2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BitOper2( proc, p, N, DVM_BITXOR );
}
static void UT_BITLFT2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BitOper2( proc, p, N, DVM_BITLFT );
}
static void UT_BITRIT2( DaoProcess *proc, DaoValue *p[], int N )
{
UT_BitOper2( proc, p, N, DVM_BITRIT );
}
static void UT_CastToInt( DaoProcess *proc, DaoValue *p[], int n )
{
DaoxUserType *self = (DaoxUserType*) p[0];
DaoProcess_PutInteger( proc, self->value );
}
static void UT_CastToString( DaoProcess *proc, DaoValue *p[], int n )
{
DaoxUserType *self = (DaoxUserType*) p[0];
DString *res = DaoProcess_PutChars( proc, "" );
DString_Reserve( res, 50 );
res->size = sprintf( res->chars, "UserType.{%" DAO_I64 "}", self->value );
}
static DaoFunctionEntry userTypeMeths[]=
{
{ UT_New1, "UserType( value: int ) => UserType" },
{ UT_GETI, "[]( self: UserType, idx: none ) => UserType" },
{ UT_GETI, "[]( self: UserType, idx: int ) => int" },
{ UT_ADD2, "+( A: UserType, B: UserType ) => UserType" },
{ UT_SUB2, "-( A: UserType, B: UserType ) => UserType" },
{ UT_MUL2, "*( A: UserType, B: UserType ) => UserType" },
{ UT_DIV2, "/( A: UserType, B: UserType ) => UserType" },
{ UT_MOD2, "%( A: UserType, B: UserType ) => UserType" },
{ UT_POW2, "**( A: UserType, B: UserType ) => UserType" },
{ UT_AND2, "&&( A: UserType, B: UserType ) => UserType" },
{ UT_OR2, "||( A: UserType, B: UserType ) => UserType" },
{ UT_LT2, "< ( A: UserType, B: UserType ) => int" },
{ UT_LE2, "<=( A: UserType, B: UserType ) => int" },
{ UT_EQ2, "==( A: UserType, B: UserType ) => int" },
{ UT_NE2, "!=( A: UserType, B: UserType ) => int" },
{ UT_MINUS, "-( A: UserType ) => UserType" },
{ UT_NOT, "!( A: UserType ) => UserType" },
{ UT_TILDE, "~( A: UserType ) => UserType" },
{ UT_BITAND2, "&( A: UserType, B: UserType ) => UserType" },
{ UT_BITOR2, "|( A: UserType, B: UserType ) => UserType" },
{ UT_BITXOR2, "^( A: UserType, B: UserType ) => UserType" },
{ UT_BITLFT2, "<<( A: UserType, B: UserType ) => UserType" },
{ UT_BITRIT2, ">>( A: UserType, B: UserType ) => UserType" },
{ UT_CastToInt, "(int)( self: UserType, hashing = false )" },
{ UT_CastToString, "(string)( self: UserType )" },
{ NULL, NULL },
};
int DaoxUserType_Compare( DaoValue *self, DaoValue *other, DMap *cycmap )
{
DaoxUserType *left = (DaoxUserType*) self;
DaoxUserType *right = (DaoxUserType*) other;
if( left->value == right->value ) return 0;
return left->value < right->value ? -1 : 1;
}
size_t DaoxUserType_Hash( DaoValue *self )
{
DaoxUserType *pod = (DaoxUserType*) self;
return pod->value;
}
DaoValue* DaoxUserType_Copy( DaoValue *self, DaoValue *target )
{
DaoxUserType *src = (DaoxUserType*) self;
DaoxUserType *dest = (DaoxUserType*) target;
if( target ){
if( src ) dest->value = src->value;
return target;
}
dest = DaoxUserType_New();
if( src ) dest->value = src->value;
return (DaoValue*) dest;
}
static DaoTypeCore daoUserTypeCore =
{
"UserType", /* name */
sizeof(DaoxUserType), /* size */
{ NULL }, /* bases */
{ NULL }, /* casts */
NULL, /* numbers */
userTypeMeths, /* methods */
DaoCstruct_CheckGetField, DaoCstruct_DoGetField, /* GetField */
DaoCstruct_CheckSetField, DaoCstruct_DoSetField, /* SetField */
DaoCstruct_CheckGetItem, DaoCstruct_DoGetItem, /* GetItem */
DaoCstruct_CheckSetItem, DaoCstruct_DoSetItem, /* SetItem */
DaoCstruct_CheckUnary, DaoCstruct_DoUnary, /* Unary */
DaoCstruct_CheckBinary, DaoCstruct_DoBinary, /* Binary */
DaoCstruct_CheckConversion, DaoCstruct_DoConversion, /* Conversion */
NULL, NULL, /* ForEach */
DaoCstruct_Print, /* Print */
NULL, /* Slice */
DaoxUserType_Compare, /* Compare */
DaoCstruct_HashPOD, /* Hash */
DaoCstruct_CreatePOD, /* Create */
DaoCstruct_CopyPOD, /* Copy */
(DaoDeleteFunction) DaoxUserType_Delete, /* Delete */
NULL /* HandleGC */
};
DAO_DLL int DaoUsertype_OnLoad( DaoVmSpace *vmSpace, DaoNamespace *ns )
{
daox_type_user_type = DaoNamespace_WrapType( ns, & daoUserTypeCore, DAO_CSTRUCT, DAO_CTYPE_INVAR );
return 0;
}