Advertisement
NovaYoshi

Portable FALSE

Nov 7th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.84 KB | None | 0 0
  1. /* Introducing: Portable False!!!!
  2.  
  3.    PortableFalse is different from AmigaFalse in:
  4.    - its Portable!!! :-)
  5.    - full stack checking
  6.    - strongly typed (no joke, really!)
  7.    - debug-modi
  8.    - real and meaningfull errormessages
  9.    - ` inline assembly not supported
  10.    - : and ; not supported for other than variable access.
  11.    - "beta" (flush) and "zero-slash" (pick) from the amiga charset
  12.      are now 'B' and 'O' resp.
  13.    - 'D' toggles stack-watch mode on and off. format:
  14.      [ bottom_of_stack , ... , top_of_stack | next_symbol ]
  15.    - "-q" on command line is quiet mode: no title printing.
  16.      (usefull for "filter"-type programs: 1> False -q filter.f <bla >burp)
  17.  
  18.    this is v1.2, changes from v1.1 include minor fixes for Ultrix and Irix.
  19.    (the stunning original formatting hast been left intact now). Also,
  20.    remaing commandline arguments are parsed as integers and put into
  21.    variables b-z (a = argc).
  22.  
  23.    this source has been writtin in good C style:
  24.    - no modularity whatsoever (only main())
  25.    - only global variables
  26.    - lots of ugly macros (replacing functions)
  27.    - great source formatting and indentation
  28.  
  29.    still compiles on a great number of ansi-C compilers on Amiga/SunOs/
  30.    Linux/Ultrix/Irix etc. if you have trouble porting it to your machine,
  31.    your compiler sucks.
  32.  
  33. */
  34.  
  35. #define MZ 10000
  36. #define MS 1000
  37. #include <stdio.h>
  38. #define NIL 0
  39. #define NUM 0
  40. #define FUNC 1
  41. #define VADR 2
  42. #define UNDEF 3
  43. #define l(x) ;break;case x:
  44. #define x(num) {ernum=num;goto er;}
  45. #define push(v,a) {if(S-2<sbeg)x(4)else{*--S=(v);*--S=(X)a;};}
  46. #define pop(v,a) {if(S+2>se)x(5)else\
  47.   {if((ex=(int)a)!=(ge=(int)*S++))x(6);v= *S++;};}
  48. #define pa(v,av) {if(S+2>se)x(5)else{av= *S++;v= *S++;};}
  49. #define ru(v) {if(rp-1<rbeg)x(13)else{*--rp=(v);};}
  50. #define ro(v) {if(rp+1>rend)x(14)else{v= *rp++;};}
  51. #define CA(c) {ru(p);p=c;}
  52. #define pu(x) push(x,NUM)
  53. #define po(x) pop(x,NUM)
  54. #define op(o) {po(b)po(d);pu((X)((int)d o (int)b));}
  55. #define cm(o,tt) {pa(b,((X)tt))pop(d,(X)tt);pu((X)(-(int)((int)d o (int)b)));}
  56. #define un(o) {po(b)pu((X)(o (int)b));}
  57. #define ne (p<ent)
  58. #define W while
  59. #define ec {W((*p!='}')&&ne)p++;p++;if(!ne)x(10);}
  60. #define P printf
  61. typedef char* X;
  62. typedef char** XP;
  63. X ST[MS],RST[MS],var[52],b,d,e,f,t1,t2,t3;
  64. XP sbeg=ST+12,se=ST+MS-12,S,ts,rbeg=RST+12,rend=RST+MS-12,rp,vp;
  65. int ernum=0,t,tt,db=0,ex,ge,qq,ac=1,ic=0,it;
  66. FILE* fh;
  67. char *fn=0,src[MZ+5],a,c=0,*s,*ent,*beg,*p=0,
  68. *erstr[]={"no args","could not open source file","source too large",
  69. "data stack overflow","data stack underflow","type conflict",
  70. "stack not empty at exit of program","unknown symbol",
  71. "portable inline assembly not available","unbalanced '{'",
  72. "unbalanced '\"'","unbalanced '['","return stack overflow",
  73. "return stack underflow"};
  74. char*types[]={"Integer","Functional","Variabele","Uninitialised"};
  75.  
  76. int main(int narg,char*args[]) {
  77.   S=se;rp=rend;t=1;for(vp=var;vp<(var+52);){*vp++=(X)UNDEF;};
  78.   while(ac<narg) {
  79.     if(args[ac][0]=='-'&&args[ac][1]=='q'){t=2;}
  80.     else if(!fn){fn=args[ac];}
  81.     else {it=0;sscanf(args[ac],"%d",&it);var[(++ic)*2+1]=(X)NUM;var[ic*2]=(X)it;};
  82.     ac++;
  83.   };
  84.   var[1]=(X)NUM;var[0]=(X)ic;
  85.   if(t==1)P("Portable False Interpreter/Debugger v0.1 (c) 1993 $#%%!\n");
  86.   if(!fn)x(1);if((fh=fopen(fn,"r"))==NIL)x(2);s=src; *s++='\n';
  87.   W(a=fgetc(fh),!feof(fh))if((src+MZ)<=s){fclose(fh);x(3)}else{*s++=a;};
  88.   *s++='\n';fclose(fh);ent=s-1;beg=src+1;p=beg;
  89.   W(ne) {
  90.     c= *p++;if(c>='0'&&c<='9'){int num;sscanf(p-1,"%d",&num);
  91.     W((*p>='0')&&(*p<='9'))p++;push((X)num,NUM);}
  92.     else if(c>='a'&&c<='z'){push((X)&var[(c-'a')*2],VADR);}
  93.     else switch(c) {
  94.       case' ':case '\n':case'\t':
  95.       l('+')op(+)
  96.       l('-')op(-)
  97.       l('*')op(*)
  98.       l('/')op(/)  
  99.       l('&')op(&)
  100.       l('|')op(|)
  101.       l('_')un(-)
  102.       l('~')un(~)
  103.       l('=')cm(==,tt)
  104.       l('>')cm(>,tt)
  105.       l('%')pa(b,e)
  106.       l('$')pa(b,e)push(b,e)push(b,e)
  107.       l('\\')pa(b,e)pa(d,f)push(b,e)push(d,f)
  108.       l('@')pa(b,t1)pa(d,t2)pa(e,t3)push(d,t2)push(b,t1)push(e,t3)
  109.       l('O')po(b)if(S+((t=(int)b*2)+2)>se)x(5)b= *(S+t);d= *(S+t+1);push(d,b)
  110.       l(':')pop(b,VADR)pa(d,e)*((XP)b)=d;*(((XP)b)+1)=e;
  111.       l(';')pop(b,VADR)push(*((XP)b),*(((XP)b)+1));
  112.       l('.')po(b)P("%d",(int)b);
  113.       l(',')po(b)P("%c",(char)b);
  114.       l('^')pu((X)fgetc(stdin));
  115.       l('B')fflush(stdout);fflush(stdin);
  116.       l('\"')W((*p!='\"')&&ne){fputc(*p,stdout);p++;};p++;if(!ne)x(11);
  117.       l('{')ec;
  118.       l('\'')pu((X)*p++);
  119.       l('`')x(9);
  120.       l('D')db=!db;
  121.       l('[')push((X)p,FUNC)t=1;W(t>0&&ne){a= *p++;if(a=='['){t++;}else
  122.         if(a==']'){t--;}else if(a=='{'){ec}else if(a=='\"'){W((*p!='\"')
  123.         &&ne)p++;p++;if(!ne)x(11);};};if(!ne)x(12);
  124.       l(']')ro(e)if((int)e==0){ro(p)po(b)if((int)b) { ro(d)ru(d)CA(d)
  125.         ru((X)1);} else { ro(d)ro(d); };}else if((int)e==1){ro(p)ro(b)
  126.         ro(d)ru(d)ru(b)CA(d)ru((X)0);}else{p=e;};
  127.       l('!')pop(b,FUNC)CA(b);
  128.       l('?')pop(b,FUNC)po(d)if((int)d){CA(b);};
  129.       l('#')pop(b,FUNC)pop(d,FUNC)ru(d)ru(b);CA(d)ru((X)0);
  130.       break;default:x(8);
  131.     };
  132.     if(db){c= *p;
  133.       if(c!=' '&&c!='\n'&&c!='\t'&&c!='{'&&c!='\"'){
  134.         ts=S+20;if(ts>se)ts=se;P("[");
  135.         W(ts>S){t=(int)*(ts-2);if(t==FUNC){P("<func>");}else if(t==VADR){
  136.           P("<var>");}else P("%d",(int)*(ts-1));ts-=2;if(ts>S)P(",");};
  137.         P("|'%c']\n",*p);
  138.       };
  139.     };
  140.   };
  141.   c=0;p=0;if(S!=se)x(7);
  142.   er:if(ernum) {
  143.     P("\nERROR: %s!\n",erstr[ernum-1]);
  144.     if(c)P("WORD:  '%c'\n",c);
  145.     if(ernum==6)P("INFO:  Expecting %s type, while reading %s type.\n",
  146.       types[ex],types[ge]);
  147.     if(p){
  148.       ent=p;beg=p;
  149.       W(*(beg-1)!='\n'){beg--;};
  150.       W(*ent!='\n'){ent++;};
  151.       t=ent-beg;*ent=0;
  152.       if(t>0) {
  153.         P("LINE:  %s\n",beg);
  154.         qq=p-beg+3;
  155.         P("AT:");for(t=0;t<qq;t++){putchar(' ');};P("^\n");
  156.       };
  157.     };
  158.   };
  159.   return 0;
  160. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement