|
| 1 | +# Introduction |
| 2 | + |
| 3 | +Welcome to Factor! Factor is a *concatenative* language: instead of |
| 4 | +calling functions with parenthesised arguments, you write a sequence of |
| 5 | +words that pass values to each other through a shared **data stack**. |
| 6 | + |
| 7 | +## Comments |
| 8 | + |
| 9 | +A `!` starts a line comment — Factor ignores everything from `!` to the |
| 10 | +end of the line. The examples below use `!` both to annotate what code |
| 11 | +does and to show what it would print. |
| 12 | + |
| 13 | +## The data stack |
| 14 | + |
| 15 | +Writing a literal pushes it onto the top of the stack. Code is read |
| 16 | +left to right. |
| 17 | + |
| 18 | +```factor |
| 19 | +2 3 ! stack (bottom → top): 2 3 |
| 20 | +``` |
| 21 | + |
| 22 | +There is no other way to pass data around — every word reads its |
| 23 | +inputs from the top of the stack and writes its outputs back there. |
| 24 | + |
| 25 | +## Words |
| 26 | + |
| 27 | +A **word** is Factor's name for a function. Calling a word pops some |
| 28 | +values from the top of the stack and pushes some values back. |
| 29 | +`.` pops the top value and prints it; the integer arithmetic words |
| 30 | +`+`, `-`, `*` pop two numbers and push the result: |
| 31 | + |
| 32 | +```factor |
| 33 | +2 3 + . ! prints 5 |
| 34 | +8 3 - . ! prints 5 (8 - 3, not 3 - 8) |
| 35 | +2 3 * . ! prints 6 |
| 36 | +``` |
| 37 | + |
| 38 | +The arithmetic words live in the `math` vocabulary, so a file that uses |
| 39 | +them needs `math` in its `USING:` line. |
| 40 | + |
| 41 | +## Stack effects |
| 42 | + |
| 43 | +Every word is documented with a **stack effect** of the form |
| 44 | +`( inputs -- outputs )`. It is the word's contract: this word pops the |
| 45 | +inputs off the top of the stack and leaves the outputs in their place. |
| 46 | +The names inside are documentation for humans — the stack itself is |
| 47 | +positional, not named. |
| 48 | + |
| 49 | +```factor |
| 50 | +! + is specified as ( x y -- sum ) |
| 51 | +! . is specified as ( x -- ) |
| 52 | +``` |
| 53 | + |
| 54 | +The top of the stack is the *right-hand* input. So `8 3 -` has `3` on |
| 55 | +top, the stack effect is `( x y -- difference )`, and the result is |
| 56 | +`8 - 3`. |
| 57 | + |
| 58 | +A trailing `?` in the outputs is the convention for "a boolean", but |
| 59 | +the lasagna exercise uses only numbers. |
| 60 | + |
| 61 | +## Defining a word |
| 62 | + |
| 63 | +`:` starts a word definition, the stack effect comes next, then the |
| 64 | +body, then `;` ends it. |
| 65 | + |
| 66 | +```factor |
| 67 | +: square ( x -- x^2 ) dup * ; |
| 68 | +
|
| 69 | +4 square . ! => 16 |
| 70 | +``` |
| 71 | + |
| 72 | +Factor's compiler checks that the body actually matches the declared |
| 73 | +stack effect: a word that claims `( x -- y )` but leaves zero or two |
| 74 | +values on the stack will not compile. |
| 75 | + |
| 76 | +## Constants |
| 77 | + |
| 78 | +`CONSTANT:` defines a name for a fixed value. A constant is itself a |
| 79 | +word — calling it pushes the value onto the stack: |
| 80 | + |
| 81 | +```factor |
| 82 | +CONSTANT: pi 3 |
| 83 | +
|
| 84 | +pi pi * . ! => 9 |
| 85 | +``` |
| 86 | + |
| 87 | +`CONSTANT:` is core syntax and does not need a `USING:` line. Place |
| 88 | +constants at the top of the file, before any word that uses them. |
| 89 | + |
| 90 | +## Calling one word from another |
| 91 | + |
| 92 | +A word's body can call any word already in scope, including ones you |
| 93 | +defined earlier in the same file: |
| 94 | + |
| 95 | +```factor |
| 96 | +: double ( x -- 2x ) 2 * ; |
| 97 | +: quadruple ( x -- 4x ) double double ; |
| 98 | +
|
| 99 | +5 quadruple . ! => 20 |
| 100 | +``` |
| 101 | + |
| 102 | +This is how the last task in the exercise reuses an earlier one. |
| 103 | + |
| 104 | +## Swapping the top two values |
| 105 | + |
| 106 | +If two values are on the stack in the wrong order for the next word, |
| 107 | +`swap` flips the top two: |
| 108 | + |
| 109 | +``` |
| 110 | +swap ( x y -- y x ) |
| 111 | +``` |
| 112 | + |
| 113 | +`swap` lives in the `kernel` vocabulary. |
| 114 | + |
| 115 | +## Naming conventions |
| 116 | + |
| 117 | +Words and constants both use `lowercase-kebab-case`: lowercase letters |
| 118 | +joined by hyphens (for example, `expected-bake-time`, |
| 119 | +`preparation-time`). |
0 commit comments