From 26e0578421b6b6f551c61b859fc36f6c4c72c713 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Fri, 23 Sep 2022 15:55:39 -0400 Subject: [PATCH] add '-' syntax for free relation --- tc/src/lang.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tc/src/lang.cpp b/tc/src/lang.cpp index 3ab5404..68cc207 100644 --- a/tc/src/lang.cpp +++ b/tc/src/lang.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -17,6 +18,7 @@ struct Op { PUSH, POP, LOOP, + FREE, }; Code code: 4; @@ -45,6 +47,8 @@ struct fmt::formatter { return fmt::format_to(ctx.out(), "pop()"); case Op::LOOP: return fmt::format_to(ctx.out(), "loop()"); + case Op::FREE: + return fmt::format_to(ctx.out(), "free()"); default: return fmt::format_to(ctx.out(), "[{}]({})", (unsigned int) op.code, @@ -76,6 +80,10 @@ struct codegen { void loop() { ops.emplace_back(Op::LOOP); } + + void free() { + ops.emplace_back(Op::FREE); + } template void insert(It begin, It end) { @@ -92,7 +100,7 @@ static const std::string GRAMMAR = R"( term <- product / op op <- block / link block <- '(' root ')' / '{' root '}' / '[' root ']' - link <- int + link <- int / '-' product <- op '*' factor factor <- int / '{' int+ '}' / '[' int+ ']' int <- < [0-9]+ > @@ -115,8 +123,12 @@ peg::parser build_parser() { parser["link"] = [](const peg::SemanticValues &vs) -> std::any { codegen cg; - auto order = std::any_cast(vs[0]); - cg.link(order); + if (vs.choice() == 0) { + auto order = std::any_cast(vs[0]); + cg.link(order); + } else { + cg.free(); + } return cg; }; @@ -211,7 +223,7 @@ std::vector compile(const std::string &source) { struct Graph { size_t rank{}; - std::vector> edges{}; + std::vector edges{}; }; Graph eval(const std::vector &ops) { @@ -222,8 +234,13 @@ Graph eval(const std::vector &ops) { for (const auto &op: ops) { switch (op.code) { + case Op::FREE: case Op::LINK: { - auto order = op.value; + tc::Mult order = tc::FREE; + + if (op.code == Op::LINK) { + order = op.value; + } auto top = stacks.back().top(); auto curr = g.rank++;