-C++

From Esolang
Jump to navigation Jump to search

-C++ (say minus C plus plus) is an esoteric subset of C++ that forces programmers to abuse with overload operators and macros. It is invented by User:None1.

Differences from C++

In -C++, there must be only one ordinary function: the main function. You have to define other functions using overload operators!

Like:

int func(){return 3;}

Is not allowed in -C++, because you have to use overload operators:

struct func{int operator!(){return 3;}};

Then call like this:

func t;
!t; // Returns 3

But this is not it, because there are other rules:

  • There are no namespaces.
  • cin and cout are built-in, you don't need to get them from the std namespace because namespaces are not allowed. (stuff like cin.get are not allowed because it uses normal functions)
  • #include is not allowed, that means things from standard library other than cin and cout is unavailable.
  • int (except in declaration of main function), unsigned int, long, unsigned long, short, unsigned short and float and their equivalents (signed (except in declaration of main function), unsigned, signed long, signed short) are unavailable.
  • code that is not preprocessor must be in one line and must only consist of _ and whitespaces.
  • If-else is not allowed. You have to use other methods to do conditionals (e.g.: conditional operator or switch-case).

So, the program has to look like this:

#define _ struct func{long long operator!(){return 3;}};int main(){func t;return !t;}
_

Examples

Hello, World!

#define _ int main(){cout<<"Hello, World!";return 0;}
_

Truth Machine

#define int long long
#define _ struct print1{int operator!(){while(1)cout<<1;return 0;}};signed main(){int x;print1 p;cin>>x;return ((!x)?(cout<<x,0):!p);}
_

Turing completeness proof

Brainfuck interpreter:

#define int long long
#define _ \
struct node{\
	node *nx;\
	node *pv;\
	unsigned char data;\
	void operator!(){\
		nx=nullptr;\
		pv=nullptr;\
		data=0;\
	}\
	node *operator++(){\
		if(nx==nullptr){\
			nx=new node;\
			!(*nx);\
			nx->pv=this;\
			return nx;\
		}\
		return nx;\
	}\
	node *operator--(){\
		if(pv==nullptr){\
			return this;\
		}\
		return pv;\
	}\
	void operator+=(unsigned char x){\
		++(*this);\
		nx->data=x;\
	}\
};\
struct bf{\
	node *t;\
	char *prog;\
	int ip;\
	void operator!(){\
		t=new node;\
		!(*t);\
		prog=">>++++++++++[[->+<<++++>]<++[<]>[.>]>.]"; /*Brainfuck program*/\
		ip=0;\
	}\
	void operator++(){\
		t=++(*t);\
	}\
	void operator--(){\
		t=--(*t);\
	}\
	void operator~(){\
		++(t->data);\
	}\
	void operator()(){\
		--(t->data);\
	}\
	void operator+=(int y){\
		cout<<(t->data);\
	}\
	void operator-=(int y){\
		cin>>(t->data);\
	}\
	void operator*=(int y){\
		switch(t->data){\
			case 0:break;\
			default:return;\
		}\
		int q=1,r=ip+1;\
		while(q){\
			switch(prog[r]){\
				case '[':q++;break;\
				case ']':q--;break;\
			}\
			r++;\
		}\
		ip=r;\
	}\
	void operator/=(int y){\
		switch(t->data){\
			case 0:return;\
		}\
		int q=1,r=ip-1;\
		while(q){\
			switch(prog[r]){\
				case '[':q--;break;\
				case ']':q++;break;\
			}\
			r--;\
		}\
		ip=r;\
	}\
};\
signed main(){\
	bf q;\
	!q;\
	while(q.prog[q.ip]){\
		switch(q.prog[q.ip]){\
			case '>':++q;break;\
			case '<':--q;break;\
			case ',':q-=0;break;\
			case '.':q+=0;break;\
			case '[':q*=1;break;\
			case ']':q/=1;break;\
			case '+':~q;break;\
			case '-':q();break;\
		}\
		q.ip++;\
	}\
	return 0;\
}
_

Implmentations

No implementations yet, but since this is a subset of C++, you can try it out in C++ by adding

#include<iostream>
using namespace std;

at the start of program.