We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

B+

From Esolang
Jump to navigation Jump to search

B+ is a C and GLSL-based esolang made by User:Cordership.

Syntax

Basics

Declaring variables

make var_type var_name;

A variable can be an integer, a float, a double, or a string.

Assigning values

var_name = var_value;

It can be a string (%s), an integer (%d), a float (%f), or a double (%e).

STDIO

#include <stdio>

Output by the printf() function:

#include <stdio>
main() {
    printf("Whatever you want");
    exit 0;
}

Assign input to a variable using getinp (reads the full input):

#include <stdio>
main() {
    make str a;
    getinp("%s", a);
}

Assign first character of input to variable:

#include <stdio>
main() {
    make str a;
    getchr("%s", a);
}

Cat program:

#include <stdio>
main() {
    make str a;
    getinp("%s", a);
    printf("%s”, a);
    exit 0;
}

Operators

The order of operators is the same as C.

Precedence Operator Description Associativity
1 ++ -- Postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal
2 ++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Cast
* Indirection (dereference)
& Address-of
count Size-of in elements
sizeof Size-of
_Alignof Alignment requirement
3 * / // % Multiplication, division, floor division, and remainder Left-to-right
4 + - Addition and subtraction (Addition is concatenation if both sides are strings)
5 << >> Bitwise left shift and right shift
6 < <= For relational operators < and ≤ respectively
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-left
14 = Simple assignment
:= Walrus operator
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right

If-else

1-branch:

#include <stdio>
main() {
    if (condition) {
        //code;
}

2-branch:

#include <stdio>
main() {
    if (condition) {
        //code;
    } else {
        //code;
}

Multiple if-then-elses:

#include <stdio>
main() {
    if (cond1) {
        //code;
    // } else if (cond2) {...

Ternary operator:

#include <stdio>
main() {
    make int a, make int b;
    getinp("%d %d", a, b);
    make int max;
    varf("%d", max, (a < b) ? b : a);
    printf("The largest number is %d", max);
    exit 0;

Loops

For loop:

for (cond1; cond2; cond3;…) {
    //code
}

While loop:

while (cond) {
    //code

Special cmds

break // Break out of current loop.
pass // Placeholder for future code. It "passes" by an element.
continue // Immediately halt execution of the current element.

in/not in

There is a special condition:

element in container

Arrays

#include <array>

In B+, arrays are the same as C. They must be the same data type. To assign the value of an array, the same format specifier for its elements are used. For example,

make array n;
varf("%d", n, {0, 1, 2, 5})

There are matrices too.

make matrix n;
varf("%d", n, {{0, 1, 2}, {1, 2, 3}}

The sizeof function returns the size of an array using bytes, while the count function returns the number of elements in an array. In C, arrays are of a predetermined length, while in B+, you can append and remove array elements.

#include <stdio>
#include <array>
main() {
    make array n;
    n = {0.0, 1.2, 2.5, 3.2});
    n.append(4.5);
    printf("%f", n); //Prints {0.0, 1.2, 2.5, 3.2, 4.5}.
    exit 0;
}

Commands:

array.append(item) // Appends an item to an array.
array.removeindex(n) // Removes item at index n.
array.replace(item1, item2, n) // Replaces the first n occurrences of an item with another item.
array.mulappend(item, n) // Appends an item to an array n times.
array.pop() // Removes the last item of an array.
array[n] // Gets the nth element of an array.

Functions

In B+, functions are defined almost the same way as C.

#include <stdio>
float f(float n) {
    exit 0;
}

Here’s how it works:

type f(type x) {
    // code
    exit thing;
}

Strings

#include <string>

Commands:

string.replace(item1, item2, n) // Replaces the first n occurrences of an item with another item.
str.index(item) // Returns the index of an item’s first occurrence in a string.
str.firstdigit() // First digit in a string.
str.isdigit() // If a string has digits only…
str.firstupper() // First uppercase letter in a string.
str.isupper() // If a string has uppercase letters only…
str.firstlower() // First lowercase letter in a string.
str.islower() // If a string has lowercase letters only…
str.firstspace() // First whitespace character in a string.
str.isspace // If a string has whitespace only…
str[n] // Gets the nth character of a string.
str[a:b] // String slicing.
count(str) // Length of a string. (doesn’t need <string>)

Programs

Hello, World!

#include <stdio>
main() {
    printf("Hello, World!”);
    exit 0;
}

Cat program

#include <stdio>
main() {
    make str a;
    getinp("%s", a);
    printf("%s", a);
    exit 0;
}

CT interpreter

#include <stdio>
#include <string>
main() {
    str ct(str d, str cmd) {
        if (cmd == ";") {
            exit d.replace(d.firstdigit(), " ", 1);
        } else if (cmd.isdigit() && d.firstdigit() == "1" {
            exit d + cmd;
        } else {
            exit d;
        }
    make str p;
    make str d;
    getinp("Enter the program: %s\nEnter the data-string: %s\n", p, d);
    make int t;
    t = 0;
    while (! d.isspace()) {
        printf("%s", d := ct(d, p[t % count(p)]));
        t += 1;
    exit 0;
}

4-function calculator

Format: {a}{op}{b}

#include <stdio>
main() {
    make int a;
    make int b;
    make str c;
    getinp("%d %s %d", a, c, b);
    int f(int a, int b) {
        if (c == "+") {
            exit a + b;
        } else if (c == "-") {
            exit a - b;
        } else if (c == "*") {
            exit a * b;
        } else if (c == "/") {
            exit a / b;
        } else
            pass;
    printf("%d", f(a, b));
    exit 0;
}