From e1390875caef8d9df4fb04544f71a9b7ce1062cb Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 17 Aug 2011 20:58:09 -0700 Subject: [PATCH 1/2] parser, source-files, syntax: have MAIN: store the main entry point in the source-file record in addition to the vocabulary. Have run-file execute the MAIN: word for a file if present after evaluating its contents. --- basis/command-line/command-line-docs.factor | 2 +- core/parser/parser-docs.factor | 4 ++-- core/parser/parser.factor | 3 ++- core/source-files/source-files.factor | 3 ++- core/syntax/syntax-docs.factor | 2 +- core/syntax/syntax.factor | 8 ++++++-- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/basis/command-line/command-line-docs.factor b/basis/command-line/command-line-docs.factor index 067360530d..264e4e8c8b 100644 --- a/basis/command-line/command-line-docs.factor +++ b/basis/command-line/command-line-docs.factor @@ -135,7 +135,7 @@ $nl ARTICLE: "cli" "Command line arguments" "Factor command line usage:" { $code "factor [VM args...] [script] [args...]" } -"Zero or more VM arguments 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 the following variable, with no further processing by Factor itself:" +"Zero or more VM arguments can be passed in, followed by an optional script file name. If the script file is specified, it will be run on startup using " { $link run-file } ". Any arguments after the script file are stored in the following variable, with no further processing by Factor itself:" { $subsections 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=" } diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor index 24ddc0b7c9..aadd651a01 100644 --- a/core/parser/parser-docs.factor +++ b/core/parser/parser-docs.factor @@ -238,8 +238,8 @@ HELP: parse-file HELP: run-file { $values { "file" "a pathname string" } } -{ $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used." } -{ $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation." } ; +{ $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used. If the source file contains a " { $link POSTPONE: MAIN: } " declaration, the main entry point of the file will be also be executed." } +{ $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation or executing the main entry point." } ; HELP: ?run-file { $values { "path" "a pathname string" } } diff --git a/core/parser/parser.factor b/core/parser/parser.factor index be43979b31..f9883837fb 100644 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -203,7 +203,8 @@ print-use-hook [ [ ] ] initialize ] recover ; : run-file ( file -- ) - parse-file call( -- ) ; + [ parse-file call( -- ) ] + [ source-file main>> [ execute( -- ) ] when* ] bi ; : ?run-file ( path -- ) dup exists? [ run-file ] [ drop ] if ; diff --git a/core/source-files/source-files.factor b/core/source-files/source-files.factor index 120d91bb22..6807e51515 100644 --- a/core/source-files/source-files.factor +++ b/core/source-files/source-files.factor @@ -13,7 +13,8 @@ TUPLE: source-file path top-level-form checksum -definitions ; +definitions +main ; : record-top-level-form ( quot file -- ) top-level-form<< diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index 2afefe1f2b..7fda5b406b 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -858,7 +858,7 @@ HELP: C: HELP: MAIN: { $syntax "MAIN: word" } { $values { "word" word } } -{ $description "Defines the main entry point for the current vocabulary. This word will be executed when this vocabulary is passed to " { $link run } "." } ; +{ $description "Defines the main entry point for the current vocabulary and source file. This word will be executed when this vocabulary is passed to " { $link run } " or the source file is passed to " { $link run-file } "." } ; HELP: " } diff --git a/core/syntax/syntax.factor b/core/syntax/syntax.factor index 07ff0d3c92..864c67d172 100644 --- a/core/syntax/syntax.factor +++ b/core/syntax/syntax.factor @@ -8,7 +8,7 @@ generic.standard generic.hook generic.math generic.parser classes io.pathnames vocabs vocabs.parser classes.parser classes.union classes.intersection classes.mixin classes.predicate classes.singleton classes.tuple.parser compiler.units -combinators effects.parser slots hash-sets ; +combinators effects.parser slots hash-sets source-files ; IN: bootstrap.syntax ! These words are defined as a top-level form, instead of with @@ -233,7 +233,11 @@ IN: bootstrap.syntax "))" parse-effect suffix! ] define-core-syntax - "MAIN:" [ scan-word current-vocab main<< ] define-core-syntax + "MAIN:" [ + scan-word + [ current-vocab main<< ] + [ file get [ main<< ] [ drop ] if* ] bi + ] define-core-syntax "<<" [ [ From d711824c10153612d7c683483ed20a163743118b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 17 Aug 2011 21:29:02 -0700 Subject: [PATCH 2/2] command-line, parser, syntax: move the MAIN: invocation from run-file to run-script to minimize the potential impact on internal machinery that uses run-file --- basis/command-line/command-line-docs.factor | 7 ++++++- basis/command-line/command-line.factor | 8 ++++++-- core/parser/parser-docs.factor | 4 ++-- core/parser/parser.factor | 3 +-- core/syntax/syntax-docs.factor | 4 ++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/basis/command-line/command-line-docs.factor b/basis/command-line/command-line-docs.factor index 264e4e8c8b..db9a6b8e12 100644 --- a/basis/command-line/command-line-docs.factor +++ b/basis/command-line/command-line-docs.factor @@ -135,7 +135,7 @@ $nl ARTICLE: "cli" "Command line arguments" "Factor command line usage:" { $code "factor [VM args...] [script] [args...]" } -"Zero or more VM arguments can be passed in, followed by an optional script file name. If the script file is specified, it will be run on startup using " { $link run-file } ". Any arguments after the script file are stored in the following variable, with no further processing by Factor itself:" +"Zero or more VM arguments can be passed in, followed by an optional script file name. If the script file is specified, it will be run on startup using " { $link run-script } ". Any arguments after the script file are stored in the following variable, with no further processing by Factor itself:" { $subsections 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=" } @@ -159,4 +159,9 @@ $nl "There is a way to override the default vocabulary to run on startup, if no script name or " { $snippet "-run" } " switch is specified:" { $subsections main-vocab-hook } ; +HELP: run-script +{ $values { "file" "a pathname string" } } +{ $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used. If the source file contains a " { $link POSTPONE: MAIN: } " declaration, the main entry point of the file will be also be executed. Loading messages will be suppressed." } +{ $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation or executing the main entry point." } ; + ABOUT: "cli" diff --git a/basis/command-line/command-line.factor b/basis/command-line/command-line.factor index 88ade747d2..e902bbc396 100644 --- a/basis/command-line/command-line.factor +++ b/basis/command-line/command-line.factor @@ -2,7 +2,8 @@ ! See http://factorcode.org/license.txt for BSD license. USING: init continuations hashtables io io.encodings.utf8 io.files io.pathnames kernel kernel.private namespaces parser -sequences strings system splitting vocabs.loader alien.strings ; +sequences source-files strings system splitting vocabs.loader +alien.strings accessors ; IN: command-line SYMBOL: script @@ -39,7 +40,10 @@ SYMBOL: command-line "=" split1 [ var-param ] [ bool-param ] if* ; : run-script ( file -- ) - t "quiet" set-global run-file ; + t "quiet" [ + [ run-file ] + [ source-file main>> [ execute( -- ) ] when* ] bi + ] with-variable ; : parse-command-line ( args -- ) [ command-line off script off ] [ diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor index aadd651a01..24ddc0b7c9 100644 --- a/core/parser/parser-docs.factor +++ b/core/parser/parser-docs.factor @@ -238,8 +238,8 @@ HELP: parse-file HELP: run-file { $values { "file" "a pathname string" } } -{ $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used. If the source file contains a " { $link POSTPONE: MAIN: } " declaration, the main entry point of the file will be also be executed." } -{ $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation or executing the main entry point." } ; +{ $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used." } +{ $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation." } ; HELP: ?run-file { $values { "path" "a pathname string" } } diff --git a/core/parser/parser.factor b/core/parser/parser.factor index f9883837fb..be43979b31 100644 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -203,8 +203,7 @@ print-use-hook [ [ ] ] initialize ] recover ; : run-file ( file -- ) - [ parse-file call( -- ) ] - [ source-file main>> [ execute( -- ) ] when* ] bi ; + parse-file call( -- ) ; : ?run-file ( path -- ) dup exists? [ run-file ] [ drop ] if ; diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index 7fda5b406b..0472d6394d 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -1,7 +1,7 @@ USING: generic help.syntax help.markup kernel math parser words effects classes classes.tuple generic.math generic.single arrays io.pathnames vocabs.loader io sequences assocs words.symbol -words.alias words.constant combinators vocabs.parser ; +words.alias words.constant combinators vocabs.parser command-line ; IN: syntax ARTICLE: "parser-algorithm" "Parser algorithm" @@ -858,7 +858,7 @@ HELP: C: HELP: MAIN: { $syntax "MAIN: word" } { $values { "word" word } } -{ $description "Defines the main entry point for the current vocabulary and source file. This word will be executed when this vocabulary is passed to " { $link run } " or the source file is passed to " { $link run-file } "." } ; +{ $description "Defines the main entry point for the current vocabulary and source file. This word will be executed when this vocabulary is passed to " { $link run } " or the source file is passed to " { $link run-script } "." } ; HELP: " }