00001 #ifndef URLAN_H
00002 #define URLAN_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef __sun__
00024 #include <inttypes.h>
00025 #ifdef _BIG_ENDIAN
00026 #define __BIG_ENDIAN__
00027 #endif
00028 #else
00029 #include <stdint.h>
00030 #endif
00031
00032
00033 #define UR_VERSION_STR "0.1.12"
00034 #define UR_VERSION 0x000112
00035
00036
00037 enum UrlanDataType
00038 {
00039 UT_UNSET,
00040 UT_DATATYPE,
00041 UT_NONE,
00042 UT_LOGIC,
00043 UT_CHAR,
00044 UT_INT,
00045 UT_DECIMAL,
00046 UT_BIGNUM,
00047 UT_TIME,
00048 UT_DATE,
00049 UT_COORD,
00050 UT_VEC3,
00051 UT_TIMECODE,
00052
00053 UT_WORD,
00054 UT_LITWORD,
00055 UT_SETWORD,
00056 UT_GETWORD,
00057 UT_OPTION,
00058
00059 UT_BINARY,
00060 UT_BITSET,
00061 UT_STRING,
00062 UT_FILE,
00063 UT_VECTOR,
00064 UT_BLOCK,
00065 UT_PAREN,
00066 UT_PATH,
00067 UT_LITPATH,
00068 UT_SETPATH,
00069
00070 UT_CONTEXT,
00071 UT_ERROR,
00072
00073 UT_BI_COUNT,
00074 UT_MAX = 64,
00075 UT_TYPEMASK = 99,
00076 UT_REFERENCE_BUF = UT_WORD,
00077 };
00078
00079
00080
00081
00082 #define UR_FLAG_INT_HEX 0x01
00083 #define UR_FLAG_TIMECODE_DF 0x01
00084 #define UR_FLAG_PRINT_RECURSION 0x40
00085 #define UR_FLAG_SOL 0x80
00086
00087
00088 enum UrlanWordBindings
00089 {
00090 UR_BIND_UNBOUND = 0,
00091 UR_BIND_THREAD,
00092 UR_BIND_ENV,
00093 UR_BIND_USER
00094 };
00095
00096
00097 enum UrlanStringEncoding
00098 {
00099 UR_ENC_LATIN1,
00100 UR_ENC_UTF8,
00101 UR_ENC_UCS2,
00102 UR_ENC_COUNT
00103 };
00104
00105
00106 enum UrlanReturnCode
00107 {
00108 UR_THROW = 0,
00109 UR_OK = 1
00110 };
00111
00112
00113 enum UrlanErrorType
00114 {
00115 UR_ERR_TYPE,
00116 UR_ERR_SCRIPT,
00117 UR_ERR_SYNTAX,
00118 UR_ERR_ACCESS,
00119 UR_ERR_INTERNAL,
00120 UR_ERR_COUNT
00121 };
00122
00123
00124 #define UR_INVALID_BUF 0
00125 #define UR_INVALID_HOLD -1
00126
00127
00128 typedef int32_t UIndex;
00129 typedef uint16_t UAtom;
00130
00131
00132 typedef struct
00133 {
00134 uint8_t type;
00135 uint8_t flags;
00136 uint16_t _pad0;
00137 }
00138 UCellId;
00139
00140
00141 typedef struct
00142 {
00143 uint8_t type;
00144 uint8_t flags;
00145 uint8_t n;
00146 uint8_t _pad0;
00147 uint32_t mask0;
00148 uint32_t mask1;
00149 uint32_t mask2;
00150 }
00151 UCellDatatype;
00152
00153
00154 typedef struct
00155 {
00156 UCellId id;
00157 int32_t i;
00158 double d;
00159 }
00160 UCellNumber;
00161
00162
00163 #define UR_COORD_MAX 6
00164
00165 typedef struct
00166 {
00167 uint8_t type;
00168 uint8_t flags;
00169 uint16_t len;
00170 int16_t n[ UR_COORD_MAX ];
00171 }
00172 UCellCoord;
00173
00174
00175 typedef struct
00176 {
00177 UCellId id;
00178 float xyz[3];
00179 }
00180 UCellVec3;
00181
00182
00183 typedef struct
00184 {
00185 uint8_t type;
00186 uint8_t flags;
00187 uint8_t binding;
00188 uint8_t _pad0;
00189 UIndex ctx;
00190 uint16_t index;
00191 UAtom atom;
00192 UAtom sel[2];
00193 }
00194 UCellWord;
00195
00196
00197 typedef struct
00198 {
00199 UCellId id;
00200 UIndex buf;
00201 UIndex it;
00202 UIndex end;
00203 }
00204 UCellSeries;
00205
00206
00207 typedef struct
00208 {
00209 uint8_t type;
00210 uint8_t flags;
00211 uint16_t exType;
00212 UIndex messageStr;
00213 UIndex traceBlk;
00214 UIndex _pad0;
00215 }
00216 UCellError;
00217
00218
00219 typedef union
00220 {
00221 UCellId id;
00222 UCellDatatype datatype;
00223 UCellWord word;
00224 UCellNumber number;
00225 UCellCoord coord;
00226 UCellVec3 vec3;
00227 UCellSeries series;
00228 UCellSeries context;
00229 UCellSeries port;
00230 UCellError error;
00231 }
00232 UCell;
00233
00234
00235 typedef struct UBuffer UBuffer;
00236
00237 struct UBuffer
00238 {
00239 uint8_t type;
00240 uint8_t elemSize;
00241 uint8_t form;
00242 uint8_t flags;
00243 UIndex used;
00244 union
00245 {
00246 UBuffer* buf;
00247 UCell* cell;
00248 char* c;
00249 uint8_t* b;
00250 int16_t* i16;
00251 uint16_t* u16;
00252 int32_t* i;
00253 uint32_t* u32;
00254 double* d;
00255 float* f;
00256 void* v;
00257 } ptr;
00258 };
00259
00260
00261 typedef struct UEnv UEnv;
00262 typedef struct UThread UThread;
00263 typedef struct UDatatype UDatatype;
00264
00265
00266 typedef enum
00267 {
00268 UR_THREAD_INIT,
00269 UR_THREAD_FREE,
00270 UR_THREAD_FREEZE
00271 }
00272 UThreadMethod;
00273
00274
00275 struct UThread
00276 {
00277 UBuffer dataStore;
00278 UBuffer holds;
00279 UBuffer gcBits;
00280 int32_t freeBufCount;
00281 UIndex freeBufList;
00282 UEnv* env;
00283 UThread* nextThread;
00284 UDatatype** types;
00285 const UCell* (*wordCell)( UThread*, const UCell* );
00286 UCell* (*wordCellM)( UThread*, const UCell* );
00287 };
00288
00289
00290 typedef struct
00291 {
00292 const UBuffer* buf;
00293 UIndex it;
00294 UIndex end;
00295 }
00296 USeriesIter;
00297
00298 typedef struct
00299 {
00300 UBuffer* buf;
00301 UIndex it;
00302 UIndex end;
00303 }
00304 USeriesIterM;
00305
00306 typedef struct
00307 {
00308 const UBuffer* buf;
00309 const uint8_t* it;
00310 const uint8_t* end;
00311 }
00312 UBinaryIter;
00313
00314 typedef struct
00315 {
00316 UBuffer* buf;
00317 uint8_t* it;
00318 uint8_t* end;
00319 }
00320 UBinaryIterM;
00321
00322
00323 typedef struct
00324 {
00325 const UBuffer* buf;
00326 const UCell* it;
00327 const UCell* end;
00328 }
00329 UBlockIter;
00330
00331 typedef struct
00332 {
00333 UBuffer* buf;
00334 UCell* it;
00335 UCell* end;
00336 }
00337 UBlockIterM;
00338
00339
00340 typedef struct
00341 {
00342 const UBuffer* ctx;
00343 UIndex ctxN;
00344 int bindType;
00345 }
00346 UBindTarget;
00347
00348
00349 enum UrlanCompareTest
00350 {
00351 UR_COMPARE_SAME,
00352 UR_COMPARE_EQUAL,
00353 UR_COMPARE_EQUAL_CASE,
00354 UR_COMPARE_ORDER,
00355 UR_COMPARE_ORDER_CASE,
00356 };
00357
00358 enum UrlanOperator
00359 {
00360 UR_OP_ADD,
00361 UR_OP_SUB,
00362 UR_OP_MUL,
00363 UR_OP_DIV,
00364 UR_OP_MOD,
00365 UR_OP_AND,
00366 UR_OP_OR,
00367 UR_OP_XOR
00368 };
00369
00370 enum UrlanRecyclePhase
00371 {
00372 UR_RECYCLE_MARK,
00373 UR_RECYCLE_SWEEP
00374 };
00375
00376 struct UDatatype
00377 {
00378 const char* name;
00379
00380 int (*make) ( UThread*, const UCell* from, UCell* res );
00381 int (*convert) ( UThread*, const UCell* from, UCell* res );
00382 void (*copy) ( UThread*, const UCell* from, UCell* res );
00383 int (*compare) ( UThread*, const UCell* a, const UCell* b, int test );
00384 int (*operate) ( UThread*, const UCell*, const UCell*, UCell* res, int );
00385 const UCell*
00386 (*select) ( UThread*, const UCell*, const UCell* sel, UCell* tmp );
00387 void (*toString) ( UThread*, const UCell* cell, UBuffer* str, int depth );
00388 void (*toText) ( UThread*, const UCell* cell, UBuffer* str, int depth );
00389
00390 void (*recycle) ( UThread*, int phase );
00391 void (*mark) ( UThread*, UCell* cell );
00392 void (*destroy) ( UBuffer* buf );
00393 void (*markBuf) ( UThread*, UBuffer* buf );
00394 void (*toShared) ( UCell* cell );
00395
00396 void (*bind) ( UThread*, UCell* cell, const UBindTarget* bt );
00397 };
00398
00399
00400 enum UrlanFindOption
00401 {
00402 UR_FIND_LAST = 1,
00403 UR_FIND_CASE
00404 };
00405
00406 typedef struct
00407 {
00408 UDatatype dt;
00409 void (*pick) ( const UBuffer* buf, UIndex n, UCell* res );
00410 void (*poke) ( UBuffer* buf, UIndex n, const UCell* val );
00411 int (*append) ( UThread*, UBuffer* buf, const UCell* val );
00412 int (*insert) ( UThread*, UBuffer* buf, UIndex index,
00413 const UCell* val, UIndex part );
00414 int (*change) ( UThread*, USeriesIterM* si, const UCell* val,
00415 UIndex part );
00416 void (*remove) ( UThread*, USeriesIterM* si, UIndex part );
00417 int (*find) ( UThread*, const USeriesIter* si, const UCell* val,
00418 int opt );
00419 }
00420 USeriesType;
00421
00422
00423 typedef struct
00424 {
00425 UAtom atom;
00426 uint16_t index;
00427 }
00428 UAtomEntry;
00429
00430
00431 #ifdef __cplusplus
00432 extern "C" {
00433 #endif
00434
00435 UThread* ur_makeEnv( int atomLimit, UDatatype** dtTable, unsigned int dtCount,
00436 unsigned int thrSize,
00437 void (*thrMethod)(UThread*,UThreadMethod) );
00438 void ur_freeEnv( UThread* );
00439 void ur_freezeEnv( UThread* );
00440 UThread* ur_makeThread( const UThread* );
00441 int ur_destroyThread( UThread* );
00442 int ur_datatypeCount( UThread* );
00443 UAtom ur_internAtom( UThread*, const char* it, const char* end );
00444 UAtom* ur_internAtoms( UThread*, const char* words, UAtom* atoms );
00445 const char* ur_atomCStr( UThread*, UAtom atom );
00446 void ur_atomsSort( UAtomEntry* entries, int low, int high );
00447 int ur_atomsSearch( const UAtomEntry* entries, int count, UAtom atom );
00448 void ur_genBuffers( UThread*, int count, UIndex* index );
00449 void ur_destroyBuffer( UThread*, UBuffer* );
00450 UIndex ur_holdBuffer( UThread*, UIndex bufN );
00451 void ur_releaseBuffer( UThread*, UIndex hold );
00452 void ur_recycle( UThread* );
00453 int ur_markBuffer( UThread*, UIndex bufN );
00454 int ur_error( UThread*, int errorType, const char* fmt, ... );
00455 UBuffer* ur_errorBlock( UThread* );
00456 UBuffer* ur_threadContext( UThread* );
00457 UBuffer* ur_envContext( UThread* );
00458 void ur_appendTrace( UThread*, UIndex blkN, UIndex it );
00459 UIndex ur_tokenize( UThread*, const char* it, const char* end, UCell* res );
00460 int ur_serialize( UThread*, UIndex blkN, UCell* res );
00461 int ur_unserialize( UThread*, const uint8_t* start, const uint8_t* end,
00462 UCell* res );
00463 void ur_toStr( UThread*, const UCell* cell, UBuffer* str, int depth );
00464 void ur_toText( UThread*, const UCell* cell, UBuffer* str );
00465 const UCell* ur_wordCell( UThread*, const UCell* cell );
00466 UCell* ur_wordCellM( UThread*, const UCell* cell );
00467 int ur_setWord( UThread*, const UCell* wordCell, const UCell* src );
00468 const UBuffer* ur_bufferEnv( UThread*, UIndex n );
00469 const UBuffer* ur_bufferSeries( UThread*, const UCell* cell );
00470 UBuffer* ur_bufferSeriesM( UThread*, const UCell* cell );
00471 void ur_seriesSlice( UThread*, USeriesIter* si, const UCell* cell );
00472 int ur_seriesSliceM( UThread*, USeriesIterM* si, const UCell* cell );
00473 void ur_bind( UThread*, UBuffer* blk, const UBuffer* ctx, int bindType );
00474 void ur_bindCells( UThread*, UCell* it, UCell* end, const UBindTarget* bt );
00475 void ur_infuse( UThread*, UCell* it, UCell* end, const UBuffer* ctx );
00476 int ur_isTrue( const UCell* cell );
00477 int ur_same( UThread*, const UCell* a, const UCell* b );
00478 int ur_equal( UThread*, const UCell* a, const UCell* b );
00479 int ur_equalCase( UThread*, const UCell* a, const UCell* b );
00480 int ur_compare( UThread*, const UCell* a, const UCell* b );
00481 int ur_compareCase( UThread*, const UCell* a, const UCell* b );
00482
00483 void ur_makeDatatype( UCell* cell, int type );
00484 int ur_isDatatype( const UCell* cell, const UCell* datatype );
00485 void ur_datatypeAddType( UCell* cell, int type );
00486
00487 int ur_charLowercase( int c );
00488 int ur_charUppercase( int c );
00489
00490 UIndex ur_makeBinary( UThread*, int size );
00491 UBuffer* ur_makeBinaryCell( UThread*, int size, UCell* cell );
00492 void ur_binInit( UBuffer*, int size );
00493 void ur_binReserve( UBuffer*, int size );
00494 void ur_binExpand( UBuffer*, int index, int count );
00495 void ur_binErase( UBuffer*, int start, int count );
00496 void ur_binAppendData( UBuffer*, const uint8_t* data, int len );
00497 void ur_binAppendArray( UBuffer*, const USeriesIter* si );
00498 void ur_binFree( UBuffer* );
00499 void ur_binSlice( UThread*, UBinaryIter*, const UCell* cell );
00500 int ur_binSliceM( UThread*, UBinaryIterM*, const UCell* cell );
00501 void ur_binToStr( UBuffer*, int encoding );
00502 const char* ur_binAppendHex( UBuffer* buf, const char* it, const char* end );
00503
00504 UIndex ur_makeString( UThread*, int enc, int size );
00505 UBuffer* ur_makeStringCell( UThread*, int enc, int size, UCell* cell );
00506 UIndex ur_makeStringUtf8( UThread*, const uint8_t* it, const uint8_t* end );
00507 void ur_strInit( UBuffer*, int enc, int size );
00508 void ur_strAppendCStr( UBuffer*, const char* );
00509 void ur_strAppendChar( UBuffer*, int );
00510 void ur_strAppendInt( UBuffer*, int32_t );
00511 void ur_strAppendInt64( UBuffer*, int64_t );
00512 void ur_strAppendHex( UBuffer*, uint32_t n, uint32_t hi );
00513 void ur_strAppendDouble( UBuffer*, double );
00514 void ur_strAppendIndent( UBuffer*, int depth );
00515 void ur_strAppend( UBuffer*, const UBuffer* strB, UIndex itB, UIndex endB );
00516 void ur_strTermNull( UBuffer* );
00517 int ur_strIsAscii( const UBuffer* );
00518 void ur_strFlatten( UBuffer* );
00519 void ur_strLowercase( UBuffer* str, UIndex start, UIndex send );
00520 void ur_strUppercase( UBuffer* str, UIndex start, UIndex send );
00521 UIndex ur_strFindChar( const UBuffer*, UIndex start, UIndex end, int ch );
00522 UIndex ur_strFindChars( const UBuffer*, UIndex start, UIndex end,
00523 uint8_t* charSet, int len );
00524 UIndex ur_strFind( const USeriesIter*, const USeriesIter*, int matchCase );
00525 UIndex ur_strMatch( const USeriesIter*, const USeriesIter*, int matchCase );
00526 int ur_strChar( const UBuffer*, UIndex pos );
00527 char* ur_cstring( const UBuffer*, UBuffer* bin, UIndex start, UIndex end );
00528 #define ur_strFree ur_arrFree
00529 #define ur_strIsUcs2(buf) ((buf)->form == UR_ENC_UCS2)
00530
00531 UIndex ur_makeBlock( UThread*, int size );
00532 UBuffer* ur_makeBlockCell( UThread*, int type, int size, UCell* cell );
00533 UIndex ur_blkClone( UThread*, UIndex blkN );
00534 void ur_blkInit( UBuffer*, int type, int size );
00535 UCell* ur_blkAppendNew( UBuffer*, int type );
00536 void ur_blkAppendCells( UBuffer*, const UCell* cells, int count );
00537 void ur_blkInsert( UBuffer*, UIndex it, const UCell* cells, int count );
00538 void ur_blkPush( UBuffer*, const UCell* cell );
00539 UCell* ur_blkPop( UBuffer* );
00540 void ur_blkSlice( UThread*, UBlockIter*, const UCell* cell );
00541 int ur_blkSliceM( UThread*, UBlockIterM*, const UCell* cell );
00542 #define ur_blkFree ur_arrFree
00543
00544 int ur_pathCell( UThread*, const UCell* pc, UCell* res );
00545 int ur_setPath( UThread*, const UCell* path, const UCell* src );
00546
00547 UIndex ur_makeContext( UThread*, int size );
00548 UBuffer* ur_makeContextCell( UThread*, int size, UCell* cell );
00549 UBuffer* ur_ctxClone( UThread*, const UBuffer* src, UCell* cell );
00550 UBuffer* ur_ctxMirror( UThread*, const UBuffer* src, UCell* cell );
00551 void ur_ctxInit( UBuffer*, int size );
00552 void ur_ctxReserve( UBuffer*, int size );
00553 void ur_ctxFree( UBuffer* );
00554 UBuffer* ur_ctxSort( UBuffer* );
00555 void ur_ctxSetWords( UBuffer*, const UCell* it, const UCell* end );
00556 int ur_ctxAppendWord( UBuffer*, UAtom atom );
00557 int ur_ctxAddWordI( UBuffer*, UAtom atom );
00558 UCell* ur_ctxAddWord( UBuffer*, UAtom atom );
00559 void ur_ctxWordAtoms( const UBuffer*, UAtom* atoms );
00560 int ur_ctxLookupNoSort( const UBuffer*, UAtom atom );
00561 int ur_ctxLookup( const UBuffer*, UAtom atom );
00562 const UBuffer* ur_sortedContext( UThread*, const UCell* );
00563 #define ur_ctxCell(c,n) ((c)->ptr.cell + n)
00564
00565 void ur_vecInit( UBuffer*, int form, int elemSize, int size );
00566
00567 void ur_arrInit( UBuffer*, int size, int count );
00568 void ur_arrReserve( UBuffer*, int count );
00569 void ur_arrExpand( UBuffer*, int index, int count );
00570 void ur_arrErase( UBuffer*, int start, int count );
00571 void ur_arrFree( UBuffer* );
00572 void ur_arrAppendInt32( UBuffer*, int32_t );
00573 void ur_arrAppendFloat( UBuffer*, float );
00574
00575 #ifdef __cplusplus
00576 }
00577 #endif
00578
00579
00580 #define ur_ptr(T,buf) ((T*) (buf)->ptr.v)
00581 #define ur_avail(buf) (buf)->ptr.i[-1]
00582 #define ur_testAvail(buf) (buf->ptr.v ? ur_avail(buf) : 0)
00583
00584 #define ur_arrExpand1(T,buf,elem) \
00585 ur_arrReserve(buf, buf->used + 1); \
00586 elem = ur_ptr(T,buf) + buf->used; \
00587 ++buf->used
00588
00589 #define ur_type(c) (c)->id.type
00590 #ifdef __BIG_ENDIAN__
00591 #define ur_setId(c,t) *((uint32_t*) (c)) = (t) << 24
00592 #else
00593 #define ur_setId(c,t) *((uint32_t*) (c)) = t
00594 #endif
00595 #define ur_setFlags(c,m) (c)->id.flags |= m
00596 #define ur_clrFlags(c,m) (c)->id.flags &= ~(m)
00597 #define ur_flags(c,m) ((c)->id.flags & m)
00598 #define ur_is(c,t) ((c)->id.type == (t))
00599
00600 #define ur_atom(c) (c)->word.atom
00601 #define ur_datatype(c) (c)->datatype.n
00602 #define ur_int(c) (c)->number.i
00603 #define ur_decimal(c) (c)->number.d
00604
00605 #define ur_isWordType(t) ((t) >= UT_WORD && (t) <= UT_OPTION)
00606 #define ur_binding(c) (c)->word.binding
00607 #define ur_setBinding(c,bindType) (c)->word.binding = bindType
00608
00609 #define ur_isBlockType(t) ((t) >= UT_BLOCK && (t) <= UT_SETPATH)
00610 #define ur_isStringType(t) ((t) == UT_STRING || (t) == UT_FILE)
00611
00612 #define ur_isSeriesType(t) ((t) >= UT_BINARY && (t) < UT_CONTEXT)
00613 #define ur_isShared(n) (n < 0)
00614 #define ur_isSliced(c) ((c)->series.end > -1)
00615
00616 #define ur_setWordUnbound(c,atm) \
00617 (c)->word.atom = atm; \
00618 (c)->word.ctx = UR_INVALID_BUF
00619
00620
00621
00622 #define ur_setSeries(c,bn,sit) \
00623 (c)->series.buf = bn; \
00624 (c)->series.it = sit; \
00625 (c)->series.end = -1
00626
00627 #define ur_setSlice(c,bn,sit,send) \
00628 (c)->series.buf = bn; \
00629 (c)->series.it = sit; \
00630 (c)->series.end = send
00631
00632 #define ur_hold(n) ur_holdBuffer(ut,n)
00633 #define ur_release(h) ur_releaseBuffer(ut,h)
00634 #define ur_buffer(n) (ut->dataStore.ptr.buf + (n))
00635 #define ur_bufferE(n) ur_bufferEnv(ut,n)
00636 #define ur_bufferSer(c) ur_bufferSeries(ut,c)
00637 #define ur_bufferSerM(c) ur_bufferSeriesM(ut,c)
00638
00639 #define ur_foreach(bi) for(; bi.it != bi.end; ++bi.it)
00640
00641 #define ur_wordCStr(c) ur_atomCStr(ut, ur_atom(c))
00642
00643 #define ur_cstr(strC,bin) \
00644 ur_cstring(ur_bufferSer(strC), bin, strC->series.it, strC->series.end)
00645
00646
00647 #endif