Vaporeon tries implementing Brainf_ck 

801 characters in total, 622 characters if not counting whitespace and newline. Reads program from standard input, and user input either from the terminal (/dev/tty) or from the file argv[1].

I feel like I'm doing something wrong and it could be made more compact? :psyduck:

Very much not portable; assumptions:

  • C compiler is modern enough to compile C99, not just ANSI C (otherwise c[65535] = {0} won't work and neither will for(int i=0;i<n;i++))
  • C compiler can do assigned GOTO; I believe this is a GNU extension?
  • sizeof(short) == 2
  • UNIX system with read() system call and stdin being file descriptor 0 (should be most UNIX systems, I think)
  • O_RDONLY == 0 (otherwise I'd have to include the header, which would need a lot of letters...), no idea whether this is only on Linux or not
#define D(n,f) for(int i=0;i<n;i++) { f; }
#define E goto *d[p[pI++]];
#define R return
#define U unsigned
U char c[65535] = {0}, *p=0, *s="><+-.,[]";
U short cI=0, r[200];
int pI=0,rI=-1,t,L=0,fd;
l(c) { D(8,if(c==s[i]) R i+1) R 9; }
main(ac,av) char**av; {
void *d[] = {&&ex, &&shr, &&shl, &&inc, &&dec,
&&pr, &&rd, &&lb, &&le, &&nop};
fd=open(ac>1?av[1]:"/dev/tty", 0);
do{p=realloc(p,L+80);t=read(0,p+L,80);} while (t>0&&(L+=t));p[L]=0;
D(L,p[i]=l(p[i]))
nop: E
shr: cI++; E
shl: cI--; E
inc: c[cI]++; E
dec: c[cI]--; E
pr: putchar(c[cI]); E
rd: read(fd,c+cI,1) || (c[cI]=-1); E
lb: if(!c[cI]) for(t=0;;pI++) { p[pI]==7 && t++;
if(p[pI]==8) if(!t--) {pI++; E } }
r[++rI] = pI; E
le: if (c[cI]) pI = r[rI]; else rI--; E
ex: R 0;
}

I think going by character count actually isn't great for this... Of course I could make the code slightly shorter by changing all variable names and labels to be 1 letter instead of 2-3 letters. But then I myself couldn't keep track of my code anymore because the variable names wouldn't make sense anymore, and also it doesn't really matter for the program logic whether a variable is 1 character long or 3 characters long...

So how do you best count the length of a program?

re: wrong 

@wallhackio Regretfully, I have already forgotten what a cyclomatic complexity is, even though you told me... Was it the number of possible paths through a program?

re: wrong 

@vaporeon_ kind of? the program starts with a complexity of 1, and every branch in an if-then-else or switch statement adds 1 and every for loop/while loop adds 1

re: wrong 

@wallhackio Oh... And how would cyclomatic complexity handle assigned GOTOs like in the above program?

re: wrong 

@wallhackio After some thinking, probably same as a switch statement, right?

But also: I can't be bothered to calculate the cyclomatic complexity even for such a short program (especially: if the condition in a while-loop is something&&something-else, does that mean cyclomatic complexity +2 instead of +1?)
Are there scripts that do it?

And yes, I am aware that you CW'd this as "wrong", I am just curious what the number would be

re: wrong 

@vaporeon_ a GOTO by itself adds no additional "cyclomatic complexity"

re: wrong 

@wallhackio Sure, an unconditional GOTO doesn't, but I do assigned GOTO. I have a list of jump addresses:

  void *d[] = {&&ex, &&shr, &&shl, &&inc, &&dec,
&&pr, &&rd, &&lb, &&le, &&nop};

And then I jump to one of them depending on the number p[pI]:

goto *d[p[pI++]];

re: wrong 

@vaporeon_ I did not realize this was a thing you could do in C haha.

Anyway, I don't know how this would be treated by a complexity metric. In many ways this essentially emulates a switch statement acting on an integer so I would guess it would be treated just like a switch statement but I really don't know

Follow

re: wrong 

@wallhackio @vaporeon_ it’s almost certainly unstandardized behavior

re: wrong 

@wallhackio @vaporeon_ GNU C lets you do interesting things. you can define inner functions with it

re: wrong 

@aescling @wallhackio Exactly, and I even pointed it out in my original post, with a link to this page from the GNU project where they explain this extension: gcc.gnu.org/onlinedocs/gcc/Lab

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.