{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Functions (buildTerm) whereimport ASTimport Builderimport Control.Exception (throwIO)import Control.Monad (when)import qualified Data.ByteString.Char8 as Bimport Data.Functorimport qualified Data.Set as Setimport Depsimport GHC.IO (unsafePerformIO)import Logger (logDebug)import Matcherimport Miscimport Parser (parseAttributeThrows, parseNumberThrows)import Printer (printAttribute, printExpression, printExtraArg)import Random (randomString)import Regexpimport Text.Printf (printf)import qualified Yaml as YbuildTerm :: BuildTermFuncbuildTerm func args subst = do logDebug (printf "Building new term using '%s' function..." func) buildTerm' func args substbuildTerm' :: BuildTermFuncbuildTerm' "contextualize" = _contextualizebuildTerm' "scope" = _scopebuildTerm' "random-tau" = _randomTaubuildTerm' "dataize" = _dataizebuildTerm' "concat" = _concatbuildTerm' "sed" = _sedbuildTerm' "random-string" = _randomStringbuildTerm' "size" = _sizebuildTerm' "tau" = _taubuildTerm' "string" = _stringbuildTerm' "number" = _numberbuildTerm' "sum" = _sumbuildTerm' "join" = _joinbuildTerm' func = _unsupported funcargToBytes :: Y.ExtraArgument -> Subst -> IO BytesargToBytes (Y.ArgBytes bytes) subst = buildBytesThrows bytes substargToBytes (Y.ArgExpression expr) subst = do (TeBytes bts) <- _dataize [Y.ArgExpression expr] subst pure btsargToBytes arg _ = throwIO (userError (printf "Can't extract bytes from given argument: %s" (printExtraArg arg)))argToString :: Y.ExtraArgument -> Subst -> IO StringargToString arg subst = argToBytes arg subst <&> btsToUnescapedStrargToNumber :: Y.ExtraArgument -> Subst -> IO DoubleargToNumber arg subst = argToBytes arg subst <&> either toDouble id . btsToNum_contextualize :: BuildTermMethod_contextualize [Y.ArgExpression expr, Y.ArgExpression context] subst = do (expr', _) <- buildExpressionThrows expr subst (context', _) <- buildExpressionThrows context subst pure (TeExpression (contextualize expr' context'))_contextualize _ _ = throwIO (userError "Function contextualize() requires exactly 2 arguments as expression")_scope :: BuildTermMethod_scope [Y.ArgExpression expr] subst = do (_, scope) <- buildExpressionThrows expr subst pure (TeExpression scope)_scope _ _ = throwIO (userError "Function scope() requires exactly 1 argument as expression")_randomTau :: BuildTermMethod_randomTau args subst = do attrs <- argsToAttrs args tau <- randomTau attrs pure (TeAttribute (AtLabel tau)) where argsToAttrs :: [Y.ExtraArgument] -> IO [String] argsToAttrs [] = pure [] argsToAttrs (arg : rest) = case arg of Y.ArgExpression _ -> argsToAttrs rest Y.ArgAttribute attr -> do attr' <- buildAttributeThrows attr subst rest' <- argsToAttrs rest pure (printAttribute attr' : rest') Y.ArgBinding bd -> do bds <- buildBindingThrows bd subst rest' <- argsToAttrs rest pure (map show (attributesFromBindings bds) ++ rest') Y.ArgBytes _ -> throwIO (userError "Bytes can't be argument of random-tau() function") randomTau :: [String] -> IO String randomTau attrs = do tau <- randomString "a🌵%d" if tau `elem` attrs then randomTau attrs else pure tau_dataize :: BuildTermMethod_dataize [Y.ArgBytes bytes] subst = do bts <- buildBytesThrows bytes subst pure (TeBytes bts)_dataize [Y.ArgExpression expr] subst = do (expr', _) <- buildExpressionThrows expr subst case expr' of DataObject _ bytes -> pure (TeBytes bytes) _ -> throwIO (userError "Only data objects and bytes are supported by 'dataize' function now")_dataize _ _ = throwIO (userError "Function dataize() requires exactly 1 argument as expression or bytes")_concat :: BuildTermMethod_concat args subst = do args' <- traverse (`argToString` subst) args pure (TeExpression (DataString (strToBts (concat args'))))_sed :: BuildTermMethod_sed args subst = do when (length args < 2) (throwIO (userError "Function sed() requires at least two arguments")) args' <- traverse ( \arg -> do bts <- argToString arg subst pure (B.pack bts) ) args res <- sed (head args') (tail args') pure (TeExpression (DataString (strToBts (B.unpack res)))) where sed :: B.ByteString -> [B.ByteString] -> IO B.ByteString sed tgt [] = pure tgt sed tgt (ptn : ptns) = do (pat, rep, global) <- parse ptn regex <- compile pat next <- if global then replaceAll regex rep tgt else replaceFirst regex rep tgt sed next ptns parse :: B.ByteString -> IO (B.ByteString, B.ByteString, Bool) parse input = case B.stripPrefix "s/" input of Just body -> let (pat, rest) = nextUntilSlash body B.empty False (rep, flag) = nextUntilSlash rest B.empty True in case flag of "g" -> pure (pat, rep, True) "" -> pure (pat, rep, False) _ -> throwIO (userError "sed pattern must be in format s/pat/rep/[g]") _ -> throwIO (userError "sed pattern must start with s/") -- Cut part from given string until regular slash. nextUntilSlash :: B.ByteString -> B.ByteString -> Bool -> (B.ByteString, B.ByteString) nextUntilSlash input acc escape = case B.uncons input of Nothing -> (acc, B.empty) Just (h, rest) | h == '\\' -> case B.uncons rest of Just (h', rest') -> nextUntilSlash rest' ((if escape then acc else B.snoc acc '\\') `B.append` B.singleton h') escape Nothing -> (if escape then acc else B.snoc acc '\\', B.empty) | h == '/' -> (acc, rest) | otherwise -> nextUntilSlash rest (B.snoc acc h) escape_randomString :: BuildTermMethod_randomString [arg] subst = do pat <- argToString arg subst str <- randomString pat pure (TeExpression (DataString (strToBts str)))_randomString _ _ = throwIO (userError "Function random-string() requires exactly 1 dataizable argument")_size :: BuildTermMethod_size [Y.ArgBinding (BiMeta meta)] subst = do bds <- buildBindingThrows (BiMeta meta) subst pure (TeExpression (DataNumber (numToBts (fromIntegral (length bds)))))_size _ _ = throwIO (userError "Function size() requires exactly 1 meta binding")_tau :: BuildTermMethod_tau [Y.ArgExpression expr] subst = do TeBytes bts <- _dataize [Y.ArgExpression expr] subst attr <- parseAttributeThrows (btsToUnescapedStr bts) pure (TeAttribute attr)_tau _ _ = throwIO (userError "Function tau() requires exactly 1 argument as expression")_string :: BuildTermMethod_string [Y.ArgExpression expr] subst = do (expr', _) <- buildExpressionThrows expr subst str <- case expr' of DataNumber bts -> pure (DataString (strToBts (either show show (btsToNum bts)))) DataString bts -> pure (DataString bts) ex -> throwIO ( userError ( printf "Couldn't convert given expression to 'Φ̇.string' object, only 'Φ̇.number' or 'Φ̇.string' are allowed\n%s" (printExpression ex) ) ) pure (TeExpression str)_string [Y.ArgAttribute attr] subst = do attr' <- buildAttributeThrows attr subst pure (TeExpression (DataString (strToBts (printAttribute attr'))))_string _ _ = throwIO (userError "Function string() requires exactly 1 argument as attribute or data expression (Φ̇.number or Φ̇.string)")_number :: BuildTermMethod_number [Y.ArgExpression expr] subst = do (expr', _) <- buildExpressionThrows expr subst case expr' of DataString bts -> do num <- parseNumberThrows (btsToUnescapedStr bts) pure (TeExpression num) _ -> throwIO (userError (printf "Function number() expects expression to be 'Φ̇.string', but got:\n%s" (printExpression expr')))_number _ _ = throwIO (userError "Function number() requires exactly 1 argument as 'Φ̇.string'")_sum :: BuildTermMethod_sum args subst = do nums <- traverse (`argToNumber` subst) args pure (TeExpression (DataNumber (numToBts (sum nums))))_join :: BuildTermMethod_join [] _ = pure (TeBindings [])_join args subst = do bds <- buildBindings args pure (TeBindings (join' bds Set.empty)) where buildBindings :: [Y.ExtraArgument] -> IO [Binding] buildBindings [] = pure [] buildBindings (Y.ArgBinding bd : args') = do bds <- buildBindingThrows bd subst next <- buildBindings args' pure (bds ++ next) buildBindings _ = throwIO (userError "Function 'join' can work with bindings only") join' :: [Binding] -> Set.Set Attribute -> [Binding] join' [] _ = [] join' (bd : bds) attrs = case attributesFromBindings [bd] of [attr] -> if Set.member attr attrs then if attr == AtRho || attr == AtDelta || attr == AtLambda then join' bds attrs else let new = case bd of BiTau at ex -> BiTau (updated at attrs) ex BiVoid at -> BiVoid (updated at attrs) other -> other in new : join' bds attrs else bd : join' bds (Set.insert attr attrs) _ -> bd : join' bds attrs updated :: Attribute -> Set.Set Attribute -> Attribute updated _ attrs = case unsafePerformIO (_randomTau (map Y.ArgAttribute (Set.toList attrs)) subst) of TeAttribute attr' -> attr' _ -> AtLabel "unknown"_unsupported :: BuildTermFunc_unsupported func _ _ = throwIO (userError (printf "Function %s() is not supported or does not exist" func))