Skip to main content
5 of 16
factored math ops, shorter 'dup'
luser droog
  • 4.9k
  • 2
  • 36
  • 61

C 924 882 chars

#include<math.h>
#include<signal.h>
#define B break;case
#define W write
#define O(_)w=o();u(o()_ w);B
int*s,*t,*i,w,a,c,d;void
S(int x){signal(SIGSEGV,S);sbrk(8*8*8);}u(x){*t++=x;}o(){return t>s?*--t:0;}p(x){char
b;if(x<0){W(1,"-",1);x=-x;}c=floor(log10(x));for(c=c>=0?c:0;c>=0;c--){b=48+(x/(int)\
pow(10.0,c))%10;W(1,&b,1);}}main(){char
b;signal(SIGSEGV,S);t=s=sbrk(8*8*8);while(read(0,&b,1)){switch(b){case
'0':w=o();a=o();for(c=ceil(log10(w));c>0;c--)a*=10;u(a+w);B
'1':u(1);O(+)'2':u(1);O(-)'3':O(*)'4':O(/)'5':O(+)'6':O(-)
'7':c=o();a=1;for(w=o();c>0;c--)a*=w;u(a);B
'8':O(%)'9':w=*s;memmove(s,s+1,4*(t-s-1));t[-1]=w;B
'A':w=t[-1];memmove(s+1,s,4*(t-s-1));*s=w;B
'B':u(t[-1]);B
'C':w=o();a=o();u(a);u(w);u(a);u(w);B
'D':w=o();a=o();u(w);u(a);B
'E':w=o();a=o();c=o();d=o();u(a);u(w);u(d);u(c);B
'F':o();break;}}W(1,"\n[",2);i=t;do{p(*--i);}while(i>s&&W(1,",",1));W(1,"]\n",2);}

Ungolfed:

#include<math.h>
#include<signal.h>
void S(int x){signal(SIGSEGV,S);sbrk(8*8*8);}
int*s,*t,*i,w,a,c,d;
u(x){*t++=x;}
o(){return t>s?*--t:0;}
p(x){
    char b[1];
    if(x<0){ write(1,"-",1); x=-x; }
    c=floor(log10(x));
    for(c=c>=0?c:0;c>=0;c--){
        *b=48+(x/(int)pow(10.0,c))%10;
        write(1,b,1);
    }
}
main(){
    char b[1];
    signal(SIGSEGV,S);
    t=s=sbrk(8*8*8);
    while(read(0,b,1)){
        write(1,b,1);
        switch(*b){
            case '0': w=o(); a=o(); for(c=ceil(log10(w));c>0;c--) a*=10; u(a+w); break;
            case '1': u(o()+1); break;
            case '2': u(o()-1); break;
            case '3': w=o(); u(o()*w); break;
            case '4': w=o(); u(o()/w); break;
            case '5': u(o()+o()); break;
            case '6': w=o(); u(o()-w); break;
            case '7': c=o();a=1; for(w=o();c>0;c--) a*=w; u(a); break;
            case '8': w=o(); u(o()%w); break;
            case '9': w=*s; memmove(s,s+1,4*(t-s-1)); t[-1]=w; break;
            case 'A': w=t[-1]; memmove(s+1,s,4*(t-s-1)); *s=w; break;
            case 'B': w=o(); u(w); u(w); break;
            case 'C': w=o(); a=o(); u(a); u(w); u(a); u(w); break;
            case 'D': w=o(); a=o(); u(w); u(a); break;
            case 'E': w=o(); a=o(); c=o(); d=o(); u(a); u(w); u(d); u(c); break;
            case 'F': o(); break;
        }
    }
    write(1,"\n[",2);
    i=t;
    do {
        p(*--i);
    } while(i>s && write(1,",",1));
    write(1,"]\n",2);
}
luser droog
  • 4.9k
  • 2
  • 36
  • 61