Skip to content

Commit

Permalink
registry test
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeathlyCow committed Sep 18, 2023
1 parent be07efb commit c7fff2a
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/production/uniform_branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ UniformBranch::UniformBranch(const UniformBranch& old)
}

std::optional<UniformBranch>
UniformBranch::parse(std::vector<String_t> raw, const Registry& registry, ErrorHolder& errors)
UniformBranch::parse(const std::vector<String_t>& raw, const Registry& registry, ErrorHolder& errors)
{
std::vector<std::shared_ptr<Production>> choices;

Expand Down
2 changes: 1 addition & 1 deletion src/production/uniform_branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace calyx

~UniformBranch() = default;

static std::optional<UniformBranch> parse(std::vector<String_t> raw, const Registry& registry, ErrorHolder& errors);
static std::optional<UniformBranch> parse(const std::vector<String_t>& raw, const Registry& registry, ErrorHolder& errors);

std::optional<Expansion> evaluate(
Registry& registry,
Expand Down
2 changes: 1 addition & 1 deletion src/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Registry::defineRule(String_t term, std::vector<String_t> production, ErrorHolde
return;
}

_rules.emplace(term, std::make_shared<Rule>(*rule));
_rules.emplace(std::move(term), std::make_shared<Rule>(*rule));
}

std::optional<Expansion>
Expand Down
16 changes: 5 additions & 11 deletions src/rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@

using namespace calyx;

Rule::Rule(String_t term, std::shared_ptr<ProductionBranch> production)
: _production(production),
_term(term)
{
}

Rule::Rule(const Rule& other)
: _production(other._production),
_term(other._term)
Rule::Rule(String_t term, std::shared_ptr<ProductionBranch> production):
_term(std::move(term)),
_production(production)
{
}

Expand All @@ -25,7 +19,7 @@ Rule::empty(String_t term)
}

std::optional<Rule>
Rule::build(String_t term, std::vector<String_t> productions, const Registry& registry, ErrorHolder& errors)
Rule::build(String_t term, const std::vector<String_t>& productions, const Registry& registry, ErrorHolder& errors)
{
std::optional<UniformBranch> branch = UniformBranch::parse(productions, registry, errors);

Expand All @@ -34,7 +28,7 @@ Rule::build(String_t term, std::vector<String_t> productions, const Registry& re
return {};
}

return Rule(term, std::make_unique<UniformBranch>(*branch));
return Rule(std::move(term), std::make_unique<UniformBranch>(*branch));
}

std::optional<Expansion>
Expand Down
4 changes: 2 additions & 2 deletions src/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ namespace calyx
*/
Rule(String_t term, std::shared_ptr<ProductionBranch> production);

Rule(const Rule& other);
Rule(const Rule& other) = default;

Rule(Rule&& other) = default;

~Rule() = default;

static Rule empty(String_t term);

static std::optional<Rule> build(String_t term, std::vector<String_t> productions, const Registry& registry, ErrorHolder& errors);
static std::optional<Rule> build(String_t term, const std::vector<String_t>& productions, const Registry& registry, ErrorHolder& errors);

std::optional<Expansion> evaluate(
Registry& registry,
Expand Down
3 changes: 2 additions & 1 deletion src/syntax/expression_node.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "expression_node.h"


using namespace calyx;

#define MEMO_SIGIL '@'
#define UNIQUE_SIGIL '$'

ExpressionNode::ExpressionNode(const String_t reference)
ExpressionNode::ExpressionNode(String_t reference)
: _reference(reference)
{

Expand Down
2 changes: 1 addition & 1 deletion src/syntax/expression_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace calyx
class ExpressionNode : public Production
{
public:
ExpressionNode(const String_t reference);
ExpressionNode(String_t reference);

~ExpressionNode() = default;

Expand Down
11 changes: 6 additions & 5 deletions src/syntax/template_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ using namespace calyx;


std::vector<std::string>
split(const std::string& s, const char delim) {
split(const std::string& s, const char delim)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delim)) {
while (std::getline(tokenStream, token, delim))
{
tokens.push_back(token);
}
return tokens;
Expand Down Expand Up @@ -68,7 +70,6 @@ TemplateNode::fragmentString(const std::string& raw)
TemplateNode::TemplateNode(std::vector<std::shared_ptr<Production>> concatNodes)
: _concatNodes(concatNodes)
{

}

std::optional<TemplateNode>
Expand All @@ -91,7 +92,7 @@ TemplateNode::parse(const String_t& raw, const Registry& registry, ErrorHolder&
if (atom.starts_with(START_TOKEN) && atom.ends_with(END_TOKEN))
{
// remove the braces
std::string expression = atom.substr(1, atom.size() - 1);
std::string expression = atom.substr(1, atom.size() - 2);

std::vector<std::string> components = split(expression, DEREF_TOKEN);

Expand All @@ -101,7 +102,7 @@ TemplateNode::parse(const String_t& raw, const Registry& registry, ErrorHolder&
}
else
{
auto prod = ExpressionNode::parse(convertedAtom, registry, errors);
auto prod = ExpressionNode::parse(ops.fromString(expression), registry, errors);

if (!prod)
{
Expand Down
53 changes: 53 additions & 0 deletions test/registry_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <catch2/catch_all.hpp>

#include <include/calyx.h>
#include <registry.h>

using namespace calyx;

TEST_CASE("Evaluate start rule")
{
Registry registry;
ErrorHolder errs;
const Options& ops = registry.getOptions();
String_t atom = ops.fromString("atom");
String_t start = ops.fromString("start");

registry.defineRule(
start,
std::vector { atom },
errs
);

REQUIRE_FALSE(errs.hasError());

std::optional<Expansion> exp = registry.evaluate(start, errs);

REQUIRE_FALSE(errs.hasError());
REQUIRE(exp.has_value());
REQUIRE(exp->getSymbol() == Exp::RESULT);
REQUIRE(exp->flatten(ops) == atom);
}

TEST_CASE("Evaluate recursive rules")
{
Registry registry;
ErrorHolder errs;
const Options& ops = registry.getOptions();
String_t start = ops.fromString("start");
String_t prod = ops.fromString("{atom}");
String_t atom = ops.fromString("atom");

registry.defineRule(start, std::vector { prod }, errs);
REQUIRE_FALSE(errs.hasError());
registry.defineRule(atom, std::vector { atom }, errs);
REQUIRE_FALSE(errs.hasError());

std::optional<Expansion> exp = registry.evaluate(start, errs);

REQUIRE_FALSE(errs.hasError());
REQUIRE(exp.has_value());
REQUIRE(exp->getSymbol() == Exp::RESULT);
REQUIRE(exp->flatten(ops) == atom);
}
1 change: 1 addition & 0 deletions vsxmake2019/test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ xmake version 2.7.8+master.d6ee79eed
<CompileAs>CompileAsCpp</CompileAs>
</ClCompile>
<ClCompile Include="..\..\test\options_test.cpp" />
<ClCompile Include="..\..\test\registry_test.cpp" />



Expand Down

0 comments on commit c7fff2a

Please sign in to comment.