Stackstack

From Esolang
Jump to navigation Jump to search

Stackstack is a stack-based programming language made in 2013 by User:T.J.S.1 at 15 years old. It can be described as a Forth-like language. The language began with one main interpreter written in Javascript, but more is being worked on.

Description

Stackstack is a stack-based language (who would've guessed), but for convenience it supports variables too. The command reference can be found on the interpreter page.

It supports numerous instructions, called terms, which all operate on the stack, popping all used arguments. The terms can be separated by any amount of whitespace, giving one complete freedom in how to structure one's program indentation-wise. If a "#"-character is reached, the rest of the line is ignored, making it function as a comment character like in Bash or // in C(++).

An interesting feature of Stackstack over other stack-based languages is that it supports the well-known if statement and (a variation on) the while loop. The if statement can have an else in between the if and the endif, but it is not obligatory. The syntax is as follows:

0
if "yes" else "no" endif print
32 char print
1
if "yes" endif print

The output of this program is "no yes".

10 while
    dup tostring print
    32 char print
1 - endwhile

The output of this program is "10 9 8 7 6 5 4 3 2 1 ".

Examples

Hello, world!

"Hello, world!" in StackStack:

"Hello, world!" print 

Obvious as this may be, it shows the easy and wonderful use of StackStack.

Quine

A quine in stackstack:

"34 char print  dup print  34 char 10 char concat print  print"  
34 char print  dup print  34 char 10 char concat print  print 

So, this piece of code prints the code itself. A nice variation of quine is a narcissist. Narcissists are based on Greek mythology, where the boy Narcissus falls in love with his own mirror image. In this program, you have to enter the program's source code as input to get the response "1", (true). If you enter anything other than the sourcecode, the output will be "0" (false). Narcissus code in Stackstack:

"34 char swap dup -3 roll concat
34 char concat 10 char concat
swap concat
inputstr eq if 1 else 0 endif tostring print"
34 char swap dup -3 roll concat
34 char concat 10 char concat
swap concat
inputstr eq if 1 else 0 endif tostring print

Fibonacci

The stackstack version of Fibonacci:

1 "a" store
1 "b" store
738 dup while
	"a" retr
	dup tostring print
	10 char print
	"b" retr
	dup tostring print
	10 char print
	+ dup "a" store
	"b" retr
	+ "b" store
1 - dup endwhile

A version without using variables, only the stack:

0 1
8 dup while -3 roll
	swap dup 3 roll dup -3 roll
	swap tostring print
	10 char print
	tostring print
	10 char print
	swap dup 3 roll dup -3 roll #o1,o2,o1,o2
	+ #o1,o2,n1
	3 roll #o2,n1,o1
	pop #o2,n1
	dup 3 roll #n1,n1,o2
	+ #n1,n2
3 roll 1 - dup endwhile

A recursively defined version which pops the top of the stack and returns the nth Fibonacci number:

$fib dup 1 greater if dup  1 - @fib swap 2 - @fib + endif

6 @fib
9 @fib
+ # [42]

Guess the number

This is a simple guessing game with hints. There's probably a cleverer way to branch on [-1,0,+1].

rand 100 *
floor

1 while
    dup inputnum -
    dup 0 less    if "Lower!"  alert endif
    dup 0 greater if "Higher!" alert endif
endwhile

"You win!" alert

99 bottles

And finally, 99 bottles of beer:

99 "bottles" store
99 dup while
	"bottles" retr tostring print
	" bottle" "bottles" retr 1 eq not if "s" concat endif " of beer on the wall, " concat print
	"bottles" retr tostring print
	" bottle" "bottles" retr 1 eq not if "s" concat endif " of beer." concat print 
 	10 char print
	"Take one down and pass it around, " print
	"bottles" retr	1 - dup	 
	"bottles" store
	dup 0 eq if pop "no more" endif
	tostring print 
	" bottle" "bottles" retr 1 eq not if "s" concat endif " of beer on the wall." concat print
	10 char dup print print
1 - dup endwhile

"No more bottles of beer on the wall, no more bottles of beer." 10 char concat
"Go to the sore and buy some more, 99 bottles of beer on the wall." concat 10 char concat
print

More examples can be found on the site of the interpreter.

Interpreter

The main interpreter for StackStack can be used on Tom Smeding's website (from the Wayback Machine; retrieved on 17 October 2015).

User:Deciode has written another one and a C++ interpreter is work-in-progress.