Haystack - A 2-Dimensional Programming Language 08-31-2015, 05:22 AM
#1
Haystack
A 2-Dimensional Programming Language
A 2-Dimensional Programming Language
Introduction
Haystack was an idea I had inspired by a code-golfing challenge, whereby a series of actions is executed
until the code reaches a needle in the haystack. So, I got to work designing this 2-dimensional language.
I believe for it to be a fairly intuitive language for those new to programming to learn control flow, which I
will show off later. Now, a 2-dimensional language is not necessarily a new idea, and you can check out some
more popular ones such as Befunge, or a personal favourite, MarioLANG.
To program in Haystack, you will need to download the interpreter from the GitHub page.
Haystack was an idea I had inspired by a code-golfing challenge, whereby a series of actions is executed
until the code reaches a needle in the haystack. So, I got to work designing this 2-dimensional language.
I believe for it to be a fairly intuitive language for those new to programming to learn control flow, which I
will show off later. Now, a 2-dimensional language is not necessarily a new idea, and you can check out some
more popular ones such as Befunge, or a personal favourite, MarioLANG.
To program in Haystack, you will need to download the interpreter from the GitHub page.
Data Types
There are only two properly supported, inline data types. These are strings (anything surrounded by quotes), and
integers 0-9. Strings can be read either horizontally or vertically, but directional operators within the string will
be ignored. At some point I will implement some way of using larger numbers, negation, etc.. For now though, you
can achieve a decent amount from what's available.
There are only two properly supported, inline data types. These are strings (anything surrounded by quotes), and
integers 0-9. Strings can be read either horizontally or vertically, but directional operators within the string will
be ignored. At some point I will implement some way of using larger numbers, negation, etc.. For now though, you
can achieve a decent amount from what's available.
Directional Operators
There are a total of 6 directional operators. Each of these will change the direction of the program, and
this direction will be preserved until another directional operator is reached. The "surface" which the program
is on has walls which wrap around, which can be used to your advantage.
The four basic directional operators are v, ^, <, and >, and I think it's fairly obvious which each of those does.
The next two operators are called reflectors, as they reflect the current direction to another one, so they don't
have just one function. The / operator will change direction from right to up, up to right, down to left and left to
down. The \ operator essentially performs the opposite, where right becomes down, down becomes right,
left becomes up and up becomes left.
Here is an example program, which does not print anything, but shows how the operators would be applied.
There are a total of 6 directional operators. Each of these will change the direction of the program, and
this direction will be preserved until another directional operator is reached. The "surface" which the program
is on has walls which wrap around, which can be used to your advantage.
The four basic directional operators are v, ^, <, and >, and I think it's fairly obvious which each of those does.
The next two operators are called reflectors, as they reflect the current direction to another one, so they don't
have just one function. The / operator will change direction from right to up, up to right, down to left and left to
down. The \ operator essentially performs the opposite, where right becomes down, down becomes right,
left becomes up and up becomes left.
Here is an example program, which does not print anything, but shows how the operators would be applied.
Code:
> \
/ v
/ <
^ <
> |Note that the vertical bar | is the "needle", so once the program hits this it will terminate. If the empty space
makes it more difficult to understand exactly what is going on, you can always fill in the blanks like so to clear it up.
makes it more difficult to understand exactly what is going on, you can always fill in the blanks like so to clear it up.
Code:
>>>>>>>>>>>>>>>>>\
/>>>v v
^ v /<<<<<<<<
^ v v
^<<< <<<
>>>>>>>>>>>|Note the usage of the space to allow for the program to cross over itself. Also, the interpreter always starts
at the first character, and goes right if there is no directional operator to tell it otherwise.
at the first character, and goes right if there is no directional operator to tell it otherwise.
General Operators
I think it is fairly easy to comprehend what these do. so I'm just going to give short descriptions on what they do.
+ adds the top two values on the stack
- subtracts the top two values on the stack
* multiplies the top two values on the stack
/ divides the top two values on the stack
] checks if the 2nd top value is greater than the top value
[ checks if the 2nd top value is greater than the top value
= checks equality of the top two values
i takes input from the user
o prints the top value on the stack
c prints the top value as an ASCII character
d duplicates the top value on the stack
@ rotates the stack right
, discards the top stack value
; swaps the top two stack values
I think it is fairly easy to comprehend what these do. so I'm just going to give short descriptions on what they do.
+ adds the top two values on the stack
- subtracts the top two values on the stack
* multiplies the top two values on the stack
/ divides the top two values on the stack
] checks if the 2nd top value is greater than the top value
[ checks if the 2nd top value is greater than the top value
= checks equality of the top two values
i takes input from the user
o prints the top value on the stack
c prints the top value as an ASCII character
d duplicates the top value on the stack
@ rotates the stack right
, discards the top stack value
; swaps the top two stack values
Conditional Operator
A big part of making a working programming language is having the ability to use conditions to control the flow
of the program. As such, I have implemented one: ?. This is simply going to check the stack's top value to see
whether it's true or false, and from there will redirect the flow depending on the result. This direction depends
on which direction the conditional operator was approached from. If it was approached from the left or the right,
then if the value is true it will redirect flow up, and if false will redirect down. However, if the conditional operator
is approach from the top or bottom, a truthy value will direct flow right, while a falsy value will direct flow left.
Here is a small program which will print a good value when input is greater than 5, and something else otherwise.
A big part of making a working programming language is having the ability to use conditions to control the flow
of the program. As such, I have implemented one: ?. This is simply going to check the stack's top value to see
whether it's true or false, and from there will redirect the flow depending on the result. This direction depends
on which direction the conditional operator was approached from. If it was approached from the left or the right,
then if the value is true it will redirect flow up, and if false will redirect down. However, if the conditional operator
is approach from the top or bottom, a truthy value will direct flow right, while a falsy value will direct flow left.
Here is a small program which will print a good value when input is greater than 5, and something else otherwise.
Code:
v /"Good!"\
>i5]? >o|
\"Bad!!"/Looping
This is my absolute favourite part of explaining the language, because looping is very intuitive even to people
who have little to no programming experience. There are no built in looping constructs, but you can use the
directional operators to create actual loops! There's no better way of describing this other than showing you,
so here is an example which will increment a value until it is greater than 9, then output it.
This is my absolute favourite part of explaining the language, because looping is very intuitive even to people
who have little to no programming experience. There are no built in looping constructs, but you can use the
directional operators to create actual loops! There's no better way of describing this other than showing you,
so here is an example which will increment a value until it is greater than 9, then output it.
Code:
v /o|
>i>d9]?
\ +1/Here's a slightly expanded version to better display the loop itself:
Code:
v >o|
v ^
>>>i>d9]?
^ v
^<+1<Commenting
Now here's a really fun bit: As long as you never reach a certain block, or that block is not a valid operation in
Haystack, it will be ignored. So, you can somewhat obfuscate what your code actually does, if you so choose. For
example, here's the same program as described in the last section, but with some added "noise".
Now here's a really fun bit: As long as you never reach a certain block, or that block is not a valid operation in
Haystack, it will be ignored. So, you can somewhat obfuscate what your code actually does, if you so choose. For
example, here's the same program as described in the last section, but with some added "noise".
Code:
v#*^sbjb/o| Note I can use whatever
fasdn2ibkbd I want on the outside of
\R&i>d9]?cD the program, since this won't be
498hKDjbU92 reached I can be sure it
KJ2n\L+1/DF will not interfere with
the execution. This makes
inline commenting extremely easy!This is all I have right now. Leave suggestions for additions below, whether it be a new feature or a new tutorial! Thanks for reading.
![[Image: CDUAq9d.png]](http://i.imgur.com/CDUAq9d.png)





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)












And commenting is brilliant. Good job.






