Пример #1
0
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 ;
}
Пример #2
0
RING_API void ring_state_runfile ( RingState *pRingState,const char *cFileName )
{
	ring_scanner_readfile(cFileName,pRingState);
}