PREFIX

From Esolang
Jump to navigation Jump to search

PREFIX is an esolang based entirely on prefix notation and function calling.

Language Constructions

In order to call a function and its arguments, one would do so like this:

function arguments

No parentheses are needed.

PREFIX has 3 basic language constructions:

  1. if condition, what to do: If condition is true, do what to do. What to do must be treated as one instruction.
  2. while execute condition, what to do: While executing execute condition returns true, do what to do. Both execute condition and what to do must be treated as one instruction.
  3. seq...end: Executes everything between seq and end. This is treated as one instruction.

Built-in Functions

Function Description
add n1 n2 return the sum n1 + n2
sub n1 n2 return the difference n1 - n2
mul n1 n2 return the product n1 * n2
div n1 n2 return the quotient n1 / n2
mod n1 n2 return the remainder n1 % n2
set var n1 set var to n1; return n1
print string print string to standard output
readin return one character from standard input
readln return one line from standard input
con var, string concatenate string to end of var
len arg return the length of tostring arg
dec var declare variable var
darray var declare array var
sarr array n1 return array[n1]
sstr string n1 return the n1th character of the zero-indexed string string
eq arg1 arg2 return true if arg1 = arg2
neq arg1 arg2 return true if arg1 != arg2
gt arg1 arg2 return true if arg1 > arg2
geq arg1 arg2 return true if arg1 >= arg2
lt arg1 arg2 return true if arg2 > arg1
leq arg1 arg2 return true if arg2 >= arg1
and arg1 arg2 return true if arg1 and arg2 are true
or arg1 arg2 return true if arg1 or arg2 is true
not arg1 return true if arg1 is not true
tostring arg return arg as a string
tonum arg return arg as a number; true is 1, false is 0
toascii arg return the ascii char for tonum arg
fromascii arg return the ascii number of the character for tostring arg
nop does nothing
eof return eof
pi return π
e return e

Turing-completeness

In order to show that PREFIX is turing-complete, I have written a BF interpreter for it. The looping semantics may be off, though, so feel free to edit it.

define main:
decl prog decl ip darray mem decl mp darray stack decl sp decl c set ip 0 set mp 0  while neq con prog readin "!" nop 
while lt ip len prog seq 
	set c sstr prog ip #Set c to current character
	if eq c "+" set sarr mem mp add sarr mem mp 1 	
	if eq c "-" set sarr mem mp sub sarr mem mp 1 	
	if eq c ">" set mp add mp 1 	      	
	if eq c "<" set mp sub mp 1	      	
	if eq c "." print toascii sarr mem mp 
	if eq c "," set sarr mem mp fromascii readin 
	if eq c "[" seq 
		if neq sarr mem mp 0 seq
			set sarr stack sp ip
			set sp add sp 1 end
		if eq sarr mem mp 0 while neq sstr prog ip "]" set ip add ip 1
	end
	if eq c "]" seq
		if neq sarr mem mp 0 seq
			set ip sarr stack sp
			set sp sub sp 1
	end
	set ip add ip 1
end

Final Comments

  • PREFIX is a rather easy-to-code in esolang. Maybe it even isn't.
  • To implement PREFIX you will need a lot of stacks. Beware.