]> gitweb.factorcode.org Git - factor.git/commitdiff
interpolate: split out format into a hook main master
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 4 May 2024 03:05:29 +0000 (22:05 -0500)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 4 May 2024 03:05:29 +0000 (22:05 -0500)
basis/formatting/formatting.factor
basis/interpolate/interpolate.factor

index a1ca2c3517a6234836991369618444fa31313298..1800c89a78aac91f5d602c37357608ec905f28eb 100644 (file)
@@ -2,10 +2,10 @@
 ! See https://factorcode.org/license.txt for BSD license
 USING: accessors arrays assocs byte-arrays calendar
 calendar.english calendar.private combinators combinators.smart
-generalizations io io.streams.string kernel math math.functions
-math.parser multiline namespaces peg.ebnf present prettyprint
-quotations sequences sequences.generalizations splitting strings
-unicode ;
+generalizations interpolate.private io io.streams.string kernel
+math math.functions math.parser multiline namespaces peg.ebnf
+present prettyprint quotations sequences
+sequences.generalizations splitting strings unicode ;
 IN: formatting
 
 ERROR: unknown-format-directive value ;
@@ -145,6 +145,12 @@ assocs    = "[%" types ": %" types " %]" => [[ [ second ] [ fourth ] bi '[ [ _ _
 formats   = (types|fmt-%|lists|assocs|unknown)
 ]=]
 
+SINGLETON: printf-formatter
+printf-formatter formatter set-global
+
+M: printf-formatter format
+    [ [ present ] ] [ format-directive ] if-empty ;
+
 EBNF: parse-printf [=[
 formats   = "%"~ <foreign format-directive formats>
 plain-text = [^%]+               => [[ >string ]]
index d65dc0716519297d9df7dff60c0c3c759af0c76a..456f0bb9372cc02abc17158af3855ecf74894b19 100644 (file)
@@ -1,14 +1,21 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See https://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs combinators formatting
-formatting.private fry generalizations io io.streams.string
-kernel make math math.order math.parser multiline namespaces
-present quotations sequences splitting strings strings.parser
-vocabs.parser ;
+
+USING: accessors arrays assocs combinators generalizations io
+io.streams.string kernel make math math.order math.parser
+multiline namespaces present quotations sequences splitting
+strings strings.parser vocabs.parser ;
+
 IN: interpolate
 
 <PRIVATE
 
+SYMBOL: formatter
+
+HOOK: format formatter ( directive -- quot )
+
+M: f format drop [ present ] ;
+
 TUPLE: named-var name ;
 
 TUPLE: stack-var n ;
@@ -27,9 +34,7 @@ TUPLE: anon-var ;
                         [ string>number ]
                         [ 1 + stack-var boa ]
                         [ [ anon-var new ] [ named-var boa ] if-empty ] ?if
-                    ] [
-                        [ [ present ] ] [ format-directive ] if-empty
-                    ] bi* 2array ,
+                    ] [ format ] bi* 2array ,
                 ]
                 [ (parse-interpolate) ] bi*
             ] when*