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