Merge branch 'master' of http://factorcode.org/git/factor
commit
bb364dfe83
|
@ -43,13 +43,10 @@ Compilation will yield an executable named 'factor' on Unix,
|
|||
|
||||
For X11 support, you need recent development libraries for libc,
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
Once you have compiled the Factor runtime, you must bootstrap the Factor
|
||||
|
|
|
@ -201,10 +201,10 @@ M: byte-array byte-length length ;
|
|||
1 swap malloc-array ; inline
|
||||
|
||||
: malloc-byte-array ( byte-array -- alien )
|
||||
dup length dup malloc [ -rot memcpy ] keep ;
|
||||
dup length [ nip malloc dup ] 2keep memcpy ;
|
||||
|
||||
: memory>byte-array ( alien len -- byte-array )
|
||||
dup <byte-array> [ -rot memcpy ] keep ;
|
||||
[ nip <byte-array> dup ] 2keep memcpy ;
|
||||
|
||||
: byte-array>memory ( byte-array base -- )
|
||||
swap dup length memcpy ;
|
||||
|
|
|
@ -38,7 +38,7 @@ C-UNION: barx
|
|||
[ 120 ] [ "barx" heap-size ] unit-test
|
||||
|
||||
"help" vocab [
|
||||
"help" "help" lookup "help" set
|
||||
"print-topic" "help" lookup "help" set
|
||||
[ ] [ \ foox-x "help" get execute ] unit-test
|
||||
[ ] [ \ set-foox-x "help" get execute ] unit-test
|
||||
] when
|
||||
|
|
|
@ -59,9 +59,9 @@ SYMBOL: bootstrap-time
|
|||
"math compiler threads help io tools ui ui.tools unicode handbook" "include" set-global
|
||||
"" "exclude" set-global
|
||||
|
||||
parse-command-line
|
||||
(command-line) parse-command-line
|
||||
|
||||
"-no-crossref" cli-args member? [ do-crossref ] unless
|
||||
do-crossref
|
||||
|
||||
! Set dll paths
|
||||
os wince? [ "windows.ce" require ] when
|
||||
|
@ -92,12 +92,7 @@ SYMBOL: bootstrap-time
|
|||
[
|
||||
boot
|
||||
do-init-hooks
|
||||
[
|
||||
parse-command-line
|
||||
run-user-init
|
||||
"run" get run
|
||||
output-stream get [ stream-flush ] when*
|
||||
] [ print-error 1 exit ] recover
|
||||
handle-command-line
|
||||
] set-boot-quot
|
||||
|
||||
millis swap - bootstrap-time set-global
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
! Copyright (C) 2006, 2007 Slava Pestov
|
||||
! Copyright (C) 2006, 2008 Slava Pestov
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel cocoa cocoa.messages cocoa.classes
|
||||
cocoa.application sequences splitting core-foundation ;
|
||||
|
@ -29,6 +29,6 @@ IN: cocoa.dialogs
|
|||
"/" split1-last [ <NSString> ] bi@ ;
|
||||
|
||||
: save-panel ( path -- paths )
|
||||
<NSSavePanel> dup
|
||||
rot split-path -> runModalForDirectory:file: NSOKButton =
|
||||
[ <NSSavePanel> dup ] dip
|
||||
split-path -> runModalForDirectory:file: NSOKButton =
|
||||
[ -> filename CF>string ] [ drop f ] if ;
|
||||
|
|
|
@ -160,7 +160,7 @@ objc>alien-types get [ swap ] assoc-map
|
|||
assoc-union alien>objc-types set-global
|
||||
|
||||
: objc-struct-type ( i string -- ctype )
|
||||
2dup CHAR: = -rot index-from swap subseq
|
||||
[ CHAR: = ] 2keep index-from swap subseq
|
||||
dup c-types get key? [
|
||||
"Warning: no such C type: " write dup print
|
||||
drop "void*"
|
||||
|
|
|
@ -34,5 +34,6 @@ IN: cocoa.windows
|
|||
dup 0 -> setReleasedWhenClosed: ;
|
||||
|
||||
: window-content-rect ( window -- rect )
|
||||
NSWindow over -> frame rot -> styleMask
|
||||
[ NSWindow ] dip
|
||||
[ -> frame ] [ -> styleMask ] bi
|
||||
-> contentRectForFrameRect:styleMask: ;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
USING: help.markup help.syntax parser vocabs.loader strings ;
|
||||
USING: help.markup help.syntax parser vocabs.loader strings
|
||||
command-line.private ;
|
||||
IN: command-line
|
||||
|
||||
HELP: run-bootstrap-init
|
||||
|
@ -7,7 +8,10 @@ HELP: run-bootstrap-init
|
|||
HELP: run-user-init
|
||||
{ $description "Runs the startup initialization file in the user's home directory, unless the " { $snippet "-no-user-init" } " command line switch was given. This file is named " { $snippet ".factor-rc" } " on Unix and " { $snippet "factor-rc" } " on Windows." } ;
|
||||
|
||||
HELP: cli-param
|
||||
HELP: load-vocab-roots
|
||||
{ $description "Loads the newline-separated list of additional vocabulary roots from the file named " { $snippet ".factor-roots" } " on Unix and " { $snippet "factor-roots" } " on Windows." } ;
|
||||
|
||||
HELP: param
|
||||
{ $values { "param" string } }
|
||||
{ $description "Process a command-line switch."
|
||||
$nl
|
||||
|
@ -17,10 +21,13 @@ $nl
|
|||
$nl
|
||||
"Otherwise, sets the global variable named by the parameter to " { $link t } "." } ;
|
||||
|
||||
HELP: cli-args
|
||||
HELP: (command-line)
|
||||
{ $values { "args" "a sequence of strings" } }
|
||||
{ $description "Outputs the command line parameters which were passed to the Factor VM on startup." } ;
|
||||
|
||||
HELP: command-line
|
||||
{ $var-description "The command line parameters which follow the name of the script on the command line." } ;
|
||||
|
||||
HELP: main-vocab-hook
|
||||
{ $var-description "Global variable holding a quotation which outputs a vocabulary name. UI backends set this so that the UI can automatically start if the prerequisites are met (for example, " { $snippet "$DISPLAY" } " being set on X11)." } ;
|
||||
|
||||
|
@ -35,9 +42,6 @@ HELP: ignore-cli-args?
|
|||
{ $values { "?" "a boolean" } }
|
||||
{ $description "On Mac OS X, source files to run are supplied by the Cocoa API, so to avoid running them twice the startup code has to call this word." } ;
|
||||
|
||||
HELP: parse-command-line
|
||||
{ $description "Called on startup to process command line arguments. This sets global variables with " { $link cli-param } ", runs source files, and evaluates the string given by the " { $snippet "-e" } " switch, if there is one." } ;
|
||||
|
||||
ARTICLE: "runtime-cli-args" "Command line switches for the VM"
|
||||
"A handful of command line switches are processed by the VM and not the library. They control low-level features."
|
||||
{ $table
|
||||
|
@ -64,9 +68,12 @@ ARTICLE: "bootstrap-cli-args" "Command line switches for bootstrap"
|
|||
}
|
||||
"Bootstrap can load various optional components:"
|
||||
{ $table
|
||||
{ { $snippet "math" } "Rational and complex number support." }
|
||||
{ { $snippet "threads" } "Thread support." }
|
||||
{ { $snippet "compiler" } "The compiler." }
|
||||
{ { $snippet "tools" } "Terminal-based developer tools." }
|
||||
{ { $snippet "help" } "The help system." }
|
||||
{ { $snippet "help.handbook" } "The help handbook." }
|
||||
{ { $snippet "ui" } "The graphical user interface." }
|
||||
{ { $snippet "ui.tools" } "Graphical developer tools." }
|
||||
{ { $snippet "io" } "Non-blocking I/O and networking." }
|
||||
|
@ -86,7 +93,6 @@ ARTICLE: "standard-cli-args" "Command line switches for general usage"
|
|||
{ { $snippet "-run=" { $emphasis "vocab" } } { { $snippet { $emphasis "vocab" } } " is the name of a vocabulary with a " { $link POSTPONE: MAIN: } " hook to run on startup, for example " { $vocab-link "listener" } ", " { $vocab-link "ui" } " or " { $vocab-link "none" } "." } }
|
||||
{ { $snippet "-no-user-init" } { "Inhibits the running of user initialization files on startup. See " { $link "rc-files" } "." } }
|
||||
{ { $snippet "-quiet" } { "If set, " { $link run-file } " and " { $link require } " will not print load messages." } }
|
||||
{ { $snippet "-script" } { "Equivalent to " { $snippet "-quiet -run=none" } "." $nl "On Unix systems, Factor can be used for scripting - just create an executable text file whose first line is:" { $code "#! /usr/local/bin/factor -script" } "The space after " { $snippet "#!" } " is necessary because of Factor syntax." } }
|
||||
} ;
|
||||
|
||||
ARTICLE: "factor-boot-rc" "Bootstrap initialization file"
|
||||
|
@ -102,11 +108,18 @@ $nl
|
|||
"A word to run this file from an existing Factor session:"
|
||||
{ $subsection run-user-init } ;
|
||||
|
||||
ARTICLE: "factor-roots" "Additional vocabulary roots file"
|
||||
"The vocabulary roots file is named " { $snippet "factor-roots" } " on Windows and " { $snippet ".factor-roots" } " on Unix. If it exists, it is loaded every time Factor starts. It contains a newline-separated list of " { $link "vocabs.roots" } "."
|
||||
$nl
|
||||
"A word to run this file from an existing Factor session:"
|
||||
{ $subsection load-vocab-roots } ;
|
||||
|
||||
ARTICLE: "rc-files" "Running code on startup"
|
||||
"Factor looks for two files in your home directory."
|
||||
"Factor looks for three optional files in your home directory."
|
||||
{ $subsection "factor-boot-rc" }
|
||||
{ $subsection "factor-rc" }
|
||||
"The " { $snippet "-no-user-init" } " command line switch will inhibit the running of these files."
|
||||
{ $subsection "factor-roots" }
|
||||
"The " { $snippet "-no-user-init" } " command line switch will inhibit loading running of these files."
|
||||
$nl
|
||||
"If you are unsure where the files should be located, evaluate the following code:"
|
||||
{ $code
|
||||
|
@ -122,8 +135,16 @@ $nl
|
|||
"100 dpi set-global"
|
||||
} ;
|
||||
|
||||
ARTICLE: "cli" "Command line usage"
|
||||
"Zero or more command line arguments may be passed to the Factor runtime. Command line arguments starting with a dash (" { $snippet "-" } ") is interpreted as switches. All other arguments are taken to be file names to be run by " { $link run-file } "."
|
||||
ARTICLE: "cli" "Command line arguments"
|
||||
"Factor command line usage:"
|
||||
{ $code "factor [system switches...] [script args...]" }
|
||||
"Zero or more system switches can be passed in, followed by an optional script file name. If the script file is specified, it will be run on startup, any arguments after the script file are stored in a variable, with no further processing by Factor itself:"
|
||||
{ $subsection command-line }
|
||||
"Instead of running a script, it is also possible to run a vocabulary; this invokes the vocabulary's " { $link POSTPONE: MAIN: } " word:"
|
||||
{ $code "factor [system switches...] -run=<vocab name>" }
|
||||
"If no script file or " { $snippet "-run=" } " switch is specified, Factor will start " { $link "listener" } " or " { $link "ui-tools" } ", depending on the operating system."
|
||||
$nl
|
||||
"As stated above, arguments in the first part of the command line, before the optional script name, are interpreted by to the Factor system. These arguments all start with a dash (" { $snippet "-" } ")."
|
||||
$nl
|
||||
"Switches can take one of the following three forms:"
|
||||
{ $list
|
||||
|
@ -134,9 +155,9 @@ $nl
|
|||
{ $subsection "runtime-cli-args" }
|
||||
{ $subsection "bootstrap-cli-args" }
|
||||
{ $subsection "standard-cli-args" }
|
||||
"The list of command line arguments can be obtained and inspected directly:"
|
||||
{ $subsection cli-args }
|
||||
"There is a way to override the default vocabulary to run on startup:"
|
||||
"The raw list of command line arguments can also be obtained and inspected directly:"
|
||||
{ $subsection (command-line) }
|
||||
"There is a way to override the default vocabulary to run on startup, if no script name or " { $snippet "-run" } " switch is specified:"
|
||||
{ $subsection main-vocab-hook } ;
|
||||
|
||||
ABOUT: "cli"
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
USING: namespaces tools.test kernel command-line ;
|
||||
IN: command-line.tests
|
||||
|
||||
[
|
||||
[ f ] [ "-no-user-init" cli-arg ] unit-test
|
||||
[ f ] [ "user-init" get ] unit-test
|
||||
|
||||
[ f ] [ "-user-init" cli-arg ] unit-test
|
||||
[ t ] [ "user-init" get ] unit-test
|
||||
|
||||
[ "sdl.factor" ] [ "sdl.factor" cli-arg ] unit-test
|
||||
] with-scope
|
|
@ -1,10 +1,15 @@
|
|||
! Copyright (C) 2003, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: init continuations debugger hashtables io kernel
|
||||
kernel.private namespaces parser sequences strings system
|
||||
splitting io.files eval ;
|
||||
USING: init continuations debugger hashtables io
|
||||
io.encodings.utf8 io.files kernel kernel.private namespaces
|
||||
parser sequences strings system splitting eval vocabs.loader ;
|
||||
IN: command-line
|
||||
|
||||
SYMBOL: script
|
||||
SYMBOL: command-line
|
||||
|
||||
: (command-line) ( -- args ) 10 getenv sift ;
|
||||
|
||||
: rc-path ( name -- path )
|
||||
os windows? [ "." prepend ] unless
|
||||
home prepend-path ;
|
||||
|
@ -19,17 +24,33 @@ IN: command-line
|
|||
"factor-rc" rc-path ?run-file
|
||||
] when ;
|
||||
|
||||
: cli-var-param ( name value -- ) swap set-global ;
|
||||
: load-vocab-roots ( -- )
|
||||
"user-init" get [
|
||||
"factor-roots" rc-path dup exists? [
|
||||
utf8 file-lines [ add-vocab-root ] each
|
||||
] [ drop ] if
|
||||
] when ;
|
||||
|
||||
: cli-bool-param ( name -- ) "no-" ?head not cli-var-param ;
|
||||
<PRIVATE
|
||||
|
||||
: cli-param ( param -- )
|
||||
"=" split1 [ cli-var-param ] [ cli-bool-param ] if* ;
|
||||
: var-param ( name value -- ) swap set-global ;
|
||||
|
||||
: cli-arg ( argument -- argument )
|
||||
"-" ?head [ cli-param f ] when ;
|
||||
: bool-param ( name -- ) "no-" ?head not var-param ;
|
||||
|
||||
: cli-args ( -- args ) 10 getenv ;
|
||||
: param ( param -- )
|
||||
"=" split1 [ var-param ] [ bool-param ] if* ;
|
||||
|
||||
: run-script ( file -- )
|
||||
t "quiet" set-global run-file ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: parse-command-line ( args -- )
|
||||
[ command-line off script off ] [
|
||||
unclip "-" ?head
|
||||
[ param parse-command-line ]
|
||||
[ script set command-line set ] if
|
||||
] if-empty ;
|
||||
|
||||
SYMBOL: main-vocab-hook
|
||||
|
||||
|
@ -53,14 +74,17 @@ SYMBOL: main-vocab-hook
|
|||
: ignore-cli-args? ( -- ? )
|
||||
os macosx? "run" get "ui" = and ;
|
||||
|
||||
: script-mode ( -- )
|
||||
t "quiet" set-global
|
||||
"none" "run" set-global ;
|
||||
: script-mode ( -- ) ;
|
||||
|
||||
: parse-command-line ( -- )
|
||||
cli-args [ cli-arg ] filter
|
||||
"script" get [ script-mode ] when
|
||||
ignore-cli-args? [ drop ] [ [ run-file ] each ] if
|
||||
"e" get [ eval ] when* ;
|
||||
: handle-command-line ( -- )
|
||||
[
|
||||
(command-line) parse-command-line
|
||||
load-vocab-roots
|
||||
run-user-init
|
||||
"e" get [ eval ] when*
|
||||
ignore-cli-args? not script get and
|
||||
[ run-script ] [ "run" get run ] if*
|
||||
output-stream get [ stream-flush ] when*
|
||||
] [ print-error 1 exit ] recover ;
|
||||
|
||||
[ default-cli-args ] "command-line" add-init-hook
|
||||
|
|
|
@ -4,7 +4,7 @@ USING: alien alien.c-types alien.strings alien.syntax kernel
|
|||
math sequences namespaces make assocs init accessors
|
||||
continuations combinators core-foundation
|
||||
core-foundation.run-loop core-foundation.run-loop.thread
|
||||
io.encodings.utf8 destructors ;
|
||||
io.encodings.utf8 destructors locals arrays ;
|
||||
IN: core-foundation.fsevents
|
||||
|
||||
: kFSEventStreamCreateFlagUseCFTypes 2 ; inline
|
||||
|
@ -105,15 +105,14 @@ FUNCTION: CFStringRef FSEventStreamCopyDescription ( FSEventStreamRef streamRef
|
|||
"FSEventStreamContext" <c-object>
|
||||
[ set-FSEventStreamContext-info ] keep ;
|
||||
|
||||
: <FSEventStream> ( callback info paths latency flags -- event-stream )
|
||||
>r >r >r >r >r
|
||||
:: <FSEventStream> ( callback info paths latency flags -- event-stream )
|
||||
f ! allocator
|
||||
r> ! callback
|
||||
r> make-FSEventStreamContext
|
||||
r> <CFStringArray> ! paths
|
||||
callback
|
||||
info make-FSEventStreamContext
|
||||
paths <CFStringArray>
|
||||
FSEventStreamEventIdSinceNow ! sinceWhen
|
||||
r> ! latency
|
||||
r> ! flags
|
||||
latency
|
||||
flags
|
||||
FSEventStreamCreate ;
|
||||
|
||||
: kCFRunLoopCommonModes ( -- string )
|
||||
|
@ -161,13 +160,11 @@ SYMBOL: event-stream-callbacks
|
|||
: remove-event-source-callback ( id -- )
|
||||
event-stream-callbacks get delete-at ;
|
||||
|
||||
: >event-triple ( n eventPaths eventFlags eventIds -- triple )
|
||||
[
|
||||
>r >r >r dup dup
|
||||
r> void*-nth utf8 alien>string ,
|
||||
r> int-nth ,
|
||||
r> longlong-nth ,
|
||||
] { } make ;
|
||||
:: >event-triple ( n eventPaths eventFlags eventIds -- triple )
|
||||
n eventPaths void*-nth utf8 alien>string
|
||||
n eventFlags int-nth
|
||||
n eventIds longlong-nth
|
||||
3array ;
|
||||
|
||||
: master-event-source-callback ( -- alien )
|
||||
"void"
|
||||
|
|
|
@ -307,7 +307,7 @@ FUNCTION: bool check_sse2 ( ) ;
|
|||
: sse2? ( -- ? )
|
||||
check_sse2 ;
|
||||
|
||||
"-no-sse2" cli-args member? [
|
||||
"-no-sse2" (command-line) member? [
|
||||
[ optimized-recompile-hook ] recompile-hook
|
||||
[ { check_sse2 } compile ] with-variable
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ M: tuple-class group-words
|
|||
|
||||
: define-consult ( group class quot -- )
|
||||
[ register-protocol ]
|
||||
[ rot group-words -rot [ consult-method ] 2curry each ]
|
||||
[ [ group-words ] 2dip [ consult-method ] 2curry each ]
|
||||
3bi ;
|
||||
|
||||
: CONSULT:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2008 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: assocs combinators kernel sequences splitting system
|
||||
vocabs.loader ;
|
||||
vocabs.loader init ;
|
||||
IN: environment
|
||||
|
||||
HOOK: os-env os ( key -- value )
|
||||
|
@ -25,3 +25,8 @@ HOOK: (set-os-envs) os ( seq -- )
|
|||
{ [ os winnt? ] [ "environment.winnt" require ] }
|
||||
{ [ os wince? ] [ ] }
|
||||
} cond
|
||||
|
||||
[
|
||||
"FACTOR_ROOTS" os-env os windows? ";" ":" ? split
|
||||
[ add-vocab-root ] each
|
||||
] "environment" add-init-hook
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
USING: help.markup help.syntax io kernel math namespaces parser
|
||||
prettyprint sequences vocabs.loader namespaces stack-checker
|
||||
help ;
|
||||
help command-line multiline ;
|
||||
IN: help.cookbook
|
||||
|
||||
ARTICLE: "cookbook-syntax" "Basic syntax cookbook"
|
||||
|
@ -263,15 +263,65 @@ ARTICLE: "cookbook-application" "Application cookbook"
|
|||
ARTICLE: "cookbook-scripts" "Scripting cookbook"
|
||||
"Factor can be used for command-line scripting on Unix-like systems."
|
||||
$nl
|
||||
"A text file can begin with a comment like the following, and made executable:"
|
||||
{ $code "#! /usr/bin/env factor -script" }
|
||||
"Running the text file will run it through Factor, assuming the " { $snippet "factor" } " binary is in your " { $snippet "$PATH" } "."
|
||||
"To run a script, simply pass it as an argument to the Factor executable:"
|
||||
{ $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."
|
||||
{ $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 io.files.listing
|
||||
sequences kernel ;
|
||||
|
||||
command-line get [
|
||||
current-directory get directory.
|
||||
] [
|
||||
dup length 1 = [ first directory. ] [
|
||||
[ [ nl write ":" print ] [ directory. ] bi ] each
|
||||
] if
|
||||
] 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
|
||||
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:"
|
||||
{ $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. The " { $snippet "-script" } " switch suppresses compiler messages, and exits Factor when the script finishes."
|
||||
"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"
|
||||
"cookbook-application"
|
||||
"images"
|
||||
} ;
|
||||
|
||||
ARTICLE: "cookbook-philosophy" "Factor philosophy"
|
||||
|
@ -325,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." }
|
||||
} ;
|
||||
|
||||
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"
|
||||
"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
|
||||
|
@ -358,7 +399,6 @@ ARTICLE: "cookbook" "Factor cookbook"
|
|||
{ $subsection "cookbook-application" }
|
||||
{ $subsection "cookbook-scripts" }
|
||||
{ $subsection "cookbook-compiler" }
|
||||
{ $subsection "cookbook-images" }
|
||||
{ $subsection "cookbook-philosophy" }
|
||||
{ $subsection "cookbook-pitfalls" }
|
||||
{ $subsection "cookbook-next" } ;
|
||||
|
|
|
@ -13,6 +13,8 @@ $nl
|
|||
{ $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:"
|
||||
{ $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."
|
||||
$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:"
|
||||
|
|
|
@ -49,10 +49,8 @@ SYMBOL: +editable+
|
|||
] [ keys ] if ;
|
||||
|
||||
: describe* ( obj mirror keys -- )
|
||||
rot summary.
|
||||
[
|
||||
drop
|
||||
] [
|
||||
[ summary. ] 2dip
|
||||
[ drop ] [
|
||||
dup enum? [ +sequence+ on ] when
|
||||
standard-table-style [
|
||||
swap [ -rot describe-row ] curry each-index
|
||||
|
|
|
@ -19,7 +19,7 @@ TUPLE: select-mx < mx read-fdset write-fdset ;
|
|||
FD_SETSIZE 8 * <bit-array> >>write-fdset ;
|
||||
|
||||
: clear-nth ( n seq -- ? )
|
||||
[ nth ] [ f -rot set-nth ] 2bi ;
|
||||
[ nth ] [ [ f ] 2dip set-nth ] 2bi ;
|
||||
|
||||
:: check-fd ( fd fdset mx quot -- )
|
||||
fd munge fdset clear-nth [ fd mx quot call ] when ; inline
|
||||
|
|
|
@ -114,7 +114,7 @@ SYMBOL: receive-buffer
|
|||
] call ;
|
||||
|
||||
M: unix (receive) ( datagram -- packet sockaddr )
|
||||
dup do-receive dup [ rot drop ] [
|
||||
dup do-receive dup [ [ drop ] 2dip ] [
|
||||
2drop [ +input+ wait-for-port ] [ (receive) ] bi
|
||||
] if ;
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ M: complex imaginary-part imaginary>> ;
|
|||
M: complex absq >rect [ sq ] bi@ + ;
|
||||
|
||||
: 2>rect ( x y -- xr yr xi yi )
|
||||
[ [ real-part ] bi@ ] 2keep
|
||||
[ imaginary-part ] bi@ ; inline
|
||||
[ [ real-part ] bi@ ]
|
||||
[ [ imaginary-part ] bi@ ] 2bi ; inline
|
||||
|
||||
M: complex hashcode*
|
||||
nip >rect [ hashcode ] bi@ bitxor ;
|
||||
|
@ -28,21 +28,21 @@ M: complex equal?
|
|||
M: complex number=
|
||||
2>rect number= [ number= ] [ 2drop f ] if ;
|
||||
|
||||
: *re ( x y -- xr*yr xi*ri ) 2>rect * >r * r> ; inline
|
||||
: *im ( x y -- xi*yr xr*yi ) 2>rect >r * swap r> * ; inline
|
||||
: *re ( x y -- xr*yr xi*ri ) 2>rect [ * ] 2bi@ ; inline
|
||||
: *im ( x y -- xi*yr xr*yi ) 2>rect [ * swap ] dip * ; inline
|
||||
|
||||
M: complex + 2>rect + >r + r> (rect>) ;
|
||||
M: complex - 2>rect - >r - r> (rect>) ;
|
||||
M: complex * 2dup *re - -rot *im + (rect>) ;
|
||||
M: complex + 2>rect [ + ] 2bi@ (rect>) ;
|
||||
M: complex - 2>rect [ - ] 2bi@ (rect>) ;
|
||||
M: complex * [ *re - ] [ *im + ] 2bi (rect>) ;
|
||||
|
||||
: complex/ ( x y -- r i m )
|
||||
dup absq >r 2dup *re + -rot *im - r> ; inline
|
||||
[ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
|
||||
|
||||
M: complex / complex/ tuck / >r / r> (rect>) ;
|
||||
M: complex / complex/ tuck [ / ] 2bi@ (rect>) ;
|
||||
|
||||
M: complex abs absq >float fsqrt ;
|
||||
|
||||
M: complex sqrt >polar swap fsqrt swap 2.0 / polar> ;
|
||||
M: complex sqrt >polar [ fsqrt ] [ 2.0 / ] bi* polar> ;
|
||||
|
||||
IN: syntax
|
||||
|
||||
|
|
|
@ -92,16 +92,6 @@ PRIVATE>
|
|||
: 0^ ( x -- z )
|
||||
dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: ^ ( x y -- z )
|
||||
{
|
||||
{ [ over zero? ] [ nip 0^ ] }
|
||||
{ [ dup integer? ] [ integer^ ] }
|
||||
{ [ 2dup real^? ] [ fpow ] }
|
||||
[ ^complex ]
|
||||
} cond ; inline
|
||||
|
||||
: (^mod) ( n x y -- z )
|
||||
1 swap [
|
||||
[ dupd * pick mod ] when [ sq over mod ] dip
|
||||
|
@ -114,6 +104,16 @@ PRIVATE>
|
|||
swap [ /mod [ over * swapd - ] dip ] keep (gcd)
|
||||
] if ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: ^ ( x y -- z )
|
||||
{
|
||||
{ [ over zero? ] [ nip 0^ ] }
|
||||
{ [ dup integer? ] [ integer^ ] }
|
||||
{ [ 2dup real^? ] [ fpow ] }
|
||||
[ ^complex ]
|
||||
} cond ; inline
|
||||
|
||||
: gcd ( x y -- a d )
|
||||
[ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
|
||||
|
||||
|
@ -177,9 +177,9 @@ M: complex log >polar swap flog swap rect> ;
|
|||
GENERIC: cos ( x -- y ) foldable
|
||||
|
||||
M: complex cos
|
||||
>float-rect 2dup
|
||||
fcosh swap fcos * -rot
|
||||
fsinh swap fsin neg * rect> ;
|
||||
>float-rect
|
||||
[ [ fcos ] [ fcosh ] bi* * ]
|
||||
[ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
|
||||
|
||||
M: real cos fcos ;
|
||||
|
||||
|
@ -188,9 +188,9 @@ M: real cos fcos ;
|
|||
GENERIC: cosh ( x -- y ) foldable
|
||||
|
||||
M: complex cosh
|
||||
>float-rect 2dup
|
||||
fcos swap fcosh * -rot
|
||||
fsin swap fsinh * rect> ;
|
||||
>float-rect
|
||||
[ [ fcosh ] [ fcos ] bi* * ]
|
||||
[ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
|
||||
|
||||
M: real cosh fcosh ;
|
||||
|
||||
|
@ -199,9 +199,9 @@ M: real cosh fcosh ;
|
|||
GENERIC: sin ( x -- y ) foldable
|
||||
|
||||
M: complex sin
|
||||
>float-rect 2dup
|
||||
fcosh swap fsin * -rot
|
||||
fsinh swap fcos * rect> ;
|
||||
>float-rect
|
||||
[ [ fsin ] [ fcosh ] bi* * ]
|
||||
[ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
|
||||
|
||||
M: real sin fsin ;
|
||||
|
||||
|
@ -210,9 +210,9 @@ M: real sin fsin ;
|
|||
GENERIC: sinh ( x -- y ) foldable
|
||||
|
||||
M: complex sinh
|
||||
>float-rect 2dup
|
||||
fcos swap fsinh * -rot
|
||||
fsin swap fcosh * rect> ;
|
||||
>float-rect
|
||||
[ [ fsinh ] [ fcos ] bi* * ]
|
||||
[ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
|
||||
|
||||
M: real sinh fsinh ;
|
||||
|
||||
|
|
|
@ -22,9 +22,9 @@ INSTANCE: range immutable-sequence
|
|||
|
||||
: twiddle 2dup > -1 1 ? ; inline
|
||||
|
||||
: (a, dup roll + -rot ; inline
|
||||
: (a, dup [ + ] curry 2dip ; inline
|
||||
|
||||
: ,b) dup neg rot + swap ; inline
|
||||
: ,b) dup [ - ] curry dip ; inline
|
||||
|
||||
: [a,b] ( a b -- range ) twiddle <range> ; inline
|
||||
|
||||
|
|
|
@ -54,7 +54,6 @@ TR: convert-separators "/\\" ".." ;
|
|||
[ monitor-thread ] "Vocabulary monitor" spawn drop ;
|
||||
|
||||
[
|
||||
"-no-monitors" cli-args member? [
|
||||
start-monitor-thread
|
||||
] unless
|
||||
"-no-monitors" (command-line) member?
|
||||
[ start-monitor-thread ] unless
|
||||
] "tools.vocabs.monitor" add-init-hook
|
||||
|
|
|
@ -110,8 +110,8 @@ M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
|
|||
swap [ swapd set-at ] curry assoc-each ;
|
||||
|
||||
: assoc-union ( assoc1 assoc2 -- union )
|
||||
2dup [ assoc-size ] bi@ + pick new-assoc
|
||||
[ rot update ] keep [ swap update ] keep ;
|
||||
[ [ [ assoc-size ] bi@ + ] [ drop ] 2bi new-assoc ] 2keep
|
||||
[ dupd update ] bi@ ;
|
||||
|
||||
: assoc-combine ( seq -- union )
|
||||
H{ } clone [ dupd update ] reduce ;
|
||||
|
|
|
@ -23,7 +23,7 @@ PREDICATE: intersection-class < class
|
|||
M: intersection-class update-class define-intersection-predicate ;
|
||||
|
||||
: define-intersection-class ( class participants -- )
|
||||
[ f f rot intersection-class define-class ]
|
||||
[ [ f f ] dip intersection-class define-class ]
|
||||
[ drop update-classes ]
|
||||
2bi ;
|
||||
|
||||
|
|
|
@ -248,7 +248,9 @@ M: tuple-class update-class
|
|||
3bi ;
|
||||
|
||||
: tuple-class-unchanged? ( class superclass slots -- ? )
|
||||
rot tuck [ superclass = ] [ "slots" word-prop = ] 2bi* and ;
|
||||
[ over ] dip
|
||||
[ [ superclass ] dip = ]
|
||||
[ [ "slots" word-prop ] dip = ] 2bi* and ;
|
||||
|
||||
: valid-superclass? ( class -- ? )
|
||||
[ tuple-class? ] [ tuple eq? ] bi or ;
|
||||
|
|
|
@ -253,6 +253,10 @@ HELP: lines
|
|||
{ $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." } ;
|
||||
|
||||
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
|
||||
{ $values { "stream" "an input stream" } { "str" string } }
|
||||
{ $description "Reads the entire contents of a stream into a string." }
|
||||
|
@ -364,6 +368,8 @@ ARTICLE: "stream-utils" "Stream utilities"
|
|||
$nl
|
||||
"First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
|
||||
{ $subsection stream-print }
|
||||
"Processing lines one by one:"
|
||||
{ $subsection each-line }
|
||||
"Sluring an entire stream into memory all at once:"
|
||||
{ $subsection lines }
|
||||
{ $subsection contents }
|
||||
|
|
|
@ -99,6 +99,9 @@ SYMBOL: error-stream
|
|||
: lines ( stream -- seq )
|
||||
[ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ;
|
||||
|
||||
: each-line ( quot -- )
|
||||
[ [ readln dup ] ] dip [ drop ] while ; inline
|
||||
|
||||
: contents ( stream -- str )
|
||||
[
|
||||
[ 65536 read dup ] [ ] [ drop ] produce concat f like
|
||||
|
|
|
@ -68,14 +68,19 @@ HELP: count-instances
|
|||
} } ;
|
||||
|
||||
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-image }
|
||||
{ $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" } "."
|
||||
$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:"
|
||||
{ $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"
|
||||
|
|
|
@ -2,6 +2,18 @@ USING: vocabs vocabs.loader.private help.markup help.syntax
|
|||
words strings io ;
|
||||
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"
|
||||
"The vocabulary loader searches for it in one of the root directories:"
|
||||
{ $subsection vocab-roots }
|
||||
|
@ -12,12 +24,8 @@ ARTICLE: "vocabs.roots" "Vocabulary roots"
|
|||
{ { $snippet "extra" } " - additional contributed libraries." }
|
||||
{ { $snippet "work" } " - a root for vocabularies which are not intended to be contributed back to Factor." }
|
||||
}
|
||||
"Your own vocabularies should go into " { $snippet "extra" } " or " { $snippet "work" } ", depending on whether or not you intend to contribute them back to the Factor project. If you wish to work on vocabularies outside of the Factor source directory, create a " { $link "factor-boot-rc" } " file like the following:"
|
||||
{ $code
|
||||
"USING: namespaces sequences vocabs.loader ;"
|
||||
"\"/home/jane/sources/\" vocab-roots get push"
|
||||
}
|
||||
"See " { $link "rc-files" } " for details." ;
|
||||
"You can store your own vocabularies in the " { $snippet "work" } " directory."
|
||||
{ $subsection "add-vocab-roots" } ;
|
||||
|
||||
ARTICLE: "vocabs.loader" "Vocabulary loader"
|
||||
"The vocabulary loader is defined in the " { $vocab-link "vocabs.loader" } " vocabulary."
|
||||
|
@ -57,6 +65,11 @@ HELP: vocab-main
|
|||
HELP: vocab-roots
|
||||
{ $var-description "A sequence of pathname strings to search for vocabularies." } ;
|
||||
|
||||
HELP: add-vocab-root
|
||||
{ $values { "path" "a pathname string" } }
|
||||
{ $description "Adds a directory pathname to the list of vocabulary roots." }
|
||||
{ $see-also "factor-roots" } ;
|
||||
|
||||
HELP: find-vocab-root
|
||||
{ $values { "vocab" "a vocabulary specifier" } { "path/f" "a pathname string" } }
|
||||
{ $description "Searches for a vocabulary in the vocabulary roots." } ;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
USING: namespaces make sequences io.files kernel assocs words
|
||||
vocabs definitions parser continuations io hashtables sorting
|
||||
source-files arrays combinators strings system math.parser
|
||||
compiler.errors splitting init accessors ;
|
||||
compiler.errors splitting init accessors sets ;
|
||||
IN: vocabs.loader
|
||||
|
||||
SYMBOL: vocab-roots
|
||||
|
@ -15,6 +15,9 @@ V{
|
|||
"resource:work"
|
||||
} clone vocab-roots set-global
|
||||
|
||||
: add-vocab-root ( root -- )
|
||||
vocab-roots get adjoin ;
|
||||
|
||||
: vocab-dir ( vocab -- dir )
|
||||
vocab-name { { CHAR: . CHAR: / } } substitute ;
|
||||
|
||||
|
|
|
@ -221,7 +221,7 @@ M: word subwords drop f ;
|
|||
"( gensym )" f <word> ;
|
||||
|
||||
: define-temp ( quot -- word )
|
||||
gensym dup rot define ;
|
||||
[ gensym dup ] dip define ;
|
||||
|
||||
: reveal ( word -- )
|
||||
dup [ name>> ] [ vocabulary>> ] bi dup vocab-words
|
||||
|
|
Loading…
Reference in New Issue