GridScript

From Esolang
Jump to: navigation, search

GridScript is an esoteric programming language designed by user SuperJedi224. It is not yet implemented.

The program is expressed as a list of points in a two-dimensional grid, with a command assigned to each point, such as "(3,1):PRINT 42".

The main program and each subroutine (if there are any) must each contain exactly one node marked START. The program tracer starts at the center of this node, pointing east.

Data storage takes the form of a dataspace, also two-dimensional; a stack-like structure called a "Buffer", and an arbitrary number of variables.

The dataspace may only store INTs (and is initially filled with 0s), the Buffer may store any number of INTs, FLOATs, STRINGs, and/or BOOLs (and is initially empty), variables may store a single INT, FLOAT, STRING, or BOOL each.

Variables and buffer values may also store the special value NULL, and this is assumed to be the value of any variable that has not yet been assigned a value.

Basic Program Format

#TITLE.

(metadata)

(list of nodes)

With subroutines

#TITLE.

(metadata)

(list of nodes)

##SUBROUTINE 1 TITLE.

(metadata)

(list of nodes)

##SUBROUTINE 2 TITLE.

(metadata)

(list of nodes)

Command Summary

These are the commands used, with a brief description of each.

Command Summary
START Program flow starts here, going east
GO direction Changes the direction of program flow. Accepts an integer between 0 and 359, inclusive; or one of several keywords
CHECKPOINT id Marks a program checkpoint with the specified id.
GOTO id Moves the program tracer directly to the checkpoint with the specified id, without affecting its direction.
SWITCH condition condition is any value either alone or directly preceded by one of the following: "!", "=", or "!=". Depending on the condition assigned, it may or may not rotate program flow 90 degrees clockwise. It also accepts the keyword RANDOM as a substitute for condition.
STORE value [TO variable] Stores the specified value, either to the specified variable or else to the selected position in the dataspace. It also accepts the keyword RANDOM as a substitute for value, in which case it stores a random float on the interval [0,1).
INPUT [type] [TO variable] Takes a value from the input. If variable is specified, it stores the value, unchanged, to that variable. Otherwise, it casts it to an INT and stores it at the selected position in the dataspace. If type is specified, it attempts to cast it to that type before storing. (When used in a subroutine, it takes from the subroutine call's argument list instead of from the main input)
LOAD FILE path [TO variable] As input, but loads from a text file instead
NEXT VALUE Move the data tracer to the next cell
PREVIOUS VALUE Move the data tracer to the previous cell
NEXT ROW Move the data tracer to the beginning of the next row
PREVIOUS ROW Move the data tracer to the beginning of the previous row
PRINT [value] The output command. Casts the specified value, or else the currently selected value in the dataspace, to a STRING and prints it to the output. Also accepts various keywords for outputting things like a newline character, an image, or the contents of a text file.
PUSH [value] Adds the specified value, or else the currently selected value from the dataspace, to the Buffer
REMOVE [position] [TO variable] Removes a value from the buffer at the specified position, or else from the bottom, and stores it to the specified variable, if applicable, or else to the selected position in the dataspace. Also accepts keywords for typecasting, or selecting a position at random.
PEEK Without modifying the buffer, copies its bottom value, casts it to an INT, and stores it at the selected position in the dataspace
HOME Returns the data tracer to its initial position
MOVE LAST NODE TO|BY x y Moves the last node that was executed
INCREMENT [variable1] [BY value] [GIVING variable2] The addition operator
DECREMENT [variable1] [BY value] [GIVING variable2] The subtraction operator
MULTIPLY [variable1] BY value [GIVING variable2] The multiplication operator
DIVIDE [variable1] BY value [GIVING variable2] The division operator
SHUFFLE Rearranges the buffer in a random order
SPLIT string [OVER separator] Splits the specified string into one or more substrings, separating on either the specified separator string, if applicable, or else over the whitespace. Then, pushes all of the resulting substrings to the Buffer.
CALL subroutine [WITH ARGUMENTS arguments...] [GIVING variable] The command for subroutine calls. Any return value from the call is stored to the specified variable, if applicable, or else to the selected position in the dataspace
WARN message Throws a warning with the specified message
RETURN [value] Only supported in subroutines. Finishes subroutine execution and returns the specified value, if applicable, or else the selected value in that subroutine instance's dataspace.

Comments

A pair of exclamation marks (!!) denotes that the rest of that line of code is a comment

Sample Programs

Hello World

#HELLO WORLD.

@width 3
@height 1

(1,1):START
(3,1):PRINT 'Hello World' 

Cat Program

This program halts when either 0 or the empty string is given.

#CAT PROGRAM.

@width 11
@height 1

(1,1):START
(3,1):CHECKPOINT 0
(5,1):INPUT STRING s
(7,1):SWITCH !s
(9,1):PRINT s
(11,1):GOTO 0

Truth Machine

#TRUTH MACHINE.

@width 9
@height 5

(1,1):START
(3,1):INPUT INT
(5,1):GO EAST
(7,1):SWITCH =1
(7,3):PRINT 1
(7,5):GO WEST
(5,5):GO NORTH
(9,1):PRINT 0

Alternate Version

#TRUTH MACHINE 2.

@width 7
@height 7

(1,1):START
(3,1):INPUT INT
(5,1):SWITCH =1
(5,3):CHECKPOINT 0
(5,5):PRINT 1
(5,7):GOTO 0
(7,1):PRINT 0

99 Bottles of Beer on the Wall

#99 BOTTLES OF BEER.

@width 7
@height 39

(1,1):START
(3,1):STORE 99
(5,1):GO EAST
(7,1):GO SOUTH
(7,3):PRINT
(7,5):PRINT " bottles of beer on the wall,"
(7,7):PRINT NEWLINE
(7,9):PRINT
(7,11):PRINT " bottles of beer."
(7,13):PRINT NEWLINE
(7,15):PRINT "Take one down and pass it around,"
(7,17):PRINT NEWLINE
(7,19):SWITCH =1
(7,21):PRINT
(7,23):PRINT " bottles of beer on the wall!"
(7,25):DECREMENT
(7,27):GO WEST
(5,27):GO NORTH
(5,25):PRINT NEWLINE
(5,23):PRINT NEWLINE
(3,19):GO SOUTH
(3,21):PRINT "1 bottle of beer on the wall!"
(3,23):PRINT NEWLINE
(3,25):PRINT NEWLINE
(3,27):PRINT "1 bottle of beer on the wall,"
(3,29):PRINT NEWLINE
(3,31):PRINT "1 bottle of beer"
(3,33):PRINT NEWLINE
(3,35):PRINT "Take one down and pass it around,"
(3,37):PRINT NEWLINE
(3,39):PRINT "No more bottles of beer on the wall."

Factorial

#FACTORIAL.

@width 14
@height 8

(1,3):START
(7,1):CHECKPOINT 0
(3,3):INPUT INT TO n
(5,3):STORE n
(7,3):GO EAST
(9,3):DECREMENT n
(11,3):SWITCH n
(11,5):MULTIPLY BY n
(11,7):GOTO 0
(13,3):PRINT

Fibonacci Sequence

(Only stores 10,000 terms at a time)

#FIBONACCI SEQUENCE.

@width 37
@height 1
@datawidth 10000
@dataheight 1

(1,1):START
(3,1):STORE 1
(5,1):NEXT VALUE
(7,1):STORE 1
(9,1):NEXT VALUE
(11,1):PRINT "1 1"
(13,1):CHECKPOINT 0
(15,1):PREVIOUS VALUE
(17,1):PREVIOUS VALUE
(19,1):STORE TO a
(21,1):NEXT VALUE
(23,1):STORE TO b
(25,1):INCREMENT a BY b GIVING c
(27,1):PRINT " "
(29,1):NEXT VALUE
(31,1):STORE c
(33,1):PRINT
(35,1):NEXT VALUE
(37,1):GOTO 0

Ackermann Function

#ACKERMANN FUNCTION.

@width 9
@height 1

(1,1):START
(3,1):INPUT INT TO x
(5,1):INPUT INT TO y
(7,1):CALL ACK WITH ARGUMENTS x y
(9,1):PRINT

##ACK.

@width 19
@height 7

(1,1):START
(3,1):INPUT INT TO x
(5,1):INPUT INT TO y
(7,1):SWITCH !x
(7,3):INCREMENT y
(7,5):RETURN y
(9,1):SWITCH !y
(9,3):DECREMENT x
(9,5):CALL ACK WITH ARGUMENTS x 1
(9,7):RETURN
(11,1):DECREMENT y
(13,1):CALL ACK WITH ARGUMENTS x y GIVING z
(15,1):DECREMENT x
(17,1):CALL ACK WITH ARGUMENTS x z
(19,1):RETURN

External resources