⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic4.c

📁 用C语言写的YABasic解释器
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "os.h"
#include "ptype.h"
#include "res.h"
/***self Func***/
FAR void get_data();
FAR void BasicFileSort(int f, int l);
FAR void adjust_poutbuf(char *ad_ptr);

FAR int atoi(char *x);
FAR void itoa(int i, char *result, char flag);
FAR double atof(char *x);
FAR void gcvt(float i, char flag, char *result);
FAR void exec_mid();
FAR void exec_right();
FAR void exec_left();
FAR void exec_fn();
FAR void exec_inkey();
FAR char get_result_type(char o, char *r, char *h);
FAR void add_type(char *result, int data_type);
FAR void get_exp(char *result);
FAR void exec_eof(char *v, char *result);
FAR void FreeOpMem();
FAR char *GetAllocBuf();
FAR void exec_cls();
FAR void scan_labels();
FAR UInt8 agree_v(char *var, char *value);
FAR void assignment();
FAR void BasicCreateFileListBox();
FAR void BasicFileFind();
FAR void CreateBasicFileSaveWindow(char flag);
FAR UInt8 IsInteger(char *value_ptr);
FAR void exec_rset();
FAR void exec_lset();
/***basic2.c Func***/
FAR void skip_white();
FAR bitnode *search_vartree(char *s);
FAR void doingassign(char *var, char *value);
FAR void IsArray();
FAR void assign_buf();
FAR char *get_type(char *s);
FAR char *find_label(char *s);
FAR void label_init();
FAR void get_next();
FAR char *gpop();
FAR void gpush(char *s);
FAR void fpush(for_stack_str *i);
FAR void fpop(for_stack_str *i);
FAR void find_eol();
FAR void wpop(while_stack_str *i);
FAR char *get_variables(char *s);

FAR void putback();
FAR char get_token(char tflag);
FAR void serror(Int8 error);
FAR void ckvar(char *s);
FAR char *get_token_last();
FAR int isalpha(char c);
FAR int isdigit(char c);
FAR char tolower(char c);
FAR int isdelim(char c);
FAR int iswhite(char c);
FAR char look_up(char *s);
FAR char get_priority(char *s);
FAR void FreeOpMem();
FAR void IsEndOfLine();

extern bitnode 		        *Variables;
extern pfnode 			*pf;     
extern fsnode 			*fl;
extern data_str 		*data_buf, *cur_buf;
extern for_stack_str 		fstack[FOR_NEST];
extern while_stack_str 	        wstack[WHILE_WEND];
extern label_str 		*label_table;
extern Do_List 	                *dl;
extern char             *TmpBuf;
extern char 		*prog;
extern char 		*token;
extern char 		token_type, tok;
extern char 		notdoelse;
extern char 		*gstack[SUB_NEST];
extern int 		ftos,wtos,gtos;
extern char 		op_b[4];
extern double           PreRndNum;
extern Int8             sl_flag;
extern jmp_buf          e_buf;
extern Int8 		*pInBuf,*pOutBuf;
extern UInt8            GTflag;
extern UInt8            nBasicRet, LastnBasieRet;
extern UInt8            ResetFlag;          /***程序复位标志***/
extern Int8             InputFlag;
extern UInt8            *hBasicWnd, *hListBox, *hMainEdit;         /***主窗口***/
extern UInt8 		*hDesktop;
extern UInt8            nBasicMode, Lastcurrent_mode;
extern Int8             *TmpBuf, *TmpOut, pCurBuf, *pBasicFileName;
extern UInt8            bPosX,bPosY;
extern Int16             CurLine;
extern Int16            FileCount;

const FunctionButtons Function_Button_List[] = {
	{IDS_AP_NEW, cmJS_new},
        {IDS_BASIC_BROWSER, cmJS_bro},
        {IDS_AP_DEL, cmJS_del},
        {IDS_BASIC_RUN,cmJS_run},
        {0, 0}};

FAR void exec_fn()
{
        pfnode *p; 
        char *exp_buf, *e, *var_buf, *v, *key_buf, *k, *t, *saveprog, *value, *s;
        int lb,rb;
        
        lb = rb = 0;
        p = pf;
        if (*prog!=32) serror(21);
        while (*prog==32) prog++;
        t = token;
        while (lb!=rb||lb==0) 
        {
                 if (*prog=='(') lb++;
                 if (*prog==')') rb++;
                 *t++ = *prog++;
        }
        *t = 0;
       
        exp_buf = GetAllocBuf();
        var_buf = GetAllocBuf();
        key_buf = GetAllocBuf();                 	       

        t = token; 
        v = var_buf; 
        k = key_buf;
        while (*t!='(') *v++ = *t++; 
        *v = 0;
        while (p!=(pfnode *)Null) 
        {
	     if (!Stricmp(p->var,var_buf)) break;
	     else p = p->next;
        }
        if (p==(pfnode *)Null) serror(21);  /***使用的自定义函数未经定义***/
        t++;
        lb = 1;
        rb = 0;
        while (lb!=rb) 
        {
	     if (*t==')') rb++;
	     if (*t=='(') lb++;
	     *k++ = *t++;
        }
        *(k-1) = 0;
        s = p->exp; 
        e = exp_buf;
        while (*s!=0)
        {
	     if (*s!=',')  *e++ = *s++;
	     else 
	     {
		  s++;
		  k = key_buf;
		  *e++ = '(';
		  while (*k!=0) *e++ = *k++;
		  *e++ = ')';
	     }
        }
       *e = 0;
       value = GetAllocBuf();
       saveprog = prog; 
       prog = exp_buf;
       get_exp(value);
       Strcpy(token,value+1);
       prog = saveprog;
}

FAR void scan_labels()
{
	int addr;
	char *temp, flag;
	label_str *Cur_Labstr, *new_Labstr;
	
	flag = 0;
	if (!Strstr(prog,"END")&&!Strstr(prog,"ENd")&&!Strstr(prog,"eND")&&!Strstr(prog,"EnD")
	  &&!Strstr(prog,"end")&&!Strstr(prog,"eNd")&&!Strstr(prog,"enD")&&!Strstr(prog,"End")) serror(5);
        Cur_Labstr = (label_str*)Null;
	temp = prog;
        addr = 0;
        while (*prog!=0) {
        	get_token(0);
        	if (*token=='-') serror(4);        /***定义的标号非法***/
        	if (token_type==NUMBER) {
        		if (Cur_Labstr!=(label_str*)Null&&Cur_Labstr->label_num>=atoi(token)) serror(4); 
        		if (Strlen(token)>4) serror(4);           /***定义的标号非法***/
        		if ((new_Labstr = (label_str*)MemAlloc(sizeof(label_str)))==(label_str*)Null) serror(0);
        		new_Labstr->label_num = (UInt16)atoi(token);
        		new_Labstr->p = prog;
        		new_Labstr->next = (label_str*)Null;
        		if (label_table == (label_str*)Null) {
        			label_table = new_Labstr;
        			Cur_Labstr = new_Labstr;
        		} else {
        			Cur_Labstr->next = new_Labstr;
        			Cur_Labstr = new_Labstr;
        		}        		               		
        		while (*prog==32) prog++;
        		if (*prog==0x0D) {
        			tok = EOL; 
        			prog++;
        		} else {        			
        		        Strncpy(token,prog,4);
        		        *(token+4) = 0;
        		}
        	}
        	if (!Stricmp(token,"data")) assign_buf();
        	if (*(token+3)==0x0D||*(token+3)==0x20) *(token+3) = 0;
        	if (!Stricmp(token,"end")) flag = 1;
        	if (tok!=EOL) {
        	        while (*prog!=0x0D&&*prog!=0) prog++;
        	        if (*prog==0x0D) prog++;
        	}
        }
        if (!flag) serror(5); /*************文件没有结束标志*****************/
	prog = temp;
	sl_flag = 0;
}

FAR void exec_cls()
{
	MRect client;
	UInt8 *hEditWin;
		
	Memset(TmpOut,0,200);
	GuiWindowGetClientArea(hBasicWnd,&client);
	hEditWin=GuiScrnEditGetEdit(hBasicWnd);
	if (hEditWin!=(UInt8*)Null) GuiEditSetText(hEditWin,(UInt8*)TmpOut); 
	bPosX = 0;
	bPosY = 0;
	Memset(pOutBuf,0,200);		
        GraphClearRect((MDC*)hBasicWnd,client.x,client.y, client.x+client.width,client.y+client.height);
}


FAR UInt8 agree_v(char *var, char *value)
{
	char *dot_pos;
	
	if (!Strchr(var,'$')&&!Strchr(var,'%')||Strchr(var,'$')&&*value==STRING_TYPE||Strchr(var,'%')&&*value==INTEGER_TYPE) return 1;	
	if (Strchr(var,'%')&&*value==REAL_TYPE)
	{
		if (dot_pos = Strchr(value,'.')) *dot_pos = 0;
		*value = INTEGER_TYPE;
		return 1;
	}
	return 0;	
}

FAR void assignment()  /*分配变量*/
{
	char *var, *value, *var_tmp, *value_tmp;
	bitnode *p;
	
	p=(bitnode *)Null;
	get_token(0);
	ckvar(token);
	var_tmp = GetAllocBuf();
	Strcpy(var_tmp, token);
	get_token(0);
	if(*token!='=') serror(0);     /***语句出现语法错误***/
	value_tmp = GetAllocBuf();
	get_exp(value_tmp);	
	IsInteger(value_tmp);
	p = search_vartree(var_tmp);
	if (p==(bitnode *)Null) {
		if (agree_v(var_tmp,value_tmp))
		{
			if ((var = (Int8*)MemAlloc(Strlen(var_tmp)+1))==(Int8*)Null) serror(1); /***内存溢出***/
			if ((value = (Int8*)MemAlloc(Strlen(value_tmp)+1))==(Int8*)Null) {
				MemFree(var);
			        serror(1);            /***内存溢出***/
			}
			Strcpy(var,var_tmp);
			Strcpy(value,value_tmp);
			doingassign(var,value);
		}else 
			serror(2);       	      /***赋值语句中,等号左右变量或数值类型值不匹配***/						
		 
	} else {	         	         
	         if (agree_v(p->var_addr,value_tmp)) {
	         	 if ((value = (Int8*)MemAlloc(Strlen(value_tmp)+1))==(Int8*)Null) serror(1);	/***内存溢出***/         	 
	                 Strcpy(value,value_tmp);
	                 MemFree(p->value_addr); 
	                 p->value_addr=value;	    
	         } else 
	         	 serror(2);	             /***赋值语句中,等号左右变量或数值类型值不匹配***/
	}
	while (*prog==0x20) prog++;
	if (*prog!=0x0d&&*prog!=':') serror(0);				
}

char *count_buffer_line(char *buf, int *stop)
{
	char *p;
	int k, j;
	k = 0;
	j = 0;
	p = buf;
	
	while (*p!=0)
	{
		if ((UInt8)*p>=0x80)
		{
			k+=2;
			p+=2;
		}else if (*p==0x0D)
		{
			k = 0;
			p++;
			if (++j==*stop) break;
		}
		else
		{
			k++;
			p++;
		}
		if (k==20)
		{
			if (*p==0x0d) p++;
			if (++j==*stop) break;
			k=0;			
		}
		if (k>20)
		{
			p -= 2;
			k = 0;
			if (++j==*stop) break;
		}		
	}
	if (k>0&&k<20) j++;
	*stop = j;
	return p;
}

FAR void adjust_poutbuf(char *ad_ptr)
{
	char *p;
	int stop;
	
	count_buffer_line(ad_ptr, &stop);
	stop = stop - 5;
	if (stop>0)
	{
	      p = count_buffer_line(ad_ptr, &stop);	
	      Strcpy(ad_ptr,p);
	}
/*				
	temp_buf = GetAllocBuf();
	ptemp = ad_ptr + Strlen(ad_ptr);
	if (*(ptemp-1) == 0x0d) 
	{
		flag = 1; j = 0;
	}
	else 
	{
		flag = 0; j = 1;
	}
        
        k = 0;
        while (j < 5) {
           	while (*ptemp!=0x0D&&ptemp!=ad_ptr&&j<5) {
               		ptemp--; 
              		k++;
                  	if (k>=20) {
        	        	j++; 
        	        	k -= 20; 
        	        }
        	}
        	if (ptemp==ad_ptr) break;
        	if (j<5) { 
        		j++;
        	        ptemp--; 
                        k = 0;
                }
        }        
        if (j==5) 
        {
        	p = ad_ptr;
        	while (p < ptemp)
        	{
        		if ((UInt8)*p>=0x80) p += 2;
        		else p++;
        	}        	
        	ptemp = p;
        	if (flag)
        	{
        	      if (*ptemp!=0x0D) Strcpy(ad_ptr,ptemp + 2);
        	      else Strcpy(ad_ptr,ptemp + 1);
        	}        		
        	else 
        		Strcpy(ad_ptr,ptemp);
        }*/
}

UInt16 BasicQuickFind(char *pfile_name)
{
       UInt16 mid, fnum, lnum;
       Int16 comp_result;
       char *dot_pos;
       
       if (*pfile_name==0) return 0;
       if (dot_pos=Strchr(pfile_name,'.')) *dot_pos = 0;
       fnum = 0;
       lnum = FileCount;
       while (fnum<=lnum) {
       	      mid = (fnum+lnum)/2;       	      
       	      comp_result = Stricmp(pfile_name,pInBuf + mid * 10);
       	      if (!comp_result) return mid;
       	      else if (comp_result<0) lnum = mid - 1;
       	      else fnum = mid + 1;
       }
       return (UInt16)0;
}

FAR void BasicCreateFileListBox()
{
       UInt16 style_win;
       MRect rect, client;
       ListColInfo colinfo;
       UInt32 func;

       func = _Far(Function_Button_List);	     
       style_win = wsBoundType0 | wsTitleArea | wsFullScreen | wsHelp | wsFunctionArea;
       hBasicWnd = GuiWindowCreate((UInt8 *)Null, 0, &rect, style_win, _Far("Basic编程"), IDH_BASIC_01, func, 0);
       if(hBasicWnd == (UInt8*)Null) return;
       GuiWindowGetClientArea(hBasicWnd, &client);        		              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -