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

业余编程爱好者

文章分类

全部博文(156)

文章存档

2014年(1)

2013年(13)

2012年(46)

2011年(38)

2010年(58)

分类: LINUX

2012-03-24 12:43:16


耳朵在《Forth系统入门手册》中,给出了用C语言实现forth系统模型的代码,我完善了这个模型,使它看起来更像一个真正的Forth系统。代码为三个文件 cforth.c code_words.h colon_words.h,使用GCC编译器。对Forth有兴趣的朋友可以编译下试试。


cforth.c文件

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. int ds[256],rs[256];
  4. int *dsp=ds,*rsp=rs;
  5. int flag;
  6. #include "code_words.h"
  7. #include "colon_words.h"
  8. char inputBuff[1024];
  9. char strBuff[30];
  10. int main(){
  11.  printf("Cforth 0.1.0, Copyright (C) 2008-2012 Free Software Foundation, Inc.\n");
  12.  printf("Cforth comes with ABSOLUTELY NO WARRANTY; for details type `license'\n");
  13.  printf("Type `bye' to exit\n");
  14.     while (1){
  15.   int i=0;
  16.   int j=0;
  17.   gets(inputBuff);
  18.   while(inputBuff[j] != '\0'){
  19.    if(inputBuff[j]!= ' '){
  20.     strBuff[i] = inputBuff[j];
  21.     i++;
  22.     j++;
  23.    }
  24.    else {
  25.     strBuff[i]='\0';
  26.     if ( !strcmp(".s",strBuff) ) showDS();
  27.           else if( !strcmp("dup",strBuff) ) dup();
  28.           else if( !strcmp(".",strBuff) ) showtopDS();
  29.           else if( !strcmp(">r",strBuff) ) tor();
  30.           else if( !strcmp("r>",strBuff) ) rto();
  31.           else if( !strcmp("drop",strBuff) ) drop();
  32.           else if( !strcmp("+",strBuff) ) add();
  33.           else if( !strcmp("-",strBuff) ) sub();
  34.           else if( !strcmp("*",strBuff) ) mul();
  35.            else if( !strcmp("/d",strBuff) ) ddiv();
  36.           else if( !strcmp("/",strBuff) ) div();
  37.           else if( !strcmp("%",strBuff) ) mod();
  38.        else if( isNum() ) chgNum();
  39.        else if( !strcmp("say",strBuff) ) say();
  40.        else if( !strcmp("bye",strBuff) ) return 0;
  41.           else printf("\n[%s]?\n",strBuff);
  42.           
  43.           for(i=0;i<30;i++) strBuff[i]='\0';
  44.           i=0;
  45.           j++;
  46.    }
  47.   }
  48.   showOK();
  49.  }
  50.  return 0;
  51. }
  52. int isNum()
  53. {
  54.         char *str=strBuff;
  55.         while(*str)
  56.         {
  57.                 if(*str<'0' || *str>'9') return 0;
  58.                 str++;
  59.         }return 1;
  60. }
  61. int chgNum()
  62. {
  63.         char *str=strBuff;
  64.         int sum=0;
  65.         while(*str)
  66.         {
  67.                 sum=10*sum+(int)(*str-'0');
  68.                 str++;
  69.         }
  70.         push(sum);
  71.         return 0;
  72. }
  73. int showOK()
  74. {
  75.  printf("OK\n");
  76.  return 0;
  77. }
  78. int say()
  79. {
  80.  gets(strBuff);
  81.  printf("cforth: [%s]\n",strBuff);
  82.  return 0;
  83. }
code_words.h

点击(此处)折叠或打开

  1. //push 输入数字到DS
  2. void push(int a){ dsp++; *dsp=a; }
  3. //>r
  4. void tor(){ rsp++; *rsp=*dsp; dsp--; }
  5. //r>
  6. void rto(){ dsp++; *dsp=*rsp; rsp--; }
  7. //丢弃DS栈顶
  8. void drop(){ dsp--; }
  9. //复制DS栈顶
  10. void dup(){ dsp++; *dsp=*(dsp-1); }
  11. //交换DS栈顶前两个数
  12. void swap(){ int tmp; tmp=*dsp,*dsp=*(dsp-1),*(dsp-1)=tmp; }
  13. //+
  14. void add(){ *(dsp-1)=*(dsp-1) + *dsp; dsp--; }
  15. //-
  16. void sub(){ *(dsp-1)=*(dsp-1) - *dsp; dsp--;}
  17. //*
  18. void mul(){ *(dsp-1)=*(dsp-1) * *dsp; dsp--;}
  19. // /d
  20. void ddiv(){ *(dsp-1)=*(dsp-1) / *dsp; dsp--;}
  21. // %
  22. void mod(){ *(dsp-1)=*(dsp-1) % *dsp; dsp--;}
  23. //显示DS
  24. void showDS()
  25. {
  26.         int *tmp=&ds[1];
  27.         while(tmp<=dsp)
  28.         {
  29.                 printf("%d ",*tmp);
  30.                 tmp++;
  31.         }
  32.         printf(");
  33. }
  34. //显示RS
  35. void showRS()
  36. {
  37.         int *tmp=&rs[1];
  38.         while(tmp<=rsp)
  39.         {
  40.                 printf("%d ",*tmp);
  41.                 tmp++;
  42.         }
  43.         printf(");
  44. }
  45. //显示DS栈顶,堆栈无影响
  46. void showTOP()
  47. {
  48.  printf("%d\n",*dsp);
  49. }
  50. //若DS栈顶为0,则丢弃栈顶
  51. void ifFlag0()
  52. {
  53.  flag = *dsp;
  54.  if(flag == 0) drop();
  55. }

colon_words.h

点击(此处)折叠或打开

  1. //2drop
  2. void drop2(){ drop(); drop(); }
  3. //over
  4. void over(){ tor(); dup(); rto(); swap(); }
  5. //2dup
  6. void dup2(){ over(); over(); }
  7. //show top of DS
  8. void showtopDS(){ showTOP(); drop(); }
  9. //带余数的除法
  10. void div() { dup2(); mod(); tor(); ddiv();rto(); ifFlag0(); }

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