耳朵在《Forth系统入门手册》中,给出了用C语言实现forth系统模型的代码,我完善了这个模型,使它看起来更像一个真正的Forth系统。代码为三个文件 cforth.c code_words.h colon_words.h,使用GCC编译器。对Forth有兴趣的朋友可以编译下试试。
cforth.c文件
- #include <stdio.h>
- #include <string.h>
- int ds[256],rs[256];
- int *dsp=ds,*rsp=rs;
- int flag;
- #include "code_words.h"
- #include "colon_words.h"
- 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");
- while (1){
- int i=0;
- int j=0;
- gets(inputBuff);
- while(inputBuff[j] != '\0'){
- if(inputBuff[j]!= ' '){
- strBuff[i] = inputBuff[j];
- i++;
- j++;
- }
- else {
- strBuff[i]='\0';
- if ( !strcmp(".s",strBuff) ) showDS();
- else if( !strcmp("dup",strBuff) ) dup();
- else if( !strcmp(".",strBuff) ) showtopDS();
- else if( !strcmp(">r",strBuff) ) tor();
- else if( !strcmp("r>",strBuff) ) rto();
- else if( !strcmp("drop",strBuff) ) drop();
- 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( isNum() ) chgNum();
- 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++;
- }
- }
- showOK();
- }
- return 0;
- }
- int isNum()
- {
- char *str=strBuff;
- while(*str)
- {
- if(*str<'0' || *str>'9') return 0;
- str++;
- }return 1;
- }
- int chgNum()
- {
- char *str=strBuff;
- int sum=0;
- while(*str)
- {
- sum=10*sum+(int)(*str-'0');
- str++;
- }
- push(sum);
- return 0;
- }
- int showOK()
- {
- printf("OK\n");
- return 0;
- }
- int say()
- {
- gets(strBuff);
- printf("cforth: [%s]\n",strBuff);
- return 0;
- }
code_words.h
- //push 输入数字到DS
- void push(int a){ dsp++; *dsp=a; }
- //>r
- void tor(){ rsp++; *rsp=*dsp; dsp--; }
- //r>
- void rto(){ dsp++; *dsp=*rsp; rsp--; }
- //丢弃DS栈顶
- void drop(){ dsp--; }
- //复制DS栈顶
- void dup(){ dsp++; *dsp=*(dsp-1); }
- //交换DS栈顶前两个数
- void swap(){ int tmp; tmp=*dsp,*dsp=*(dsp-1),*(dsp-1)=tmp; }
- //+
- void add(){ *(dsp-1)=*(dsp-1) + *dsp; dsp--; }
- //-
- void sub(){ *(dsp-1)=*(dsp-1) - *dsp; dsp--;}
- //*
- void mul(){ *(dsp-1)=*(dsp-1) * *dsp; dsp--;}
- // /d
- void ddiv(){ *(dsp-1)=*(dsp-1) / *dsp; dsp--;}
- // %
- void mod(){ *(dsp-1)=*(dsp-1) % *dsp; dsp--;}
- //显示DS
- void showDS()
- {
- int *tmp=&ds[1];
- while(tmp<=dsp)
- {
- printf("%d ",*tmp);
- tmp++;
- }
- printf(");
- }
- //显示RS
- void showRS()
- {
- int *tmp=&rs[1];
- while(tmp<=rsp)
- {
- printf("%d ",*tmp);
- tmp++;
- }
- printf(");
- }
- //显示DS栈顶,堆栈无影响
- void showTOP()
- {
- printf("%d\n",*dsp);
- }
- //若DS栈顶为0,则丢弃栈顶
- void ifFlag0()
- {
- flag = *dsp;
- if(flag == 0) drop();
- }
colon_words.h
- //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(); }
阅读(760) | 评论(0) | 转发(0) |