MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower_regex.cpp
Go to the documentation of this file.
2
3#include <automaton/dfa.h>
4#include <automaton/dfamin.h>
5#include <automaton/nfa2dfa.h>
6
7#include <mim/def.h>
8
10#include <mim/plug/cps/cps.h>
11#include <mim/plug/mem/mem.h>
12
16
17// clang-format off
18template<> struct std::formatter<automaton::DFA> : fe::ostream_formatter {};
19template<> struct std::formatter<automaton::NFA> : fe::ostream_formatter {};
20// clang-format on
21
22namespace mim::plug::regex {
23
24namespace {
25const Def* wrap_in_cps2ds(const Def* callee) { return cps::op_cps2ds_dep(callee); }
26} // namespace
27
29 if (is_bootstrapping()) return RWPhase::rewrite_imm_App(app);
30
31 auto callee = app->callee();
34 || Axm::isa<quant>(callee) || Axm::isa<regex::empty>(callee)) {
35 // The NFA is derived from the old callee's structure; only the argument needs rewriting.
36 auto new_n = rewrite(app->arg());
37 auto nfa = regex2nfa(callee);
38 DLOG("nfa: {}", *nfa);
39
40 auto dfa = automaton::nfa2dfa(*nfa);
41 DLOG("dfa: {}", *dfa);
42
43 auto min_dfa = automaton::minimize_dfa(*dfa);
44 return wrap_in_cps2ds(dfa2matcher(new_world(), *min_dfa, new_n));
45 }
46
47 return RWPhase::rewrite_imm_App(app);
48}
49
50} // namespace mim::plug::regex
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
World & new_world()
Create new Defs into this.
Definition phase.h:368
bool is_bootstrapping() const
Returns whether we are currently bootstrapping (rewriting annexes).
Definition phase.h:356
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
const Def * rewrite_imm_App(const App *) final
const mim::Def * dfa2matcher(mim::World &, const automaton::DFA &, const mim::Def *)
You can dl::get this function.
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:94
std::unique_ptr< DFA > minimize_dfa(const DFA &dfa)
Definition dfamin.cpp:113
std::unique_ptr< DFA > nfa2dfa(const NFA &nfa)
Definition nfa2dfa.cpp:39
const Def * op_cps2ds_dep(const Def *k)
Definition cps.h:16
The regex Plugin
Definition lower_regex.h:5
std::unique_ptr< automaton::NFA > regex2nfa(const Def *regex)