Csub

From Esolang
Jump to navigation Jump to search

Csub is a C-like language. It looks like C, yet it is way simpler that you have to define most operators by yourself. It is designed by User:A.

Syntax

Define a variable:

int a=10;

Define an if statement:

if(condition){...}

Else statement:

if{...}

condition:an expression containing numbers, variables, +, -, <, and >.

Define a while loop

while(condition){...}

Input(equivalent to the C++ std::cin>>)

read(...);

Output(equivalent to the C++ std::cout<<)

write(...);

Define a function

int function(...){...}

Specification

There are only 5 keywords: int, if, else, return and while.

There are only 5 operators: +, -, <, >, and =.

There are only 2 built-ins:write() and read().

Implementation

Official compiler to C++

Turing-completeness

This is Turing-complete, because 3-celled brainfuck can compile to it.

Here, "a" is any variable.

+: a++;
-: a--;
> and <: these don't directly produce code; instead, they switch the currently operating variable.
[: while a{
]: }

Example programs

Hello, world!Program

int main()
{
    write("Hello, world!\n");
}

Cat program

int main()
{
    int a[999]="";
    while(1)
    {
        read(a);
        write(a);
    }
}

Fibonacci sequence

int main()
{
    int a=0,b=1;
    write(a);write(" ");write(b);write(" ");
    while(1){
        a=a+b;
        write(a);write(" ");
        b=a+b;
        write(b);write(" ");
    }
}

99 bottles of beer

int main()
{       
        int b=99;
        while(ge(b,0)) {
                if(eq(b,0)) {
                        write("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n");
                }else{
                if(eq(b,1)){
                        write("1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall\n");
                }else{
                        write(b);
                        write("bottles of beer on the wall, ");
                        write(b);
                        write(" bottles of beer.\n");
                        write("Take one down and pass it around, ");
                        write(b-1);
                        if(gt(b-1,1)){
                        write("bottles")
                        }else{
                        write("bottle");
                        }
                        write("of beer on the wall.\n");
                }
                }
                b=b-1;
        }
}

Endless Square Numbers Program

int main()
{
int a=0;
while(a~<0){
b=a*a
write(b);write("\n");
a=a+1
}
}

Digital root calculator

int main()
{
int a;
read(a);
while(a>9){
a=a-9;
}
write(a);
}

Implementing other operators

Csub only has 5 operators(+,-,<,>,and =).

This turns out to be not quite useful for actual programming. These are functions defined to represent various operators.

NOT function

int not(int a){
	if(a){
		return 0;
	}else{
		return 1;
	}
}

AND function

int and(int a,int b){
	if(a){
		if(b){
			return 1;
		}
	}
	return 0;
}

OR function

int or(int a,int b){
	if(a){
		return 1;
	}
	if(b){
		return 1;
	}
	return 0;
}

LE(less than or equal) function

int le(int a,int b){
	if(a<b+1){
		return 1;
	}if{
		return 0;
	}
}

GE(greater than or equal) function

int ge(int a,int b){
	if(a>b-1){
		return 1;
	}if{
		return 0;
	}
}

NE(not equal) function

int ne(int a,int b){
	if(or(a<b,a>b)){
		return 1;
	}
	return 0;
}

EQ(equal) function

int eq(int a,int b){
	if(and(ge(a,b),le(a,b))){
		return 1;
	}
	return 0;
}

MOD(remainder of division) function

int mod(int a,int b){
	return a-mul(b,div(a,b));
}

MUL(multiplication) function

int mul(int a,int b){
	int cnt=a;
	int res=0;
	while(cnt>0){
		res=res+b;
		cnt=cnt-1;
	}
	return res;
}

DIV(division) function

int div(int a,int b){
	int cnt=a;
	int res=0;
	while(cnt>b){
		res=res+1;
		cnt=cnt-b;
	}
	return cnt;
}

POW(to the power of) function

int pow(int a,int b){
	int cnt=b;
	int res=1;
	while(cnt>0){
		res=mul(res,a);
		cnt=cnt-1;
	}
	return res;
}

LSH(left shift) function

int lsh(int a,int b){
	return mul(a,pow(2,b));
}

RSH(right shift) function

int rsh(int a,int b){
	return div(a,pow(2,b));
}

Implementing breaks and continues

break:You have to put your code inside a function. Return 0 does the job.

This code breaks if i is equal to 5.

def whileLoop(){
    var i=9;
    while(i>0)
    {
        write(i);
        i=i-1;
        if(eq(i,5))
        {
            return 0;
        }
    }
    return 0;
}

continue

continue:Also, you have to put your code inside a function. Calling the function does the job.

This code skips the printing i when i is equal to 4.

def continueDemo()
{
    var i=10;
    while(i>0)
    {
        i=i-1;
        if(eq(i,4))
        {
            continueDemo();
        }
        write(i);
    }
}