Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for AST to optree converter #164

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/include/compiler/optree/adaptors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ struct FunctionOp : Adaptor {
struct FunctionCallOp : Adaptor {
OPTREE_ADAPTOR_HELPER(Adaptor, "FunctionCall")

void init(const std::string &name, const Type::Ptr &resultType, const std::vector<Value::Ptr> &arguments);
void init(const FunctionOp &callee, const std::vector<Value::Ptr> &arguments);
void init(const std::string &name, const Type::Ptr &resultType, const std::vector<Value::Ptr> &arguments = {});
void init(const FunctionOp &callee, const std::vector<Value::Ptr> &arguments = {});

OPTREE_ADAPTOR_ATTRIBUTE(name, setName, std::string, 0)
OPTREE_ADAPTOR_RESULT(result, 0)
Expand Down
23 changes: 19 additions & 4 deletions compiler/lib/frontend/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ void processVariableDeclaration(const Node::Ptr &node, ConverterContext &ctx) {
return;
}
auto defValue = visitNode(defNode, ctx);
const auto &defType = defValue->type;
if (*type != *defType) {
if (auto castOp = insertNumericCastOp(type, defValue, ctx.builder, defNode->ref))
defValue = castOp.result();
}
ctx.insert<StoreOp>(defNode->ref, allocOp.result(), defValue);
}
}
Expand Down Expand Up @@ -257,18 +262,28 @@ Value::Ptr visitBinaryOperation(const Node::Ptr &node, ConverterContext &ctx) {
}
auto lhs = visitNode(lhsNode, ctx);
auto rhs = visitNode(rhsNode, ctx);
const Type::Ptr &lhsType = lhs->type;
Type::Ptr lhsType = lhs->type;
const Type::Ptr &rhsType = rhs->type;
auto typeError = [](const Type::Ptr &type) {
std::stringstream error;
error << "unexpected expression type: " << prettyTypeName(type) << ", supported types are: int, bool, float";
return error.str();
};
if (!utils::isAny<IntegerType, FloatType>(lhsType))
if (isAssignment(binOp)) {
if (lhsType->is<PointerType>())
lhsType = lhsType->as<PointerType>().pointee;
else
ctx.pushError(node, "left-handed operand of an assignment expression must be a variable name");
}
if (!utils::isAny<IntegerType, FloatType>(lhsType)) {
ctx.pushError(node, typeError(lhsType));
if (!utils::isAny<IntegerType, FloatType>(rhsType))
throw ctx.errors;
}
if (!utils::isAny<IntegerType, FloatType>(rhsType)) {
ctx.pushError(node, typeError(rhsType));
if (lhsType != rhsType) {
throw ctx.errors;
}
if (*lhsType != *rhsType) {
auto needsType = deduceTargetCastType(lhsType, rhsType, isAssignment(binOp));
if (auto castOp = insertNumericCastOp(needsType, lhs, ctx.builder, lhsNode->ref))
lhs = castOp.result();
Expand Down
Loading