Skip to content

Commit

Permalink
Use map instead of switch-case
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Mar 24, 2023
1 parent b274fc3 commit 6b22fa1
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/codegen/CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "llvm/Support/Host.h"

#include <memory>
#include <unordered_map>

CodeGenerator::CodeGenerator(llvm::Module &module)
: module(module)
Expand Down Expand Up @@ -164,17 +165,13 @@ llvm::Value *CodeGenerator::allocate_variable(const std::string &name) {
}

llvm::Value *CodeGenerator::create_if_condition(const IfStatement *if_statement) {
using enum IfStatementType;
using enum llvm::CmpInst::Predicate;
const std::unordered_map type_predicate_mapping = {std::pair{positive, ICMP_SLT},
std::pair{zero, ICMP_EQ},
std::pair{negative, ICMP_SGT}};
auto condition = visit(if_statement->expression.get());
auto null = llvm::ConstantInt::get(builder.getInt32Ty(), 0);
switch (if_statement->type) {
using enum IfStatementType;
case positive:
return builder.CreateICmp(llvm::CmpInst::ICMP_SLT, null, condition);
case zero:
return builder.CreateICmp(llvm::CmpInst::ICMP_EQ, null, condition);
case negative:
return builder.CreateICmp(llvm::CmpInst::ICMP_SGT, null, condition);
default:
llvm_unreachable("Unknown if-statement type.");
}
auto predicate = type_predicate_mapping.at(if_statement->type);
return builder.CreateICmp(predicate, null, condition);
}

0 comments on commit 6b22fa1

Please sign in to comment.