packages feed

phino-0.0.0.54 / src / Filter.hs

-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Filter (include, exclude) whereimport ASTimport Logger (logDebug)import Miscimport Printer (logPrintConfig, printExpression, printExpression', printProgram)import Rewriterimport Text.Printf (printf)fqnToAttrs :: Expression -> [Attribute]fqnToAttrs = reverse . fqnToAttrs'  where    fqnToAttrs' :: Expression -> [Attribute]    fqnToAttrs' (ExDispatch ExGlobal attr) = [attr]    fqnToAttrs' (ExDispatch expr attr) = attr : fqnToAttrs' expr    fqnToAttrs' expr = []exclude' :: Program -> [Expression] -> Programexclude' prog [] = progexclude' (Program expr@(ExFormation _)) (fqn : rest) = exclude' (Program (excludedFormation expr (fqnToAttrs fqn))) rest  where    excludedFormation :: Expression -> [Attribute] -> Expression    excludedFormation (ExFormation bds) [attr] = ExFormation [bd | bd <- bds, attributeFromBinding bd /= Just attr]    excludedFormation (ExFormation bds) attrs = ExFormation (excludedBindings bds attrs)      where        excludedBindings :: [Binding] -> [Attribute] -> [Binding]        excludedBindings [] _ = []        excludedBindings (bd@(BiTau attr form@(ExFormation _)) : bds) attrs@(attr' : rest)          | attr == attr' = BiTau attr (excludedFormation form rest) : bds          | otherwise = bd : excludedBindings bds attrs        excludedBindings (bd : bds) attrs = bd : excludedBindings bds attrs    excludedFormation expr _ = exprexclude' prog _ = progexclude :: [Rewritten] -> [Expression] -> [Rewritten]exclude [] _ = []exclude rs [] = rsexclude ((program, maybeRule) : rest) exprs = (exclude' program exprs, maybeRule) : exclude rest exprsinclude' :: Program -> Expression -> Programinclude' prog@(Program expr@(ExFormation _)) fqn = case includedFormation expr (fqnToAttrs fqn) of  Just expr -> Program expr  _ -> Program (ExFormation [BiVoid AtRho])  where    includedFormation :: Expression -> [Attribute] -> Maybe Expression    includedFormation (ExFormation bds) [attr] =      let bds' = [bd | bd <- bds, attributeFromBinding bd == Just attr]       in if null bds' then Nothing else Just (ExFormation (withVoidRho bds'))    includedFormation (ExFormation bds) attrs = includedBindings bds attrs >>= (Just . ExFormation . (: [BiVoid AtRho]))      where        includedBindings :: [Binding] -> [Attribute] -> Maybe Binding        includedBindings (bd@(BiTau attr form@(ExFormation _)) : bds) attrs@(attr' : rest)          | attr == attr' = includedFormation form rest >>= Just . BiTau attr          | otherwise = includedBindings bds attrs        includedBindings _ _ = Nothing    includedFormation _ _ = Nothinginclude' _ _ = Program (ExFormation [BiVoid AtRho])include :: [Rewritten] -> [Expression] -> [Rewritten]include [] _ = []include rs [] = rsinclude ((program, maybeRule) : rest) (expr : _) = (include' program expr, maybeRule) : include rest [expr]