]> gitweb.factorcode.org Git - factor.git/blob - basis/help/tutorial/tutorial.factor
Update documentation for stricter vocabulary search path semantics
[factor.git] / basis / help / tutorial / tutorial.factor
1 USING: help.markup help.syntax ui.commands ui.operations
2 editors vocabs.loader kernel sequences prettyprint tools.test
3 vocabs.refresh strings unicode.categories unicode.case
4 ui.tools.browser ui.tools.common ;
5 IN: help.tutorial
6
7 ARTICLE: "first-program-start" "Creating a vocabulary for your first program"
8 "Factor source code is organized into " { $link "vocabularies" } ". Before we can write our first program, we must create a vocabulary for it."
9 $nl
10 "Start by loading the scaffold tool:"
11 { $code "USE: tools.scaffold" }
12 "Then, ask the scaffold tool to create a new vocabulary named " { $snippet "palindrome" } ":"
13 { $code "\"resource:work\" \"palindrome\" scaffold-vocab" }
14 "If you look at the output, you will see that a few files were created in your “work” directory. The following phrase will print the full path of your work directory:"
15 { $code "\"work\" resource-path ." }
16 "The work directory is one of several " { $link "vocabs.roots" } " where Factor searches for vocabularies. It is possible to define new vocabulary roots; see " { $link "add-vocab-roots" } ". To keep things simple in this tutorial, we'll just use the work directory, though."
17 $nl
18 "Open the work directory in your file manager, and open the subdirectory named " { $snippet "palindrome" } ". Inside this subdirectory you will see a file named " { $snippet "palindrome.factor" } ". We will be editing this file."
19 $nl
20 "Notice that the file ends with an " { $link POSTPONE: IN: } " form telling Factor that all definitions in this source file should go into the " { $snippet "palindrome" } " vocabulary using the " { $link POSTPONE: IN: } " word:"
21 { $code "IN: palindrome" }
22 "We will add new definitions after the " { $link POSTPONE: IN: } " form."
23 $nl
24 "You are now ready to go on to the next section: " { $link "first-program-logic" } "." ;
25
26 ARTICLE: "first-program-logic" "Writing some logic in your first program"
27 "Your " { $snippet "palindrome.factor" } " file should look like the following after the previous section:"
28 { $code
29     "! Copyright (C) 2008 <your name here>"
30     "! See http://factorcode.org/license.txt for BSD license."
31     "IN: palindrome"
32 }
33 "We will now write our first word using " { $link POSTPONE: : } ". This word will test if a string is a palindrome; it will take a string as input, and give back a boolean as output. We will call this word " { $snippet "palindrome?" } ", following a naming convention that words returning booleans have names ending with " { $snippet "?" } "."
34 $nl
35 "Recall that a string is a palindrome if it is spelled the same forwards or backwards; that is, if the string is equal to its reverse. We can express this in Factor as follows:"
36 { $code ": palindrome? ( string -- ? ) dup reverse = ;" }
37 "Place this definition at the end of your source file."
38 $nl
39 "Now we have changed the source file, we must reload it into Factor so that we can test the new definition. To do this, simply go to the Factor listener and press " { $command tool "common" refresh-all } ". This will find any previously-loaded source files which have changed on disk, and reload them."
40 $nl
41 "When you do this, you will get an error about the " { $link dup } " word not being found. This is because this word is part of the " { $vocab-link "kernel" } " vocabulary, but this vocabulary is not part of the source file's " { $link "word-search" } ". You must explicitly list dependencies in source files. This allows Factor to automatically load required vocabularies and makes larger programs easier to maintain."
42 $nl
43 "To add the word to the search path, first convince yourself that this word is in the " { $vocab-link "kernel" } " vocabulary. Enter " { $snippet "dup" } " in the listener's input area, and press " { $operation com-browse } ". This will open the documentation browser tool, viewing the help for the " { $link dup } " word. One of the subheadings in the help article will mention the word's vocabulary."
44 $nl
45 "So now, add the following at the start of the source file:"
46 { $code "USING: kernel ;" }
47 "Next, find out what vocabulary " { $link reverse } " lives in; type the word name " { $snippet "reverse" } " in the listener's input area, and press " { $operation com-browse } "."
48 $nl
49 "It lives in the " { $vocab-link "sequences" } " vocabulary, so we add that to the search path:"
50 { $code "USING: kernel sequences ;" }
51 "Finally, check what vocabulary " { $link = } " lives in, and confirm that it's in the " { $vocab-link "kernel" } " vocabulary, which we've already added to the search path."
52 $nl
53 "Now press " { $command tool "common" refresh-all } " again, and the source file should reload without any errors. You can now go on and learn about " { $link "first-program-test" } "." ;
54
55 ARTICLE: "first-program-test" "Testing your first program"
56 "Your " { $snippet "palindrome.factor" } " file should look like the following after the previous section:"
57 { $code
58     "! Copyright (C) 2008 <your name here>"
59     "! See http://factorcode.org/license.txt for BSD license."
60     "IN: palindrome"
61     "USING: kernel sequences ;"
62     ""
63     ": palindrome? ( str -- ? ) dup reverse = ;"
64 }
65 "We will now test our new word in the listener. First we have add the palindrome vocabulary to the listener's vocabulary search path:"
66 { $code "USE: palindrome"}
67 "Next, push a string on the stack:"
68 { $code "\"hello\"" }
69 "Note that the stack display in the listener now shows this string. Having supplied the input, we call our word:"
70 { $code "palindrome?" }
71 "The stack display should now have a boolean false - " { $link f } " - which is the word's output. Since “hello” is not a palindrome, this is what we expect. We can get rid of this boolean by calling " { $link drop } ". The stack should be empty after this is done."
72 $nl
73 "Now, let's try it with a palindrome; we will push the string and call the word in the same line of code:"
74 { $code "\"racecar\" palindrome?" }
75 "The stack should now contain a boolean true - " { $link t } ". We can print it and drop it using the " { $link . } " word:"
76 { $code "." }
77 "What we just did is called " { $emphasis "interactive testing" } ". A more advanced technique which comes into play with larger programs is " { $link "tools.test" } "."
78 $nl
79 "Create a test harness file using the scaffold tool:"
80 { $code "\"palindrome\" scaffold-tests" }
81 "Now, open the file named " { $snippet "palindrome-tests.factor" } "; it is located in the same directory as " { $snippet "palindrome.factor" } ", and it was created by the scaffold tool."
82 $nl
83 "We will add some unit tests, which are similar to the interactive tests we did above. Unit tests are defined with the " { $link POSTPONE: unit-test } " word, which takes a sequence of expected outputs, and a piece of code. It runs the code, and asserts that it outputs the expected values."
84 $nl
85 "Add the following three lines to " { $snippet "palindrome-tests.factor" } ":"
86 { $code
87     "USING: palindrome tools.test ;"
88     "[ f ] [ \"hello\" palindrome? ] unit-test"
89     "[ t ] [ \"racecar\" palindrome? ] unit-test"
90 }
91 "Now, you can run unit tests:"
92 { $code "\"palindrome\" test" }
93 "It should report that all tests have passed. Now you can read about " { $link "first-program-extend" } "." ;
94
95 ARTICLE: "first-program-extend" "Extending your first program"
96 "Our palindrome program works well, however we'd like to extend it to ignore spaces and non-alphabetical characters in the input."
97 $nl
98 "For example, we'd like it to identify the following as a palindrome:"
99 { $code "\"A man, a plan, a canal: Panama.\"" }
100 "However, right now, the simplistic algorithm we use says this is not a palindrome:"
101 { $unchecked-example "\"A man, a plan, a canal: Panama.\" palindrome?" "f" }
102 "We would like it to output " { $link t } " there. We can encode this requirement with a unit test that we add to " { $snippet "palindrome-tests.factor" } ":"
103 { $code "[ t ] [ \"A man, a plan, a canal: Panama.\" palindrome? ] unit-test" }
104 "If you now run unit tests, you will see a unit test failure:"
105 { $code "\"palindrome\" test" }
106 "The next step is to, of course, fix our code so that the unit test can pass."
107 $nl
108 "We begin by writing a word called " { $snippet "normalize" } " which removes blanks and non-alphabetical characters from a string, and then converts the string to lower case. We call this word " { $snippet "normalize" } ". To figure out how to write this word, we begin with some interactive experimentation in the listener."
109 $nl
110 "Start by pushing a character on the stack; notice that characters are really just integers:"
111 { $code "CHAR: a" }
112 "Now, use the " { $link Letter? } " word to test if it is an alphabetical character, upper or lower case:"
113 { $unchecked-example "Letter? ." "t" }
114 "This gives the expected result."
115 $nl
116 "Now try with a non-alphabetical character:"
117 { $code "CHAR: #" }
118 { $unchecked-example "Letter? ." "f" }
119 "What we want to do is given a string, remove all characters which do not match the " { $link Letter? } " predicate. Let's push a string on the stack:"
120 { $code "\"A man, a plan, a canal: Panama.\"" }
121 "Now, place a quotation containing " { $link Letter? } " on the stack; quoting code places it on the stack instead of executing it immediately:"
122 { $code "[ Letter? ]" }
123 "Finally, pass the string and the quotation to the " { $link filter } " word:"
124 { $code "filter" }
125 "Now the stack should contain the following string:"
126 { "\"AmanaplanacanalPanama\"" }
127 "This is almost what we want; we just need to convert the string to lower case now. This can be done by calling " { $link >lower } "; the " { $snippet ">" } " prefix is a naming convention for conversion operations, and should be read as “to”:"
128 { $code ">lower" }
129 "Finally, let's print the top of the stack and discard it:"
130 { $code "." }
131 "This will output " { $snippet "amanaplanacanalpanama" } ". This string is in the form that we want, and we evaluated the following code to get it into this form:"
132 { $code "[ Letter? ] filter >lower" }
133 "This code starts with a string on the stack, removes non-alphabetical characters, and converts the result to lower case, leaving a new string on the stack. We put this code in a new word, and add the new word to " { $snippet "palindrome.factor" } ":"
134 { $code ": normalize ( str -- newstr ) [ Letter? ] filter >lower ;" }
135 "You will need to add " { $vocab-link "unicode.case" } " and " { $vocab-link "unicode.categories" } " to the vocabulary search path, so that " { $link Letter? } " can be used in the source file."
136 $nl
137 "We modify " { $snippet "palindrome?" } " to first apply " { $snippet "normalize" } " to its input:"
138 { $code ": palindrome? ( str -- ? ) normalize dup reverse = ;" }
139 "Factor compiles the file from the top down. So, be sure to place the definition for " { $snippet "normalize" } " above the definition for " { $snippet "palindrome?" } "."
140 $nl
141 "Now if you press " { $command tool "common" refresh-all } ", the source file should reload without any errors. You can run unit tests again, and this time, they will all pass:"
142 { $code "\"palindrome\" test" } ;
143
144 ARTICLE: "first-program" "Your first program"
145 "In this tutorial, we will write a simple Factor program which prompts the user to enter a word, and tests if it is a palindrome (that is, the word is spelled the same backwards and forwards)."
146 $nl
147 "In this tutorial, you will learn about basic Factor development tools."
148 { $subsection "first-program-start" }
149 { $subsection "first-program-logic" }
150 { $subsection "first-program-test" }
151 { $subsection "first-program-extend" } ;
152
153 ABOUT: "first-program"