cforth源代码更新,解决了DS栈、RS栈下溢出的问题。增加了取DS、RS长度的子函数。源代码如下:
cforth.c
- /********************** cforth主程序 ************************/
- #include <stdio.h>
- #include <string.h>
- #include "code_words.h"
- #include "colon_words.h"
- /*定义数组inputBuff,申请一段空间用以存放键盘数入的字符串 */
- /*定义数组strBuff,用以存放字符串分解后的每一个cforth词 */
- char inputBuff[1024];
- char strBuff[30];
- int main()
- {
- printf("Cforth 0.1.0, Copyright (C) 2008-2012 Free Software Foundation, Inc.\n");
- printf("Cforth comes with ABSOLUTELY NO WARRANTY; for details type 'license'\n");
- printf("Type 'bye' to exit\n");
-
- /*cforth主循环,从键盘接受输入的命令,调用对应的子程序并执行*/
- while (1){
- int i=0;
- int j=0;
- int k;
- printf(">>>");
- gets(inputBuff); //读取键盘输入的一行命令,存入inputBuff
- /*以下一小段代码,用来解决必须在每行命令后加一个空格的缺陷 */
- for(k=0;inputBuff[k]!='\0';k++);//取得inputBuff字符串的长度
- inputBuff[k]=' '; //将inputBuff字符串结尾的'\0'换成一个空格
- inputBuff[k+1]='\0'; //在字符串末尾空格后添一个'\0'
- /*分解键盘输入的字符串,并执行每个cforth词,直到遇到inputBuff字符串的结尾'\0'*/
- while(inputBuff[j] != '\0') {
- if(inputBuff[j]!= ' ') {
- strBuff[i] = inputBuff[j];
- i++;
- j++;
- }
-
- else {
- strBuff[i]='\0';
- if( isNum() ) chgNum();
- else if( !strcmp(".s",strBuff) ) showDS();
- else if( !strcmp(".rs",strBuff) ) showRS();
- else if( !strcmp(".",strBuff) ) showtopDS();
- else if( !strcmp("swap",strBuff)) swap();
- else if( !strcmp(">r",strBuff) ) tor();
- else if( !strcmp("r>",strBuff) ) rto();
- else if( !strcmp("dup",strBuff) ) dup();
- else if( !strcmp("drop",strBuff) ) drop();
- else if( !strcmp("2drop",strBuff) ) drop2();
- else if( !strcmp("2dup",strBuff) ) dup2();
- else if( !strcmp("over",strBuff) ) over();
- else if( !strcmp("+",strBuff) ) add();
- else if( !strcmp("-",strBuff) ) sub();
- else if( !strcmp("*",strBuff) ) mul();
- else if( !strcmp("/d",strBuff) ) ddiv();
- else if( !strcmp("/",strBuff) ) div();
- else if( !strcmp("%",strBuff) ) mod();
- else if( !strcmp("say",strBuff) ) say();
- else if( !strcmp("bye",strBuff) ) return 0;
- else printf("\n[%s]?\n",strBuff);
-
- for(i=0;i<30;i++) strBuff[i]='\0';
- i=0;
- j++;
- }
- }
- printf("OK\n");
- }
- return 0;
- }
- //判断键盘输入流中是否有数字
- int isNum()
- {
- char *str=strBuff;
- while(*str) {
- if(*str<'0' || *str>'9') return 0;
- str++;
- }
- return 1;
- }
- //转换键盘输入流中的数字,并压入数据栈DS
- int chgNum()
- {
- char *str=strBuff;
- int sum=0;
- while(*str) {
- sum=10*sum+(int)(*str-'0');
- str++;
- }
- //如果这个数字字符串只有一个'\0',则直接返回0。
- //用来解决多余的空格键误操作DS的BUG
- if(str==&strBuff[0]) return 0;
- push(sum);
- return 0;
- }
- //接收键盘输入的字符串,并打印到屏幕
- int say()
- {
- gets(strBuff);
- printf("cforth: [%s]\n",strBuff);
- return 0;
- }
code_words.h
- /********************** cforth核心词 ************************/
- /* 定义数组ds与rs,申请一段空间用作数据栈和DS返回栈RS */
- /* 定义指针*dsp与*rsp,分别指向dS数组与rS数组的首地址 */
- int ds[256],rs[256];
- int *dsp=ds,*rsp=rs;
- //push 将数压入数据栈DS栈顶
- void push(int a){ dsp++; *dsp=a; }
- //>r 将DS栈顶的数弹出,压入RS栈顶
- int tor(){if(dsLen() < 1) return 0; rsp++; *rsp=*dsp; dsp--; return 0;}
- //r> 将RS栈顶的数弹出,压入DS栈顶
- int rto(){ if(rsLen() < 1) return 0;dsp++; *dsp=*rsp; rsp--; return 0;}
- //drop 丢弃DS栈顶
- int drop(){if(dsLen() < 1) return 0; dsp--; return 0;}
- //dup 复制DS栈顶数
- int dup(){if(dsLen() < 1) return 0; dsp++; *dsp=*(dsp-1); return 0;}
- //swap 交换DS栈顶前两个数
- int swap(){if(dsLen() < 2) return 0; int tmp; tmp=*dsp,*dsp=*(dsp-1),*(dsp-1)=tmp;return 0;}
- //+ 将DS栈顶前两个数相加
- int add(){if(dsLen() < 2) return 0; *(dsp-1)=*(dsp-1) + *dsp; dsp--; return 0;}
- //- 将DS次栈顶数减DS栈顶数
- int sub(){if(dsLen() < 2) return 0; *(dsp-1)=*(dsp-1) - *dsp; dsp--;return 0;}
- //* 将DS次栈顶数乘以DS栈顶数
- int mul(){if(dsLen() < 2) return 0; *(dsp-1)=*(dsp-1) * *dsp; dsp--;return 0;}
- // /d 将DS次栈顶数除以DS栈顶数,舍弃余数
- int ddiv(){if(dsLen() < 2) return 0; *(dsp-1)=*(dsp-1) / *dsp; dsp--;return 0;}
- // % 将DS次栈顶数除以DS栈顶数,只留余数
- int mod(){if(dsLen() < 2) return 0; *(dsp-1)=*(dsp-1) % *dsp; dsp--;return 0;}
- //.s 显示数据栈DS
- void showDS()
- {
- int *tmp=&ds[1];
- while(tmp<=dsp) {
- printf("%d ",*tmp);
- tmp++;
- }
- printf(");
- }
- //.rs 显示返回栈RS
- void showRS()
- {
- int *tmp=&rs[1];
- while(tmp<=rsp) {
- printf("%d ",*tmp);
- tmp++;
- }
- printf(");
- }
- //showTop 显示DS栈顶,对DS栈无影响
- void showTop()
- {
- printf("%d\n",*dsp);
- }
- //ifFlag0 若DS栈顶数为0,则丢弃DS栈顶,用于带余数除法
- void ifFlag0()
- {
- int flag = *dsp;
- if(flag == 0) drop();
- }
- //dsLen 返回DS栈的长度
- int dsLen()
- {
- int *tmp=&ds[1];
- int n;
- for(n=0;tmp<=dsp;n++)
- tmp++;
- // printf("\n %d\n",n);
- return n;
- }
- //rsLen 返回RS栈的长度
- int rsLen()
- {
- int *tmp=&rs[1];
- int n;
- for(n=0;tmp<=rsp;n++)
- tmp++;
- return n;
- }
colon_words.h
- /********************** cforth扩展词 ************************/
- //2drop
- void drop2(){ drop(); drop(); }
- //over
- void over(){ tor(); dup(); rto(); swap(); }
- //2dup
- void dup2(){ over(); over(); }
- //show top of DS
- void showtopDS(){ showTop(); drop(); }
- //带余数的除法
- void div() { dup2(); mod(); tor(); ddiv();rto(); ifFlag0(); }
阅读(870) | 评论(0) | 转发(0) |