# Broken Calculator

Paradigm(s) | Imperative |
---|---|

Designed by | User:PixelatedStarfish |

Appeared in | 2021 |

Memory system | Cell-based |

Computational class | Turing Incomplete |

Major implementations | Interpreter (Java) |

File extension(s) | `.bcal` |

**Broken Calculator** is a programming language created by User:PixelatedStarfish; it is designed such that a program has a random chance of crashing on a given line of code. The chance of crashing is always greater than 0, which ensures that every program in the language must halt. Therefore, this programming language does not meet the criteria for Turing Completeness because the Halting Problem is decidable for all possible programs. If a random error occurs, the message **"Broken Calculator!"** is printed, with an error code, and the probability of crashing.

## Memory

Broken Calculator stores data in memory cells on an unbounded tape. Cells have the following properties.

- A cell can store an integer. By default 0 is stored.
- A cell has an index on the tape in the nonnegative integers. (i.e. 0, 1, 2, 3...).
- A cell can be accessed by the Cell Access Operation, denoted by an at symbol "
**@**".

Broken Calculator also has an accumulator to increment.
The value of the accumulator can be accessed with the parameter "**A**".

## Syntax

### Instructions

Note that many instructions in this language take parameters, which are separated by a space. A parameter can be a cell, value, or both, as indicated by the following symbols.

**C**for cell**V**for value**$**for cell or value

These symbols are not valid operations in this language. They are tokens of the following table.

Command | Parameters | Desc. |
---|---|---|

INP | C | Take input (value) at cell. |

SET | C $ | Set a cell to some value. |

ADD | C $ $ | Add two values and store at cell C. |

SUB | C $ $ | Subtract right value from left value and store at cell C. |

MUL | C $ $ | Multiply two values and store at cell C. |

POW | C $ $ | Raise left value to the right value and store at C. |

MOD | C $ $ | Modulo left value by right value and store at C. |

DIV | C $ $ | Divide left value by right value and store at C. |

RFL | C $ $ | Take the floor of the root of the left value by right value and store at C. |

GOTO | V | Go to label V. |

LBL | V | Example |

CBEQ | V $ $ | Compare and branch if equal. Go to label V if values are equal. |

CBLE | V $ $ | Compare and branch less than. Go to label V if right value is less than left value. |

CBGR | V $ $ | Compare and branch greater than. Go to label V if right value is greater than left value. |

PCR | $ | Print value as ASCII character. |

PIN | $ | Print value. |

ACZ | None | Set Accumulator to zero. |

ACI | None | Increment Accumulator. |

HLT | None | Halt; end program. |

NOP | None | No operation. Adds a delay of a tenth of a second. |

CMT | None | A comment. |

NOTE | None | A comment for those who like INTERCAL. |

### Cell Access Operation

This operation is invoked via an at symbol "**@**" and it indicates that the integer immediately following this operation is not a literal value, but a pointer to a cell on the tape as indicated. (For example, `@0`

points to cell 0 on the tape.) Depending on instruction and parameter, this cell may store a value, or it may access the value currently stored for use in the instruction.

### Grammar in EBNF

Program ::= {Instruction} Instruction ::= Command, {Param, Space}, Newline Param ::= Value | Cell | 'A' Cell ::= '@', Value Command::= {Letter}, Space Value ::= {Digit} Space ::= ' ' Newline ::= '\n' Digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' Letter ::= 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'

## Crash Probability Formula

This formula describes the odds of crashing for a given source; given that the source would not crash due to some other error in the source.

The following constants are defined:

- Let
*L*be the number of instructions in the source code - Let
*X*be a random value between 1 and (150 +*L*) inclusive - Let
*R*be equal to (X / 4)

Let *r(x)* be a function that rounds x to the nearest whole number, such that the following holds:

- If
*x*is greater than or equal to*(floor(x) + .5)*, then*r(x)*is equal to*(floor(x) + 1)* - If the above condition is not met, then
*r(x)*is equal to*floor(x)*

Let the formula be defined as:
*F = r((L * 10) / R)*

## Program Examples

### Broken Hello World

PCR 72 PCR 69 PCR 76 PCR 76 PCR 79 PCR 32 PCR 87 PCR 79 PCR 82 PCR 76 PCR 68

### Broken Truth Machine

INP @0 CBEQ 0 @0 0 LBL 1 PIN 1 GOTO 1 LBL 0 PIN 0

### Accumulator Test

ACI PIN A ACZ PIN A NOTE Should print '10'