Skip to content

Commit 91fd350

Browse files
committed
updated doc
1 parent 1dc20ff commit 91fd350

23 files changed

Lines changed: 1347 additions & 1610 deletions
Lines changed: 1 addition & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -1,241 +1 @@
1-
Basic Operations
2-
================
3-
4-
Introduction
5-
------------
6-
7-
In stack-based programming with Reverse Polish Notation (RPN), operations are performed by manipulating a stack data structure. RPN expressions are written in postfix notation, where operators follow their operands. This allows for efficient calculations without the need for parentheses or precedence rules.
8-
9-
Mathematical Operations
10-
------------------------
11-
12-
**Addition (+)**
13-
~~~~~~~~~~~~~~~~
14-
15-
The `+` operator adds two numbers by popping them from the the stack, performing the addition operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
16-
17-
Example:
18-
19-
.. code-block:: text
20-
21-
5 3 + => 8.0
22-
23-
**Subtraction (-)**
24-
~~~~~~~~~~~~~~~~~~~
25-
26-
The `-` operator subtracts the first value on the stack from the second value by popping them from the stack, performing the subtraction operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
27-
28-
Example:
29-
30-
.. code-block:: text
31-
32-
5 3 - => 2.0
33-
34-
**Multiplication (*)**
35-
~~~~~~~~~~~~~~~~~~~~~~~
36-
37-
The `*` operator multiplies two numbers by popping them from the the stack, performing the multiplication operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
38-
39-
Example:
40-
41-
.. code-block:: text
42-
43-
5 3 * => 15.0
44-
45-
**Division (/)**
46-
~~~~~~~~~~~~~~~~
47-
48-
The `/` operator divides the second value on the stack by the first value by popping them from the stack, performing the division operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
49-
50-
Example:
51-
52-
.. code-block:: text
53-
54-
11 5 / => 2
55-
56-
**Modulus (%)**
57-
~~~~~~~~~~~~~~~~
58-
59-
The `%` operator calculates the remainder of dividing the second value on the stack by the first value by popping them from the stack, performing the modulus operation, and pushing the result back onto the stack. This operation is only supported for integer numbers.
60-
61-
Example:
62-
63-
.. code-block:: text
64-
65-
10 3 % => 1
66-
67-
**Exponential (pow)**
68-
~~~~~~~~~~~~~~~~~~~~~
69-
70-
The `pow` operator raises the second value on the stack to the power of the first value by popping them from the stack, performing the exponentiation operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
71-
72-
Example:
73-
74-
.. code-block:: text
75-
76-
2 3 pow => 8.0
77-
78-
**Square Root (sqrt)**
79-
~~~~~~~~~~~~~~~~~~~~~~
80-
81-
The `sqrt` compute the square root of the first value in the stack and pushes the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
82-
83-
Example:
84-
85-
.. code-block:: text
86-
87-
9 sqrt => 3.00000
88-
89-
Boolean Operations
90-
-------------------
91-
92-
**Logical AND (and)**
93-
~~~~~~~~~~~~~~~~~~~~~
94-
95-
The `and` operator performs a logical AND operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical AND operation, and pushes the result back onto the stack.
96-
97-
Example:
98-
99-
.. code-block:: text
100-
101-
true true and => true
102-
true false and => false
103-
104-
**Logical OR (or)**
105-
~~~~~~~~~~~~~~~~~~~~
106-
107-
The `or` operator performs a logical OR operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical OR operation, and pushes the result back onto the stack.
108-
109-
Example:
110-
111-
.. code-block:: text
112-
113-
true false or => true
114-
false false or => false
115-
116-
**Logical XOR (xor)**
117-
~~~~~~~~~~~~~~~~~~~~~~
118-
119-
The `xor` operator performs a logical XOR operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical XOR operation, and pushes the result back onto the stack.
120-
121-
Example:
122-
123-
.. code-block:: text
124-
125-
true true xor => false
126-
true false xor => true
127-
128-
**Logical NOT (not)**
129-
~~~~~~~~~~~~~~~~~~~~~~
130-
131-
The `not` operator performs a logical NOT operation on the first boolean value on the stack. It pops the value from the stack, performs the logical NOT operation, and pushes the result back onto the stack.
132-
133-
Example:
134-
135-
.. code-block:: text
136-
137-
true not => false
138-
false not => true
139-
140-
Comparison Operations
141-
----------------------
142-
143-
**Equal (==)**
144-
~~~~~~~~~~~~~~~
145-
146-
The `==` operator compares the first two values on the stack for equality. It pops two values from the stack, compares them, and pushes `true` if they are equal, otherwise pushes `false`.
147-
148-
Example:
149-
150-
.. code-block:: text
151-
152-
5 5 == => true
153-
5 6 == => false
154-
155-
**Not Equal (!=)**
156-
~~~~~~~~~~~~~~~~~~~
157-
158-
The `!=` operator compares the first two values on the stack for inequality. It pops the two values from the stack, compares them, and pushes `true` if they are not equal, otherwise pushes `false`.
159-
160-
Example:
161-
162-
.. code-block:: text
163-
164-
5 5 != => false
165-
5 6 != => true
166-
167-
**Less Than (<)**
168-
~~~~~~~~~~~~~~~~~~
169-
170-
The `<` operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes `true` if the second value is greater than the first, otherwise pushes `false`.
171-
172-
Example:
173-
174-
.. code-block:: text
175-
176-
5 6 < => true
177-
6 5 < => false
178-
5 5 < => false
179-
180-
**Greater Than (>)**
181-
~~~~~~~~~~~~~~~~~~~~~
182-
183-
The `>` operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes `true` if the first value is greater than the second, otherwise pushes `false`.
184-
185-
Example:
186-
187-
.. code-block:: text
188-
189-
5 6 > => false
190-
6 5 > => true
191-
5 5 > => false
192-
193-
**Less Than or Equal To (<=)**
194-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195-
196-
The `<=` operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes `true` if the second value is greater than or equal to the first, otherwise pushes `false`.
197-
198-
Example:
199-
200-
.. code-block:: text
201-
202-
5 6 <= => true
203-
6 5 <= => false
204-
5 5 <= => true
205-
206-
**Greater Than or Equal To (>=)**
207-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208-
209-
The `>=` operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes `true` if the first value is greater than or equal to the second, otherwise pushes `false`.
210-
211-
Example:
212-
213-
.. code-block:: text
214-
215-
5 6 >= => false
216-
6 5 >= => true
217-
5 5 >= => true
218-
219-
220-
Type Casting
221-
------------
222-
223-
**Integer Casting (int)**
224-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
225-
226-
The `int` operator converts a floating-point number on the stack to an integer by truncating the decimal part. It pops a value from the stack, performs the conversion, and pushes the integer result back onto the stack.
227-
228-
Example:
229-
230-
.. code-block:: text
231-
232-
5.7 int => 5
233-
3.14 int => 3
234-
235-
Exit Operation
236-
--------------
237-
238-
**Exit (exit)**
239-
~~~~~~~~~~~~~~~~
240-
241-
The `exit` operation terminates the execution of the program.
1+
.. include:: _sources/basic_operations.rst.txt

docs/_sources/examples.rst.txt

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1 @@
1-
Code Examples
2-
=============
3-
4-
5-
Example 1
6-
---------
7-
8-
We want to define an instruction that fill the stack with a value `value`, many times as going from the value `from` to the value `to`.
9-
We can do it in different ways:
10-
11-
Here we define the instruction an give to it the desired values by defining also them as instructions
12-
13-
.. code-block:: text
14-
15-
[from [value swap(size 1 -) 1 + dup to < swap swap(size 1 -) swap] loop swap(size 1 -) drop] define(fill)
16-
17-
[0] define(from) [10] define(to) [3.1415926535897932] define(value)
18-
19-
fill
20-
21-
Alternatively we assume that the values are in the stack in this order: `from to value`
22-
23-
.. code-block:: text
24-
25-
[[swap(size 1 -) 1 + dup] [< swap swap(size 1 -) swap] swap3 quote compose swap quote swap2 compose compose loop swap(size 1 -) drop] define(fill)
26-
27-
0 10 3.1415926535897932
28-
29-
fill
30-
31-
Here are the steps to convert the first way in the second
32-
33-
.. code-block:: text
34-
35-
from to value [swap(size 1 -) 1 + dup] [< swap swap(size 1 -) swap]
36-
37-
swap3 quote compose => from [< swap swap(size 1 -) swap] value [swap(size 1 -) 1 + dup to]
38-
39-
swap quote swap2 compose => from [value] [swap(size 1 -) 1 + dup to < swap swap(size 1 -) swap]
40-
41-
compose => from [value swap(size 1 -) 1 + dup to < swap swap(size 1 -) swap]
42-
43-
from to value [swap(size 1 -) 1 + dup] [< swap swap(size 1 -) swap] swap3 quote compose swap quote swap2 compose compose loop swap(size 1 -) drop => that's our fill instruction!
44-
45-
[[swap(size 1 -) 1 + dup] [< swap swap(size 1 -) swap] swap3 quote compose swap quote swap2 compose compose loop swap(size 1 -) drop] define(fill)
1+
.. include:: _sources/examples.rst.txt

docs/_sources/index.rst.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
.. stack_script documentation master file, created by
2-
sphinx-quickstart on Fri Apr 19 04:14:57 2024.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
1+
.. stack_script documentation master file, created by sphinx-quickstart
52
63
Welcome to stack_script's documentation!
74
========================================
@@ -12,12 +9,13 @@ Welcome to stack_script's documentation!
129

1310
basic_operations
1411
stack_operations
15-
loop_control
1612
input_output
1713
instruction_definition
14+
loop_control
15+
inner_stack
16+
string_operations
1817
examples
1918

20-
2119
Indices and tables
2220
==================
2321

docs/_sources/inner_stack.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.. include:: _sources/inner_stack.rst.txt

docs/_sources/input_output.rst.txt

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1 @@
1-
Input/Output
2-
------------
3-
4-
**print**
5-
~~~~~~~~~
6-
7-
The `print` operation prints the first value of the stack to be popped without removing it.
8-
9-
Example:
10-
11-
Suppose we have the following instructions on the stack:
12-
13-
.. code-block:: text
14-
15-
42 print
16-
17-
After executing the `print` operation, the value 42 will be printed to the output.
18-
19-
**printall**
20-
~~~~~~~~~~~~
21-
22-
The `printall` operation prints all values on the stack without removing them. It prints the values from top to bottom.
23-
24-
Example:
25-
26-
Suppose we have the following instructions on the stack:
27-
28-
.. code-block:: text
29-
30-
1 2 3 printall
31-
32-
After executing the `printall` operation, the values 1, 2, and 3 will be printed to the output, each separated by a new line.
33-
34-
**save()**
35-
The `save` instruction saves the whole content of the stack in the specified file (in appending mode)
36-
37-
Example:
38-
39-
.. code-block:: text
40-
41-
10 32 3 2 4 3 save(examples/example0.sksp)
42-
43-
44-
**load()**
45-
The `save` instruction read the content of the specified file and execute its content
46-
47-
48-
Example:
49-
50-
.. code-block:: text
51-
52-
load(examples/example0.sksp) => 10 32 3 2 4 3
53-
54-
load(examples/example1.sksp)
55-
mean => 9
1+
.. include:: _sources/input_output.rst.txt

0 commit comments

Comments
 (0)