Basis

From Esolang
Jump to navigation Jump to search

Basis is a simpler language based on Element invented by User:A. It is supposedly designed as "very compact and human-readable". Each instruction is one character long and performs a single function.

Basis has only one stack as its only memory structure.

Implicit input system

Whenever a value is needed, the value will be given from the first to the last input. If the last input is consumed, the consumption maps back to the first input.

Documentation

Instruction Meaning
. print without popping
, convert to a character
NUMBER Push the number onto the stack
\ Escape the next instruction and push onto the stack as a separate string
" Quotation starter/ender. You are allowed to escape quotations using the \ instruction.
A/B Push either the first input or the second input given to the program
+-*/% Arithmetic: Adds/subtracts/multiplies/divides/modulos them.
$ Swap the two items on the stack.
: Duplicate the top of the stack.
[] Repeat the top of the stack times. This consumes at least one item on the stack, and them takes items on the stack whenever neccecary. The stack's items can be accessed like inputs given to the loop, as if you were using a function.
{} While the top of the stack is nonzero, do the body. This also provides the functionality in the for loop.

Example programs

"This is a language that I invented in early 2012 
to be a simple golfing language. By this, I mean 
that there is very little to no operator overloading. 
The operators are also simpler and fewer in number 
than most modern golfing languages."

This language seems to surpass the simplicity of
Element. It has even fewer instructions provided,
yet it is capable of beating Element on math-based
holes. (But it doesn't even provide a representation
of strings yet...)

Hello World

Hello, World!

There is an implicit output of the stack.

Cat program


Input implicitly prepended, implicit output after the input is prepended.

Nth Fibonacci number

1 1A[+A]

Explanation

Implicit prepend input if and only if this value will be used in the program.

1 1      Push 1 & 1
   A     Append input given to the program.
    [  ] Repeat input times (We don't know how many operands our expression will need yet):
     +   Arity: 2. Add the provided operands given to the for loop. (Which is 0 and 1)
      A  Arity: 0. Return the first operand given to the for loop.
	 This expression gets returned to the stack and continues after each for loop iteration.

The TOS gets implicitly output.

Execution:

1 1:4
2 1:3
3 2:2
5 3:1

Ended. The output is 3.

Nth Triangular Number

1+*2/

Explanation:

Implicitly prepend input, which will evidently be used later

1+    Add the input by 1
  *   Multiply. Since only one input is given, the input is repeatedly provided.
   2/ Divide the value by 2.

Implicit output

Digital root calculator

1-9%1+

Explanation

1-     Decrement the implicit input by 1
  9%   Modulo it by 9
    1+ And increment it by 1

Implicit output

Truth-machine

{.}

Explanation:

Implicit input

{    While the top of the stack is truthy
 .}  Output the top of the stack

The value will be implicitly outputted

Factorial

Sample execution

3 1
2 3
1 6
0 6
1A[*A1-$]

Explanation:

Implicitly provides 1 input

1         Push the accumulator
 A[     ] Repeat input times:
   *      Return accumulator * input
    A1-$  Put input - 1 below

Implicit print the accumulator

GCD of two numbers

{%B}+

Explanation

Provides implicit input twice

{  }  While loop based on input
 %    Return A%B
  B   Following the value of the original second operand
    + Loop gets broken when b == 0.
	  Adding them will result in just the GCD value.

Print "Element" without using letters

69 108:7-:8+:8-:9+:6+{,.}

Quine

:\"+$":\"+$

Explanation

:\"+$"      The string :\"+$
      :     Duplicate
       \"+  Append it with a string
          $ Swap

Implicit output

Reference implementation (WIP)

input=[3]
prog="1 1A"
stack=[]
in_loop=False
for i in input:
    stack.append(i)

ic=0 # What is the current input?

pc = 0
while pc<len(prog):
	if prog[pc] in "0123456789":
		out=prog[pc]
		pc+=1
		while prog[pc] in "0123456789":
			out+=prog[pc]
			pc+=1
		stack.append(int(out))
		pc-=1
	elif prog[pc] in "+-*/%":
		if len(stack)==1:
			stack.insert(0,input[ic%len(input)])
			ic+=1
		elif len(stack)==0:
			stack.insert(0,input[ic%len(input)])
			ic+=1
			stack.insert(0,input[ic%len(input)])
			ic+=1
		stack[-1],stack[-2]=stack[-2],stack[-1]
		exec("stack.append(stack.pop()"+prog[pc]+"stack.pop())")

	elif prog[pc] in "$":
		stack[-1],stack[-2]=stack[-2],stack[-1]
	elif prog[pc] == "A":
		if not in_loop:
			stack.append(input[0])
	elif prog[pc] == "B":
		if not in_loop:
			stack.append(input[1])
	elif prog[pc] == ".":
		print(stack[-1],end="")
	pc+=1

print(stack[-1])