Commit 14788f27d05097373b35d12031e2787e88c1a2d2
- Diff rendering mode:
- inline
- side by side
doc/introduction_guide.md
(103 / 0)
|   | |||
| 1 | Stackd Programming Language Guide | ||
| 2 | ========== | ||
| 3 | |||
| 4 | 1. Defining Words | ||
| 5 | ---------- | ||
| 6 | |||
| 7 | To define a new word (similar to functions or procedures in applicative languages), | ||
| 8 | there are different ways to do so. | ||
| 9 | For normal words (not generic method implementations), use the colon syntax: | ||
| 10 | |||
| 11 | : <wordname> <stackeffect> | ||
| 12 | <body> ; | ||
| 13 | |||
| 14 | The stackeffect declaration has no implications on the actual usage of the word. It | ||
| 15 | is simply used for documentation - and in case of generic words, to find the apropriate | ||
| 16 | generic method implementation. | ||
| 17 | |||
| 18 | A short example is the `sq` word, which squares a number: | ||
| 19 | |||
| 20 | : sq ( x -- x*x ) | ||
| 21 | dup * ; | ||
| 22 | |||
| 23 | Here, `sq` takes one argument from the data stack (called `x` in the stackeffect declaration), | ||
| 24 | `dup`licates it, meaning, that the value is twice on the stack, and multiplies them (`*`), putting | ||
| 25 | the square back onto the stack. | ||
| 26 | Here's a simple diagramm to illustrate this process when calling `sq`. Everything within brackets resembles the values | ||
| 27 | on the stack with the most right value being the top of stack element: | ||
| 28 | |||
| 29 | Example call: `5 sq ;` | ||
| 30 | |||
| 31 | Literal/Word Stackvalues Comment | ||
| 32 | --------------------------------------------------- | ||
| 33 | 5 [ 5 ] (before calling sq) | ||
| 34 | dup [ 5 5 ] (in sq) | ||
| 35 | * [ 25 ] (in sq) | ||
| 36 | |||
| 37 | For more examples take a look at the code within the files in the core/ or examples/ directories. | ||
| 38 | |||
| 39 | |||
| 40 | 2. Defining Tuples | ||
| 41 | ---------- | ||
| 42 | |||
| 43 | With Tuples one can define custom data types and operations on them (via generic words). They contain | ||
| 44 | so called slots, which are data fields. In contrast to most object-oriented programming languages, | ||
| 45 | the operations on these datatypes are not part of the type definition, but kept indepedently (as, for | ||
| 46 | example generic methods in CLOS - the Common Lisp Object System, are). | ||
| 47 | |||
| 48 | Syntax: | ||
| 49 | tuple: <tuplename> <slotnames> ; | ||
| 50 | |||
| 51 | Example: | ||
| 52 | tuple: person name age sex ; | ||
| 53 | |||
| 54 | To create a new instance of a tuple: | ||
| 55 | person new ; | ||
| 56 | |||
| 57 | Which will create a new instance of the `person` tuple, with all the slots undefined. | ||
| 58 | A common idiom is to define a custom constructor word for a tuple, that can take some arguments | ||
| 59 | to initialize the slots, if needed. By convention, these constructor words are named like the | ||
| 60 | tuple, surrounded by `<` and `>`, e.g.: `<person>`. | ||
| 61 | |||
| 62 | When not differently stated, accessor words (getters and setters) for all slots will be defined. | ||
| 63 | Getter accessors have the following naming scheme: | ||
| 64 | slotname>> | ||
| 65 | Whereas setter accessors are named like this: | ||
| 66 | >>slotname | ||
| 67 | |||
| 68 | For our `person` tuple, the following accessors will be defined automatically by the Stackd runtime: | ||
| 69 | : name>> ( person -- name ) ; | ||
| 70 | : >>name ( person name -- person ) ; | ||
| 71 | : age>> ( person -- age ) ; | ||
| 72 | : >>age ( person age -- person ) ; | ||
| 73 | : sex>> ( person -- sex ) ; | ||
| 74 | : >>sex ( person sex -- person ) ; | ||
| 75 | |||
| 76 | Note, that all setters keep the person instance on the stack, when finished, whereas getter accessors | ||
| 77 | don't. This is meant to make instance initialization easier and makes more sense, since you usually | ||
| 78 | want to keep on doing something with the tuple instance, after altering a slot value. | ||
| 79 | |||
| 80 | Here's a complete overview of our `person` tuple example: | ||
| 81 | |||
| 82 | tuple: person name age sex ; | ||
| 83 | |||
| 84 | : <person> ( -- person ) | ||
| 85 | person new ; | ||
| 86 | |||
| 87 | ! example word with using a person instance: | ||
| 88 | : say-hello ( person -- ) | ||
| 89 | "hello, " print- | ||
| 90 | name>> print- | ||
| 91 | ", how are you today?" print ; | ||
| 92 | |||
| 93 | ! example usage: | ||
| 94 | ! this will create a new instance of person, | ||
| 95 | ! and call say-hello, which will then output: | ||
| 96 | ! "hello, Christopher Bertels, how are you today?" | ||
| 97 | <person> | ||
| 98 | >>name "Christopher Bertels" | ||
| 99 | >>age 22 | ||
| 100 | >>sex "male" | ||
| 101 | say-hello ; | ||
| 102 | |||
| 103 | # _more coming soon_ # |

