{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DerivingStrategies #-}{-# LANGUAGE RecordWildCards #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Rewriter (rewrite, rewrite', RewriteContext (..)) whereimport Astimport Builderimport Control.Exception (Exception, throwIO)import Data.Foldable (foldlM)import qualified Data.Map.Strict as Mimport Data.Maybe (catMaybes, fromMaybe, isJust)import Debug.Trace (trace)import Logger (logDebug)import Matcher (MetaValue (MvAttribute, MvBindings, MvBytes, MvExpression), Subst (Subst), combine, combineMany, defaultScope, matchProgram, substEmpty, substSingle)import Misc (ensuredFile)import Parser (parseProgram, parseProgramThrows)import Pretty (PrintMode (SWEET), prettyAttribute, prettyBytes, prettyExpression, prettyExpression', prettyProgram, prettyProgram', prettySubsts)import Replacer (replaceProgram, replaceProgramThrows)import Rule (RuleContext (RuleContext), meetMaybeCondition)import qualified Rule as Rimport Termimport Text.Printfimport Yaml (ExtraArgument (..))import qualified Yaml as Ydata RewriteContext = RewriteContext { _program :: Program, _maxDepth :: Integer, _buildTerm :: BuildTermFunc, _must :: Integer }data MustException = StoppedBefore {must :: Integer, count :: Integer} | ContinueAfter {must :: Integer} deriving (Exception)instance Show MustException where show StoppedBefore {..} = printf "With option --must=%d it's expected exactly %d rewriting cycles happened, but rewriting stopped after %d" must must count show ContinueAfter {..} = printf "With option --must=%d it's expected exactly %d rewriting cycles happened, but rewriting is still going" must must-- Build pattern and result expression and replace patterns to results in given programbuildAndReplace :: Program -> Expression -> Expression -> [Subst] -> IO ProgrambuildAndReplace program ptn res substs = do ptns <- buildExpressions ptn substs repls <- buildExpressions res substs let repls' = map fst repls ptns' = map fst ptns replaceProgramThrows program ptns' repls'rewrite :: Program -> [Y.Rule] -> RewriteContext -> IO Programrewrite program [] _ = pure programrewrite program (rule : rest) ctx = do let ruleName = fromMaybe "unknown" (Y.name rule) ptn = Y.pattern rule res = Y.result rule matched <- R.matchProgramWithRule program rule (RuleContext (_program ctx) (_buildTerm ctx)) prog <- if null matched then pure program else do logDebug (printf "Rule '%s' has been matched, applying..." ruleName) prog' <- buildAndReplace program ptn res matched if program == prog' then logDebug (printf "Applied '%s', no changes made" ruleName) else logDebug ( printf "Applied '%s' (%d nodes -> %d nodes):\n%s" ruleName (countNodes program) (countNodes prog') (prettyProgram' prog' SWEET) ) pure prog' rewrite prog rest ctx-- @todo #169:30min Memorize previous rewritten programs. Right now in order not to-- get an infinite recursion during rewriting we just count have many times we apply-- rewriting rules. If we reach given amount - we just stop. It's not idiomatic and may-- not work on big programs. We need to introduce some mechanism which would memorize-- all rewritten program on each step and if on some step we get the program that have already-- been memorized - we fail because we got into infinite recursion. Ofc we should keep counting-- rewriting cycles if program just only grows on each rewriting.rewrite' :: Program -> [Y.Rule] -> RewriteContext -> IO Programrewrite' prog rules ctx = _rewrite prog 1 where _rewrite :: Program -> Integer -> IO Program _rewrite prog count = do let depth = _maxDepth ctx must = _must ctx if must /= 0 && count - 1 > must then throwIO (ContinueAfter must) else if count - 1 == depth then do logDebug (printf "Max amount of rewriting cycles (%d) has been reached, rewriting is stopped" depth) pure prog else do logDebug (printf "Starting rewriting cycle %d out of %d" count depth) rewritten <- rewrite prog rules ctx if rewritten == prog then do logDebug "No rule matched, rewriting is stopped" if must /= 0 && count - 1 /= must then throwIO (StoppedBefore must (count - 1)) else pure rewritten else _rewrite rewritten (count + 1)