Merge branch 'master' of git://factorcode.org/git/factor

db4
Doug Coleman 2008-12-01 17:29:25 -06:00
commit 4188d41a02
8 changed files with 71 additions and 24 deletions

View File

@ -43,13 +43,10 @@ Compilation will yield an executable named 'factor' on Unix,
For X11 support, you need recent development libraries for libc, For X11 support, you need recent development libraries for libc,
Freetype, X11, OpenGL and GLUT. On a Debian-derived Linux distribution Freetype, X11, OpenGL and GLUT. On a Debian-derived Linux distribution
(like Ubuntu), you can use the line (like Ubuntu), you can use the following line to grab everything:
sudo apt-get install libc6-dev libfreetype6-dev libx11-dev glutg3-dev sudo apt-get install libc6-dev libfreetype6-dev libx11-dev glutg3-dev
to grab everything (if you're on a non-debian-derived distro please tell
us what the equivalent command is on there and it can be added).
* Bootstrapping the Factor image * Bootstrapping the Factor image
Once you have compiled the Factor runtime, you must bootstrap the Factor Once you have compiled the Factor runtime, you must bootstrap the Factor

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators kernel sequences splitting system USING: assocs combinators kernel sequences splitting system
vocabs.loader ; vocabs.loader init ;
IN: environment IN: environment
HOOK: os-env os ( key -- value ) HOOK: os-env os ( key -- value )
@ -25,3 +25,8 @@ HOOK: (set-os-envs) os ( seq -- )
{ [ os winnt? ] [ "environment.winnt" require ] } { [ os winnt? ] [ "environment.winnt" require ] }
{ [ os wince? ] [ ] } { [ os wince? ] [ ] }
} cond } cond
[
"FACTOR_ROOTS" os-env os windows? ";" ":" ? split
[ add-vocab-root ] each
] "environment" add-init-hook

View File

@ -266,7 +266,7 @@ $nl
"To run a script, simply pass it as an argument to the Factor executable:" "To run a script, simply pass it as an argument to the Factor executable:"
{ $code "./factor cleanup.factor" } { $code "./factor cleanup.factor" }
"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." "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."
$nl { $heading "Example: ls" }
"Here is an example implementing a simplified version of the Unix " { $snippet "ls" } " command in Factor:" "Here is an example implementing a simplified version of the Unix " { $snippet "ls" } " command in Factor:"
{ $code { $code
<" USING: command-line namespaces io io.files io.files.listing <" USING: command-line namespaces io io.files io.files.listing
@ -282,6 +282,36 @@ command-line get [
} }
"You can put it in a file named " { $snippet "ls.factor" } ", and then run it, to list the " { $snippet "/usr/bin" } " directory for example:" "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" } { $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
regexp command-line namespaces ;
IN: grep
: grep-lines ( pattern -- )
'[ dup _ matches? [ print ] [ drop ] if ] each-line ;
: grep-file ( pattern filename -- )
ascii [ grep-lines ] with-file-reader ;
: grep-usage ( -- )
"Usage: factor grep.factor <pattern> [<file>...]" print ;
command-line get [
grep-usage
] [
unclip <regexp> swap [
grep-lines
] [
[ grep-file ] with each
] 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:" "It is also possible to make executable scripts. A Factor file can begin with a comment like the following:"
{ $code "#! /usr/bin/env factor" } { $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" } "." "If the text file is made executable, then it can be run, assuming the " { $snippet "factor" } " binary is in your " { $snippet "$PATH" } "."
@ -291,6 +321,7 @@ $nl
{ } { }
"cli" "cli"
"cookbook-application" "cookbook-application"
"images"
} ; } ;
ARTICLE: "cookbook-philosophy" "Factor philosophy" ARTICLE: "cookbook-philosophy" "Factor philosophy"
@ -344,15 +375,6 @@ ARTICLE: "cookbook-pitfalls" "Pitfalls to avoid"
{ "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." } { "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." }
} ; } ;
ARTICLE: "cookbook-images" "Image file cookbook"
"Factor has the ability to save the entire state of the system into an " { $emphasis "image file" } "."
$nl
"You can save a custom image if you find yourself loading the same libraries in every Factor session; some libraries take a little while to compile, so saving an image with those libraries loaded can save you a lot of time."
$nl
"For example, to save an image with the web framework loaded,"
{ $code "USE: furnace" "save" }
"See " { $link "images" } " for details." ;
ARTICLE: "cookbook-next" "Next steps" ARTICLE: "cookbook-next" "Next steps"
"Once you have read through " { $link "first-program" } " and " { $link "cookbook" } ", the best way to keep learning Factor is to start looking at some simple example programs. Here are a few particularly nice vocabularies which should keep you busy for a little while:" "Once you have read through " { $link "first-program" } " and " { $link "cookbook" } ", the best way to keep learning Factor is to start looking at some simple example programs. Here are a few particularly nice vocabularies which should keep you busy for a little while:"
{ $list { $list
@ -377,7 +399,6 @@ ARTICLE: "cookbook" "Factor cookbook"
{ $subsection "cookbook-application" } { $subsection "cookbook-application" }
{ $subsection "cookbook-scripts" } { $subsection "cookbook-scripts" }
{ $subsection "cookbook-compiler" } { $subsection "cookbook-compiler" }
{ $subsection "cookbook-images" }
{ $subsection "cookbook-philosophy" } { $subsection "cookbook-philosophy" }
{ $subsection "cookbook-pitfalls" } { $subsection "cookbook-pitfalls" }
{ $subsection "cookbook-next" } ; { $subsection "cookbook-next" } ;

View File

@ -13,6 +13,8 @@ $nl
{ $code "\"resource:work\" \"palindrome\" scaffold-vocab" } { $code "\"resource:work\" \"palindrome\" scaffold-vocab" }
"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:" "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:"
{ $code "\"work\" resource-path ." } { $code "\"work\" resource-path ." }
"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."
$nl
"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." "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."
$nl $nl
"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:" "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:"

View File

@ -253,6 +253,10 @@ HELP: lines
{ $values { "stream" "an input stream" } { "seq" "a sequence of strings" } } { $values { "stream" "an input stream" } { "seq" "a sequence of strings" } }
{ $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ; { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
HELP: each-line
{ $values { "quot" { $quotation "( str -- )" } } }
{ $description "Calls the quotatin with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
HELP: contents HELP: contents
{ $values { "stream" "an input stream" } { "str" string } } { $values { "stream" "an input stream" } { "str" string } }
{ $description "Reads the entire contents of a stream into a string." } { $description "Reads the entire contents of a stream into a string." }
@ -364,6 +368,8 @@ ARTICLE: "stream-utils" "Stream utilities"
$nl $nl
"First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":" "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
{ $subsection stream-print } { $subsection stream-print }
"Processing lines one by one:"
{ $subsection each-line }
"Sluring an entire stream into memory all at once:" "Sluring an entire stream into memory all at once:"
{ $subsection lines } { $subsection lines }
{ $subsection contents } { $subsection contents }

View File

@ -99,6 +99,9 @@ SYMBOL: error-stream
: lines ( stream -- seq ) : lines ( stream -- seq )
[ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ; [ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ;
: each-line ( quot -- )
[ [ readln dup ] ] dip [ drop ] while ; inline
: contents ( stream -- str ) : contents ( stream -- str )
[ [
[ 65536 read dup ] [ ] [ drop ] produce concat f like [ 65536 read dup ] [ ] [ drop ] produce concat f like

View File

@ -68,14 +68,19 @@ HELP: count-instances
} } ; } } ;
ARTICLE: "images" "Images" ARTICLE: "images" "Images"
"The current image can be saved; the image contains a complete dump of all data and code in the current Factor instance:" "Factor has the ability to save the entire state of the system into an " { $emphasis "image file" } ". The image contains a complete dump of all data and code in the current Factor instance."
{ $subsection save } { $subsection save }
{ $subsection save-image } { $subsection save-image }
{ $subsection save-image-and-exit } { $subsection save-image-and-exit }
"To start Factor with a custom image, use the " { $snippet "-i=" { $emphasis "image" } } " command line switch; see " { $link "runtime-cli-args" } "." "To start Factor with a custom image, use the " { $snippet "-i=" { $emphasis "image" } } " command line switch; see " { $link "runtime-cli-args" } "."
$nl $nl
"One reason to save a custom image is if you find yourself loading the same libraries in every Factor session; some libraries take a little while to compile, so saving an image with those libraries loaded can save you a lot of time."
$nl
"For example, to save an image with the web framework loaded,"
{ $code "USE: furnace" "save" }
"New images can be created from scratch:" "New images can be created from scratch:"
{ $subsection "bootstrap.image" } { $subsection "bootstrap.image" }
{ $see-also "tools.memory" "tools.deploy" } ; "The " { $link "tools.deploy" } " tool creates stripped-down images containing just enough code to run a single application."
{ $see-also "tools.memory" } ;
ABOUT: "images" ABOUT: "images"

View File

@ -2,6 +2,18 @@ USING: vocabs vocabs.loader.private help.markup help.syntax
words strings io ; words strings io ;
IN: vocabs.loader IN: vocabs.loader
ARTICLE: "add-vocab-roots" "Working with code outside of the Factor source tree"
"You can work with code outside of the Factor source tree by adding additional directories to the list of vocabulary roots."
$nl
"There are three ways of doing this."
$nl
"The first way is to use an environment variable. Factor looks at the " { $snippet "FACTOR_ROOTS" } " environment variable for a list of " { $snippet ":" } "-separated paths (on Unix) or a list of " { $snippet ";" } "-separated paths (on Windows)."
$nl
"The second way is to create a configuration file. You can list additional vocabulary roots in a file that Factor reads at startup:"
{ $subsection "factor-roots" }
"Finally, you can add vocabulary roots dynamically using a word:"
{ $subsection add-vocab-root } ;
ARTICLE: "vocabs.roots" "Vocabulary roots" ARTICLE: "vocabs.roots" "Vocabulary roots"
"The vocabulary loader searches for it in one of the root directories:" "The vocabulary loader searches for it in one of the root directories:"
{ $subsection vocab-roots } { $subsection vocab-roots }
@ -12,12 +24,8 @@ ARTICLE: "vocabs.roots" "Vocabulary roots"
{ { $snippet "extra" } " - additional contributed libraries." } { { $snippet "extra" } " - additional contributed libraries." }
{ { $snippet "work" } " - a root for vocabularies which are not intended to be contributed back to Factor." } { { $snippet "work" } " - a root for vocabularies which are not intended to be contributed back to Factor." }
} }
"You can store your own vocabularies in the " { $snippet "work" } " directory. You can also store code outside of the Factor source tree by making Factor aware of it first. There are two ways of doing this." "You can store your own vocabularies in the " { $snippet "work" } " directory."
$nl { $subsection "add-vocab-roots" } ;
"You can list additional vocabulary roots in a file that Factor reads at startup:"
{ $subsection "factor-roots" }
"Or you can add them dynamically using a word:"
{ $subsection add-vocab-root } ;
ARTICLE: "vocabs.loader" "Vocabulary loader" ARTICLE: "vocabs.loader" "Vocabulary loader"
"The vocabulary loader is defined in the " { $vocab-link "vocabs.loader" } " vocabulary." "The vocabulary loader is defined in the " { $vocab-link "vocabs.loader" } " vocabulary."