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 1d18942 commit 1178f54
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 41 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
9 changes: 6 additions & 3 deletions include/qasmtools/ast/decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ class GateDecl final : public Stmt, public Decl {
* \param f A void function taking a reference to a Gate
*/
void foreach_stmt(std::function<void(Gate&)> f) {
for (auto it = body_.begin(); it != body_.end(); it++)
for (auto it = body_.begin(); it != body_.end(); it++) {
f(**it);
}
}

/**
Expand All @@ -183,8 +184,9 @@ class GateDecl final : public Stmt, public Decl {
void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os,
bool suppress_std) const override {
if (suppress_std && is_std_qelib(id_))
if (suppress_std && is_std_qelib(id_)) {
return os;
}

os << (opaque_ ? "opaque " : "gate ") << id_;
if (c_params_.size() > 0) {
Expand Down Expand Up @@ -381,8 +383,9 @@ class AncillaDecl final : public Gate, public Decl {

void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os, bool) const override {
if (dirty_)
if (dirty_) {
os << "dirty ";
}
os << "ancilla " << id_ << "[" << size_ << "];\n";
return os;
}
Expand Down
10 changes: 6 additions & 4 deletions include/qasmtools/ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,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::Plus:
Expand Down Expand Up @@ -293,8 +294,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 All @@ -320,9 +322,9 @@ class UExpr final : public Expr {
(void)ctx;

os << op_;
if (op_ == UnaryOp::Neg)
if (op_ == UnaryOp::Neg) {
exp_->pretty_print(os, true);
else {
} else {
os << "(";
exp_->pretty_print(os, false);
os << ")";
Expand Down
6 changes: 4 additions & 2 deletions include/qasmtools/ast/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ class Program : public ASTNode {
* \param f Void function accepting a reference to a statement
*/
void foreach_stmt(std::function<void(Stmt&)> f) {
for (auto it = body_.begin(); it != body_.end(); it++)
for (auto it = body_.begin(); it != body_.end(); it++) {
f(**it);
}
}

/**
Expand All @@ -120,8 +121,9 @@ class Program : public ASTNode {
void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os) const override {
os << "OPENQASM 2.0;\n";
if (std_include_)
if (std_include_) {
os << "include \"qelib1.inc\";\n";
}
os << "\n";
for (auto it = body_.begin(); it != body_.end(); it++) {
(*it)->pretty_print(os, std_include_);
Expand Down
5 changes: 3 additions & 2 deletions include/qasmtools/ast/replacer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,11 @@ class GateReplacer final : public Replacer {
std::optional<std::list<ptr<Gate>>> replace_gate(Gate& gate) {
auto it = replacements_.find(gate.uid());

if (it != replacements_.end())
if (it != replacements_.end()) {
return std::move(it->second);
else
} else {
return std::nullopt;
}
}
};

Expand Down
6 changes: 4 additions & 2 deletions include/qasmtools/ast/semantic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ class SemanticChecker final : public Visitor {
* \param typ The type of the symbol
*/
void set(const ast::symbol& id, Type typ) {
if (symbol_table_.empty())
if (symbol_table_.empty()) {
throw std::logic_error("No current symbol table!");
}

symbol_table_.front()[id] = typ;
}
Expand Down Expand Up @@ -467,8 +468,9 @@ class SemanticChecker final : public Visitor {
*/
inline void check_source(Program& prog) {
SemanticChecker analysis;
if (analysis.run(prog))
if (analysis.run(prog)) {
throw SemanticError();
}
}

} /* namespace ast */
Expand Down
9 changes: 6 additions & 3 deletions include/qasmtools/ast/stmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,9 @@ class BarrierGate final : public Gate {
* \param f Void function accepting a reference to the argument
*/
void foreach_arg(std::function<void(VarAccess&)> f) {
for (auto it = args_.begin(); it != args_.end(); it++)
for (auto it = args_.begin(); it != args_.end(); it++) {
f(*it);
}
}

/**
Expand Down Expand Up @@ -612,8 +613,9 @@ class DeclaredGate final : public Gate {
* \param f Void function accepting an expression reference
*/
void foreach_carg(std::function<void(Expr&)> f) {
for (auto it = c_args_.begin(); it != c_args_.end(); it++)
for (auto it = c_args_.begin(); it != c_args_.end(); it++) {
f(**it);
}
}

/**
Expand All @@ -622,8 +624,9 @@ class DeclaredGate final : public Gate {
* \param f Void function accepting a reference to an argument
*/
void foreach_qarg(std::function<void(VarAccess&)> f) {
for (auto it = q_args_.begin(); it != q_args_.end(); it++)
for (auto it = q_args_.begin(); it != q_args_.end(); it++) {
f(*it);
}
}

/**
Expand Down
15 changes: 10 additions & 5 deletions include/qasmtools/ast/traversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,32 @@ class Traverse : public Visitor {
gate.tgt().accept(*this);
}
void visit(BarrierGate& gate) override {
for (int i = 0; i < gate.num_args(); i++)
for (int i = 0; i < gate.num_args(); i++) {
gate.arg(i).accept(*this);
}
}
void visit(DeclaredGate& gate) override {
for (int i = 0; i < gate.num_cargs(); i++)
for (int i = 0; i < gate.num_cargs(); i++) {
gate.carg(i).accept(*this);
for (int i = 0; i < gate.num_qargs(); i++)
}
for (int i = 0; i < gate.num_qargs(); i++) {
gate.qarg(i).accept(*this);
}
}

void visit(GateDecl& decl) override {
for (auto it = decl.begin(); it != decl.end(); it++)
for (auto it = decl.begin(); it != decl.end(); it++) {
(**it).accept(*this);
}
}

void visit(OracleDecl& decl) override {}
void visit(RegisterDecl& decl) override {}
void visit(AncillaDecl& decl) override {}
void visit(Program& prog) override {
for (auto it = prog.begin(); it != prog.end(); it++)
for (auto it = prog.begin(); it != prog.end(); it++) {
(**it).accept(*this);
}
}
};

Expand Down
13 changes: 8 additions & 5 deletions include/qasmtools/ast/var.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ class VarAccess final : public ASTNode {
* Used to allow variable accesses as keys in ordered maps
*/
bool operator<(const VarAccess& v) const {
if (var_ == v.var_)
if (var_ == v.var_) {
return offset_ < v.offset_;
else
} else {
return var_ < v.var_;
}
}

/**
Expand All @@ -130,10 +131,11 @@ class VarAccess final : public ASTNode {
* \return true if the variable access contains v
*/
bool contains(const VarAccess& v) const {
if (offset_)
if (offset_) {
return *this == v;
else
} else {
return v.var_ == var_;
}
}

/**
Expand All @@ -157,8 +159,9 @@ class VarAccess final : public ASTNode {
void accept(Visitor& visitor) override { visitor.visit(*this); }
std::ostream& pretty_print(std::ostream& os) const override {
os << var_;
if (offset_)
if (offset_) {
os << "[" << *offset_ << "]";
}
return os;
}

Expand Down
5 changes: 3 additions & 2 deletions include/qasmtools/parser/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ class Lexer {
}

pos_.advance_column(consumed);
if (consumed != 0)
if (consumed != 0) {
return true;
else
} else {
return false;
}
}

/**
Expand Down
9 changes: 6 additions & 3 deletions include/qasmtools/parser/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ class Parser {
ast::ptr<ast::Program> parse(bool check = true) {
// Parse the program
auto result = parse_program();
if (error_)
if (error_) {
throw ParseError();
}

// Perform semantic analysis before returning
if (check)
if (check) {
ast::check_source(*result);
}

return result;
}
Expand All @@ -102,8 +104,9 @@ class Parser {
*/
void consume_token(bool reset = false) {
current_token_ = pp_lexer_.next_token();
if (reset)
if (reset) {
supress_errors_ = false;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion include/qasmtools/parser/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ class Preprocessor {
}

auto target = token.as_string();
if (target == "qelib1.inc")
if (target == "qelib1.inc") {
std_include_ = true;
}

token = current_lexer_->next_token();
if (token.is_not(Token::Kind::semicolon)) {
Expand Down
21 changes: 14 additions & 7 deletions include/qasmtools/tools/ast_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class ASTPrinter final : public ast::Visitor {
// Variables
void visit(ast::VarAccess& ap) {
os_ << prefix_ << "|- Var(" << ap.var();
if (ap.offset())
if (ap.offset()) {
os_ << "[" << *ap.offset() << "]";
}
os_ << ")\n";
}

Expand Down Expand Up @@ -153,14 +154,17 @@ class ASTPrinter final : public ast::Visitor {
// Declarations
void visit(ast::GateDecl& decl) {
os_ << prefix_ << "|- Gate Decl(" << decl.id() << "(";
for (auto& param : decl.c_params())
for (auto& param : decl.c_params()) {
os_ << param << ",";
}
os_ << ")[";
for (auto& param : decl.q_params())
for (auto& param : decl.q_params()) {
os_ << param << ",";
}
os_ << "]";
if (decl.is_opaque())
if (decl.is_opaque()) {
os_ << ", opaque";
}
os_ << ")\n";

prefix_ += " ";
Expand All @@ -170,24 +174,27 @@ class ASTPrinter final : public ast::Visitor {

void visit(ast::OracleDecl& decl) {
os_ << prefix_ << "|- Oracle Decl(" << decl.id() << "[";
for (auto& param : decl.params())
for (auto& param : decl.params()) {
os_ << param << ",";
}
os_ << "] = " << decl.fname() << ")\n";
}

void visit(ast::RegisterDecl& decl) {
os_ << prefix_ << "|- Register Decl(" << decl.id() << "[" << decl.size()
<< "]";
if (decl.is_quantum())
if (decl.is_quantum()) {
os_ << ", quantum";
}
os_ << ")\n";
}

void visit(ast::AncillaDecl& decl) {
os_ << prefix_ << "|- Ancilla Decl(" << decl.id() << "[" << decl.size()
<< "]";
if (decl.is_dirty())
if (decl.is_dirty()) {
os_ << ", dirty";
}
os_ << ")\n";
}

Expand Down
6 changes: 4 additions & 2 deletions include/qasmtools/utils/angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ class Angle {

public:
constexpr Angle(int n, int d) : value_(std::make_pair(n, d)) {
if (d == 0)
if (d == 0) {
throw std::invalid_argument(
"Trying to construct angle with denominator 0");
}

normalize();
}
constexpr Angle(fraction angle) : value_(angle) {
if (angle.second == 0)
if (angle.second == 0) {
throw std::invalid_argument(
"Trying to construct angle with denominator 0");
}

normalize();
}
Expand Down

0 comments on commit 1178f54

Please sign in to comment.