int ring_vm_findvar ( VM *pVM,const char *cStr ) { int x,nPos,nMax1 ; List *pList,*pList2 ; assert(pVM->pMem); nMax1 = ring_list_getsize(pVM->pMem); /* The scope of the search result */ pVM->nVarScope = RING_VARSCOPE_NOTHING ; if ( nMax1 > 0 ) { /* Loop to search in each Scope */ for ( x = 1 ; x <= 3 ; x++ ) { /* 1 = last scope (function) , 2 = Object State , 3 = global scope */ if ( x == 1 ) { pList = pVM->pActiveMem ; } else if ( x == 2 ) { if ( ring_list_getsize(pVM->pObjState) == 0 ) { continue ; } /* Search in Object State */ pList = ring_list_getlist(pVM->pObjState,ring_list_getsize(pVM->pObjState)) ; pList = (List *) ring_list_getpointer(pList,RING_OBJSTATE_SCOPE) ; if ( pList == NULL ) { continue ; } /* Pass Braces for Class Init() method */ if ( (ring_list_getsize(pVM->pObjState) > 1) && (pVM->nCallClassInit) ) { pList = ring_list_getlist(pVM->pObjState,ring_list_getsize(pVM->pObjState)-1) ; pList = (List *) ring_list_getpointer(pList,RING_OBJSTATE_SCOPE) ; if ( pList == NULL ) { continue ; } } } else { pList = ring_list_getlist(pVM->pMem,RING_MEMORY_GLOBALSCOPE); } if ( ring_list_getsize(pList) < 10 ) { /* Search Using Linear Search */ nPos = ring_list_findstring(pList,cStr,1); if ( nPos != 0 ) { pList2 = ring_list_getlist(pList,nPos); return ring_vm_findvar2(pVM,x,pList2,cStr) ; } } else { /* Search Using the HashTable */ if ( pList->pHashTable == NULL ) { ring_list_genhashtable2(pList); } pList2 = (List *) ring_hashtable_findpointer(pList->pHashTable,cStr); if ( pList2 != NULL ) { return ring_vm_findvar2(pVM,x,pList2,cStr) ; } } } } return 0 ; }
void ring_vm_refmeta_methods ( void *pPointer ) { List *pList, *pList2 ; int x ; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_BADPARACOUNT); return ; } if ( RING_API_ISLIST(1) ) { pList = RING_API_GETLIST(1) ; if ( ring_vm_oop_isobject(pList) ) { pList = (List *) ring_list_getpointer(pList,RING_OBJECT_CLASSPOINTER); pList = ring_list_getlist(pList,RING_CLASSMAP_METHODSLIST); pList2 = RING_API_NEWLIST ; for ( x = 1 ; x <= ring_list_getsize(pList) ; x++ ) { ring_list_addstring(pList2,ring_list_getstring(ring_list_getlist(pList,x),RING_FUNCMAP_NAME)); } RING_API_RETLIST(pList2); } else { RING_API_ERROR(RING_API_BADPARATYPE); } } else { RING_API_ERROR(RING_API_BADPARATYPE); } }
void ring_vm_gc_checkupdatereference ( VM *pVM,List *pList ) { Item *pItem ; /* Reference Counting to Destination before copy from Source */ if ( ring_list_getint(pList,RING_VAR_TYPE) == RING_VM_POINTER ) { if ( ring_list_getint(pList,RING_VAR_PVALUETYPE) == RING_OBJTYPE_LISTITEM ) { pItem = (Item *) ring_list_getpointer(pList,RING_VAR_VALUE) ; ring_item_delete_gc(pVM->pRingState,pItem); } } }
void ring_vm_savestate2 ( VM *pVM,List *pList ) { List *pThis ; /* Save State */ ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pExitMark)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pLoopMark)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pTry)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aBraceObjects)); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pBraceObject); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pObjState)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nInsideBraceFlag); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aForStep)); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pActiveMem); ring_list_addint_gc(pVM->pRingState,pList,pVM->nFuncExecute2); pVM->nInsideBraceFlag = 0 ; /* Save BlockFlag */ ring_list_addint_gc(pVM->pRingState,pList,pVM->nBlockFlag); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->aPCBlockFlag); pVM->nBlockFlag = 0 ; pVM->aPCBlockFlag = ring_list_new_gc(pVM->pRingState,0); /* Save nPrivateFlag, set it to 0 (public not private) */ ring_list_addint_gc(pVM->pRingState,pList,pVM->nPrivateFlag); pVM->nPrivateFlag = 0 ; /* Save nCallClassInit */ ring_list_addint_gc(pVM->pRingState,pList,pVM->nCallClassInit); pVM->nCallClassInit = 0 ; ring_list_addint_gc(pVM->pRingState,pList,pVM->nFuncExecute); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pAssignment); ring_list_addint_gc(pVM->pRingState,pList,pVM->nInClassRegion); ring_list_addint_gc(pVM->pRingState,pList,pVM->nActiveScopeID); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aScopeNewObj)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aScopeID)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nLineNumber); ring_list_addint_gc(pVM->pRingState,pList,pVM->nBeforeEqual); ring_list_addint_gc(pVM->pRingState,pList,pVM->nNOAssignment); ring_list_addint_gc(pVM->pRingState,pList,pVM->nGetSetProperty); ring_list_addint_gc(pVM->pRingState,pList,pVM->nGetSetObjType); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pGetSetObject); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->aLoadAddressScope); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pLoadAddressScope)); /* Save This variable */ pThis = ring_list_getlist(ring_vm_getglobalscope(pVM),RING_VM_STATICVAR_THIS) ; ring_list_addpointer_gc(pVM->pRingState,pList,ring_list_getpointer(pThis,RING_VAR_VALUE)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getint(pThis,RING_VAR_PVALUETYPE)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nCurrentGlobalScope); pVM->nInClassRegion = 0 ; pVM->pAssignment = NULL ; pVM->nNOAssignment = 0 ; }
void ring_vm_savestate ( VM *pVM,List *pList ) { List *pThis ; pList = ring_list_newlist_gc(pVM->pRingState,pList); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pMem)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pFuncCallList)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nFuncExecute); ring_list_addint_gc(pVM->pRingState,pList,pVM->nSP); ring_list_addint_gc(pVM->pRingState,pList,pVM->nFuncSP); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pObjState)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aBraceObjects)); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pBraceObject); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->cFileName); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aPCBlockFlag)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nBlockFlag); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aScopeNewObj)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aActivePackage)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aScopeID)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nActiveScopeID); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pExitMark)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pLoopMark)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pTry)); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pActiveMem); ring_list_addint_gc(pVM->pRingState,pList,pVM->nListStart); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pNestedLists); ring_list_addint_gc(pVM->pRingState,pList,pVM->nInsideBraceFlag); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aForStep)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->aBeforeObjState)); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->aPCBlockFlag); ring_list_addint_gc(pVM->pRingState,pList,pVM->nLineNumber); ring_list_addint_gc(pVM->pRingState,pList,pVM->nInClassRegion); ring_list_addint_gc(pVM->pRingState,pList,pVM->nPrivateFlag); ring_list_addint_gc(pVM->pRingState,pList,pVM->nGetSetProperty); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pGetSetObject); ring_list_addint_gc(pVM->pRingState,pList,pVM->nGetSetObjType); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->pAssignment); ring_list_addint_gc(pVM->pRingState,pList,pVM->nBeforeEqual); ring_list_addint_gc(pVM->pRingState,pList,pVM->nNOAssignment); ring_list_addint_gc(pVM->pRingState,pList,pVM->nFuncExecute2); ring_list_addint_gc(pVM->pRingState,pList,pVM->nCallClassInit); ring_list_addpointer_gc(pVM->pRingState,pList,pVM->aLoadAddressScope); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getsize(pVM->pLoadAddressScope)); /* Save This variable */ pThis = ring_list_getlist(ring_vm_getglobalscope(pVM),RING_VM_STATICVAR_THIS) ; ring_list_addpointer_gc(pVM->pRingState,pList,ring_list_getpointer(pThis,RING_VAR_VALUE)); ring_list_addint_gc(pVM->pRingState,pList,ring_list_getint(pThis,RING_VAR_PVALUETYPE)); ring_list_addint_gc(pVM->pRingState,pList,pVM->nCurrentGlobalScope); }
void ring_parser_icg_showoutput ( List *pListGenCode,int nStatus ) { int x,y,nCount,nCount2 ; List *pList ; assert(pListGenCode != NULL); /* Header */ printf( "\n\n" ) ; ring_print_line(); if ( nStatus == 1 ) { puts("Byte Code - Before Execution by the VM"); } else { puts("Byte Code - After Execution by the VM"); } ring_print_line(); nCount = ring_list_getsize(pListGenCode); if ( nCount > 0 ) { printf( "\n %6s %10s %10s\n", "PC","OPCode","Data" ) ; for ( x = 1 ; x <= nCount ; x++ ) { pList = ring_list_getlist(pListGenCode,x); nCount2 = ring_list_getsize(pList); printf( "\n %6d %10s ", x , RING_IC_OP[ring_list_getint(pList,1)] ) ; if ( nCount2 > 1 ) { for ( y = 2 ; y <= nCount2 ; y++ ) { if ( ring_list_isstring(pList,y) ) { printf( " %5s ",ring_list_getstring(pList,y) ) ; } else if ( ring_list_isnumber(pList,y) ) { if ( ring_list_isdouble(pList,y) ) { printf( " %f",ring_list_getdouble(pList,y) ) ; } else { printf( " %5d ",ring_list_getint(pList,y) ) ; } } else { printf( " %5p ",ring_list_getpointer(pList,y) ) ; } } } } printf( "\n" ) ; } /* End */ puts(""); ring_print_line(); puts(""); }
void ring_vm_refmeta_classname ( void *pPointer ) { List *pList ; char *cStr ; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_BADPARACOUNT); return ; } if ( RING_API_ISLIST(1) ) { pList = RING_API_GETLIST(1) ; if ( ring_vm_oop_isobject(pList) ) { cStr = ring_list_getstring((List *) ring_list_getpointer(pList,RING_OBJECT_CLASSPOINTER),RING_CLASSMAP_CLASSNAME); RING_API_RETSTRING(cStr); } else { RING_API_ERROR(RING_API_BADPARATYPE); } } else { RING_API_ERROR(RING_API_BADPARATYPE); } }
void ring_objfile_writelist ( List *pList,FILE *fObj ) { List *pList2 ; int x,x2 ; char *cString ; char cKey[11] ; strcpy(cKey,"ringstring"); fprintf( fObj , "{\n" ) ; /* Write List Items */ for ( x = 1 ; x <= ring_list_getsize(pList) ; x++ ) { pList2 = ring_list_getlist(pList,x); fprintf( fObj , "[T]\n" ) ; for ( x2 = 1 ; x2 <= ring_list_getsize(pList2) ; x2++ ) { if ( ring_list_isstring(pList2,x2) ) { fprintf( fObj , "[S][%d]" , ring_list_getstringsize(pList2,x2) ) ; /* Encrypt String */ cString = ring_list_getstring(pList2,x2) ; ring_objfile_xorstring(cString,ring_list_getstringsize(pList2,x2),cKey,10); fwrite( ring_list_getstring(pList2,x2) , 1 , ring_list_getstringsize(pList2,x2) , fObj ); /* Decrypt String */ ring_objfile_xorstring(cString,ring_list_getstringsize(pList2,x2),cKey,10); fprintf( fObj , "\n" ) ; } else if ( ring_list_isint(pList2,x2) ) { fprintf( fObj , "[I]%d\n" , ring_list_getint(pList2,x2) ) ; } else if ( ring_list_isdouble(pList2,x2) ) { fprintf( fObj , "[D]%f\n" , ring_list_getdouble(pList2,x2) ) ; } else if ( ring_list_ispointer(pList2,x2) ) { fprintf( fObj , "[P]%p\n" , (void *) ring_list_getpointer(pList2,x2) ) ; } else if ( ring_list_islist(pList2,x2) ) { fprintf( fObj , "[L]\n" ) ; ring_objfile_writelist(ring_list_getlist(pList2,x2) ,fObj); } } fprintf( fObj , "[E]\n" ) ; } fprintf( fObj , "}\n" ) ; }
void ring_vm_restorestate2 ( VM *pVM,List *pList,int x ) { List *pThis ; /* Restore State */ ring_vm_backstate(pVM,ring_list_getint(pList,x),pVM->pExitMark); ring_vm_backstate(pVM,ring_list_getint(pList,x+1),pVM->pLoopMark); ring_vm_backstate(pVM,ring_list_getint(pList,x+2),pVM->pTry); ring_vm_backstate(pVM,ring_list_getint(pList,x+3),pVM->aBraceObjects); pVM->pBraceObject = (List *) ring_list_getpointer(pList,x+4) ; ring_vm_backstate(pVM,ring_list_getint(pList,x+5),pVM->pObjState); pVM->nInsideBraceFlag = ring_list_getint(pList,x+6) ; ring_vm_backstate(pVM,ring_list_getint(pList,x+7),pVM->aForStep); pVM->pActiveMem = (List *) ring_list_getpointer(pList,x+8) ; pVM->nFuncExecute2 = ring_list_getint(pList,x+9) ; /* Restore BlockFLag */ pVM->aPCBlockFlag = ring_list_delete_gc(pVM->pRingState,pVM->aPCBlockFlag); pVM->nBlockFlag = ring_list_getint(pList,x+10) ; pVM->aPCBlockFlag = (List *) ring_list_getpointer(pList,x+11) ; /* Restore nPrivateFlag */ pVM->nPrivateFlag = ring_list_getint(pList,x+12) ; /* Restore nCallClassInit */ pVM->nCallClassInit = ring_list_getint(pList,x+13) ; pVM->nFuncExecute = ring_list_getint(pList,x+14) ; pVM->pAssignment = (void *) ring_list_getpointer(pList,x+15) ; pVM->nInClassRegion = ring_list_getint(pList,x+16) ; pVM->nActiveScopeID = ring_list_getint(pList,x+17) ; ring_vm_backstate(pVM,ring_list_getint(pList,x+18),pVM->aScopeNewObj); ring_vm_backstate(pVM,ring_list_getint(pList,x+19),pVM->aScopeID); pVM->nLineNumber = ring_list_getint(pList,x+20) ; pVM->nBeforeEqual = ring_list_getint(pList,x+21) ; pVM->nNOAssignment = ring_list_getint(pList,x+22) ; pVM->nGetSetProperty = ring_list_getint(pList,x+23) ; pVM->nGetSetObjType = ring_list_getint(pList,x+24) ; pVM->pGetSetObject = (void *) ring_list_getpointer(pList,x+25) ; pVM->aLoadAddressScope = (List *) ring_list_getpointer(pList,x+26) ; ring_vm_backstate(pVM,ring_list_getint(pList,x+27),pVM->pLoadAddressScope); /* Restore global scope, Must be before this because this depend on it */ pVM->nCurrentGlobalScope = ring_list_getint(pList,x+30) ; /* Restore This variable */ pThis = ring_list_getlist(ring_vm_getglobalscope(pVM),RING_VM_STATICVAR_THIS) ; ring_list_setpointer_gc(pVM->pRingState,pThis,RING_VAR_VALUE,ring_list_getpointer(pList,x+28)); ring_list_setint_gc(pVM->pRingState,pThis,RING_VAR_PVALUETYPE,ring_list_getint(pList,x+29)); }
void ring_vm_restorestack ( VM *pVM,List *pList ) { int x ; List *pList2 ; pVM->nSP = 0 ; if ( ring_list_getsize(pList) == 0 ) { return ; } for ( x = ring_list_getsize(pList) ; x >= 1 ; x-- ) { if ( ring_list_isstring(pList,x) ) { RING_VM_STACK_PUSHCVALUE(ring_list_getstring(pList,x)); } else if ( ring_list_isnumber(pList,x) ) { RING_VM_STACK_PUSHNVALUE(ring_list_getdouble(pList,x)); } else if ( ring_list_islist(pList,x) ) { pList2 = ring_list_getlist(pList,x); RING_VM_STACK_PUSHPVALUE(ring_list_getpointer(pList2,1)); RING_VM_STACK_OBJTYPE = ring_list_getint(pList2,2) ; } } }
void ring_vm_gc_checkreferences ( VM *pVM ) { int x ; List *pList, *pList2 ; Item *pItem ; /* Check References (Called when we delete a scope) */ pList = ring_list_getlist(pVM->pMem,ring_list_getsize(pVM->pMem)); for ( x = ring_list_getsize(pList) ; x >= 1 ; x-- ) { pList2 = ring_list_getlist(pList,x); if ( ring_list_getsize(pList2) == RING_VAR_LISTSIZE ) { if ( ring_list_getint(pList2,RING_VAR_TYPE) == RING_VM_POINTER ) { if ( ring_list_getint(pList2,RING_VAR_PVALUETYPE) == RING_OBJTYPE_LISTITEM ) { pItem = (Item *) ring_list_getpointer(pList2,RING_VAR_VALUE) ; #if GCLog printf( "GC CheckReferences - Free Memory %p \n",pItem ) ; #endif ring_item_delete_gc(pVM->pRingState,pItem); } } } } }
void ring_parser_icg_duplicate ( Parser *pParser,int nStart,int nEnd ) { List *pList,*pList2 ; int x ; #if RING_SHOWIC int y,nCount2 ; #endif assert(pParser != NULL); if ( (nStart <= nEnd) && ( nEnd <= ring_parser_icg_instructionscount(pParser) ) ) { for ( x = nStart ; x <= nEnd ; x++ ) { pList = ring_list_newlist(pParser->GenCode); pList2 = ring_list_getlist(pParser->GenCode,x); ring_list_copy(pList,pList2); #if RING_SHOWIC nCount2 = ring_list_getsize(pList); printf( "\n %6d [ %s ] ", ring_list_getsize(pParser->GenCode) , RING_IC_OP[ring_list_getint(pList,1)] ) ; if ( nCount2 > 1 ) { for ( y = 2 ; y <= nCount2 ; y++ ) { if ( ring_list_isstring(pList,y) ) { printf( " Operand : %s ",ring_list_getstring(pList,y) ) ; } else if ( ring_list_isnumber(pList,y) ) { if ( ring_list_isdouble(pList,y) ) { printf( " Operand : %f ",ring_list_getdouble(pList,y) ) ; } else { printf( " Operand : %5d ",ring_list_getint(pList,y) ) ; } } else { printf( " Operand : %5p ",ring_list_getpointer(pList,y) ) ; } } } #endif } } }
int ring_objfile_readfile ( const char *cFileName,RingState *pRingState ) { FILE *fObj; signed char c ; List *pListFunctions, *pListClasses, *pListPackages, *pListCode, *pList, *pListStack ; int nActiveList,nValue,nBraceEnd ; double dValue ; char *cString ; char cKey[11] ; char cFileType[100] ; strcpy(cKey,"ringstring"); /* Create Lists */ pListFunctions = ring_list_new(0); pListClasses = ring_list_new(0); pListPackages = ring_list_new(0); pListCode = ring_list_new(0); pListStack = ring_list_new(0); pList = NULL ; /* Set Active List (1=functions 2=classes 3=packages 4=code) */ nActiveList = 0 ; nBraceEnd = 0 ; /* Open File */ fObj = fopen(cFileName , "rb" ); if ( fObj==NULL ) { printf( "Can't open file %s \n ",cFileName ) ; return 0 ; } fread( cFileType , 1 , 18 , fObj ); cFileType[18] = '\0' ; if ( strcmp(cFileType,"# Ring Object File") != 0 ) { printf( "The file type is not correct - the VM expect a ring object file\n" ) ; return 0 ; } c = getc(fObj); fread( cFileType , 1 , 13 , fObj ); cFileType[13] = '\0' ; if ( strcmp(cFileType,"# Version 1.1") != 0 ) { printf( "The file version is not correct - the VM expect a ring object file version 1.1\n" ) ; return 0 ; } /* Process File */ c = getc(fObj); while ( c != EOF ) { /* Check Char */ switch ( c ) { case '#' : /* Read Line */ while ( c != '\n' ) { c = getc(fObj); #ifdef DEBUG_OBJFILE printf( "%c ",c ) ; #endif } #ifdef DEBUG_OBJFILE puts("Read Comment ! "); #endif break ; case '{' : nActiveList++ ; switch ( nActiveList ) { case 1 : pList = pListFunctions ; break ; case 2 : pList = pListClasses ; break ; case 3 : pList = pListPackages ; break ; case 4 : pList = pListCode ; break ; } break ; case '[' : c = getc(fObj); switch ( c ) { case 'S' : c = getc(fObj); fscanf( fObj , "[%d]" , &nValue ) ; cString = (char *) malloc(nValue+1) ; fread( cString , 1 , nValue , fObj ); cString[nValue] = '\0' ; /* Decrypt String */ ring_objfile_xorstring(cString,nValue,cKey,10); ring_list_addstring2(pList,cString,nValue); free( cString ) ; #ifdef DEBUG_OBJFILE printf( "Read String %s Size %d \n",cString,nValue ) ; #endif break ; case 'I' : c = getc(fObj); fscanf( fObj , "%d" , &nValue ) ; ring_list_addint(pList,nValue); #ifdef DEBUG_OBJFILE printf( "Read Number %d \n ",nValue ) ; #endif break ; case 'D' : c = getc(fObj); fscanf( fObj , "%lf" , &dValue ) ; ring_list_adddouble(pList,dValue); #ifdef DEBUG_OBJFILE printf( "Read Double %d \n",dValue ) ; #endif break ; case 'P' : ring_list_addpointer(pList,NULL); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read Pointer "); #endif break ; case 'T' : ring_list_addpointer(pListStack,pList); pList = ring_list_newlist(pList); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read T "); #endif break ; case 'E' : pList = (List *) ring_list_getpointer(pListStack,ring_list_getsize(pListStack)) ; ring_list_deletelastitem(pListStack); /* Read Line */ while ( c != '\n' ) { c = getc(fObj); } #ifdef DEBUG_OBJFILE puts("Read E "); #endif break ; case 'L' : /* Read Until { */ while ( c != '{' ) { c = getc(fObj); } ring_list_addpointer(pListStack,pList); pList = ring_list_newlist(pList); nBraceEnd = 1 ; #ifdef DEBUG_OBJFILE puts("Read L "); #endif break ; } break ; case '}' : if ( nBraceEnd ) { pList = (List *) ring_list_getpointer(pListStack,ring_list_getsize(pListStack)) ; ring_list_deletelastitem(pListStack); nBraceEnd = 0 ; #ifdef DEBUG_OBJFILE puts("Read } "); #endif } break ; } c = getc(fObj); } /* Close File */ fclose( fObj ) ; ring_list_delete(pListStack); /* Update Ring State */ #ifdef DEBUG_OBJFILE puts("Old Code List "); ring_list_print(pRingState->pRingGenCode); #endif /* Update Lists */ pRingState->pRingFunctionsMap = pListFunctions ; pRingState->pRingClassesMap = pListClasses ; pRingState->pRingPackagesMap = pListPackages ; pRingState->pRingGenCode = pListCode ; #ifdef DEBUG_OBJFILE puts("Update Done! "); puts("New Code List "); ring_list_print(pRingState->pRingGenCode); #endif /* Update Classes Pointers */ ring_objfile_updateclassespointers(pRingState); return 1 ; }
int ring_vm_findvar2 ( VM *pVM,int x,List *pList2,const char *cStr ) { int nPC,nType ; Item *pItem ; List *pList ; /* ** Now We have the variable List ** The Scope of the search result */ if ( ( x == 1 ) && (pVM->pActiveMem == ring_list_getlist(pVM->pMem,RING_MEMORY_GLOBALSCOPE)) ) { x = RING_VARSCOPE_GLOBAL ; } else if ( (x == 1) && (pVM->pActiveMem != ring_list_getlist(pVM->pMem,ring_list_getsize(pVM->pMem))) ) { x = RING_VARSCOPE_NEWOBJSTATE ; } pVM->nVarScope = x ; pVM->nSP++ ; if ( ring_list_getint(pList2,RING_VAR_TYPE) == RING_VM_POINTER ) { if ( pVM->nFirstAddress == 1 ) { RING_VM_STACK_SETPVALUE(pList2); RING_VM_STACK_OBJTYPE = RING_OBJTYPE_VARIABLE ; return 1 ; } RING_VM_STACK_SETPVALUE(ring_list_getpointer(pList2,RING_VAR_VALUE )); RING_VM_STACK_OBJTYPE = ring_list_getint(pList2,RING_VAR_PVALUETYPE) ; /* ** Here we don't know the correct scope of the result ** becauase a global variable may be a reference to local variable ** And this case happens with setter/getter of the attributes using eval() */ pVM->nVarScope = RING_VARSCOPE_NOTHING ; } else { /* Check Private Attributes */ if ( ring_list_getint(pList2,RING_VAR_PRIVATEFLAG) == 1 ) { if ( pVM->nVarScope != RING_VARSCOPE_OBJSTATE ) { if ( ring_vm_oop_callmethodinsideclass(pVM) == 0 ) { ring_vm_error2(pVM,RING_VM_ERROR_USINGPRIVATEATTRIBUTE,cStr); return 0 ; } } } RING_VM_STACK_SETPVALUE(pList2); RING_VM_STACK_OBJTYPE = RING_OBJTYPE_VARIABLE ; /* Check Setter/Getter for Public Attributes */ if ( pVM->nGetSetProperty == 1 ) { ring_vm_oop_setget(pVM,pList2); } else if ( ( x == RING_VARSCOPE_OBJSTATE ) && ( ring_vm_oop_callmethodinsideclass(pVM) == 0 ) ) { /* Accessing Object Attribute Using { } */ if ( ring_list_getsize(pVM->aBraceObjects) > 0 ) { pList = ring_list_getlist(pVM->aBraceObjects,ring_list_getsize(pVM->aBraceObjects)); /* Get Object List */ pList = (List *) ring_list_getpointer(pList,RING_ABRACEOBJECTS_BRACEOBJECT); nType = ring_vm_oop_objtypefromobjlist(pList); /* Set Object Pointer & Type */ if ( nType == RING_OBJTYPE_VARIABLE ) { pList = ring_vm_oop_objvarfromobjlist(pList); pVM->pGetSetObject = pList ; } else if ( nType == RING_OBJTYPE_LISTITEM ) { pItem = ring_vm_oop_objitemfromobjlist(pList); pVM->pGetSetObject = pItem ; } pVM->nGetSetObjType = nType ; /* Change Assignment Instruction to SetProperty */ if ( RING_VM_IR_PARACOUNT >= 4 ) { if ( RING_VM_IR_READIVALUE(3) != 0 ) { nPC = pVM->nPC ; pVM->nPC = RING_VM_IR_READIVALUE(3) ; RING_VM_IR_LOAD ; RING_VM_IR_OPCODE = ICO_SETPROPERTY ; pVM->nPC = nPC ; RING_VM_IR_UNLOAD ; /* Avoid AssignmentPointer , we don't have assignment */ pVM->nNOAssignment = 1 ; } } ring_vm_oop_setget(pVM,pList2); } } } return 1 ; }
void ring_vm_restorestate ( VM *pVM,List *pList,int nPos,int nFlag ) { List *pThis ; pList = ring_list_getlist(pList,nPos); /* Set Scope */ pVM->pActiveMem = (List *) ring_list_getpointer(pList,19) ; /* ** Delete Scopes using the correct function ** We need to delete each scope using ring_vm_deletescope() - so don't use ring_vm_backstate ** We also avoid doing this in the Class Region (After class name) ** Because in the class region we don't use pVM->pMEM */ if ( ! pVM->nInClassRegion ) { while ( ring_list_getlist(pVM->pMem,ring_list_getsize(pVM->pMem)) != pVM->pActiveMem ) { ring_vm_deletescope(pVM); } } /* We also return to the function call list */ ring_vm_backstate(pVM,ring_list_getint(pList,2),pVM->pFuncCallList); /* Stack & Executing Functions */ pVM->nFuncExecute = ring_list_getint(pList,3) ; pVM->nSP = ring_list_getint(pList,4) ; pVM->nFuncSP = ring_list_getint(pList,5) ; /* We also return to the Active Object */ ring_vm_backstate(pVM,ring_list_getint(pList,6),pVM->pObjState); ring_vm_backstate(pVM,ring_list_getint(pList,7),pVM->aBraceObjects); pVM->pBraceObject = (List *) ring_list_getpointer(pList,8) ; /* FileName & Packages */ pVM->cFileName = (char *) ring_list_getpointer(pList,9) ; /* aPCBlockFlag, aScopeNewObj , aActivePackage & aScopeID */ if ( ((List *) ring_list_getpointer(pList,25)) != pVM->aPCBlockFlag ) { pVM->aPCBlockFlag = ring_list_delete_gc(pVM->pRingState,pVM->aPCBlockFlag); pVM->aPCBlockFlag = (List *) ring_list_getpointer(pList,25) ; } ring_vm_backstate(pVM,ring_list_getint(pList,10),pVM->aPCBlockFlag); pVM->nBlockFlag = ring_list_getint(pList,11) ; ring_vm_backstate(pVM,ring_list_getint(pList,12),pVM->aScopeNewObj); ring_vm_backstate(pVM,ring_list_getint(pList,13),pVM->aActivePackage); ring_vm_backstate(pVM,ring_list_getint(pList,14),pVM->aScopeID); pVM->nActiveScopeID = ring_list_getint(pList,15) ; /* Loop/Exit Mark */ if ( nFlag != RING_STATE_EXIT ) { ring_vm_backstate(pVM,ring_list_getint(pList,16),pVM->pExitMark); ring_vm_backstate(pVM,ring_list_getint(pList,17),pVM->pLoopMark); /* For Step */ ring_vm_backstate(pVM,ring_list_getint(pList,23),pVM->aForStep); } /* Try/Catch/Done */ if ( nFlag != RING_STATE_TRYCATCH ) { ring_vm_backstate(pVM,ring_list_getint(pList,18),pVM->pTry); } /* List Status */ pVM->nListStart = ring_list_getint(pList,20) ; if ( ring_list_getpointer(pList,21) != pVM->pNestedLists ) { pVM->pNestedLists = ring_list_delete_gc(pVM->pRingState,pVM->pNestedLists); pVM->pNestedLists = (List *) ring_list_getpointer(pList,21) ; } pVM->nInsideBraceFlag = ring_list_getint(pList,22) ; ring_vm_backstate(pVM,ring_list_getint(pList,24),pVM->aBeforeObjState); pVM->nLineNumber = ring_list_getint(pList,26) ; pVM->nInClassRegion = ring_list_getint(pList,27) ; pVM->nPrivateFlag = ring_list_getint(pList,28) ; pVM->nGetSetProperty = ring_list_getint(pList,29) ; pVM->pGetSetObject = (void *) ring_list_getpointer(pList,30) ; pVM->nGetSetObjType = ring_list_getint(pList,31) ; pVM->pAssignment = (void *) ring_list_getpointer(pList,32) ; pVM->nBeforeEqual = ring_list_getint(pList,33) ; pVM->nNOAssignment = ring_list_getint(pList,34) ; pVM->nFuncExecute2 = ring_list_getint(pList,35) ; pVM->nCallClassInit = ring_list_getint(pList,36) ; pVM->aLoadAddressScope = (List *) ring_list_getpointer(pList,37) ; ring_vm_backstate(pVM,ring_list_getint(pList,38),pVM->pLoadAddressScope); /* We restore the global scope befor the This variable, because This use global scope */ pVM->nCurrentGlobalScope = ring_list_getint(pList,41) ; /* Restore This variable */ pThis = ring_list_getlist(ring_vm_getglobalscope(pVM),RING_VM_STATICVAR_THIS) ; ring_list_setpointer_gc(pVM->pRingState,pThis,RING_VAR_VALUE,ring_list_getpointer(pList,39)); ring_list_setint_gc(pVM->pRingState,pThis,RING_VAR_PVALUETYPE,ring_list_getint(pList,40)); }
int ring_parser_stmt ( Parser *pParser ) { int x,nMark1,nMark2,nMark3,nStart,nEnd,nPerformanceLocations ; String *pString ; List *pMark,*pMark2,*pMark3,*pList2 ; double nNum1 ; char cStr[50] ; nPerformanceLocations = 0 ; char cFileName[200] ; assert(pParser != NULL); /* Statement --> Load Literal */ if ( ring_parser_iskeyword(pParser,K_LOAD) ) { ring_parser_nexttoken(pParser); if ( ring_parser_isliteral(pParser) ) { /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_FILENAME); ring_parser_icg_newoperand(pParser,pParser->TokenText); ring_parser_icg_newoperation(pParser,ICO_BLOCKFLAG); pMark = ring_parser_icg_getactiveoperation(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Load' Literal"); #endif /* No package at the start of the file */ pParser->ClassesMap = pParser->pRingState->pRingClassesMap ; strcpy(cFileName,pParser->TokenText); if ( ring_fexists(pParser->TokenText) == 0 ) { ring_exefolder(cFileName); strcat(cFileName,pParser->TokenText); if ( ring_fexists(cFileName) == 0 ) { strcpy(cFileName,pParser->TokenText); } } ring_scanner_readfile(cFileName,pParser->pRingState); /* ** Generate Code ** Return NULL */ ring_parser_icg_newoperation(pParser,ICO_RETNULL); nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark1); /* Set Active File */ ring_parser_icg_newoperation(pParser,ICO_FILENAME); ring_parser_icg_newoperand(pParser,ring_list_getstring(pParser->pRingState->pRingFilesStack,ring_list_getsize(pParser->pRingState->pRingFilesStack))); ring_parser_nexttoken(pParser); return 1 ; } return 0 ; } /* Statement --> See Expr */ if ( ring_parser_iskeyword(pParser,K_SEE) ) { ring_parser_nexttoken(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_FUNCEXE); x = ring_parser_expr(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_PRINT); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'See' Expr"); #endif return x ; } /* Statement --> Give Identifier */ if ( ring_parser_iskeyword(pParser,K_GIVE) ) { ring_parser_nexttoken(pParser); if ( ring_parser_isidentifier(pParser) ) { /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_LOADADDRESS); ring_parser_icg_newoperand(pParser,pParser->TokenText); ring_parser_nexttoken(pParser); x = ring_parser_mixer(pParser); if ( x == 0 ) { return 0 ; } /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_GIVE); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Give' Identifier|ListItem|Object.Attribute"); #endif return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_VARNAME); return 0 ; } } /* Statement --> For Identifier = Expr to Expr {Statement} Next | For Identifier in Expr {Statemen */ if ( ring_parser_iskeyword(pParser,K_FOR) ) { ring_parser_nexttoken(pParser); if ( ring_parser_isidentifier(pParser) ) { pString = ring_string_new(pParser->TokenText); ring_parser_nexttoken(pParser); if ( ring_parser_isoperator(pParser,"=") ) { /* ** Generate Code ** Mark for Exit command to go to outside the loop */ ring_parser_icg_newoperation(pParser,ICO_EXITMARK); pMark3 = ring_parser_icg_getactiveoperation(pParser); ring_parser_icg_newoperation(pParser,ICO_LOADAFIRST); ring_parser_icg_newoperand(pParser,ring_string_get(pString)); ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { /* ** Generate Code ** Before Equal ( = ) not += , -= ,... etc */ ring_parser_icg_newoperation(pParser,ICO_BEFOREEQUAL); ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperation(pParser,ICO_ASSIGNMENT); nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_newoperation(pParser,ICO_LOADAPUSHV); ring_parser_icg_newoperand(pParser,ring_string_get(pString)); if ( ring_parser_iskeyword(pParser,K_TO) ) { ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* Generate Code */ if ( (ring_parser_icg_getlastoperation(pParser) == ICO_PUSHN) && (ring_parser_icg_newlabel(pParser) == (nMark1+2)) ) { /* ** We check nMark2+2 to avoid executing next instructions when we have expr ** for example for x = 1 to 10+5 */ nNum1 = ring_list_getdouble(pParser->ActiveGenCodeList,2) ; ring_parser_icg_deletelastoperation(pParser); ring_parser_icg_setlastoperation(pParser,ICO_JUMPVARLENUM); ring_parser_icg_newoperanddouble(pParser,nNum1); /* Add Locations Needed for Instruction change for performance */ nPerformanceLocations = 1 ; } else { ring_parser_icg_newoperation(pParser,ICO_JUMPFOR); } pMark = ring_parser_icg_getactiveoperation(pParser); /* Step <expr> */ x = ring_parser_step(pParser,&nMark1); if ( x == 0 ) { ring_string_delete(pString); return 0 ; } #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'For' Identifier '=' Expr to Expr ['step' Expr]"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_NEXT) ) { /* Generate Code */ nMark3 = ring_parser_icg_newlabel(pParser); /* Increment Jump */ ring_parser_icg_newoperation(pParser,ICO_INCJUMP); ring_parser_icg_newoperand(pParser,ring_string_get(pString)); ring_parser_icg_newoperandint(pParser,nMark1); /* Add Locations needed for instruction change for performance */ ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperandint(pParser,0); nMark2 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark2); /* Performance Locations */ if ( nPerformanceLocations ) { /* Add Locations Needed for Instruction JUMPVARLENUM change for performance */ ring_parser_icg_addoperandint(pMark,0); ring_parser_icg_addoperandint(pMark,0); } /* Set Exit Mark */ ring_parser_icg_addoperandint(pMark3,nMark2); /* Set Loop Mark */ ring_parser_icg_addoperandint(pMark3,nMark3); /* End Loop (Remove Exit Mark) */ ring_parser_icg_newoperation(pParser,ICO_POPEXITMARK); /* POP Step */ ring_parser_icg_newoperation(pParser,ICO_POPSTEP); ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Next --> 'Next'"); #endif ring_string_delete(pString); return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_NEXT); } } } } } else if ( ring_parser_iskeyword(pParser,K_IN) ) { /* Generate Code */ sprintf( cStr , "n_sys_var_%d" , ring_parser_icg_instructionscount(pParser) ) ; /* Mark for Exit command to go to outside the loop */ ring_parser_icg_newoperation(pParser,ICO_EXITMARK); pMark3 = ring_parser_icg_getactiveoperation(pParser); ring_parser_icg_newoperation(pParser,ICO_LOADADDRESS); ring_parser_icg_newoperand(pParser,cStr); ring_parser_icg_newoperation(pParser,ICO_PUSHN); ring_parser_icg_newoperanddouble(pParser,1.0); /* Before Equal ( = ) not += , -= ,... etc */ ring_parser_icg_newoperation(pParser,ICO_BEFOREEQUAL); ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperation(pParser,ICO_ASSIGNMENT); /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_newoperation(pParser,ICO_LOADAPUSHV); ring_parser_icg_newoperand(pParser,cStr); ring_parser_icg_newoperation(pParser,ICO_LOADFUNC); ring_parser_icg_newoperand(pParser,"len"); nStart = ring_parser_icg_instructionscount(pParser) + 1 ; ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* Generate Code */ nEnd = ring_parser_icg_instructionscount(pParser) ; /* Note (nEnd-1) , -1 to remove instruction PushV (avoid error with for x in string) */ if ( ring_parser_icg_getlastoperation(pParser) == ICO_PUSHV ) { nEnd-- ; } ring_parser_icg_newoperation(pParser,ICO_CALL); /* Generate 0 For Operator OverLoading */ ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperation(pParser,ICO_JUMPFOR); pMark = ring_parser_icg_getactiveoperation(pParser); ring_parser_icg_newoperation(pParser,ICO_LOADAFIRST); ring_parser_icg_newoperand(pParser,ring_string_get(pString)); ring_parser_icg_duplicate(pParser,nStart,nEnd); ring_parser_icg_newoperation(pParser,ICO_LOADAPUSHV); ring_parser_icg_newoperand(pParser,cStr); ring_parser_icg_newoperation(pParser,ICO_LOADINDEXADDRESS); /* Generate 0 For Operator OverLoading */ ring_parser_icg_newoperandint(pParser,0); /* Item by reference */ ring_parser_icg_newoperation(pParser,ICO_SETREFERENCE); /* Step <expr> */ x = ring_parser_step(pParser,&nMark1); if ( x == 0 ) { ring_string_delete(pString); return 0 ; } #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'For' Identifier 'in' Expr ['step' Expr]"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_NEXT) ) { ring_parser_nexttoken(pParser); /* Generate Code */ nMark3 = ring_parser_icg_newlabel(pParser); /* Increment Jump */ ring_parser_icg_newoperation(pParser,ICO_INCJUMP); ring_parser_icg_newoperand(pParser,cStr); ring_parser_icg_newoperandint(pParser,nMark1); /* Add Locations needed for instruction change for performance */ ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperandint(pParser,0); nMark2 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark2); /* Set Exit Mark */ ring_parser_icg_addoperandint(pMark3,nMark2); /* Set Loop Mark */ ring_parser_icg_addoperandint(pMark3,nMark3); /* End Loop (Remove Exit Mark) */ ring_parser_icg_newoperation(pParser,ICO_POPEXITMARK); /* POP Step */ ring_parser_icg_newoperation(pParser,ICO_POPSTEP); /* Remove Reference Value */ ring_parser_icg_newoperation(pParser,ICO_LOADAFIRST); ring_parser_icg_newoperand(pParser,ring_string_get(pString)); ring_parser_icg_newoperation(pParser,ICO_KILLREFERENCE); ring_parser_icg_newoperation(pParser,ICO_PUSHN); ring_parser_icg_newoperanddouble(pParser,1.0); /* Before Equal ( = ) not += , -= ,... etc */ ring_parser_icg_newoperation(pParser,ICO_BEFOREEQUAL); ring_parser_icg_newoperandint(pParser,0); ring_parser_icg_newoperation(pParser,ICO_ASSIGNMENT); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Next --> 'Next'"); #endif ring_string_delete(pString); return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_NEXT); } } } ring_string_delete(pString); } return 0 ; } /* Statement --> IF Expr Statements OK */ if ( ring_parser_iskeyword(pParser,K_IF) ) { ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* ** First Condition ** Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMPZERO); pMark = ring_parser_icg_getactiveoperation(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'If' Expr {Statement} { But } [Else] Ok"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } /* Generate Code */ pList2 = ring_list_new(0); ring_parser_icg_newoperation(pParser,ICO_JUMP); ring_list_addpointer(pList2,ring_parser_icg_getactiveoperation(pParser)); /* { 'But' Statements } 'Else' Statements */ while ( ring_parser_iskeyword(pParser,K_BUT) ) { /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark1); ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMPZERO); pMark = ring_parser_icg_getactiveoperation(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : But --> 'But' Expr {Statement}"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMP); ring_list_addpointer(pList2,ring_parser_icg_getactiveoperation(pParser)); } } if ( ring_parser_iskeyword(pParser,K_ELSE) ) { /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark1); pMark = NULL ; ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Else --> 'Else' {Statement} "); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } } if ( ring_parser_iskeyword(pParser,K_OK) ) { /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); if ( pMark != NULL ) { ring_parser_icg_addoperandint(pMark,nMark1); } if ( ring_list_getsize(pList2) > 0 ) { for ( x = 1 ; x <= ring_list_getsize(pList2) ; x++ ) { ring_parser_icg_addoperandint(((List *) ring_list_getpointer(pList2,x)),nMark1); } } ring_list_delete(pList2); ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Ok --> 'OK'"); #endif return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_OK); ring_list_delete(pList2); } } return 0 ; } /* Statement --> WHILE Expr Statements END */ if ( ring_parser_iskeyword(pParser,K_WHILE) ) { /* ** Generate Code ** Mark for Exit command to go to outsize the loop */ ring_parser_icg_newoperation(pParser,ICO_EXITMARK); pMark3 = ring_parser_icg_getactiveoperation(pParser); nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMPZERO); pMark = ring_parser_icg_getactiveoperation(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'While' Expr {Statement} End"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_END) ) { /* Generate Code */ nMark3 = ring_parser_icg_newlabel(pParser); ring_parser_icg_newoperation(pParser,ICO_JUMP); ring_parser_icg_newoperandint(pParser,nMark1); nMark2 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark2); /* Set Exit Mark */ ring_parser_icg_addoperandint(pMark3,nMark2); /* Set Loop Mark */ ring_parser_icg_addoperandint(pMark3,nMark3); /* End Loop (Remove Exit Mark) */ ring_parser_icg_newoperation(pParser,ICO_POPEXITMARK); ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : End --> 'End'"); #endif return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_END); } } return 0 ; } /* Statement --> DO Statements AGAIN Expr */ if ( ring_parser_iskeyword(pParser,K_DO) ) { /* ** Generate Code ** Mark for Exit command to go to outsize the loop */ ring_parser_icg_newoperation(pParser,ICO_EXITMARK); pMark3 = ring_parser_icg_getactiveoperation(pParser); nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Do' {Statement} Again"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_AGAIN) ) { /* Generate Code */ ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { /* Generate Code (Test Condition) */ ring_parser_icg_newoperation(pParser,ICO_JUMPZERO); pMark = ring_parser_icg_getactiveoperation(pParser); /* Generate Code */ nMark3 = ring_parser_icg_newlabel(pParser); ring_parser_icg_newoperation(pParser,ICO_JUMP); ring_parser_icg_newoperandint(pParser,nMark1); nMark2 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark2); /* Set Exit Mark */ ring_parser_icg_addoperandint(pMark3,nMark2); /* Set Loop Mark */ ring_parser_icg_addoperandint(pMark3,nMark3); /* End Loop (Remove Exit Mark) */ ring_parser_icg_newoperation(pParser,ICO_POPEXITMARK); pParser->nAssignmentFlag = 1 ; #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Again --> 'Again' Expr"); #endif return 1 ; } } else { ring_parser_error(pParser,RING_PARSER_ERROR_AGAIN); } return 0 ; } /* Statement --> Return Expr */ if ( ring_parser_iskeyword(pParser,K_RETURN) ) { ring_parser_nexttoken(pParser); x = 1 ; if ( ring_parser_isendline(pParser) == 0 ) { /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_FUNCEXE); pParser->nAssignmentFlag = 0 ; x = ring_parser_expr(pParser); pParser->nAssignmentFlag = 1 ; /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_ENDFUNCEXE); ring_parser_icg_newoperation(pParser,ICO_RETURN); } else { /* ** Generate Code ** Return NULL */ ring_parser_icg_newoperation(pParser,ICO_RETNULL); } #if RING_PARSERTRACE if ( x == 1 ) { RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Return'"); } #endif return x ; } /* Statement --> Try {Statement} Catch {Statement} Done */ if ( ring_parser_iskeyword(pParser,K_TRY) ) { ring_parser_nexttoken(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_TRY); pMark = ring_parser_icg_getactiveoperation(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Try' {Statement} Catch Done"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_CATCH) ) { ring_parser_nexttoken(pParser); /* ** Generate Code ** Jump from end of try block to label after done */ ring_parser_icg_newoperation(pParser,ICO_JUMP); pMark2 = ring_parser_icg_getactiveoperation(pParser); nMark1 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark,nMark1); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Catch --> 'Catch' {Statement}"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } if ( ring_parser_iskeyword(pParser,K_DONE) ) { #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Done --> 'Done'"); #endif ring_parser_nexttoken(pParser); /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMP); pMark3 = ring_parser_icg_getactiveoperation(pParser); nMark2 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark2,nMark2); ring_parser_icg_newoperation(pParser,ICO_DONE); nMark3 = ring_parser_icg_newlabel(pParser); ring_parser_icg_addoperandint(pMark3,nMark3); return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_NODONE); } } else { ring_parser_error(pParser,RING_PARSER_ERROR_NOCATCH); } } /* Statement --> Bye (Close the Program) */ if ( ring_parser_iskeyword(pParser,K_BYE) ) { ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Bye' "); #endif /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_BYE); return 1 ; } /* Statement --> Exit (Go to outside the loop) */ if ( ring_parser_iskeyword(pParser,K_EXIT) ) { ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Exit' "); #endif /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_EXIT); /* Check Number (Exit from more than one loop) */ if ( ring_parser_isnumber(pParser) ) { ring_parser_icg_newoperanddouble(pParser,atof(pParser->TokenText)); ring_parser_nexttoken(pParser); } return 1 ; } /* Statement --> Loop (Continue) */ if ( ring_parser_iskeyword(pParser,K_LOOP) ) { ring_parser_nexttoken(pParser); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Loop'"); #endif /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_LOOP); /* Check Number (Continue from more than one loop) */ if ( ring_parser_isnumber(pParser) ) { ring_parser_icg_newoperanddouble(pParser,atof(pParser->TokenText)); ring_parser_nexttoken(pParser); } return 1 ; } /* Statement --> Switch Expr { ON Expr {Statement} } OFF */ if ( ring_parser_iskeyword(pParser,K_SWITCH) ) { ring_parser_nexttoken(pParser); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> 'Switch' Expr {ON} [Other] OFF"); #endif RING_PARSER_IGNORENEWLINE ; /* ON Statements */ pList2 = ring_list_new(0); pMark = NULL ; while ( ring_parser_iskeyword(pParser,K_ON) ) { ring_parser_nexttoken(pParser); /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); if ( pMark != NULL ) { ring_parser_icg_addoperandint(pMark,nMark1); } ring_parser_icg_newoperation(pParser,ICO_DUPLICATE); pParser->nAssignmentFlag = 0 ; if ( ring_parser_expr(pParser) ) { pParser->nAssignmentFlag = 1 ; /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_EQUAL); ring_parser_icg_newoperation(pParser,ICO_JUMPZERO); pMark = ring_parser_icg_getactiveoperation(pParser); ring_parser_icg_newoperation(pParser,ICO_FREESTACK); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : ON --> 'on' Expr {Statement}"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } /* Generate Code */ ring_parser_icg_newoperation(pParser,ICO_JUMP); ring_list_addpointer(pList2,ring_parser_icg_getactiveoperation(pParser)); } } /* Other */ if ( ring_parser_iskeyword(pParser,K_OTHER) ) { ring_parser_nexttoken(pParser); /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); if ( pMark != NULL ) { ring_parser_icg_addoperandint(pMark,nMark1); pMark = NULL ; } ring_parser_icg_newoperation(pParser,ICO_FREESTACK); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Other --> 'Other' {Statement}"); #endif while ( ring_parser_stmt(pParser) ) { if ( pParser->ActiveToken == pParser->TokensCount ) { break ; } } } /* OFF */ if ( ring_parser_iskeyword(pParser,K_OFF) ) { ring_parser_nexttoken(pParser); /* Generate Code */ nMark1 = ring_parser_icg_newlabel(pParser); if ( pMark != NULL ) { ring_parser_icg_addoperandint(pMark,nMark1); } if ( ring_list_getsize(pList2) > 0 ) { for ( x = 1 ; x <= ring_list_getsize(pList2) ; x++ ) { ring_parser_icg_addoperandint(((List *) ring_list_getpointer(pList2,x)),nMark1); } } ring_list_delete(pList2); ring_parser_icg_newoperation(pParser,ICO_FREESTACK); #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : OFF --> 'Off'"); #endif return 1 ; } else { ring_parser_error(pParser,RING_PARSER_ERROR_SWITCHOFF); } } else { ring_parser_error(pParser,RING_PARSER_ERROR_SWITCHEXPR); } } /* Statement --> epslion */ if ( ring_parser_epslion(pParser) ) { return 1 ; } /* Statement --> Expr */ if ( ring_parser_expr(pParser) ) { #if RING_PARSERTRACE RING_STATE_CHECKPRINTRULES puts("Rule : Statement --> Expr "); #endif ring_parser_icg_newoperation(pParser,ICO_FREESTACK); return 1 ; } return 0 ; }