RAND

From Esolang
Jump to: navigation, search

RAND is an esoteric programming language created by User:Smallhacker, partially based on non-determinism. It's believed to be Turing complete, but this hasn't been proven.

Memory

RAND's memory consists of an unspecified amount of bytes (where "unspecified" should be interpreted as "as much as neccesary"). When the program starts, all memory locations are set to random values. The memory is accessed using variables.

The memory isn't global; every call to a subroutine backs up the real memory by pushing it onto the stack and sets all bytes in the real memory to random values. When the program leaves the subroutine, the backed up data is popped into the real memory.

There's only one completely global byte and one "one way" global byte: the variable "Com" which isn't affected by the stack pushing/popping (which allows it to be used as input/output for subroutines) and the variable "Arg" which is affected by the stack popping, but not pushing (meaning that it can be used to send data into the subroutine, but not back from it).

Commands

  • RAND var - Sets 'var' to a random value
  • AND var1 var2 - Sets 'var1' to 'var1' AND 'var2' (bitwise)
  • OR var1 var2 - Sets 'var1' to 'var1' OR 'var2' (bitwise)
  • ANDB var1 var2 - Same as AND, but only affects the lowest bit
  • ORB var1 var2 - Same as OR, but only affects the lowest bit
  • ROTR var1 - Shifts 'var1' to the right. The value wraps around (meaning that 1 becomes 128).
  • LABEL name - A label used to jump around in the code
  • IFEQ var1 var2 name - Jumps to label 'name' if 'var1' == 'var2'
  • CALL name - Jumps to 'name' as a subroutine (pushing/clearing the memory)
  • END - Leaves the subroutine (popping the memory) or exits the program if it's not in a subroutine.

C++-like comments (// and /* ... */) are allowed in the language.

Variables/Labels

Variable/label names can contain a-z, A-Z, 0-9 and underscore. Names don't have to start with a letter.

Unlike variables, labels are global.

Examples

Get0

Description: A subroutine that sets Com to 0

LABEL	Get0
RAND	Com
LABEL	Get0_Loop
RAND	Tmp
IFEQ	Com Tmp Get0_Loop
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
ROTR	Com
AND	Com Tmp
END

Get1

Description: A subroutine that sets Com to 1

LABEL	Get1
CALL	Get0
LABEL	Get1_Loop
RAND	Tmp
IFEQ	Com Tmp Get1_Loop
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
ROTR	Tmp
ORB	Com Tmp
END

Get2

Description: A subroutine that sets Com to 2

LABEL	Get2
CALL	Get1
ROTR
ROTR
ROTR
ROTR
ROTR
ROTR
ROTR
END

Get4

Description: A subroutine that sets Com to 4

LABEL	Get4
CALL	Get1
ROTR
ROTR
ROTR
ROTR
ROTR
ROTR
END

Get8

Description: A subroutine that sets Com to 8

LABEL	Get8
CALL	Get1
ROTR
ROTR
ROTR
ROTR
ROTR
END

...etc.