Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

The golfed version actually changed substantially compared to the ungolfed version here, under the (welcome) pressure of Oberon's fine answerOberon's fine answer.

The golfed version actually changed substantially compared to the ungolfed version here, under the (welcome) pressure of Oberon's fine answer.

The golfed version actually changed substantially compared to the ungolfed version here, under the (welcome) pressure of Oberon's fine answer.

add some explanation/commentary, nod to other C answer
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61

Ungolfed:The golfed version actually changed substantially compared to the ungolfed version here, under the (welcome) pressure of Oberon's fine answer.

I found that replacing the switch statement in favor of an if...else chain enabled me to factor-out all of the digits from my cases. Instead it initializes the w variable to 47 so one increment raises it to 48 (== ascii '0') then each case increments w until we need to skip to 'A' at which point we make use of the mostly empty first macro argument which adds an extra 7 to get up to 'A'. The ungolfed version does show my favorite sbrk/SIGSEGV trick to get "free" memory with no further allocations.

Ungolfed:

The golfed version actually changed substantially compared to the ungolfed version here, under the (welcome) pressure of Oberon's fine answer.

I found that replacing the switch statement in favor of an if...else chain enabled me to factor-out all of the digits from my cases. Instead it initializes the w variable to 47 so one increment raises it to 48 (== ascii '0') then each case increments w until we need to skip to 'A' at which point we make use of the mostly empty first macro argument which adds an extra 7 to get up to 'A'. The ungolfed version does show my favorite sbrk/SIGSEGV trick to get "free" memory with no further allocations.

add W macro to ungolfed code (used in p())
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
#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;    //stack top index working accumulator count data
u(x){*t++=x;}           //push()
o(){return t>s?*--t:0;} //pop()
#define W(x)write(1,&x,1);  //output a byte
p(x){                   //print()
    if(x<0){    //negative?
        W(*"-") //output '-'
        x=-x;   //negate
    }
    if(x>9)     //more than one digit?
        p(x/10); //recurse after integer-divide
    b=48+x%10;   //isolate and convert single digit to ascii
    W(b)         //output ascii digit
}
main(){
    char b[1];
    signal(SIGSEGV,S);  //auto-allocate memory for stack
    t=s=sbrk(8*8*8);  //get start of memory and allocate
    while(read(0,b,1)){
        write(1,b,1); //for debugging: echo the command being executed
        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);   //dump the stack
    i=t;
    do {
        p(*--i);
    } while(i>s && write(1,",",1));
    write(1,"]\n",2);
}
#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;    //stack top index working accumulator count data
u(x){*t++=x;}           //push()
o(){return t>s?*--t:0;} //pop()
p(x){                   //print()
    if(x<0){    //negative?
        W(*"-") //output '-'
        x=-x;   //negate
    }
    if(x>9)     //more than one digit?
        p(x/10); //recurse after integer-divide
    b=48+x%10;   //isolate and convert single digit to ascii
    W(b)         //output ascii digit
}
main(){
    char b[1];
    signal(SIGSEGV,S);  //auto-allocate memory for stack
    t=s=sbrk(8*8*8);  //get start of memory and allocate
    while(read(0,b,1)){
        write(1,b,1); //for debugging: echo the command being executed
        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);   //dump the stack
    i=t;
    do {
        p(*--i);
    } while(i>s && write(1,",",1));
    write(1,"]\n",2);
}
#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;    //stack top index working accumulator count data
u(x){*t++=x;}           //push()
o(){return t>s?*--t:0;} //pop()
#define W(x)write(1,&x,1);  //output a byte
p(x){                   //print()
    if(x<0){    //negative?
        W(*"-") //output '-'
        x=-x;   //negate
    }
    if(x>9)     //more than one digit?
        p(x/10); //recurse after integer-divide
    b=48+x%10;   //isolate and convert single digit to ascii
    W(b)         //output ascii digit
}
main(){
    char b[1];
    signal(SIGSEGV,S);  //auto-allocate memory for stack
    t=s=sbrk(8*8*8);  //get start of memory and allocate
    while(read(0,b,1)){
        write(1,b,1); //for debugging: echo the command being executed
        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);   //dump the stack
    i=t;
    do {
        p(*--i);
    } while(i>s && write(1,",",1));
    write(1,"]\n",2);
}
a few more
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
replace switch with if .. else so match can be a run-encoded variable
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
exploited precedence of << vs -. removed echo of command, removed & from W().
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
parens around P, range check before `log10`
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
updated un-golfed printing function and comments
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
broke 600!
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
more golfing
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
credit, comments
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
factored math ops, shorter 'dup'
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
deleted 48 characters in body
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
macros and spaces
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
added 1192 characters in body
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading
Source Link
luser droog
  • 4.9k
  • 2
  • 36
  • 61
Loading