]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/help/cookbook/cookbook.factor
add mention of run-file to scripting cookbook
[factor.git] / basis / help / cookbook / cookbook.factor
index ff385f9a65a55af5928a3def203861bc401b84f5..e4b753a961e7203fe9051870e5eb1506cf505cc7 100644 (file)
@@ -1,6 +1,6 @@
 USING: help.markup help.syntax io kernel math parser
 prettyprint sequences vocabs.loader namespaces stack-checker
-help command-line multiline see ;
+help command-line see ;
 IN: help.cookbook
 
 ARTICLE: "cookbook-syntax" "Basic syntax cookbook"
@@ -16,12 +16,12 @@ $nl
 $nl
 "Coming back to the example in the beginning of this article, the following series of steps occurs as the code is evaluated:"
 { $table
-    { "Action" "Stack contents" }
+    { { $strong "Action" } { $strong "Stack contents" } }
     { "10 is pushed on the stack." { $snippet "10" } }
-    { { "The " { $link sq } " word is executed. It pops one input from the stack - the integer 10 - and squares it, pushing the result." } { $snippet "100" } }
+    { { "The " { $link sq } " word is executed. It pops one input from the stack (the integer 10) and squares it, pushing the result." } { $snippet "100" } }
     { { "5 is pushed on the stack." } { $snippet "100 5" } }
-    { { "The " { $link - } " word is executed. It pops two inputs from the stack - the integers 100 and 5 - and subtracts 5 from 100, pushing the result." } { $snippet "95" } }
-    { { "The " { $link . } " word is executed. It pops one input from the stack - the integer 95 - and prints it in the listener's output area." } { } }
+    { { "The " { $link - } " word is executed. It pops two inputs from the stack (the integers 100 and 5) and subtracts 5 from 100, pushing the result." } { $snippet "95" } }
+    { { "The " { $link . } " word is executed. It pops one input from the stack (the integer 95) and prints it in the listener's output area." } { } }
 }
 "Factor supports many other data types:"
 { $code
@@ -29,7 +29,6 @@ $nl
     "\"character strings\""
     "{ 1 2 3 }"
     "! by the way, this is a comment"
-    "#! and so is this"
 }
 { $references
     { "Factor's syntax can be extended, the parser can be called reflectively, and the " { $link . } " word is in fact a general facility for turning almost any object into a form which can be parsed back in again. If this interests you, consult the following sections:" }
@@ -45,7 +44,7 @@ ARTICLE: "cookbook-colon-defs" "Shuffle word and definition cookbook"
 { $code ": sq ( x -- y ) dup * ;" }
 "(You could have looked this up yourself by clicking on the " { $link sq } " word itself.)"
 $nl
-"Note the key elements in a word definition: The colon " { $link POSTPONE: : } " denotes the start of a word definition. The name of the new word must immediately follow. The word definition then continues on until the " { $link POSTPONE: ; } " token signifies the end of the definition. This type of word definition is called a " { $emphasis "compound definition." }
+"Note the key elements in a word definition: The colon " { $link POSTPONE: : } " denotes the start of a word definition. The name of the new word and a stack effect declaration must immediately follow. The word definition then continues on until the " { $link POSTPONE: ; } " token signifies the end of the definition. This type of word definition is called a " { $emphasis "compound definition." }
 $nl
 "Factor is all about code reuse through short and logical colon definitions. Breaking up a problem into small pieces which are easy to test is called " { $emphasis "factoring." }
 $nl
@@ -154,11 +153,11 @@ $nl
 }
 "Note that words must be defined before being referenced. The following is generally invalid:"
 { $code
-    ": frob accelerate particles ;"
-    ": accelerate accelerator on ;"
-    ": particles [ (particles) ] each ;"
+    ": frob ( what -- ) accelerate particles ;"
+    ": accelerate ( -- ) accelerator on ;"
+    ": particles ( what -- ) [ (particles) ] each ;"
 }
-"You would have to place the first definition after the two others for the parser to accept the file."
+"You would have to place the first definition after the two others for the parser to accept the file. If you have a set of mutually recursive words, you can use " { $link POSTPONE: DEFER: } "."
 { $references
     { }
     "word-search"
@@ -191,26 +190,28 @@ ARTICLE: "cookbook-scripts" "Scripting cookbook"
 $nl
 "To run a script, simply pass it as an argument to the Factor executable:"
 { $code "./factor cleanup.factor" }
+"To test a script in the listener, you can use " { $link run-file } "." 
+$nl
 "The script may access command line arguments by inspecting the value of the " { $link command-line } " variable. It can also get its own path from the " { $link script } " variable."
 { $heading "Example: ls" }
 "Here is an example implementing a simplified version of the Unix " { $snippet "ls" } " command in Factor:"
 { $code
-    <" USING: command-line namespaces io io.files
+    "USING: command-line namespaces io io.files
 io.pathnames tools.files sequences kernel ;
 
 command-line get [
-    current-directory get directory.
+    \".\" directory.
 ] [
     dup length 1 = [ first directory. ] [
-        [ [ nl write ":" print ] [ directory. ] bi ] each
+        [ [ nl write \":\" print ] [ directory. ] bi ] each
     ] if
-] if-empty">
+] if-empty"
 }
 "You can put it in a file named " { $snippet "ls.factor" } ", and then run it, to list the " { $snippet "/usr/bin" } " directory for example:"
 { $code "./factor ls.factor /usr/bin" }
 { $heading "Example: grep" }
 "The following is a more complicated example, implementing something like the Unix " { $snippet "grep" } " command:"
-{ $code <" USING: kernel fry io io.files io.encodings.ascii sequences
+{ $code "USING: kernel fry io io.files io.encodings.ascii sequences
 regexp command-line namespaces ;
 IN: grep
 
@@ -221,7 +222,7 @@ IN: grep
     ascii [ grep-lines ] with-file-reader ;
 
 : grep-usage ( -- )
-    "Usage: factor grep.factor <pattern> [<file>...]" print ;
+    \"Usage: factor grep.factor <pattern> [<file>...]\" print ;
 
 command-line get [
     grep-usage
@@ -231,21 +232,19 @@ command-line get [
     ] [
         [ grep-file ] with each
     ] if-empty
-] if-empty"> }
+] if-empty" }
 "You can run it like so,"
 { $code "./factor grep.factor '.*hello.*' myfile.txt" }
 "You'll notice this script takes a while to start. This is because it is loading and compiling the " { $vocab-link "regexp" } " vocabulary every time. To speed up startup, load the vocabulary into your image, and save the image:"
 { $code "USE: regexp" "save" }
 "Now, the " { $snippet "grep.factor" } " script will start up much faster. See " { $link "images" } " for details."
 { $heading "Executable scripts" }
-"It is also possible to make executable scripts. A Factor file can begin with a comment like the following:"
-{ $code "#! /usr/bin/env factor" }
+"It is also possible to make executable scripts. A Factor file can begin with a 'shebang' like the following:"
+{ $code "#!/usr/bin/env factor" }
 "If the text file is made executable, then it can be run, assuming the " { $snippet "factor" } " binary is in your " { $snippet "$PATH" } "."
-$nl
-"The space between " { $snippet "#!" } " and " { $snippet "/usr/bin/env" } " is necessary, since " { $link POSTPONE: #! } " is a parsing word, and a syntax error would otherwise result."
 { $references
     { }
-    "cli"
+    "command-line"
     "cookbook-application"
     "images"
 } ;
@@ -277,7 +276,7 @@ $nl
     "Don't worry about efficiency unless your program is too slow. Don't prefer complex code to simple code just because you feel it will be more efficient. The Factor compiler is designed to make idiomatic code run fast."
     { "None of the above are hard-and-fast rules: there are exceptions to all of them. But one rule unconditionally holds: " { $emphasis "there is always a simpler way" } "." }
 }
-"Factor tries to implement as much of itself as possible, because this improves simplicity and performance. One consequence is that Factor exposes its internals for extension and study. You even have the option of using low-level features not usually found in high-level languages, such manual memory management, pointer arithmetic, and inline assembly code."
+"Factor tries to implement as much of itself as possible, because this improves simplicity and performance. One consequence is that Factor exposes its internals for extension and study. You even have the option of using low-level features not usually found in high-level languages, such as manual memory management, pointer arithmetic, and inline assembly code."
 $nl
 "Unsafe features are tucked away so that you will not invoke them by accident, or have to use them to solve conventional programming problems. However when the need arises, unsafe features are invaluable, for example you might have to do some pointer arithmetic when interfacing directly with C libraries." ;
 
@@ -287,9 +286,9 @@ ARTICLE: "cookbook-pitfalls" "Pitfalls to avoid"
     "Factor only makes use of one native thread, and Factor threads are scheduled co-operatively. C library calls block the entire VM."
     "Factor does not hide anything from the programmer, all internals are exposed. It is your responsibility to avoid writing fragile code which depends too much on implementation detail."
     { "If a literal object appears in a word definition, the object itself is pushed on the stack when the word executes, not a copy. If you intend to mutate this object, you must " { $link clone } " it first. See " { $link "syntax-literals" } "." }
+    { "Also, " { $link dup } " and related shuffle words don't copy entire objects or arrays; they only duplicate the reference to them. If you want to guard an object against mutation, use " { $link clone } "." }
     { "For a discussion of potential issues surrounding the " { $link f } " object, see " { $link "booleans" } "." }
     { "Factor's object system is quite flexible. Careless usage of union, mixin and predicate classes can lead to similar problems to those caused by “multiple inheritance” in other languages. In particular, it is possible to have two classes such that they have a non-empty intersection and yet neither is a subclass of the other. If a generic word defines methods on two such classes, various disambiguation rules are applied to ensure method dispatch remains deterministic, however they may not be what you expect. See " { $link "method-order" } " for details." }
-    { "Be careful when calling words which access variables from a " { $link make-assoc } " which constructs an assoc with arbitrary keys, since those keys might shadow variables." }
     { "If " { $link run-file } " throws a stack depth assertion, it means that the top-level form in the file left behind values on the stack. The stack depth is compared before and after loading a source file, since this type of situation is almost always an error. If you have a legitimate need to load a source file which returns data in some manner, define a word in the source file which produces this data on the stack and call the word after loading the file." }
 } ;
 
@@ -308,15 +307,17 @@ ARTICLE: "cookbook-next" "Next steps"
 
 ARTICLE: "cookbook" "Factor cookbook"
 "The Factor cookbook is a high-level overview of the most important concepts required to program in Factor."
-{ $subsection "cookbook-syntax" }
-{ $subsection "cookbook-colon-defs" }
-{ $subsection "cookbook-combinators" }
-{ $subsection "cookbook-variables" }
-{ $subsection "cookbook-vocabs" }
-{ $subsection "cookbook-application" }
-{ $subsection "cookbook-scripts" }
-{ $subsection "cookbook-philosophy" }
-{ $subsection "cookbook-pitfalls" }
-{ $subsection "cookbook-next" } ;
+{ $subsections
+    "cookbook-syntax"
+    "cookbook-colon-defs"
+    "cookbook-combinators"
+    "cookbook-variables"
+    "cookbook-vocabs"
+    "cookbook-application"
+    "cookbook-scripts"
+    "cookbook-philosophy"
+    "cookbook-pitfalls"
+    "cookbook-next"
+} ;
 
 ABOUT: "cookbook"