From 404ac5b61a422792bb8bda54da71e180aee26b13 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 26 Jan 2022 10:41:30 -0800 Subject: [PATCH] syntax: adding INITIALIZE: --- basis/bootstrap/syntax.factor | 1 + core/syntax/syntax-docs.factor | 19 +++++++++++++++++++ core/syntax/syntax.factor | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/basis/bootstrap/syntax.factor b/basis/bootstrap/syntax.factor index ba32f887ca..5e62320f07 100644 --- a/basis/bootstrap/syntax.factor +++ b/basis/bootstrap/syntax.factor @@ -44,6 +44,7 @@ IN: bootstrap.syntax "BUILTIN:" "SYMBOL:" "SYMBOLS:" + "INITIALIZE:" "CONSTANT:" "TUPLE:" "final" diff --git a/core/syntax/syntax-docs.factor b/core/syntax/syntax-docs.factor index bbca72ea4f..f98a694cf1 100644 --- a/core/syntax/syntax-docs.factor +++ b/core/syntax/syntax-docs.factor @@ -503,6 +503,25 @@ HELP: SYMBOLS: { $description "Creates a new symbol for every token until the " { $snippet ";" } "." } { $examples { $example "USING: prettyprint ;" "IN: scratchpad" "SYMBOLS: foo bar baz ;\nfoo . bar . baz ." "foo\nbar\nbaz" } } ; +HELP: INITIALIZE: +{ $syntax "INITIALIZE: word ... ;" } +{ $description "If " { $snippet "word" } " does not have a value in the global namespace, calls the definition and assigns the result to " { $snippet "word" } " in the global namespace." } +{ $examples + { $unchecked-example + "USING: math namespaces prettyprint ;" + "SYMBOL: foo" + "INITIALIZE: foo 15 sq ;" + "foo get-global ." + "225" } + { $unchecked-example + "USING: math namespaces prettyprint ;" + "SYMBOL: foo" + "1234 foo set-global" + "INITIALIZE: foo 15 sq ;" + "foo get-global ." + "1234" } +} ; + HELP: SINGLETON: { $syntax "SINGLETON: class" } { $values diff --git a/core/syntax/syntax.factor b/core/syntax/syntax.factor index 7f48f5fc43..99efa805a7 100644 --- a/core/syntax/syntax.factor +++ b/core/syntax/syntax.factor @@ -134,6 +134,10 @@ IN: bootstrap.syntax ";" [ create-word-in [ reset-generic ] [ define-symbol ] bi ] each-token ] define-core-syntax + "INITIALIZE:" [ + scan-word parse-definition [ initialize ] 2curry append! + ] define-core-syntax + "SINGLETONS:" [ ";" [ create-class-in define-singleton-class ] each-token ] define-core-syntax -- 2.34.1