Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoftco committed Dec 19, 2023
1 parent b924602 commit b7a0338
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 195 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ UseTab: Never
PointerAlignment: Left
IndentCaseLabels: true
AlwaysBreakTemplateDeclarations: Yes
InsertBraces: True
30 changes: 20 additions & 10 deletions include/qasm3tools/ast/decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ class SubroutineDecl final : public GlobalStmt, public Decl {
os << (it == params_.begin() ? "" : ", ") << **it;
}
os << ")";
if (return_type_)
if (return_type_) {
os << " -> " << **return_type_;
}
os << " ";
body_->pretty_print(os, indents);
return os;
Expand All @@ -216,11 +217,13 @@ class SubroutineDecl final : public GlobalStmt, public Decl {
protected:
SubroutineDecl* clone() const override {
std::vector<ptr<Param>> tmp_params;
for (auto& x : params_)
for (auto& x : params_) {
tmp_params.emplace_back(object::clone(*x));
}
std::optional<ptr<NonArrayType>> tmp_return = std::nullopt;
if (return_type_)
if (return_type_) {
tmp_return = object::clone(**return_type_);
}
return new SubroutineDecl(pos_, id_, std::move(tmp_params),
std::move(tmp_return), object::clone(*body_));
}
Expand Down Expand Up @@ -283,20 +286,23 @@ class ExternDecl final : public GlobalStmt, public Decl {
os << (it == param_types_.begin() ? "" : ", ") << **it;
}
os << ")";
if (return_type_)
if (return_type_) {
os << " -> " << **return_type_;
}
os << ";\n";
return os;
}

protected:
ExternDecl* clone() const override {
std::vector<ptr<ClassicalType>> tmp_params;
for (auto& x : param_types_)
for (auto& x : param_types_) {
tmp_params.emplace_back(object::clone(*x));
}
std::optional<ptr<NonArrayType>> tmp_return = std::nullopt;
if (return_type_)
if (return_type_) {
tmp_return = object::clone(**return_type_);
}
return new ExternDecl(pos_, id_, std::move(tmp_params),
std::move(tmp_return));
}
Expand Down Expand Up @@ -377,8 +383,9 @@ class GateDecl final : public GlobalStmt, public Decl {
void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os, bool suppress_std,
size_t indents) const override {
if (suppress_std && is_std_gate(id_))
if (suppress_std && is_std_gate(id_)) {
return os;
}

os << "gate " << id_;
if (c_params_.size() > 0) {
Expand Down Expand Up @@ -510,20 +517,23 @@ class ClassicalDecl final : public Stmt, public Decl {

void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os, bool, size_t) const override {
if (is_const_)
if (is_const_) {
os << "const ";
}
os << *type_ << " " << id_;
if (equalsexp_)
if (equalsexp_) {
os << " = " << **equalsexp_;
}
os << ";\n";
return os;
}

protected:
ClassicalDecl* clone() const override {
std::optional<ptr<Expr>> tmp = std::nullopt;
if (equalsexp_)
if (equalsexp_) {
tmp = object::clone(**equalsexp_);
}
return new ClassicalDecl(pos_, id_, object::clone(*type_),
std::move(tmp), is_const_);
}
Expand Down
36 changes: 24 additions & 12 deletions include/qasm3tools/ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ class BExpr final : public Expr {
auto lexp = lexp_->constant_eval();
auto rexp = rexp_->constant_eval();

if (!lexp || !rexp)
if (!lexp || !rexp) {
return std::nullopt;
}

switch (op_) {
case BinaryOp::EQ:
Expand Down Expand Up @@ -420,8 +421,9 @@ class UExpr final : public Expr {
std::optional<double> constant_eval() const override {
auto expr = exp_->constant_eval();

if (!expr)
if (!expr) {
return std::nullopt;
}

switch (op_) {
case UnaryOp::Neg:
Expand Down Expand Up @@ -505,13 +507,15 @@ class MathExpr final : public Expr {
void set_arg(int i, ptr<Expr> expr) { args_[i] = std::move(expr); }

std::optional<double> constant_eval() const override {
if (args_.size() != 1)
if (args_.size() != 1) {
return std::nullopt;
}

auto expr = args_[0]->constant_eval();

if (!expr)
if (!expr) {
return std::nullopt;
}

switch (op_) {
case MathOp::Arcsin:
Expand Down Expand Up @@ -541,8 +545,9 @@ class MathExpr final : public Expr {
(void)ctx;

os << op_ << "(";
for (auto it = args_.begin(); it != args_.end(); it++)
for (auto it = args_.begin(); it != args_.end(); it++) {
os << (it == args_.begin() ? "" : ",") << **it;
}
os << ")";

return os;
Expand All @@ -551,8 +556,9 @@ class MathExpr final : public Expr {
protected:
MathExpr* clone() const override {
std::vector<ptr<Expr>> tmp;
for (auto& x : args_)
for (auto& x : args_) {
tmp.emplace_back(object::clone(*x));
}
return new MathExpr(pos_, op_, std::move(tmp));
}
};
Expand Down Expand Up @@ -691,8 +697,9 @@ class SizeofExpr final : public Expr {
(void)ctx;

os << "sizeof(" << *arr_;
if (dim_)
if (dim_) {
os << ", " << **dim_;
}
os << ")";

return os;
Expand All @@ -701,8 +708,9 @@ class SizeofExpr final : public Expr {
protected:
SizeofExpr* clone() const override {
std::optional<ptr<Expr>> tmp = std::nullopt;
if (dim_)
if (dim_) {
tmp = object::clone(**dim_);
}
return new SizeofExpr(pos_, object::clone(*arr_), std::move(tmp));
}
};
Expand Down Expand Up @@ -774,8 +782,9 @@ class FunctionCall final : public Expr {
(void)ctx;

os << name_ << "(";
for (auto it = args_.begin(); it != args_.end(); it++)
for (auto it = args_.begin(); it != args_.end(); it++) {
os << (it == args_.begin() ? "" : ",") << **it;
}
os << ")";

return os;
Expand All @@ -784,8 +793,9 @@ class FunctionCall final : public Expr {
protected:
FunctionCall* clone() const override {
std::vector<ptr<Expr>> tmp;
for (auto& x : args_)
for (auto& x : args_) {
tmp.emplace_back(object::clone(*x));
}
return new FunctionCall(pos_, name_, std::move(tmp));
}
};
Expand Down Expand Up @@ -1258,8 +1268,9 @@ class ArrayInitExpr final : public Expr {
(void)ctx;

os << "{";
for (auto it = arr_.begin(); it != arr_.end(); it++)
for (auto it = arr_.begin(); it != arr_.end(); it++) {
os << (it == arr_.begin() ? "" : ", ") << **it;
}
os << "}";

return os;
Expand All @@ -1268,8 +1279,9 @@ class ArrayInitExpr final : public Expr {
protected:
ArrayInitExpr* clone() const override {
std::vector<ptr<Expr>> tmp;
for (auto& x : arr_)
for (auto& x : arr_) {
tmp.emplace_back(object::clone(*x));
}
return new ArrayInitExpr(pos_, std::move(tmp));
}
};
Expand Down
42 changes: 28 additions & 14 deletions include/qasm3tools/ast/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,19 @@ class CtrlModifier : public GateModifier {
void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os) const override {
os << (neg_ ? "negctrl" : "ctrl");
if (n_)
if (n_) {
os << "(" << **n_ << ")";
}
os << " @ ";
return os;
}

protected:
CtrlModifier* clone() const override {
std::optional<ptr<Expr>> tmp = std::nullopt;
if (n_)
if (n_) {
tmp = object::clone(**n_);
}
return new CtrlModifier(pos_, neg_, std::move(tmp));
}
};
Expand Down Expand Up @@ -242,8 +244,9 @@ class Gate : public QuantumStmt {
* \param f Void function accepting a reference to an argument
*/
void foreach_qarg(std::function<void(IndexId&)> f) {
for (auto& x : q_args_)
for (auto& x : q_args_) {
f(*x);
}
}

/**
Expand All @@ -261,13 +264,15 @@ class Gate : public QuantumStmt {
std::vector<ptr<IndexId>> q_args_; ///< list of quantum arguments

void print_modifiers(std::ostream& os) const {
for (auto& x : modifiers_)
for (auto& x : modifiers_) {
os << *x;
}
}

void print_qargs(std::ostream& os) const {
for (auto it = q_args_.begin(); it != q_args_.end(); it++)
for (auto it = q_args_.begin(); it != q_args_.end(); it++) {
os << (it == q_args_.begin() ? " " : ",") << **it;
}
}

virtual Gate* clone() const = 0;
Expand Down Expand Up @@ -367,11 +372,13 @@ class UGate final : public Gate {
protected:
UGate* clone() const override {
std::list<ptr<GateModifier>> tmp;
for (auto& x : modifiers_)
for (auto& x : modifiers_) {
tmp.emplace_back(object::clone(*x));
}
std::vector<ptr<IndexId>> q_tmp;
for (auto& x : q_args_)
for (auto& x : q_args_) {
q_tmp.emplace_back(object::clone(*x));
}
return new UGate(pos_, std::move(tmp), object::clone(*theta_),
object::clone(*phi_), object::clone(*lambda_),
std::move(q_tmp));
Expand Down Expand Up @@ -437,11 +444,13 @@ class GPhase final : public Gate {
protected:
GPhase* clone() const override {
std::list<ptr<GateModifier>> tmp;
for (auto& x : modifiers_)
for (auto& x : modifiers_) {
tmp.emplace_back(object::clone(*x));
}
std::vector<ptr<IndexId>> q_tmp;
for (auto& x : q_args_)
for (auto& x : q_args_) {
q_tmp.emplace_back(object::clone(*x));
}
return new GPhase(pos_, std::move(tmp), object::clone(*gamma_),
std::move(q_tmp));
}
Expand Down Expand Up @@ -513,8 +522,9 @@ class DeclaredGate final : public Gate {
* \param f Void function accepting an expression reference
*/
void foreach_carg(std::function<void(Expr&)> f) {
for (auto& x : c_args_)
for (auto& x : c_args_) {
f(*x);
}
}

/**
Expand All @@ -531,8 +541,9 @@ class DeclaredGate final : public Gate {
os << name_;
if (c_args_.size() > 0) {
os << "(";
for (auto it = c_args_.begin(); it != c_args_.end(); it++)
for (auto it = c_args_.begin(); it != c_args_.end(); it++) {
os << (it == c_args_.begin() ? "" : ",") << **it;
}
os << ")";
}
print_qargs(os);
Expand All @@ -543,14 +554,17 @@ class DeclaredGate final : public Gate {
protected:
DeclaredGate* clone() const override {
std::list<ptr<GateModifier>> tmp;
for (auto& x : modifiers_)
for (auto& x : modifiers_) {
tmp.emplace_back(object::clone(*x));
}
std::vector<ptr<Expr>> c_tmp;
for (auto& x : c_args_)
for (auto& x : c_args_) {
c_tmp.emplace_back(object::clone(*x));
}
std::vector<ptr<IndexId>> q_tmp;
for (auto& x : q_args_)
for (auto& x : q_args_) {
q_tmp.emplace_back(object::clone(*x));
}

return new DeclaredGate(pos_, std::move(tmp), name_, std::move(c_tmp),
std::move(q_tmp));
Expand Down
Loading

0 comments on commit b7a0338

Please sign in to comment.