Skip to content

Commit

Permalink
Add ==/!= operators for error instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmmr committed Jun 12, 2024
1 parent e2ab699 commit e0728cc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class HasLabel;

namespace error {
class Ctor;
class Equal;
class Unequal;
class Description;
} // namespace error

Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/node-tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ constexpr Tag Unequal = 1106;
namespace error {
constexpr Tag Ctor = 1200;
constexpr Tag Description = 1201;
constexpr Tag Equal = 2702;
constexpr Tag Unequal = 2703;
} // namespace error

namespace exception {
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/operators/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace hilti::operator_ {

HILTI_NODE_OPERATOR(error, Ctor)
HILTI_NODE_OPERATOR(error, Equal)
HILTI_NODE_OPERATOR(error, Unequal)
HILTI_NODE_OPERATOR(error, Description)

} // namespace hilti::operator_
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/visitor-dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class Dispatcher {
virtual void operator()(hilti::operator_::enum_::CtorUnsigned* n) {}
virtual void operator()(hilti::operator_::enum_::HasLabel* n) {}
virtual void operator()(hilti::operator_::error::Ctor* n) {}
virtual void operator()(hilti::operator_::error::Equal* n) {}
virtual void operator()(hilti::operator_::error::Unequal* n) {}
virtual void operator()(hilti::operator_::error::Description* n) {}
virtual void operator()(hilti::operator_::exception::Ctor* n) {}
virtual void operator()(hilti::operator_::exception::Description* n) {}
Expand Down
34 changes: 34 additions & 0 deletions hilti/toolchain/src/ast/operators/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ class Ctor : public Operator {
};
HILTI_OPERATOR_IMPLEMENTATION(Ctor);

class Equal : public Operator {
public:
Signature signature(Builder* builder) const final {
return Signature{
.kind = Kind::Equal,
.op0 = {parameter::Kind::In, builder->typeError()},
.op1 = {parameter::Kind::In, builder->typeError()},
.result = {Constness::Const, builder->typeBool()},
.ns = "error",
.doc = "Compares two error descriptions lexicographically.",
};
}

HILTI_OPERATOR(hilti, error::Equal)
};
HILTI_OPERATOR_IMPLEMENTATION(Equal);

class Unequal : public Operator {
public:
Signature signature(Builder* builder) const final {
return Signature{
.kind = Kind::Unequal,
.op0 = {parameter::Kind::In, builder->typeError()},
.op1 = {parameter::Kind::In, builder->typeError()},
.result = {Constness::Const, builder->typeBool()},
.ns = "error",
.doc = "Compares two error descriptions lexicographically.",
};
}

HILTI_OPERATOR(hilti, error::Unequal)
};
HILTI_OPERATOR_IMPLEMENTATION(Unequal);

class Description : public BuiltInMemberCall {
public:
Signature signature(Builder* builder) const final {
Expand Down
4 changes: 4 additions & 0 deletions hilti/toolchain/src/compiler/codegen/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ struct Visitor : hilti::visitor::PreOrder {
result = fmt("::hilti::rt::result::Error(%s)", args[0]);
}

void operator()(operator_::error::Equal* n) final { result = binary(n, "=="); }

void operator()(operator_::error::Unequal* n) final { result = binary(n, "!="); }

// Exception

void operator()(operator_::exception::Ctor* n) final {
Expand Down

0 comments on commit e0728cc

Please sign in to comment.