int __cdecl main( int argc, char *argv[] ) { lex ilex; plexitem pil; buf in; str fn; arr out; uint i; uint fout; gentee_init(); printf("Start\n"); str_init( &fn ); str_copyzero( &fn, "gttbl.dat"); fout = os_fileopen( &fn, FOP_CREATE ); printf("Fout=%i %s\n", fout, str_ptr( &fn )); os_filewrite( fout, ( pubyte )&tbl_gt, 97 * sizeof( uint )); os_fileclose( ( pvoid )fout ); str_copyzero( &fn, "gtdotbl.dat"); fout = os_fileopen( &fn, FOP_CREATE ); printf("Fout=%i %s\n", fout, str_ptr( &fn )); str_delete( &fn ); os_filewrite( fout, ( pubyte )&tbl_gtdo, 81 * sizeof( uint )); os_fileclose( ( pvoid )fout ); arr_init( &out, sizeof( lexitem )); buf_init( &in ); buf_copyzero( &in, "</r/&xfa; &xfa; #ap/dfield( 'qwer ()ty' , \"my , name\" , qqq)#asdf.fgwsw/# se# &xaa;" "<2 qqq> </2><1 a2345=&xf0;> 223&</1><-qwe-rty->" "<mygt /asd = \"qwerty sese'\" qq21 = 'dedxd' 'esese;' aqaq=325623/>" "<a asdff /a> <mygtdd><a /><-ooops-><ad />< qq</>" "xxx </r/nm <_aa aqaqa /_aaaa /_a/_aa><a22222/ >" "<*abc ></abc><|*aaa = qqqq></aaa>ooops aaa</eee>\"\r\n</>"); // buf_copyzero( &in, "<mygt > <aaa asdff>qqqq</> </mygtdd>qq </> xxx </r/nm <_aa aqaqa /_aaaa /_aa> <a22222/ /> </ > "); printf("lex_init\n"); lex_init( &ilex, (puint)&tbl_gtdo ); printf("gentee_lex\n"); gentee_lex( &in, &ilex, &out ); if (arr_count(&ilex.state)) printf("================= State=%x/%i \n", arr_getuint( &ilex.state, arr_count(&ilex.state) - 1 ), arr_count(&ilex.state)); for ( i = 0; i < arr_count( &out ); i++ ) { pil = ( plexitem )arr_ptr( &out, i ); printf("ID=%x pos=%i len=%i \n", pil->type, pil->pos, pil->len, buf_ptr( &in ) + pil->pos ); } // gentee_compile(); lex_delete( &ilex ); buf_delete( &in ); arr_delete( &out ); gentee_deinit(); printf("OK\n"); getch(); return 0; }
pubyte STDCALL hash_name( phash ph, uint id ) { if ( id >= arr_count( &ph->names )) return NULL; return ( pubyte )*(puint)arr_ptr( &ph->names, id ) + sizeof( hashitem ) + ph->isize; }
phashitem STDCALL _hash_find( phash ph, pubyte name, uint create ) { pubyte ptr = name; ushort val = 0; uint len = 0; ubyte shift = 0; ushort nameoff; phashitem phi = 0, prev = 0; // Вычисляем хэш-значение строки while ( *ptr ) { if ( ph->ignore ) val += (( ushort )_lower[ *ptr++ ] ) << shift; else val += (( ushort )*ptr++ ) << shift; if ( ++shift > 7 ) shift = 0; len++; } // получаем номер в хэш-таблице val &= HASH_SIZE - 1; phi = prev = ( phashitem )arr_getuint( &ph->values, val ); nameoff = sizeof( hashitem ) + ph->isize; while ( phi ) { // printf("CMp <%s><%s> %i %i\n", name, ( pubyte )phi + nameoff, len, phi->len ); if ( len == phi->len && !( ph->ignore ? mem_cmpign( name, ( pubyte )phi + nameoff, len ) : mem_cmp( name, ( pubyte )phi + nameoff, len ))) // нашли совпадение return phi; phi = ( phashitem )phi->next; } if ( create ) { // Будем добавлять элемент в таблицу имен phi = ( phashitem )mem_alloc( nameoff + len + 1 ); phi->len = ( ushort )len; phi->next = ( pvoid )prev; phi->id = arr_count( &ph->names ); mem_zero( ( pubyte )phi + sizeof( hashitem ), ph->isize ); mem_copy( ( pubyte )phi + nameoff, name, len + 1 ); // printf("Append %x %s\n", phi, name ); arr_appendnum( &ph->names, ( uint )phi ); arr_setuint( &ph->values, val, ( uint )phi ); } return phi; }
int main(void) { int a[] = { 2, 5, 0, 4 ,-1, 7, 4, 2 }; int b[] = { 2 }; int c[] = { 2, 5, 0, 4 ,-1, 7 }; CHECK(arr_min(a, 8) == -1); CHECK(arr_min(b, 1) == -1); CHECK(arr_min(c, 6) == -1); CHECK(arr_count(a, 8, 4) == 2); CHECK(arr_count(b, 1, 2) == 1); CHECK(arr_count(c, 6, -1) == 1); CHECK(arr_all_even(a, 8) == 0); CHECK(arr_all_even(b, 1) == 1); CHECK(arr_all_even(c, 6) == 0); CHECK(arr_find_last(a, 8, 2) == 7); CHECK(arr_find_last(b, 1, 2) == 0); CHECK(arr_find_last(c, 6, 2) == 0); getchar(); return 0; }
pbuf STDCALL file2buf( pstr name, pbuf ret, uint pos ) { str filename; uint search = 0; uint handle, size; parrdata pad; str_init( &filename ); os_filefullname( name, &filename ); if ( _compile && str_findch( name, SLASH ) >= str_len( name )) { search = 1; pad = &_compile->libdirs; } again: handle = os_fileopen( &filename, FOP_READONLY ); if ( !handle ) { if ( search && search <= arr_count( pad )) { str_dirfile( arrdata_get( pad, search++ - 1 ), name, &filename ); goto again; } msg( MFileopen | MSG_STR | MSG_EXIT | MSG_POS, &filename, pos ); } size = ( uint )os_filesize( handle ); buf_reserve( ret, size ); if ( size && !os_fileread( handle, buf_ptr( ret ), size )) msg( MFileread | MSG_STR | MSG_EXIT | MSG_POS, &filename, pos ); buf_setlen( ret, size ); os_fileclose( handle ); str_copy( name, &filename ); str_delete( &filename ); return ret; }
uint STDCALL ge_save( pbuf out ) { gehead head; pgehead phead; uint i, count, off = 0; pvmobj pvmo; gesave = out; buf_reserve( out, 0x1ffff ); if ( _compile->flag & CMPL_OPTIMIZE ) ge_optimize(); *( puint )&head.idname = GE_STRING;//0x00004547; // строка GE head.flags = 0; head.crc = 0; head.headsize = sizeof( gehead ); head.size = 0; head.vermajor = GEVER_MAJOR; head.verminor = GEVER_MINOR; buf_append( out, ( pubyte )&head, sizeof( gehead )); // Save resources at the first ! gesave_resource(); count = arr_count( &_vm.objtbl ); // Settings new id depending on GHRT_SKIP gecodes = ( puint )mem_alloc( count * sizeof( uint )); for ( i = KERNEL_COUNT; i < count ; i++ ) { pvmo = ( pvmobj )PCMD( i ); if ( pvmo->flag & GHRT_SKIP ) { gecodes[ i ] = 0; off++; } else gecodes[ i ] = i - off; } for ( i = KERNEL_COUNT; i < count ; i++ ) { pvmo = ( pvmobj )PCMD( i ); if ( pvmo->flag & GHRT_SKIP ) continue; // print("i=%i name=%s\n", i, ((pvmobj)PCMD( i ))->name ); gesave_head( pvmo->type, pvmo->flag & GHCOM_NAME ? pvmo->name : NULL, pvmo->flag ); switch ( pvmo->type ) { case OVM_NONE: break; case OVM_BYTECODE: gesave_bytecode( ( povmbcode )pvmo ); break; case OVM_EXFUNC: gesave_exfunc( ( povmfunc )pvmo ); break; case OVM_TYPE: gesave_type( ( povmtype )pvmo ); break; case OVM_GLOBAL: gesave_var( (( povmglobal )pvmo)->type ); break; case OVM_DEFINE: gesave_define( ( povmdefine )pvmo ); break; case OVM_IMPORT: gesave_import( ( povmimport )pvmo ); break; case OVM_ALIAS: gesave_bwdc( (( povmalias )pvmo)->idlink ); break; } gesave_finish(); } mem_free( gecodes ); // Specify the full size and crc phead = ( pgehead )buf_ptr( out ); phead->size = buf_len( out ); phead->crc = crc( ( pubyte )phead + 12, phead->size - 12, 0xFFFFFFFF ); return 1; }
uint STDCALL ge_load( pbuf in ) { pubyte cur, end, ptemp; uint size; pgehead phead = ( pgehead )buf_ptr( in ); // Проверка заголовка и целостности // Сравниваем с 'GE' с двумя нулями на конце if ( *( puint )phead != GE_STRING )//0x00004547 ) msg( MNotGE | MSG_EXIT ); if ( phead->crc != crc( ( pubyte )phead + 12, phead->size - 12, 0xFFFFFFFF )) msg( MCrcGE | MSG_EXIT ); if ( phead->vermajor != GEVER_MAJOR || phead->verminor > GEVER_MINOR ) msg( MVerGE | MSG_EXIT ); _vm.loadmode = VMLOAD_GE; _vm.icnv = arr_count( &_vm.objtbl ) - KERNEL_COUNT; // print("icnv=%i\n", _vm.icnv ); cur = ( pubyte )phead + phead->headsize; end = ( pubyte )phead + phead->size; while ( cur < end ) { ptemp = cur + 5; // type + flag _vm.ipack = ( *( puint )( cur + 1 )) & GHCOM_PACK ? 1 : 0; size = load_bwd( &ptemp ); ptemp = cur; // print("size=%i type=%i flag = %x\n", size, *cur, *( puint )( cur + 1 ) ); switch ( *cur ) { case OVM_NONE: load_none(); break; case OVM_BYTECODE: load_bytecode( &cur, VMLOAD_GE ); break; case OVM_EXFUNC: load_exfunc( &cur, 0 ); _vm.loadmode = VMLOAD_GE; break; case OVM_TYPE: load_type( &cur ); break; case OVM_GLOBAL: load_global( &cur ); break; case OVM_DEFINE: load_define( &cur ); break; case OVM_IMPORT: load_import( &cur ); break; case OVM_RESOURCE: load_resource( &cur ); break; case OVM_ALIAS: load_alias( &cur ); break; default: msg( MUnkGE | MSG_DVAL, cur - ( pubyte )phead ); } cur = ptemp + size; } _vm.loadmode = VMLOAD_G; _vm.icnv = 0; return 1; }
uint STDCALL ge_save( pvmEngine pThis, char* fileName, char* isSave) { gehead head; pgehead phead; uint i, ii, count; pvmobj pvmo; buf out; str filename; if ( setjmp( pThis->stack_state) == -1 ) return 0; str_init( pThis, &filename ); str_copyzero( pThis, &filename, fileName ); buf_init( pThis, &out ); pThis->gesave = &out; buf_reserve( pThis, &out, 0x1ffff ); *( puint )&head.idname = GE_STRING;//0x00004547; // строка GE head.flags = 0; head.crc = 0; head.headsize = sizeof( gehead ); head.size = 0; head.vermajor = GEVER_MAJOR; head.verminor = GEVER_MINOR; buf_append( pThis, &out, ( pubyte )&head, sizeof( gehead )); // Save resources at the first ! gesave_resource(pThis); count = arr_count( pThis, &pThis->_vm.objtbl ); for ( i = KERNEL_COUNT; i < count ; i++ ) { if(isSave && isSave[i] == 0) { /* gesave_addubyte( pThis, OVM_NONE ); gesave_adduint( pThis, GHCOM_PACK); gesave_bwd( pThis, 6 );*/ pThis->popravka ++; continue; } pvmo = ( pvmobj )PCMD( i ); pvmo->id -= pThis->popravka; //@init @delete @array @oftype @index -не удалять имена if(pThis->isDelName&&(pvmo->flag&GHCOM_NAME)&&pvmo->name&&lstrcmpA("@init",pvmo->name)&& lstrcmpA("@delete",pvmo->name)&&lstrcmpA("@array",pvmo->name)&& lstrcmpA("@oftype",pvmo->name)&&lstrcmpA("@index",pvmo->name)) { pvmo->flag &= ~GHCOM_NAME; }else if(pvmo->name) pvmo->flag |= ~GHCOM_NAME; gesave_head( pThis, pvmo->type, pvmo->flag & GHCOM_NAME ? pvmo->name : NULL, pvmo->flag ); switch ( pvmo->type ) { case OVM_NONE: break; case OVM_BYTECODE: gesave_bytecode( pThis, ( povmbcode )pvmo ); break; case OVM_EXFUNC: ((povmfunc)pvmo)->import = ((pvmobj)PCMD(((povmfunc)pvmo)->import))->id; gesave_exfunc( pThis, ( povmfunc )pvmo ); break; case OVM_TYPE: { for(ii = 0; ii<((povmtype)pvmo)->count; ii++) { if(pThis->isDelName) ((povmtype)pvmo)->children[ii].flag &=~GHCOM_NAME; else if(((povmtype)pvmo)->children[ii].name) ((povmtype)pvmo)->children[ii].flag |=GHCOM_NAME; } gesave_type( pThis, ( povmtype )pvmo ); }break; case OVM_GLOBAL: gesave_var( pThis, (( povmglobal )pvmo)->type ); break; case OVM_DEFINE: gesave_define( pThis, ( povmdefine )pvmo ); break; case OVM_IMPORT: gesave_import( pThis, ( povmimport )pvmo ); break; case OVM_ALIAS: gesave_bwd( pThis, (( povmalias )pvmo)->idlink ); break; } gesave_finish(pThis); } // Specify the full size and crc phead = ( pgehead )buf_ptr( pThis, &out ); phead->size = buf_len( pThis, &out ); phead->crc = crc( pThis, ( pubyte )phead + 12, phead->size - 12, 0xFFFFFFFF ); buf2file( pThis, &filename, &out ); buf_delete( pThis, &out ); str_delete( pThis, &filename ); return 1; }