Chinaunix首页 | 论坛 | 博客
  • 博客访问: 676236
  • 博文数量: 156
  • 博客积分: 3402
  • 博客等级: 中校
  • 技术积分: 1639
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-13 14:06
个人简介

业余编程爱好者

文章分类

全部博文(156)

文章存档

2014年(1)

2013年(13)

2012年(46)

2011年(38)

2010年(58)

分类: LINUX

2012-03-26 21:06:46

增加了cforth代码的注释,并添加了几个cforth扩展词

cforth.c

点击(此处)折叠或打开

  1. /********************** cforth主程序 ************************/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "code_words.h"
  5. #include "colon_words.h"
  6. /*定义数组inputBuff,申请一段空间用以存放键盘数入的字符串    */
  7. /*定义数组strBuff,用以存放字符串分解后的每一个cforth词        */
  8. char inputBuff[1024];
  9. char strBuff[30];

  10. int main()
  11. {
  12.     printf("Cforth 0.1.0, Copyright (C) 2008-2012 Free Software Foundation, Inc.\n");
  13.     printf("Cforth comes with ABSOLUTELY NO WARRANTY; for details type 'license'\n");
  14.     printf("Type 'bye' to exit\n");
  15.     
  16.     /*cforth主循环,从键盘接受输入的命令,调用对应的子程序并执行*/
  17.     while (1){
  18.           int i=0;
  19.           int j=0;
  20.           int k;
  21.           printf(">>>");
  22.         gets(inputBuff);                //读取键盘输入的一行命令,存入inputBuff
  23.         
  24.         /*以下一小段代码,用来解决必须在每行命令后加一个空格的缺陷 */
  25.         for(k=0;inputBuff[k]!='\0';k++);//取得inputBuff字符串的长度
  26.         inputBuff[k]=' '; //将inputBuff字符串结尾的'\0'换成一个空格
  27.         inputBuff[k+1]='\0'; //在字符串末尾空格后添一个'\0'

  28.         /*分解键盘输入的字符串,并执行每个cforth词,直到遇到inputBuff字符串的结尾'\0'*/
  29.           while(inputBuff[j] != '\0') {
  30.                if(inputBuff[j]!= ' ') {    
  31.                 strBuff[i] = inputBuff[j];
  32.                 i++;
  33.                 j++;
  34.             }
  35.             else {
  36.                 strBuff[i]='\0';
  37.                 if( isNum() )                         chgNum();
  38.                 else if ( !strcmp(".s",strBuff) )     showDS();
  39.                 else if ( !strcmp(".rs",strBuff) )    showRS();
  40.                 else if( !strcmp(".",strBuff) )       showtopDS();
  41.                 else if( !strcmp(">r",strBuff) )       tor();
  42.                 else if( !strcmp("r>",strBuff) )       rto();
  43.                 else if( !strcmp("dup",strBuff) )      dup();
  44.                 else if( !strcmp("drop",strBuff) )     drop();
  45.                 else if( !strcmp("2drop",strBuff) )    drop2();
  46.                 else if( !strcmp("2dup",strBuff) )     dup2();
  47.                 else if( !strcmp("over",strBuff) )     over();
  48.                 else if( !strcmp("+",strBuff) )        add();
  49.                 else if( !strcmp("-",strBuff) )        sub();
  50.                 else if( !strcmp("*",strBuff) )        mul();
  51.                 else if( !strcmp("/d",strBuff) )       ddiv();
  52.                 else if( !strcmp("/",strBuff) )        div();
  53.                 else if( !strcmp("%",strBuff) )        mod();
  54.                 else if( !strcmp("say",strBuff) )      say();
  55.                 else if( !strcmp("bye",strBuff) )      return 0;
  56.                 else printf("\n[%s]?\n",strBuff);
  57.           
  58.                 for(i=0;i<30;i++) strBuff[i]='\0';
  59.                 i=0;
  60.                 j++;
  61.             }
  62.         }
  63.         printf("OK\n");
  64.      }
  65.     return 0;
  66. }

  67. //判断键盘输入流中是否有数字
  68. int isNum()
  69. {
  70.     char *str=strBuff;
  71.     while(*str) {
  72.         if(*str<'0' || *str>'9') return 0;
  73.         str++;
  74.     }
  75.         return 1;
  76. }

  77. //转换键盘输入流中的数字,并压入数据栈DS
  78. int chgNum()
  79. {
  80.     char *str=strBuff;
  81.     int sum=0;
  82.     while(*str) {
  83.         sum=10*sum+(int)(*str-'0');
  84.         str++;
  85.     }
  86.     //如果这个数字字符串只有一个'\0',则直接返回0。
  87.     //用来解决多余的空格键误操作DS的BUG
  88.     if(str==&strBuff[0]) return 0;
  89.     push(sum);
  90.     return 0;
  91. }
  92.  
  93. //接收键盘输入的字符串,并打印到屏幕
  94. int say()
  95. {
  96.     gets(strBuff);
  97.     printf("cforth: [%s]\n",strBuff);
  98.     return 0;
  99. }

code_words.h

点击(此处)折叠或打开

  1. /********************** cforth核心词 ************************/
  2. /* 定义数组ds与rs,申请一段空间用作数据栈和DS返回栈RS        */
  3. /* 定义指针*dsp与*rsp,分别指向dS数组与rS数组的首地址         */
  4. int ds[256],rs[256];
  5. int *dsp=ds,*rsp=rs;

  6. //push 将数压入数据栈DS栈顶
  7. void push(int a){ dsp++; *dsp=a; }

  8. //>r 将DS栈顶的数弹出,压入RS栈顶
  9. void tor(){ rsp++; *rsp=*dsp; dsp--; }

  10. //r> 将RS栈顶的数弹出,压入DS栈顶
  11. void rto(){ dsp++; *dsp=*rsp; rsp--; }

  12. //drop 丢弃DS栈顶
  13. void drop(){ dsp--; }

  14. //dup 复制DS栈顶数
  15. void dup(){ dsp++; *dsp=*(dsp-1); }

  16. //swap 交换DS栈顶前两个数
  17. void swap(){ int tmp; tmp=*dsp,*dsp=*(dsp-1),*(dsp-1)=tmp; }

  18. //+ 将DS栈顶前两个数相加
  19. void add(){ *(dsp-1)=*(dsp-1) + *dsp; dsp--; }

  20. //- 将DS次栈顶数减DS栈顶数
  21. void sub(){ *(dsp-1)=*(dsp-1) - *dsp; dsp--;}

  22. //* 将DS次栈顶数乘以DS栈顶数
  23. void mul(){ *(dsp-1)=*(dsp-1) * *dsp; dsp--;}

  24. // /d 将DS次栈顶数除以DS栈顶数,舍弃余数
  25. void ddiv(){ *(dsp-1)=*(dsp-1) / *dsp; dsp--;}

  26. // % 将DS次栈顶数除以DS栈顶数,只留余数
  27. void mod(){ *(dsp-1)=*(dsp-1) % *dsp; dsp--;}

  28. //.s 显示数据栈DS
  29. void showDS()
  30. {
  31.     int *tmp=&ds[1];
  32.     while(tmp<=dsp) {
  33.         printf("%d ",*tmp);
  34.         tmp++;
  35.     }
  36.     printf(");
  37. }

  38. //.rs 显示返回栈RS
  39. void showRS()
  40. {
  41.     int *tmp=&rs[1];
  42.     while(tmp<=rsp) {
  43.         printf("%d ",*tmp);
  44.         tmp++;
  45.     }
  46.     printf(");
  47. }

  48. //showTop 显示DS栈顶,对DS栈无影响
  49. void showTop()
  50. {
  51.     printf("%d\n",*dsp);
  52. }

  53. //ifFlag0 若DS栈顶数为0,则丢弃DS栈顶,用于带余数除法
  54. void ifFlag0()
  55. {
  56.     int flag = *dsp;
  57.     if(flag == 0) drop();
  58. }

colon_words.h

点击(此处)折叠或打开

  1. /********************** cforth扩展词 ************************/
  2. //2drop
  3. void drop2(){ drop(); drop(); }

  4. //over
  5. void over(){ tor(); dup(); rto(); swap(); }

  6. //2dup
  7. void dup2(){ over(); over(); }

  8. //show top of DS
  9. void showtopDS(){ showTop(); drop(); }

  10. //带余数的除法
  11. void div() { dup2(); mod(); tor(); ddiv();rto(); ifFlag0(); }

阅读(765) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~