packages feed

phino-0.0.166 / src / Sugar.hs

{-# LANGUAGE InstanceSigs #-}{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE RecordWildCards #-}{-# OPTIONS_GHC -Wno-name-shadowing #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Sugar (toSalty, withSugarType, withoutRho, revealXi, SugarType (..), ToSalty) whereimport ASTimport Bytes (nonFiniteBts, numToBts, strToBts, unescapeStr)import CSTwithSugarType :: (ToSalty a) => SugarType -> a -> awithSugarType SWEET node = nodewithSugarType SALTY node = toSalty nodedata SugarType = SWEET | SALTY  deriving (Eq, Show)withoutRho :: SugarType -> EXPRESSION -> EXPRESSIONwithoutRho sugar = goExpr  where    goExpr :: EXPRESSION -> EXPRESSION    goExpr EX_FORMATION{..} = case goBinding binding of      empty@BI_EMPTY{} -> EX_FORMATION lsb NO_EOL NO_TAB empty NO_EOL NO_TAB rsb      binding'@BI_PAIR{pair = pair', bindings = BDS_EMPTY{}}        | sugar == SWEET && sugared pair' -> EX_SINGLE pair' NO_SPACE (EX_FORMATION lsb eol tab binding' eol' tab' rsb)      binding' -> EX_FORMATION lsb eol tab binding' eol' tab' rsb    goExpr EX_DISPATCH{..} = EX_DISPATCH (goExpr expr) space attr    goExpr EX_APPLICATION{..} = case goArgument argument of      Nothing -> goExpr expr      Just argument' -> EX_APPLICATION (goExpr expr) space eol tab argument' eol' tab' indent    goExpr EX_PHI_MEET{..} = EX_PHI_MEET prefix idx (goExpr expr)    goExpr EX_PHI_AGAIN{..} = EX_PHI_AGAIN prefix idx (goExpr expr)    goExpr EX_SINGLE{..}      | isRho pair = goExpr formation      | otherwise = EX_SINGLE (goPair pair) space (goExpr formation)    goExpr EX_NUMBER{..} = EX_NUMBER num tab (filter (not . isRhoArgument) rhos)    goExpr EX_STRING{..} = EX_STRING str tab (filter (not . isRhoArgument) rhos)    goExpr EX_NONFINITE{..} = EX_NONFINITE global nonfinite tab (filter (not . isRhoArgument) rhos)    goExpr expr = expr    goBinding :: BINDING -> BINDING    goBinding empty@BI_EMPTY{} = empty    goBinding BI_META{..} = BI_META meta (goBindings bindings) tab    goBinding BI_PAIR{..}      | isRho pair = promote tab (goBindings bindings)      | otherwise = BI_PAIR (goPair pair) (goBindings bindings) tab    goBindings :: BINDINGS -> BINDINGS    goBindings empty@BDS_EMPTY{} = empty    goBindings BDS_META{..} = BDS_META eol tab meta (goBindings bindings)    goBindings BDS_PAIR{..}      | isRho pair = goBindings bindings      | otherwise = BDS_PAIR eol tab (goPair pair) (goBindings bindings)    promote :: TAB -> BINDINGS -> BINDING    promote tab (BDS_EMPTY _) = BI_EMPTY tab    promote tab (BDS_PAIR _ _ pair bindings) = BI_PAIR pair bindings tab    promote tab (BDS_META _ _ meta bindings) = BI_META meta bindings tab    goArgument :: APP_ARGUMENT -> Maybe APP_ARGUMENT    goArgument (AA_TAU (APP_BINDING pair))      | isRho pair = Nothing      | otherwise = Just (AA_TAU (APP_BINDING (goPair pair)))    goArgument (AA_TAUS binding) = case goArgBinding binding of      BI_EMPTY{} -> Nothing      binding'        | sugar == SWEET, Just args <- positional binding' -> Just (AA_EXPRS args)        | otherwise -> Just (AA_TAUS binding')    goArgument (AA_EXPRS args) = Just (AA_EXPRS (goAppArg args))    positional :: BINDING -> Maybe APP_ARG    positional (BI_PAIR (PA_ALPHA (AL_IDX _ 0) _ first) rest _) = APP_ARG first <$> following 1 rest      where        following :: Int -> BINDINGS -> Maybe APP_ARGS        following _ BDS_EMPTY{} = Just AAS_EMPTY        following next (BDS_PAIR eol' tab' (PA_ALPHA (AL_IDX _ index) _ arg) more)          | index == next = AAS_EXPR eol' tab' arg <$> following (next + 1) more        following _ _ = Nothing    positional _ = Nothing    goArgBinding :: BINDING -> BINDING    goArgBinding empty@BI_EMPTY{} = empty    goArgBinding BI_META{..} = BI_META meta (goArgBindings bindings) tab    goArgBinding BI_PAIR{..}      | isRho pair = promote tab (goArgBindings bindings)      | otherwise = BI_PAIR (goPair pair) (goArgBindings bindings) tab    goArgBindings :: BINDINGS -> BINDINGS    goArgBindings empty@BDS_EMPTY{} = empty    goArgBindings BDS_META{..} = BDS_META eol tab meta (goArgBindings bindings)    goArgBindings BDS_PAIR{..}      | isRho pair = goArgBindings bindings      | otherwise = BDS_PAIR eol tab (goPair pair) (goArgBindings bindings)    goAppArg :: APP_ARG -> APP_ARG    goAppArg APP_ARG{..} = APP_ARG (goExpr expr) (goAppArgs args)    goAppArgs :: APP_ARGS -> APP_ARGS    goAppArgs AAS_EMPTY = AAS_EMPTY    goAppArgs AAS_EXPR{..} = AAS_EXPR eol tab (goExpr expr) (goAppArgs args)    goPair :: PAIR -> PAIR    goPair PA_TAU{..} = PA_TAU attr arrow (goExpr expr)    goPair PA_ALPHA{..} = PA_ALPHA alpha arrow (goExpr expr)    goPair PA_FORMATION{..} = case filter (not . rho) voids of      [] -> PA_TAU attr arrow (goExpr expr)      voids' -> PA_FORMATION attr voids' arrow (goExpr expr)      where        rho :: ATTRIBUTE -> Bool        rho AT_RHO{} = True        rho _ = False    goPair pair = pair    sugared :: PAIR -> Bool    sugared PA_TAU{attr = AT_DELTA{}} = False    sugared PA_TAU{attr = AT_LAMBDA{}} = False    sugared PA_ALPHA{} = False    sugared PA_FORMATION{voids = []} = True    sugared PA_FORMATION{} = False    sugared _ = True    isRho :: PAIR -> Bool    isRho PA_VOID{attr = AT_RHO _} = True    isRho PA_TAU{attr = AT_RHO _} = True    isRho PA_FORMATION{attr = AT_RHO _} = True    isRho _ = False    isRhoArgument :: Argument -> Bool    isRhoArgument (ArTau AtRho _) = True    isRhoArgument _ = FalserevealXi :: SugarType -> EXPRESSION -> EXPRESSIONrevealXi _ = goExpr  where    goExpr :: EXPRESSION -> EXPRESSION    goExpr EX_ATTR{..} = EX_DISPATCH (EX_XI XI) NO_SPACE attr    goExpr EX_DISPATCH{..} = EX_DISPATCH (goExpr expr) space attr    goExpr EX_APPLICATION{..} = EX_APPLICATION (goExpr expr) space eol tab (goArgument argument) eol' tab' indent    goExpr EX_FORMATION{..} = EX_FORMATION lsb eol tab (goBinding binding) eol' tab' rsb    goExpr EX_PHI_MEET{..} = EX_PHI_MEET prefix idx (goExpr expr)    goExpr EX_PHI_AGAIN{..} = EX_PHI_AGAIN prefix idx (goExpr expr)    goExpr EX_SINGLE{..} = EX_SINGLE (goPair pair) space (goExpr formation)    goExpr expr = expr    goArgument :: APP_ARGUMENT -> APP_ARGUMENT    goArgument (AA_TAU (APP_BINDING pair)) = AA_TAU (APP_BINDING (goPair pair))    goArgument (AA_TAUS binding) = AA_TAUS (goBinding binding)    goArgument (AA_EXPRS arg) = AA_EXPRS (goAppArg arg)    goAppArg :: APP_ARG -> APP_ARG    goAppArg APP_ARG{..} = APP_ARG (goExpr expr) (goAppArgs args)    goAppArgs :: APP_ARGS -> APP_ARGS    goAppArgs AAS_EMPTY = AAS_EMPTY    goAppArgs AAS_EXPR{..} = AAS_EXPR eol tab (goExpr expr) (goAppArgs args)    goBinding :: BINDING -> BINDING    goBinding BI_PAIR{..} = BI_PAIR (goPair pair) (goBindings bindings) tab    goBinding binding = binding    goBindings :: BINDINGS -> BINDINGS    goBindings BDS_PAIR{..} = BDS_PAIR eol tab (goPair pair) (goBindings bindings)    goBindings bindings = bindings    goPair :: PAIR -> PAIR    goPair PA_TAU{..} = PA_TAU attr arrow (goExpr expr)    goPair PA_ALPHA{..} = PA_ALPHA alpha arrow (goExpr expr)    goPair PA_FORMATION{..} = PA_FORMATION attr voids arrow (goExpr expr)    goPair pair = pairclass ToSalty a where  toSalty :: a -> ainstance ToSalty EXPRESSION where  toSalty EX_ATTR{..} = EX_DISPATCH (EX_XI XI) NO_SPACE attr  toSalty EX_DISPATCH{..} = EX_DISPATCH (toSalty expr) space attr  toSalty formation@EX_FORMATION{binding = BI_EMPTY{}} = formation  toSalty EX_FORMATION{..} = EX_FORMATION lsb eol tab (toSalty binding) eol' tab' rsb  toSalty EX_APPLICATION{argument = AA_TAU tau, ..} = EX_APPLICATION (toSalty expr) space EOL (TAB indent) (AA_TAU (toSalty tau)) EOL (TAB (indent - 1)) indent  toSalty EX_APPLICATION{argument = AA_TAUS taus, ..} =    foldl      toApplication      expr      (tauToPairs taus)    where      toApplication :: EXPRESSION -> PAIR -> EXPRESSION      toApplication exp pair =        EX_APPLICATION (toSalty exp) space EOL (TAB indent) (AA_TAU (APP_BINDING (toSalty pair))) EOL (TAB (indent - 1)) indent      tauToPairs :: BINDING -> [PAIR]      tauToPairs BI_PAIR{..} = pair : tausToPairs bindings      tauToPairs BI_EMPTY{} = []      tauToPairs (BI_META mt _ _) = error $ "BI_META " ++ show mt ++ " unexpected in tauToPairs"      tausToPairs :: BINDINGS -> [PAIR]      tausToPairs BDS_EMPTY{} = []      tausToPairs BDS_PAIR{..} = pair : tausToPairs bindings      tausToPairs (BDS_META _ _ mt _) = error $ "BDS_META " ++ show mt ++ " unexpected in tausToPairs"  toSalty EX_APPLICATION{argument = AA_EXPRS args, ..} = toSalty (EX_APPLICATION expr space EOL (TAB indent) (AA_TAUS (argToBinding args tab)) EOL (TAB (indent - 1)) indent)    where      argToBinding :: APP_ARG -> TAB -> BINDING      argToBinding APP_ARG{..} =        BI_PAIR          (PA_ALPHA (AL_IDX ALPHA 0) ARROW expr)          (argsToBindings args 1 tab)      argsToBindings :: APP_ARGS -> Int -> TAB -> BINDINGS      argsToBindings AAS_EMPTY _ tab = BDS_EMPTY tab      argsToBindings AAS_EXPR{..} idx tb = BDS_PAIR eol tb (PA_ALPHA (AL_IDX ALPHA idx) ARROW expr) (argsToBindings args (idx + 1) tb)  toSalty EX_NUMBER{num, tab = tab@TAB{..}, rhos} =    saltifyPrimitive      (toCST (BaseObject "number") (indent + 1, EOL))      (toCST (BaseObject "bytes") (indent + 2, EOL))      (toCST (ExFormation [BiDelta (numToBts num)]) (indent + 2, EOL))      tab      rhos  toSalty EX_NONFINITE{nonfinite, tab = tab@TAB{..}, rhos} =    saltifyPrimitive      (toCST (BaseObject "number") (indent + 1, EOL))      (toCST (BaseObject "bytes") (indent + 2, EOL))      (toCST (ExFormation [BiDelta (nonFiniteBts nonfinite)]) (indent + 2, EOL))      tab      rhos  toSalty EX_STRING{str, tab = tab@TAB{..}, rhos} =    saltifyPrimitive      (toCST (BaseObject "string") (indent + 1, EOL))      (toCST (BaseObject "bytes") (indent + 2, EOL))      (toCST (ExFormation [BiDelta (strToBts (unescapeStr str))]) (indent + 2, EOL))      tab      rhos  toSalty EX_PHI_MEET{..} = EX_PHI_MEET prefix idx (toSalty expr)  toSalty EX_PHI_AGAIN{..} = EX_PHI_AGAIN prefix idx (toSalty expr)  toSalty EX_SINGLE{..} = toSalty formation  toSalty expr = exprsaltifyPrimitive :: EXPRESSION -> EXPRESSION -> EXPRESSION -> TAB -> [Argument] -> EXPRESSIONsaltifyPrimitive base bytes data' tb@TAB{..} rhos =  let next = TAB (indent + 1)   in toSalty        ( EX_APPLICATION            base            NO_SPACE            EOL            next            ( AA_TAUS                ( BI_PAIR                    ( PA_TAU                        (AT_PHI PHI)                        ARROW                        ( EX_APPLICATION                            bytes                            NO_SPACE                            EOL                            (TAB (indent + 2))                            ( AA_TAUS                                ( BI_PAIR                                    (PA_TAU (AT_PHI PHI) ARROW data')                                    (BDS_EMPTY (TAB (indent + 2)))                                    (TAB (indent + 2))                                )                            )                            EOL                            next                            (indent + 2)                        )                    )                    (toCST rhos (indent + 1, EOL))                    next                )            )            EOL            tb            (indent + 1)        )saltifyPrimitive _ _ _ TAB' _ = error "saltifyPrimitive requires TAB with indent, got TAB'"saltifyPrimitive _ _ _ NO_TAB _ = error "saltifyPrimitive requires TAB with indent, got NO_TAB"instance ToSalty BINDING where  toSalty BI_PAIR{..} = BI_PAIR (toSalty pair) (toSalty bindings) tab  toSalty bd = bdinstance ToSalty APP_BINDING where  toSalty APP_BINDING{..} = APP_BINDING (toSalty pair)instance ToSalty BINDINGS where  toSalty BDS_PAIR{..} = BDS_PAIR eol tab (toSalty pair) (toSalty bindings)  toSalty bds = bdsinstance ToSalty PAIR where  toSalty PA_TAU{..} = PA_TAU attr arrow (toSalty expr)  toSalty PA_ALPHA{..} = PA_ALPHA alpha arrow (toSalty expr)  toSalty PA_FORMATION{voids, attr, arrow, expr = EX_SINGLE{formation}} = toSalty (PA_FORMATION attr voids arrow formation)  toSalty PA_FORMATION{voids, attr, arrow, expr = EX_FORMATION{..}} =    PA_TAU attr arrow (toSalty (EX_FORMATION lsb eol tab (joinToBinding voids binding) eol' tab' rsb))    where      joinToBinding :: [ATTRIBUTE] -> BINDING -> BINDING      joinToBinding [] bd = bd      joinToBinding (attr : rest) bd = BI_PAIR (PA_VOID attr arrow EMPTY) (joinToBindings rest bd) tab      joinToBindings :: [ATTRIBUTE] -> BINDING -> BINDINGS      joinToBindings [] BI_EMPTY{..} = BDS_EMPTY tab      joinToBindings [] BI_PAIR{..} = BDS_PAIR eol tab pair bindings      joinToBindings [] BI_META{..} = BDS_META eol tab meta bindings      joinToBindings (attr : rest) bd = BDS_PAIR eol tab (PA_VOID attr arrow EMPTY) (joinToBindings rest bd)  toSalty pair = pairinstance ToSalty SET where  toSalty ST_BINDING{..} = ST_BINDING (toSalty binding)  toSalty st = stinstance ToSalty NUMBER where  toSalty LENGTH{..} = LENGTH (toSalty binding)  toSalty DOMAIN{..} = DOMAIN (toSalty binding)  toSalty num = numinstance ToSalty COMPARABLE where  toSalty comp@CMP_ATTR{} = comp  toSalty CMP_EXPR{..} = CMP_EXPR (toSalty expr)  toSalty CMP_NUM{..} = CMP_NUM (toSalty num)instance ToSalty CONDITION where  toSalty CO_BELONGS{..} = CO_BELONGS attr belongs (toSalty set)  toSalty CO_LOGIC{..} = CO_LOGIC (map toSalty conditions) operator  toSalty CO_NF{..} = CO_NF (toSalty expr)  toSalty CO_ABSOLUTE{..} = CO_ABSOLUTE (toSalty expr) belongs  toSalty CO_NOT{..} = CO_NOT (toSalty condition)  toSalty CO_COMPARE{..} = CO_COMPARE (toSalty left) equal (toSalty right)  toSalty CO_MATCHES{..} = CO_MATCHES regex (toSalty expr)  toSalty CO_PART_OF{..} = CO_PART_OF (toSalty expr) (toSalty binding)  toSalty CO_DISJOINT{..} = CO_DISJOINT attrs (map toSalty groups)  toSalty CO_SUBSET{..} = CO_SUBSET attrs belongs (map toSalty groups)  toSalty CO_FORMATION{..} = CO_FORMATION (toSalty expr)  toSalty CO_EMPTY = CO_EMPTYinstance ToSalty EXTRA_ARG where  toSalty ARG_EXPR{..} = ARG_EXPR (toSalty expr)  toSalty ARG_BINDING{..} = ARG_BINDING (toSalty binding)  toSalty at@ARG_ATTR{} = at  toSalty bts@ARG_BYTES{} = btsinstance ToSalty EXTRA where  toSalty EXTRA{..} = EXTRA (toSalty meta) func (map toSalty args)