
-----------------------------------
btiffin
Fri Mar 29, 2013 2:05 am

cbrain, cbrain run
-----------------------------------
Bumped into pbrain.c on esolangs.org.  Nice.  Extended it with intent of CALLing from COBOL.  Adds bitwise integer ops like xor, which is kinda klunky in COBOL.

Esoteric programming is a lot of fun.

pbrain adds labeled procedures to brainf**k (a name I'm not a fan of), so I'll just reference pbrain.

I've cheated on pbrain and allow numbers, binary ops, a few 'stack' ops like o for over.

[code]
/* pbrain interpreter in old-style C 
   This is an interpreter for Paul M. Parks's pbrain programming language,
       a variant of Urban Mueller's programming language.
   Do anything you want with this program.
   I welcome bug reports and feature requests.
   Daniel B Cristofani (http://www.hevanet.com/cristofd/)

   20130327, extended and put into service with OpenCOBOL as
   cbrain

   btiffin (https://sourceforge.net/users/btiffin)
   Tectonics: cobc -x callcbrain.cob cbrain.c
              01 brain-cells.
                 05 brain-cell usage binary-c-long occurs 65546 times.
              CALL "cbrain" USING brain-cells
                  BY CONTENT "file pathname" & x"00"
               or
                  BY CONTENT "{cbrain source text} 99 . 10 ." & x"00"
                  RETURNING cell
              END-CALL
              DISPLAY brain-cell(cell) END-DISPLAY
*/

#include 
#include 
#include 
#include 
#include 
#define SIZE 65536
#define CA(x) case x: fprintf(stderr, "Error: " 

//unsigned short a[SIZE];
long a[SIZE];
int s[SIZE], sp, ptable[USHRT_MAX+1], t[SIZE], p, q, length, c, tmp, scale, tracer, tracing;
char code[SIZE], *f, bin[sizeof(long)*8+1];
FILE *input;

const char *long_to_binary(unsigned long x) {
    unsigned long z;
    char *d = bin;
    for (z = (unsigned long)1>= 1)
        *d++ = ((x & z) == z) ? '1' : '0';
    *d++ = '\0';
    return bin;
}

void e(int i){
    switch(i){
        CA(2) "call to undefined procedure (%hu) with %d at %d of %s", a[p], p, q, f); break;
        CA(3) "pointer too far %s at %d of %s", p>0?"right":"left", q, f); break;
        CA(4) "unmatched '[' at byte %d of %s", s[sp], f); break;
        CA(5) "unmatched ']' at byte %d of %s", q, f); break;
        CA(6) "unmatched '(' at byte %d of %s", s[sp], f); break;
        CA(7) "unmatched ')' at byte %d of %s", q, f); break;
        CA(8) "can't open %s", f); break;
        CA(9) "unmatched '{' at byte %d of %s", s[sp], f); break;
        CA(10) "unmatched '}' at byte %d of %s", q, f); break;
    }
    printf(".\n");
    exit(i);
}

int cbrain(long *a, char *args){
    if (strncmp("file ", args, 5) == 0) {
        args += 5; 
        if(!(input = fopen(f=args, "r"))) e(8);
        length = fread(code, 1, SIZE, input);
        fclose(input);
    } else {
        f = "cbrain";
        length=strlen(args);
        strncpy(code, args, SIZE);
    }
    scale=0;
    for(q=0;q0)?10:0; a[p]+=3; break;
            case '4': a[p]*=(scale++>0)?10:0; a[p]+=4; break;
            case '5': a[p]*=(scale++>0)?10:0; a[p]+=5; break;
            case '6': a[p]*=(scale++>0)?10:0; a[p]+=6; break;
            case '7': a[p]*=(scale++>0)?10:0; a[p]+=7; break;
            case '8': a[p]*=(scale++>0)?10:0; a[p]+=8; break;
            case '9': a[p]*=(scale++>0)?10:0; a[p]+=9; break;
            case '': if(++p>=SIZE) e(3); scale=0; break;
            case ',': if((c=getchar())!=EOF) a[p]=c=='\n'?10:c; scale=0; break;
            case '=': scanf("%d", &a[p]); scale=0; break;
            case '.': putchar(a[p]==10?'\n':a[p]); scale=0; break;
            case '#': printf("%+09d ", a[p]); scale=0; break;
            case 'b': printf("%64s ", long_to_binary(a[p])); scale=0; break;
            case '[': if(!a[p]) q=t[q]; scale=0; break;
            case ']': if(a[p]) q=t[q]; scale=0; break;
            case '(': ptable[a[p]]=q; q=t[q]; scale=0; break;
            case ')': q=s[--sp]; scale=0; break;
            case ':': s[sp++]=q; if((q=ptable[a[p]])=SIZE) e(3); a[p]=a[p-1]; scale=0; break;
            case 'o': if(p=SIZE) e(3); a[p]=a[p-2]; scale=0; break;
            case 'x': tmp=a[p];a[p]=a[p-1];a[p-1]=tmp; scale=0; break;
            case 't': tracer=(tracer?0:1); scale=0; break;
            case 'g': return (p+1);
            case 'q': exit(0);
            default: scale=0;
        }
        if (tracing && !isspace(code[q])) {
            fprintf(stderr, " after %05d is %9lu]\n", p, a[p]);
            tracing=0;
        }
    }
    /* COBOL is an ordinal language, first is 1 */
    return(p+1);
}
[/code]

A sample of 
[code]
[-][old school]
+++++ +++++             
[                       
    > +++++ ++          
    > +++++ +++++       
    > +++               
    > +                 
     ++ .                  
 + .                   
----- ----- - .         
+++++ ++++ .            
< ----- ----- -- .      
+++++ +++++ ++ .        
----- ----- --- .       
+++++ +++++ +++ .       
--- .                   
>> + .                  
> .                     

0[eso style]
39
> 83
> 117
> 112
> 32
> 101
> 97
> 114
> 116
> 104
> 0
< < < < < < < < < <
[. >]
> 01 * . 

{cbrain style}0(0: