Day 2: Painting the Fence
Today, we will move our exploration of Factor from the Listener to source files. We’ll learn how to define words, organize them into modules called vocabularies, run them as standalone programs, and test them with unit tests.
Defining Words
We’ve been using Factor words that are defined in the library. Let’s see how we can define our own words. A word definition starts with a colon, a space, and the name of the word. Then comes the stack effect, the code for the word, and finally a space and a semicolon. For example:
| : add-42 ( x -- y ) 42 + ; |
That defines a word which adds 42 to the number that’s on the stack. The ( x -- y ) part is the stack effect: the number of values that the word takes from the stack and pushes ...
Get Seven More Languages in Seven Weeks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.