golfuck

From Esolang
Jump to navigation Jump to search

golfuck is an extension of brainfuck. It is created in order to write short programs. In addition to brainfuck, golfuck adds a stack.

Commands added(still extending)

{}:repeat while the top of the stack is not "0".
$:starts an infinite loop
%:ends an infinite loop
: input a string and push it into the stack.
; output the top of the stack.
' output the whole line. This command is specifically for printing hello, world. It will only execute after every other code is executed.
! pop the stack
* duplicate the top of the stack
| set the current pointing byte as the top of the stack
^ pop the top value of the stack to the current pointing byte
@ push the current pointing value into the stack
` swap 2 top elements
? concentrates the top 2 elements of the stack if the two elements are strings; add them if they are integers.
# execute the top element of the stack as the program, then pop the stack
(x) push the string x into the stack
r replace all the *s in a string, and all the **s as *.
"" replace the output using regular expressions. E.g. "a,"+1",b" finds "a", sets the replacing at one character behind, and replaces the characters equal length as the first search as "b".
9 prints the 99 bottles of beer lyrics.

Example programs

Hello, world!

'Hello, world!

Cat program

$:;%

Quine(Directly translated from GolfScript)

(*#)*#

Truth-machine

:{;};

99 bottles of beer program

(* bottles of beer on the wall,
* bottles of beer.
Take one down and pass it around,)
(* bottles of beer on the wall.)(99)=|![`*r;!-`*r;!]"1,"+9",""0,"+0",no more"

Another cheating way:

9

Computational class

golfuck is Turing-complete, as it can be shown to be computationally equivalent to Underload: the golfuck commands `*!?()# correspond to the TC subset of Underload commands ~:!*()^.

Partial implementation in C++

Temporarily written here because it is short. This is a part of it with the brainfuck commands and the commands (, ), ', *, ":" and ";" .

#include<stdio.h>
#include<string>
#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;
int c,d[999999],a[999999],i,p,l,it;//c:char; d:code; a:array; i:iterator; p:pointer; l:level
//it: int temp
stack<string> s;
//s:stack
string t,t2;
int main(int j,char *k[]){//j:argc; k:argv
	ios::sync_with_stdio(false);//make it faster
	FILE *f=fopen(k[1],"r");
	for(;(c=fgetc(f))!=EOF;i++)d[i]=c;
	for(i=0;d[i]>0;i++){
		if(d[i]==43)a[p]++;//+
		if(d[i]==45)a[p]--;//-
		if(d[i]==62)p++;//>
		if(d[i]==60)p--;//<
		if(d[i]==46)putchar(a[p]);//.
		if(d[i]==44)a[p]=getchar();//,
		if(d[i]==91&&!a[p])//[
			for(l=0;i++;){
				if(d[i]==93&&!l)break;
				if(d[i]==91)l++;
				if(d[i]==93)l--;
			}//]
		if(d[i]==93&&a[p])
			for(l=0;i--;){
				if(d[i]==91&&!l)break;
				if(d[i]==93)l++;
				if(d[i]==91)l--;
			}
		if(d[i]=='%')
			while(d[i]!='$')i--;
		if(d[i]==':')
			cin>>t;
			s.push(t);
		if(d[i]==';')
			cout<<s.top();
		if(d[i]=='\'')
		{
			i++;
			while(d[i]!='\n')
			{
				printf("%c",d[i]);
				i++;
				if(d[i]=='\0')break;
			}
		}
		if(d[i]=='*')
			s.push(s.top());
		if(d[i]=='(')
		{
			i++;
			int le=0,n=0;
			while(d[i]!=')'||le!=0)
			{
				if(d[i]=='(')
				{
					t+="(";
					le++;
				}
				else if(d[i]==')')
				{
					t+=")";
					le--;
				}
				else
					t+=d[i];
				i++;
			}
		}
	}
}