Documents of Stack Programming Language
This documentations describes details of Stack programming language.
This document will refer the Stack language as "Stack", while stack as a data structure will be referred as "stack" (with a lower case s).
Stack's slogan is "Powerful script language with stack-oriented approach". It means Stack's execution logic is using stack, and Stack is very efficient. It's very simple naming, because only using stack as execution logic.
Stack has variety distributions for support many use-case. Because if it doesn't split function each distributions, the binary will become very heavy. So, There are a lot of Satck distributions.
Reading this documents is ideal way to learn Stack. Let's jump into the engaging Stack world!
Getting Started
This is journey that learn Stack by many project. If you feel too easy, you should skip it.
Are you ready? Let's go to the journey!
Try Stack in the OXE
You can try Stack without installation if use OXE
OXE is stand for online execution environment.
below link is OXE's site
Let's write a code and run of Stack program! But, to install Stack is best way to use Stack than OXE. So, in next page, It teach how to install the Stack.
Installation
Paste below shell script on the your terminal. It installed Stack develop environments.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
git clone https://github.com/stack-community/stack-lang.git
cargo install --path ./stack-lang
It's sucessful installation! if you enter stack
and stack-server
on the your shell then displayed REPL.
REPL is stand for read-eval-print loop. It's usual way in many language's interpreter.
Look, isn't it easy?
Hello, World!
To write program that display string "Hello, world!" is traditional learning way first time touch new language. Let's write the program in the Stack!
enter command stack
on the your shell to start up the REPL of Stack.
If REPL is started, enter below code on the REPL and push enter-key twice.
(Hello, world!) print
If the code isn't wrong, Maybe it's output log like below.
Stack〔 〕 ← (Hello, world!)
Stack〔 (Hello, world!) 〕 ← print
[Output]: Hello, world!
Stack〔 〕
Great! you really become Stacker.
Stacker is means programmer who use Stack
Solve Fizz Buzz
To solve Fizz Buzz problem is using elementary programing tests. Below code is Stack program to solve that problem.
1 101 1 range (i) (
(FizzBuzz) (
(Fizz) (
(Buzz) i
i 5 mod 0 equal if
)
i 3 mod 0 equal if
)
i 15 mod 0 equal if
) map ( ) join print
Let's run this program on your console! We recommend enable debug mode. It's easy to understaning execution logic. We hope you do self-learning, Nothing is explain of program
CLI Tools
Let's build CLI tools! It's simple tool show chart memory used rate only. So, the name is memrate.
(memrate) println (
now-time round print ( |) print
(=) mem-used sys-info mem-size sys-info div 100 mul repeat println
1 sleep
) true while
This tool check memery used rate and show chart once 1 secound. It's success if the output became like below.
Build Web Server
Grammar
In this chapter, Explain about unique grammar of Stack. Stack's grammar is very strange, That makes beginner difficult to understanding. But, It's feeling good to write the code if used to it.
String Notation
Stack's string notation is using parentheses to surround. You might think why is it strange notation, usually quotation. Of course, that has justifiable reason.
Stack hasn't code block, in place of use string. code block should be able to nest, So string should be it too.
Take for example, the notation is like below.
(text)
List Notation
Command Notation
The Stack's command is using chain-case as command style.
Take for example, If we want to read the file, we can use read-file
commmand.
Arguments of command is get from stack. So, we have to push arguments on the stack before command.
(example.txt) read-file
Data Types
List of Distribution
Stack Lang
Stack Server
Stack Wasm
Stack Java
Commands Reference
Calculation
Stack's expression is Reverse Polish Notation (RPN). That's result of stack oriented approach.
RPN is a mathematical notation wherein every operator follows all of its operands. It is also known as postfix notation. In contrast to the more common infix notation, where operators are placed between operands (e.g., 3 + 4
), RPN eliminates the need for parentheses to define operation order.
Advantages of RPN
- No Need for Parentheses: RPN eliminates the need for parentheses that are required in Usual notation to dictate the order of operations.
- Efficient for Computers: RPN is easier to implement on computers as it suits stack-based calculations.
- Reduces Errors: Human errors related to misplacing parentheses are reduced.
Addition
In Stack, to add two numbers, you would write the operands first, followed by the operator.
the "Usual" is based on Python's syntax
Example:
Usual: 3 + 4
Stack: 3 4 add
Result:
Stack〔 〕 ← 3
Stack〔 3 〕 ← 4
Stack〔 3 | 4 〕 ← add
Stack〔 7 〕
Subtraction
For subtraction, the Stack format follows the same pattern: operands first, then the operator.
Example:
Usual: 7 - 2
Stack: 7 2 sub
Result:
Stack〔 〕 ← 7
Stack〔 7 〕 ← 2
Stack〔 7 | 2 〕 ← sub
Stack〔 5 〕
Multiplication
Multiplication in Stack is handled similarly.
Example:
Usual: 5 * 6
Stack: 5 6 mul
Result:
Stack〔 〕 ← 5
Stack〔 5 〕 ← 6
Stack〔 5 | 6 〕 ← mul
Stack〔 30 〕
Division
Division follows the same postfix notation rules.
Example:
Usual: 8 / 4
Stack: 8 4 div
Result:
Stack〔 〕 ← 8
Stack〔 8 〕 ← 4
Stack〔 8 | 4 〕 ← div
Stack〔 2 〕
Modulo
If we want modulo of division, we can use mod
command.
Example:
Usual: 90 % 7
Stack: 90 7 mod
Result:
Stack〔 〕 ← 90
Stack〔 90 〕 ← 7
Stack〔 90 | 7 〕 ← mod
Stack〔 6 〕
Power
Sometimes, we have to powering number. If so case, we can use pow
command.
Example:
Usual: 2 ** 10
Stack: 2 10 pow
Result:
Stack〔 〕 ← 2
Stack〔 2 〕 ← 10
Stack〔 2 | 10 〕 ← pow
Stack〔 1024 〕
Rounding off
Disision isn't always divisible, It need rounding off.
Example:
Usual: round(22 / 7)
Stack: 22 7 div round
Result:
Stack〔 〕 ← 22
Stack〔 22 〕 ← 7
Stack〔 22 | 7 〕 ← div
Stack〔 3.142857142857143 〕 ← round
Stack〔 3 〕
Combining Operations
Stack can also handle more complex expressions by maintaining the order of operations through its postfix structure.
Example 1:
Usual: (3 + 4) * 2
Stack: 3 4 add 2 mul
Result 1:
Stack〔 〕 ← 3
Stack〔 3 〕 ← 4
Stack〔 3 | 4 〕 ← add
Stack〔 7 〕 ← 2
Stack〔 7 | 2 〕 ← mul
Stack〔 14 〕
Example 2:
Usual: 5 + ((1 + 2) * 4) - 3
Stack: 5 1 2 add 4 mul add 3 sub
Result 2:
Stack〔 〕 ← 5
Stack〔 5 〕 ← 1
Stack〔 5 | 1 〕 ← 2
Stack〔 5 | 1 | 2 〕 ← add
Stack〔 5 | 3 〕 ← 4
Stack〔 5 | 3 | 4 〕 ← mul
Stack〔 5 | 12 〕 ← add
Stack〔 17 〕 ← 3
Stack〔 17 | 3 〕 ← sub
Stack〔 14 〕
Input/Output
I/O is nessesaly thing in any applications. In this chapter, Let you know how to use I/O in Stack.
print is common thing in standard outputs.
Example:
In this code, print the "Hello, world!" message.
(Hello, world!) print
Result:
Stack〔 〕 ← (Hello, world!)
Stack〔 (Hello, world!) 〕 ← print
[Output]: Hello, world!
PrintLn
println is like print.
differnnt point is just with line feed.
Example:
In this code, print the "Hello, world!" message.
(Hello, world!) println
Result:
Stack〔 〕 ← (Hello, world!)
Stack〔 (Hello, world!) 〕 ← println
[Output]: Hello, world!
Input
input is enter from user keyboard.
Example:
In this code, Ask user name.
(your name: ) input
(Welcome, ) swap concat println
Result:
Stack〔 〕 ← (Hello, world!)
Stack〔 〕 ← (your name: )
Stack〔 (your name: ) 〕 ← input
your name: StackStick
Stack〔 (StackStick) 〕 ← (Welcome, )
Stack〔 (StackStick) | (Welcome, ) 〕 ← swap
Stack〔 (Welcome, ) | (StackStick) 〕 ← concat
Stack〔 (Welcome, StackStick) 〕 ← println
[Output]: Welcome, StackStick
Control
Any programming language needs control syntax.
In this chapter, Explain control syntax such as if
, while
etc of Stack.
If
If is important way to branch program execution.
Example 1:
In this code, judgement which even or odd is 5987.
(Even print)
(Odd print)
5987 2 mod 0 equal if
Result 1:
Stack〔 〕 ← (Even print)
Stack〔 (Even print) 〕 ← (Odd print)
Stack〔 (Even print) | (Odd print) 〕 ← 5987
Stack〔 (Even print) | (Odd print) | 5987 〕 ← 2
Stack〔 (Even print) | (Odd print) | 5987 | 2 〕 ← mod
Stack〔 (Even print) | (Odd print) | 1 〕 ← 0
Stack〔 (Even print) | (Odd print) | 1 | 0 〕 ← equal
Stack〔 (Even print) | (Odd print) | false 〕 ← if
Stack〔 〕 ← Odd
Stack〔 (Odd) 〕 ← print
[Output]: Odd
Example 2:
It needs otherwise, judgement which even or odd is 8762.
(Even print)
(Odd print)
8762 2 mod 0 equal if
Result 2:
Stack〔 〕 ← (Even print)
Stack〔 (Even print) 〕 ← (Odd print)
Stack〔 (Even print) | (Odd print) 〕 ← 8762
Stack〔 (Even print) | (Odd print) | 8762 〕 ← 2
Stack〔 (Even print) | (Odd print) | 8762 | 2 〕 ← mod
Stack〔 (Even print) | (Odd print) | 0 〕 ← 0
Stack〔 (Even print) | (Odd print) | 0 | 0 〕 ← equal
Stack〔 (Even print) | (Odd print) | true 〕 ← if
Stack〔 〕 ← Even
Stack〔 (Even) 〕 ← print
[Output]: Even
While
While is usual way to loop program.
Example:
1 (i) var
(
i println
i 1 add (i) var
) (i 2 less) while
(Finished) println
Result:
Stack〔 〕 ← 1
Stack〔 1 〕 ← (i)
Stack〔 1 | (i) 〕 ← var
Variables {
i: 1
}
Stack〔 〕 ← ( i println i 1 add (i) var )
Stack〔 ( i println i 1 add (i) var ) 〕 ← (i 2 less)
Stack〔 ( i println i 1 add (i) var ) | (i 2 less) 〕 ← while
Stack〔 〕 ← i
Stack〔 1 〕 ← 2
Stack〔 1 | 2 〕 ← less
Stack〔 true 〕
Stack〔 〕 ← i
Stack〔 1 〕 ← println
[Output]: 1
Stack〔 〕 ← i
Stack〔 1 〕 ← 1
Stack〔 1 | 1 〕 ← add
Stack〔 2 〕 ← (i)
Stack〔 2 | (i) 〕 ← var
Variables {
i: 2
}
Stack〔 〕
Stack〔 〕 ← i
Stack〔 2 〕 ← 2
Stack〔 2 | 2 〕 ← less
Stack〔 false 〕
Stack〔 〕 ← (Finished)
Stack〔 (Finished) 〕 ← println
[Output]: Finished
String
List
List is way to use plural value together. In this chapter, Let you know how to processing list in Stack.
Get
This command is to get list value of specified index.
Example: In this code, print the "Hello, world!" message.
[(a) (b) (c)] 1 get
Result:
Stack〔 〕 ← [(a) (b) (c)]
Stack〔 〕 ← (a)
Stack〔 (a) 〕 ← (b)
Stack〔 (a) | (b) 〕 ← (c)
Stack〔 (a) | (b) | (c) 〕
Stack〔 [(a) (b) (c)] 〕 ← 1
Stack〔 [(a) (b) (c)] | 1 〕 ← get
Stack〔 (b) 〕
Set
This command is to sex list value of specified index.
Example: In this code, print the "Hello, world!" message.
[(a) (b) (c)] 1 (B) set
Result:
Stack〔 〕 ← [(a) (b) (c)]
Stack〔 〕 ← (a)
Stack〔 (a) 〕 ← (b)
Stack〔 (a) | (b) 〕 ← (c)
Stack〔 (a) | (b) | (c) 〕
Stack〔 [(a) (b) (c)] 〕 ← 1
Stack〔 [(a) (b) (c)] | 1 〕 ← (B)
Stack〔 [(a) (b) (c)] | 1 | (B) 〕 ← set
Stack〔 [(a) (B) (c)] 〕