blob: ccf9e5dd7e3946715dd7b7731ed03c91d21966b6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# slang
…is a experimental Forth-like stack language implemented in D.
## Example
```
1 i $
§ incr dup @ 1 + swp $ ;
§ withinBounds @ 100 < ;
§ fizz? @ 3 % 0 = ;
§ buzz? @ 5 % 0 = ;
§ fizzbuzz_or_fizz buzz? if fizzbuzz then fizz else . pop ;
§ buzz_or_print dup buzz? if pop buzz then @ else . pop ;
§ branch dup fizz? if fizzbuzz_or_fizz then buzz_or_print else ;
§ loop i withinBounds if i branch i incr loop then else ;
loop
```
This listing implements the common _FizzBuzz_ example in _slang_. It may be executed by compiling the REPL using `dub build` in the project directory and running `./slang example/fizzbuzz.slang`.
## Words
Currently implemented primitives:
| Word | Description |
| --- | --- |
| `§` | Custom word definition |
| `$`, `@` | Single token variable binding, resolution |
| `if`, `then`, `else` | Conditional primitives |
| `+`, `*`, `/`, `%` | Common artithmetics |
| `.` | Non destructive printing of top-of-stack |
| `pop` | Remove uppermost stack element |
| `dup` | Duplicate top-of-stack |
| `swp` | Swap the first two stack elements |
| `ovr` | Place a copy of the second stack element on top-of-stack |
| `rot` | Rotate the top three stack elements |
| `true` | Write true boolean value to top-of-stack |
| `false` | Write false boolean value to top-of-stack |
| `not` | Negate boolean value |
| `and` | Boolean and |
| `or` | Boolean or |
| `<` | Compare size of two integers |
| `=` | Compare equality of two stack values |
| `#` | Debug word printing the whole stack to _stdout_ |
Further words are implemented in `library/base.slang`.
|