示例#1
0
文件: update.c 项目: dgod/yong
static int allow_update(const char *file)
{
	FILE *fp;
	int i;
	int allow=0;
	char line[256];

	if(file[0]=='/') file++;
	if(strncmp(file,"mb/",3) || !l_str_has_suffix(file,".txt"))
	{
		return 1;
	}
	fp=l_file_open(file,"rb",y_im_get_path("DATA"),NULL);
	if(!fp)
	{
		return 0;
	}
	for(i=0;i<16 && !feof(fp);i++)
	{
		l_get_line(line,sizeof(line),fp);
		if(!strcmp(line,"update=1"))
		{
			allow=1;
			break;
		}
	}
	fclose(fp);
	return allow;
}
示例#2
0
void l_scan (SOURCE_FILE * sf)
{
	int r = 1,in_sub = FALSE;
	int block_stack[STACK_SIZE],block_size = 0;

	list_init(&list_sub);
	list_init(&list_var);
	list_init(&list_array);

	while(r)
	{
		r = l_get_line(sf);
		pline = line;
		l_get_token ();
		// skip empty line
		if (token_type==TT_LINE_END) continue;
		// check command
		if (IS_KEYWORD(token_type))
		{
			if (token_type==KEY_SUB)
			{
				USER_SUB * sub;
				
				if(in_sub)
				{
					merror_msg("sub procedure can not be nested");
				}
				in_sub = TRUE;

				match_type(TT_ID);
				sub = (USER_SUB*)calloc(sizeof(USER_SUB),1);
				sub->name	= s_strdup(token);
				sub->pos	= sf->pos;
				list_push(&list_sub,sub);
				match_type(TT_LINE_END);
				push_block(B_SUB);
				continue;
			}
			else if (token_type==KEY_END)
			{
				if (block_size<=0 || block_top==B_REPEAT)
					merror_illegal_token();
				match_type(TT_LINE_END);
				if(pop_block==B_SUB)
				{
					in_sub = FALSE;
				}
				continue;
			}
			else if(token_type==KEY_VAR)
			{
				while(1)
				{
					match_type(TT_ID);
					l_get_token();
					if (token_type==TT_END)			break;
					else if (token_type==TT_COM)	continue;
					else 							merror_illegal_token();
				}
			}
			if (!in_sub) merror_illegal_token();
			if(token_type==KEY_IF)
			{
				match_exp(pline);
				match_type(TT_LINE_END);
				push_block(B_IF);
			}
			else if (token_type==KEY_ELSEIF)
			{
				if (block_size<=0 || block_top!=B_IF)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_ELSE)
			{
				if (block_size<=0 || !(block_top==B_IF || block_top==B_CASE))
					merror_illegal_token();
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_WHILE)
			{
				match_exp(pline);
				match_type(TT_LINE_END);
				push_block(B_WHILE);
			}
			else if (token_type==KEY_FOR)
			{
				// 'for' ID '=' EXP 'to' EXP
				match_type(TT_ID);
				match_type(OPR_EQ);
				match_exp(pline);
				match_str("to");
				match_exp(pline);
				l_get_token();
				if (token_type!=TT_LINE_END)
				{
					if (!str_eq(token,"step"))
						merror_expect("step");
					match_exp(pline);
					match_type(TT_LINE_END);
				}
				push_block(B_FOR);
			}
			else if (token_type==KEY_CASE)
			{
				match_type(TT_ID);
				match_type(TT_LINE_END);
				push_block(B_CASE);
			}
			else if (token_type==KEY_REPEAT)
			{
				match_type(TT_LINE_END);
				push_block(B_REPEAT);
			}
			else if (token_type==KEY_UNTIL)
			{
				if (block_size<=0 || block_top!=B_REPEAT)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
				pop_block;
			}
			else if (token_type==KEY_WHEN)
			{
				if (block_size<=0 || block_top!=B_CASE)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_GOSUB)
			{
				match_type(TT_ID);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_EXIT)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_BREAK)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_RETURN)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_DIM)
			{
				while(1)
				{
					match_type	(TT_ID);
					match_type	(TT_LBK);
					match_exp	(pline);
					match_type	(TT_RBK);
					l_get_token();
					if		(token_type==TT_COM)		continue;
					else if	(token_type==TT_LINE_END)	break;
					else								merror_illegal_token();
				}
			}
		}
		else if (token_type==TT_ID)
		{
			if (!in_sub) merror_illegal_token();
			if (str_eq(token,"print"))
			{
				while(1)
				{
					l_get_token();
					if(token_type!=TT_STRING)
					{
						l_put_back();
						match_exp(pline);
					}
					l_get_token();
					if(token_type==TT_LINE_END) break;
					else if (token_type==TT_COM) continue;
					else merror_expect(",");
				}
			}
			else if (str_eq(token,"input"))
			{
				while(1)
				{
					match_type(TT_ID);
					l_get_token();
					if(token_type==TT_LINE_END) break;
					else if (token_type==TT_COM) continue;
					else merror_expect(",");
				}
			}
			else
			{	// match: [var][=][exp]
				l_get_token();
				if (token_type==OPR_EQ)
				{
					match_exp(pline);
					match_type(TT_LINE_END);
				}
				else
				{
					if (token_type!=TT_LBK)merror_expect("(");
					match_exp(pline);
					match_type(TT_RBK);
					match_type(OPR_EQ);
					match_exp(pline);
					match_type(TT_LINE_END);
				}
			}
		}
		else 
			merror_illegal_token ();
	}
	if (block_size>0)
		merror_msg("incompleted '%s' block!",BLOCK_NAME[block_top]);
}
示例#3
0
int i_execute_line (SOURCE_FILE * sf,const int todo)
{
	l_get_line(sf);pline = line;
	l_get_token();
	if (token_type==TT_LINE_END)
	{
		return EXE_DO;// skip empty line
	}
	else if (IS_KEYWORD(token_type))
	{
		if (token_type==KEY_END)
		{
			return EXE_END;
		}
		else if (token_type==KEY_IF)
		{
			return i_execute_if (sf,todo);
		}
		else if (token_type==KEY_WHILE)
		{
			return i_execute_while(sf,todo);
		}
		else if (token_type==KEY_REPEAT)
		{
			return i_execute_repeat(sf,todo);
		}
		if (token_type==KEY_UNTIL)
		{
			return EXE_UNTIL;
		}
		else if (token_type==KEY_FOR)
		{
			return i_execute_for(sf,todo);
		}
		else if (token_type==KEY_CASE)
		{
			return i_execute_case(sf,todo);
		}
		else if (token_type==KEY_ELSEIF)
		{
			return EXE_ELSEIF;
		}
		else if (token_type==KEY_WHEN)
		{
			return EXE_WHEN;
		}
		else if (token_type==KEY_ELSE)
		{
			return EXE_ELSE;
		}
		if (!todo) return EXE_DO;
		if (token_type==KEY_BREAK)
		{
			return EXE_BREAK;
		}
		else if (token_type==KEY_RETURN)
		{
			return EXE_RETURN;
		}
		else if (token_type==KEY_GOSUB)
		{
			extern int i_call_sub (SOURCE_FILE *,const char *);
			l_get_token();
			return i_call_sub(sf,token);
		}
		else if (token_type==KEY_DIM)
		{
			ARRAY *	array;
			long	size;
			
			while(1)
			{
				l_get_token();
				// new array
				array = (ARRAY*)calloc(sizeof(ARRAY),1);
				array->name  = s_strdup(token);
				list_push(&list_array,array);
				//
				l_get_token();// skip '('
				size = (long)calc_check(FALSE,pline);
				if (size<=0)
				{
					merror_msg("illegal array size %d!",size);
				}
				array->size  = size;
				array->array = calloc(sizeof(real),size);
				{
					int i;
					for (i=0;i<size;++i) array->array[i] = 0.0;
				}
				l_get_token();// skip ')'
				l_get_token();
				if		(token_type==TT_COM)		continue;
				else if	(token_type==TT_LINE_END)	break;
			}
		}
		else if (token_type==KEY_EXIT)
		{
			d_exit();
		}
	}
	else if(token_type==TT_ID)
	{
		if (str_eq(token,"print"))
		{
			while(1)
			{
				l_get_token();
				if (token_type==TT_STRING)
				{
					printf("%s",token);
				}
				else
				{
					real result;
					char buf[128];
				
					l_put_back();
					result = calc_check(FALSE,pline);
					d_ftoa(result,buf);
					printf(buf);
				}
				l_get_token();
				if (token_type==TT_LINE_END)break;
			}//while
		}//print
		else if (str_eq(token,"input"))
		{
			VAR * v;char buf[64];
			while(1)
			{
				l_get_token();
				v = get_var(token);
				gets(64,buf);puts(buf);
				v->value = atof(buf);
				l_get_token();
				if (token_type==TT_LINE_END)break;
			}//while
		}//input
		else
		{
			ARRAY * a;
			VAR * var;
			var = find_var(token);
			if (var!=NULL)
			{
				l_get_token(); //skip '='
				var->value = calc_check(FALSE,pline);
				return EXE_DO;
			}
			a = find_array(token);
			if(a!=NULL)
			{
				int index;
				l_get_token();//skip (
				index = (int)calc_check(FALSE,pline);
				// delete this line,calc_check call skip ( automatically
				//l_get_token();//skip )
				l_get_token();// skip =
				assign_element(a,index,calc_check(FALSE,pline));
				return EXE_DO;
			}
			var = create_var(token);
			l_get_token(); //skip '='
			var->value = calc_check(FALSE,pline);
			return EXE_DO;
		}//assign
	}// TT_ID
	else
	{
		merror_illegal_token();
	}
	return EXE_DO;
}