Skip to content

Commit

Permalink
fix: added custom operator for type equality. Fixed recursive type comp
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed Apr 24, 2023
1 parent 14b819c commit 1bcccf7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ void Codegen::visit(const FuncDecl *node) {
typeResult = new Value("", new Type(TY_PTR, Builder->getPtrTy(), result->getType()));
}

if (defaultValResult != nullptr && *typeResult->getType() != *defaultValResult->getType()) {
if (defaultValResult != nullptr && !typeResult->getType()->isEqual(defaultValResult->getType())) {
throw CodegenError(node->getSpan(), "Declared parameter type and default value do not match for {}", param->name);
}

Expand Down Expand Up @@ -660,7 +660,7 @@ void Codegen::visit(const ExternFuncDecl *node) {
typeResult = new Value("", new Type(TY_PTR, Builder->getPtrTy(), result->getType()));
}

if (defaultValResult != nullptr && *typeResult->getType() != *defaultValResult->getType()) {
if (defaultValResult != nullptr && !typeResult->getType()->isEqual(defaultValResult->getType())) {
throw CodegenError(node->getSpan(), "Declared parameter type and default value do not match for {}", param->name);
}

Expand Down Expand Up @@ -1264,9 +1264,9 @@ void Codegen::visit(const IsOp *node) {
llvm::Value *val;

if (node->getOperator() == TokenType::IS) {
val = *left_type == *right_type ? Builder->getTrue() : Builder->getFalse();
val = left_type->isEqual(right_type) ? Builder->getTrue() : Builder->getFalse();
} else {
val = *left_type == *right_type ? Builder->getFalse() : Builder->getTrue();
val = left_type->isEqual(right_type) ? Builder->getFalse() : Builder->getTrue();
}

result = new Value("", new Type(TY_BOOL, Builder->getInt1Ty()), val);
Expand Down Expand Up @@ -1464,8 +1464,11 @@ lesma::Type *Codegen::GetExtendedType(lesma::Type *left, lesma::Type *right) {
}

lesma::Value *Codegen::Cast(llvm::SMRange span, lesma::Value *val, lesma::Type *type) {
if (type == nullptr)
return val;

// If they're the same type
if (*val->getType() == *type)
if (val->getType()->isEqual(type))
return val;

if (type->is(TY_INT)) {
Expand Down
2 changes: 1 addition & 1 deletion src/liblesma/Symbol/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Value *SymbolTable::lookupFunction(const std::string &name, std::vector<lesma::T
size_t numParams = std::max(funcParamTypes.size(), paramTypes.size());
for (size_t i = 0; i < numParams; ++i) {
if (i < funcParamTypes.size() && i < paramTypes.size()) {
if (*funcParamTypes[i]->type != *paramTypes[i]) {
if (!funcParamTypes[i]->type->isEqual(paramTypes[i])) {
paramsMatch = false;
break;
}
Expand Down
11 changes: 6 additions & 5 deletions src/liblesma/Symbol/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ namespace lesma {
void setElementType(lesma::Type *type) { elementType = type; }
void setReturnType(lesma::Type *type) { returnType = type; }

friend bool operator==(const Type &lhs, const Type &rhs) {
return lhs.getBaseType() == rhs.getBaseType() && lhs.getElementType() == rhs.getElementType();
}
friend bool operator!=(const Type &lhs, const Type &rhs) {
return !(lhs == rhs);
bool isEqual(Type *rhs) {
if (this == nullptr && rhs == nullptr)
return true;
if (this == nullptr || rhs == nullptr)
return false;
return this->getBaseType() == rhs->getBaseType() && this->getElementType()->isEqual(rhs->getElementType());
}

[[nodiscard]] std::string toString() const {
Expand Down

0 comments on commit 1bcccf7

Please sign in to comment.