From f17d3c906b9ae5a4db5039385583469993d05a29 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 19 Mar 2021 16:11:09 -0700 Subject: [PATCH] locals: simplify by merging def and multi-def (again). --- basis/locals/prettyprint/prettyprint.factor | 20 +++++++++---------- core/locals/parser/parser-tests.factor | 2 +- core/locals/rewrite/closures/closures.factor | 2 -- .../rewrite/point-free/point-free.factor | 14 ++++++------- core/locals/rewrite/sugar/sugar.factor | 8 +------- core/locals/types/types.factor | 12 +++++------ 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/basis/locals/prettyprint/prettyprint.factor b/basis/locals/prettyprint/prettyprint.factor index ac72801d91..3a6e601dea 100644 --- a/basis/locals/prettyprint/prettyprint.factor +++ b/basis/locals/prettyprint/prettyprint.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2007, 2008 Slava Pestov, Eduardo Cavazos. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel locals locals.types prettyprint.backend -prettyprint.custom prettyprint.sections sequences words ; +USING: accessors combinators kernel locals locals.types math +prettyprint.backend prettyprint.custom prettyprint.sections +sequences words ; IN: locals.prettyprint : pprint-var ( var -- ) @@ -29,12 +30,11 @@ M: lambda pprint* M: let pprint* \ [let pprint-let ; -M: def pprint* - dup local>> word? - [ pprint-word local>> pprint-var block> ] - [ pprint-tuple ] if ; - M: multi-def pprint* - dup locals>> [ word? ] all? - [ pprint-word "(" text locals>> [ pprint-var ] each ")" text block> ] - [ pprint-tuple ] if ; + dup locals>> [ word? ] all? [ + pprint-word locals>> { + [ length 1 > [ "(" text ] when ] + [ [ pprint-var ] each ] + [ length 1 > [ ")" text ] when ] + } cleave block> + ] [ pprint-tuple ] if ; diff --git a/core/locals/parser/parser-tests.factor b/core/locals/parser/parser-tests.factor index 85a6913d08..adb1d16a65 100644 --- a/core/locals/parser/parser-tests.factor +++ b/core/locals/parser/parser-tests.factor @@ -30,7 +30,7 @@ IN: locals.parser.tests { "um" t } [ [ "um" parse-def - local>> name>> + locals>> first name>> qualified-vocabs last words>> keys "um" swap member? ] with-compilation-unit ] unit-test diff --git a/core/locals/rewrite/closures/closures.factor b/core/locals/rewrite/closures/closures.factor index 71cc8bbf95..36762575cf 100644 --- a/core/locals/rewrite/closures/closures.factor +++ b/core/locals/rewrite/closures/closures.factor @@ -20,8 +20,6 @@ GENERIC: defs-vars* ( seq form -- seq' ) : defs-vars ( form -- vars ) { } [ defs-vars* ] reduce members ; -M: def defs-vars* local>> unquote suffix ; - M: multi-def defs-vars* locals>> [ unquote suffix ] each ; M: quotation defs-vars* [ defs-vars* ] each ; diff --git a/core/locals/rewrite/point-free/point-free.factor b/core/locals/rewrite/point-free/point-free.factor index 090bc817b4..e956062956 100644 --- a/core/locals/rewrite/point-free/point-free.factor +++ b/core/locals/rewrite/point-free/point-free.factor @@ -27,17 +27,17 @@ M: local-writer localize dupd "local-reader" word-prop read-local-quot [ set-local-value ] append ; -M: def localize - local>> - [ prefix ] - [ local-reader? [ 1array load-local ] [ load-local ] ? ] - bi ; - M: multi-def localize locals>> [ prepend ] [ [ [ local-reader? ] dip '[ [ 1array ] _ [ndip] ] [ [ ] ] if ] map-index concat ] - [ length [ load-locals ] curry ] tri append ; + [ + length { + { [ dup 1 > ] [ [ load-locals ] curry ] } + { [ dup 1 = ] [ drop [ load-local ] ] } + [ drop [ ] ] + } cond + ] tri append ; M: object localize 1quotation ; diff --git a/core/locals/rewrite/sugar/sugar.factor b/core/locals/rewrite/sugar/sugar.factor index 5ce22c567f..8dc3babed4 100644 --- a/core/locals/rewrite/sugar/sugar.factor +++ b/core/locals/rewrite/sugar/sugar.factor @@ -20,11 +20,7 @@ GENERIC: quotation-rewrite ( form -- form' ) M: callable quotation-rewrite [ [ rewrite-sugar* ] each ] [ ] make ; : var-defs ( vars -- defs ) - dup length 1 > [ - 1quotation - ] [ - [ ] [ ] map-as - ] if ; + [ [ ] ] [ 1quotation ] if-empty ; M: lambda quotation-rewrite [ body>> ] [ vars>> var-defs ] bi prepend quotation-rewrite ; @@ -102,8 +98,6 @@ M: vector rewrite-sugar* rewrite-element ; M: tuple rewrite-sugar* rewrite-element ; -M: def rewrite-sugar* , ; - M: multi-def rewrite-sugar* , ; M: hashtable rewrite-sugar* rewrite-element ; diff --git a/core/locals/types/types.factor b/core/locals/types/types.factor index 9f401bbf9c..ba7c974945 100644 --- a/core/locals/types/types.factor +++ b/core/locals/types/types.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2007, 2010 Slava Pestov, Eduardo Cavazos. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors combinators kernel sequences words -quotations ; +USING: accessors arrays combinators kernel sequences +quotations words ; IN: locals.types TUPLE: lambda vars body ; @@ -18,14 +18,12 @@ C: quote : unquote ( quote -- local ) dup quote? [ local>> ] when ; inline -TUPLE: def local ; - -C: def - TUPLE: multi-def locals ; C: multi-def +: ( local -- def ) 1array ; + PREDICATE: local < word "local?" word-prop ; : ( name -- word ) @@ -53,4 +51,4 @@ PREDICATE: local-writer < word "local-writer?" word-prop ; } 2cleave ; UNION: lexical local local-reader local-writer ; -UNION: special lexical quote def ; +UNION: special lexical quote ; -- 2.34.1