Feat: documentation, UI/UX
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
# About Forth
|
||||
|
||||
Forth is a _stack-based_ programming language created by Charles H. Moore in the early 1970s. It was designed with simplicity, directness, and interactive exploration in mind. Forth has been used for scientific work and embedded systems: it controlled telescopes and even ran on hardware aboard space missions. It evolved into many implementations targeting various architectures, but none of them really caught on. Nonetheless, the ideas behind Forth continue to attract people from very different, often unrelated fields. Today, Forth languages are used by hackers and artists for their unconventional nature. Forth is simple, direct, and beautiful to implement. Forth is an elegant, minimal language, easy to understand, extend, and tailor to a specific task. The Forth we use in Cagire is specialized in making live music. It is used as a DSL: a _Domain Specific Language_.
|
||||
Forth is a _stack-based_ programming language created by Charles H. Moore in the early 1970s. It was designed with simplicity, directness, and interactive exploration in mind. Forth has been used for scientific work and embedded systems: it controlled telescopes and even ran on hardware aboard space missions. It evolved into many implementations targeting various architectures, but none of them really caught on. Nonetheless, the ideas behind Forth continue to attract people from very different, often unrelated fields. Today, Forth languages are used by hackers and artists for their unconventional nature. Forth is simple, direct, and beautiful to implement. Forth is an elegant, minimal language, easy to understand, extend, and tailor to a specific task. The Forth we use in Cagire is specialized in making live music. It is used as a DSL: a _Domain Specific Language_.
|
||||
|
||||
**TLDR:** Forth is a really nice language to play music with.
|
||||
|
||||
## Why Forth?
|
||||
|
||||
Most programming languages rely on a complex syntax of `variables`, `expressions` and `statements` like `x = 3 + 4` or `do_something(()=>bob(4))`. Forth works differently. It has almost no syntax at all. Instead, you push values onto a `stack` and apply `words` that transform them:
|
||||
|
||||
```forth
|
||||
3 4 +
|
||||
3 4 + print
|
||||
```
|
||||
|
||||
The program above leaves the number `7` on the stack. There are no variables, no parentheses, no syntax to remember. You just end up with words and numbers separated by spaces. For live coding music, this directness is quite exciting. All you do is think in terms of transformations and add things to the stack: take a note, shift it up, add reverb, play it.
|
||||
@@ -20,6 +22,7 @@ The stack is where values live. When you type a number, it goes on the stack. Wh
|
||||
3 ;; stack: 3
|
||||
4 ;; stack: 3 4
|
||||
+ ;; stack: 7
|
||||
print
|
||||
```
|
||||
|
||||
The stack is `last-in, first-out`. The most recent value is always on top. This means that it's often better to read Forth programs from right to left, bottom to top.
|
||||
@@ -38,7 +41,7 @@ Words compose naturally on the stack. To double a number:
|
||||
|
||||
```forth
|
||||
;; 3 3 +
|
||||
3 dup +
|
||||
3 dup + print
|
||||
```
|
||||
|
||||
Forth has a large vocabulary, so Cagire includes a `Dictionary` directly in the application. You can also create your own words. They will work just like existing words. The only difference is that these words will not be included in the dictionary. There are good reasons to create new words on-the-fly:
|
||||
@@ -58,13 +61,22 @@ Four basic types of values can live on the stack:
|
||||
|
||||
Floats can omit the leading zero: `.25` is the same as `0.25`, and `-.5` is `-0.5`.
|
||||
|
||||
Parentheses are ignored by the parser. You can use them freely for visual grouping without affecting execution:
|
||||
Parentheses are used to "quote" a section of a program. The code inside does not run immediately — it is pushed onto the stack as a value. A quotation only runs when a consuming word decides to execute it. This is how conditionals and loops work:
|
||||
|
||||
```forth
|
||||
(c4 note) (0.5 gain) "sine" s .
|
||||
( 60 note 0.3 verb ) 1 ?
|
||||
```
|
||||
|
||||
Quotations are special. They let you pass code around as a value. This is how conditionals and loops work. Don't worry about them for now — you'll learn how to use them later.
|
||||
Here `?` pops the quotation and the condition. The code inside runs only when the condition is truthy. Words like `?`, `!?`, `times`, `cycle`, `choose`, `ifelse`, `every`, `chance`, and `apply` all consume quotations this way.
|
||||
|
||||
Because parentheses defer execution, wrapping code in `( ... )` without a consuming word means it never runs. Quotations are transparent to sound and parameter words — they stay on the stack untouched. This is a useful trick for temporarily disabling part of a step:
|
||||
|
||||
```forth
|
||||
( 0.5 gain ) ;; this quotation is ignored
|
||||
"kick" sound
|
||||
0.3 decay
|
||||
.
|
||||
```
|
||||
|
||||
Any word that is not recognized as a built-in or a user definition becomes a string on the stack. This means `kick s` and `"kick" s` are equivalent. You only need quotes when the string contains spaces or when it conflicts with an existing word name.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user