Sharp

From Esolang
Jump to navigation Jump to search
The title of this article is not correct because of technical limitations. The correct title is actually #.

# (Pronounced as "sharp") is a stack-based, Turing-complete programing language drawing inspiration from BrainFuck and Fish. It was invented by User:Nick-1666 in 2022.

Concepts

# uses the concept of "Hex Strings". These hexadecimal strings will be read from ( to ). In a "Hex String", the values are only evaluated at the end of the hex string when the marker is reached. This allows easy pushing of values to the stack with place value being respected. (10) would be interpreted as 16 and (1) would be interpreted as 1. Hex strings also possess a special property which allows popping off the stack when a null string is passed like so: (). This removes the necessity of yet another token.

Syntax

Note: All operations are performed with reverse polish notation in mind

Stack Manipulation
Command Description
$ Swaps the top two values of the stack.
= Duplicates the top of the stack.
| Reverses the stack.
( Opens a hexadecimal string. When a hex string is immediately terminated, the top value is popped.
) Closes a hexadecimal string and pushes the value to the stack.
0-9, A-F Evaluated inside brackets as hexadecimal strings.
Arithmetic
Command Description
+ Pops two values off the stack and adds them, pushing the sum.
- Pops two values off the stack and subtracts them, pushing the difference.
* Pops two values off the stack and multiplies them, pushing the product.
/ Pops two values off the stack and divides them (rounding to an integer), pushing the quotient.
% Pops two values off the stack and divides them (rounding to an integer), pushing the remainder.
I/O
Command Description
. Gets input from the user and stores it as a number. Any character that is not 0-9 will be stored as their ascii codes - 48
, Pops a value from the stack and output its numerical value.
: Gets input from the user and stores the character's ascii value.
; Pops a value from the stack and output it as a an ascii character.
Specials
Command Description
[ Pops a value from the stack and jumps past the matching ] if it is 0.
] Jump back to the matching [ if the cell at the pointer is nonzero.
^ Pushes the stack size onto the stack.
# Marks an inline comment.

Examples

 1  # Truth machine program by Nick-1666 29/05/2022
 2 .=[(1)=,],

The program above first takes in user input and stores it as a number. It then duplicates the top value and then pops it so it can be checked to be 0 at the beginning of the loop. If it is, the program then skips to the "]" and prints 0. If it is not 0, the program will push two 1s onto the stack. One of them is printed and the other is evaluated at the start of the loop. Since the stack will always have a 1 at the end, it can never be 0 so it loops infinitely.

 1  # Hello, World! program by Nick-1666 29/05/2022
 2 (48)(65)(6C)(6F)(20)(57)(6F)(72)(6C)(64)(21)|=[;=]

The program pushes the ascii values of "Hello World!" onto the stack. The stack is then reversed so values can be popped correctly and a copy of H's ascii value is also pushed. The top value (H's ascii code) is then checked to be 0 (which it isn't) and so the program enters a loop which pops a value and prints it as ascii then duplicating the next one as it is evaluated when looping to check it is not 0. This will cause the program to print all values off the stack as ascii then reach the end where it halts.

Implementations