/brackets

An esolang with brackets, a lot of brackets

Primary LanguageJavaScriptMIT LicenseMIT

Brackets

Brackets is an esoteric stack-based programming language. A little bit confusing, because consists only of 4 unique characters. The language uses Instruction Modification Parameter (IMP) and was heavily inspired by whitespace.

 

Syntax

IMP Meaning
() Stack Manipulation
{} Arithmetic
[] Flow Control
<> Input / Output
IMP Command Parameter Meaning
() () Number Push the number onto the stack
() {} - Duplicate the top item on the stack
() [] - Swap the top two items on the stack
() <> - Discard the top item on the stack
{} () - Pop a and b and push a + b
{} {} - Pop a and b and push a * b
{} [] - Pop a and b and push b / a rounded down
{} <> - Pop a and b and push b % a
[] () - Pop a, if (top == a) { ... }
[] {} - Pop a, if (top != a) { ... }
[] [] - Pop a, while (top != a) { ... }
[] <> - Exit the program
<> () - Pop c and output c as a character
<> {} - Pop n and output n as a number
<> [] - Read c and push it onto the stack as a character
<> <> - Read n and push it onto the stack as a number

Numbers

Numbers in Braces are in binary, where () represents 0, and {} represents 1. The first bit tells us if the number is positive -(), or negative -{}. Numbers have to end with an opening arrow bracket <.

Here is an example:

(){}(){}()< => 0-1010 => 10
{}{}(){}()< => 1-1100 => -12

Any characters other than brackets, are simply ignored.

 

Examples

Alphabet

The following program prints the whole english alphabet.

()() (){}{}()()()(){}<   push 97 'a'
()() (){}{}{}{}(){}{}<   push 123 'z' + 1
[][]{                    while top != 123:
  (){}                      dup
  <>()                      printn
  ()() (){}<                push 1
  {}()                      add
}

Loops

Simple while loop:

()() (){}{}()(){}()()<   push 100
()() ()()<               push 0
[]<>{                    while top != 0 do top /= 2:
  (){}                      dup
  <>{}                      printn
  ()() (){}(){}()<          push 10 '\n'
  <>()                      printc
  ()() (){}()<              push 2
  {}[]                      div
}

Statements

Simple if statements:

()() (){}<     push 1
()() ()()<     push 0
[](){          if 1 == 0:
  ()() (){}<      push 1
  <>{}            printn
}

()() (){}<     push 1
()() ()()<     push 0
[]{}{          if 1 != 0:
  ()() {}{}<      push -1
  <>{}            printn
}

Simple if-else statement:

()() (){}{}<   push 3
()() (){}<     push 1
[](){          if 3 == 1:
  ()() (){}<      push 1
  <>{}            printn
  ()() (){}<      push 1
}
()() (){}<     push 1
[]{}{          if 3 != 1:
  ()() {}{}<      push -1
  <>{}            printn
}

You can find more examples on the online Brackets interpreter below.

 

Interpreter

Try out official Brackets interpreter online.