2023-01-07 07:54:53 +00:00
|
|
|
#include <error.hh>
|
2023-01-06 02:56:33 +00:00
|
|
|
#include <typecheck.hh>
|
|
|
|
|
|
|
|
namespace xlang {
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
std::string typeToString(Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case Type::Integer:
|
|
|
|
return "int";
|
|
|
|
case Type::Boolean:
|
|
|
|
return "bool";
|
2023-01-07 14:23:02 +00:00
|
|
|
default:
|
|
|
|
case Type::Invalid:
|
|
|
|
return "<invalid>";
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeCheckVisitor::TypeCheckVisitor(ErrorListener &errorlistener)
|
2023-01-07 08:56:27 +00:00
|
|
|
: errorlistener{errorlistener}, loopcount{0} {}
|
2023-01-06 02:56:33 +00:00
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitFile(xlangParser::FileContext *ctx) {
|
|
|
|
for (auto function : ctx->function()) {
|
|
|
|
auto token = function->Identifier()->getSymbol();
|
|
|
|
auto name = token->getText();
|
2023-01-07 07:54:53 +00:00
|
|
|
Signature signature{std::any_cast<Type>(visitType(function->type())), {}};
|
2023-01-06 17:56:53 +00:00
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
if (signatures.find(name) != signatures.end()) {
|
|
|
|
errorlistener.duplicateFunction(token, name);
|
2023-01-06 02:56:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-06 17:46:45 +00:00
|
|
|
if (auto param_list = function->parameterList()) {
|
2023-01-07 07:54:53 +00:00
|
|
|
for (auto type : param_list->type()) {
|
|
|
|
signature.parametertypes.push_back(
|
|
|
|
std::any_cast<Type>(visitType(type)));
|
|
|
|
}
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
signatures.emplace(name, signature);
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
visitChildren(ctx);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitFunction(xlangParser::FunctionContext *ctx) {
|
2023-01-07 07:54:53 +00:00
|
|
|
scope.enter();
|
2023-01-06 17:46:45 +00:00
|
|
|
if (auto param_list = ctx->parameterList()) {
|
2023-01-07 07:54:53 +00:00
|
|
|
for (size_t i = 0, n = param_list->Identifier().size(); i < n; i++) {
|
|
|
|
auto name = param_list->Identifier(i)->getSymbol()->getText();
|
|
|
|
auto type = std::any_cast<Type>(visitType(param_list->type(i)));
|
|
|
|
|
|
|
|
scope.add(name, type);
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
returntype = std::any_cast<Type>(visitType(ctx->type()));
|
2023-01-06 02:56:33 +00:00
|
|
|
visitBlock(ctx->block());
|
2023-01-07 07:54:53 +00:00
|
|
|
scope.leave();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitType(xlangParser::TypeContext *ctx) {
|
|
|
|
if (ctx->TypeInteger()) {
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
if (ctx->TypeBoolean()) {
|
|
|
|
return Type::Boolean;
|
|
|
|
}
|
|
|
|
// unreachable
|
|
|
|
errorlistener.compilerError(__FILE__, __LINE__);
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitBlock(xlangParser::BlockContext *ctx) {
|
2023-01-07 07:54:53 +00:00
|
|
|
scope.enter();
|
2023-01-06 02:56:33 +00:00
|
|
|
visitChildren(ctx);
|
2023-01-07 07:54:53 +00:00
|
|
|
scope.leave();
|
2023-01-06 02:56:33 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitStatement(xlangParser::StatementContext *ctx) {
|
2023-01-07 07:54:53 +00:00
|
|
|
if (auto condition = ctx->condition()) {
|
|
|
|
auto type = std::any_cast<Type>(visitCondition(condition));
|
|
|
|
|
|
|
|
if (type != Type::Boolean) {
|
|
|
|
errorlistener.typeMismatch(condition->getStart(), Type::Boolean, type);
|
|
|
|
}
|
2023-01-07 08:56:27 +00:00
|
|
|
if (ctx->While()) {
|
|
|
|
loopcount++;
|
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
visitBlock(ctx->block(0));
|
2023-01-07 08:56:27 +00:00
|
|
|
if (ctx->While()) {
|
|
|
|
loopcount--;
|
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
if (ctx->Else()) {
|
|
|
|
visitBlock(ctx->block(1));
|
|
|
|
}
|
2023-01-06 02:56:33 +00:00
|
|
|
return {};
|
|
|
|
}
|
2023-01-07 08:56:27 +00:00
|
|
|
if (ctx->Break()) {
|
|
|
|
if (!loopcount) {
|
|
|
|
errorlistener.loopControlWithoutLoop(ctx->Break()->getSymbol());
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (ctx->Continue()) {
|
|
|
|
if (!loopcount) {
|
|
|
|
errorlistener.loopControlWithoutLoop(ctx->Continue()->getSymbol());
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
if (ctx->Return()) {
|
|
|
|
auto type = std::any_cast<Type>(visitValue(ctx->value()));
|
|
|
|
|
|
|
|
if (type != returntype) {
|
|
|
|
errorlistener.typeMismatch(ctx->value()->getStart(), returntype, type);
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 02:56:33 +00:00
|
|
|
visitChildren(ctx);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
std::any TypeCheckVisitor::visitValue(xlangParser::ValueContext *ctx) {
|
|
|
|
if (auto expr = ctx->expr()) {
|
|
|
|
return visitExpr(expr);
|
|
|
|
}
|
|
|
|
if (auto condition = ctx->condition()) {
|
|
|
|
return visitCondition(condition);
|
|
|
|
}
|
|
|
|
// unreachable
|
|
|
|
errorlistener.compilerError(__FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECKOPERATOR(visitLeft, left, visitRight, right, expected) \
|
|
|
|
auto lefttype = std::any_cast<Type>(visitLeft(left)); \
|
|
|
|
auto righttype = std::any_cast<Type>(visitRight(right)); \
|
|
|
|
\
|
|
|
|
if (lefttype != expected) { \
|
|
|
|
errorlistener.typeMismatch(left->getStart(), expected, lefttype); \
|
|
|
|
} \
|
|
|
|
if (righttype != expected) { \
|
|
|
|
errorlistener.typeMismatch(right->getStart(), expected, righttype); \
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitCondition(xlangParser::ConditionContext *ctx) {
|
|
|
|
if (auto condition = ctx->condition()) {
|
|
|
|
CHECKOPERATOR(visitCondition, condition, visitBoolean, ctx->boolean(),
|
|
|
|
Type::Boolean);
|
|
|
|
return Type::Boolean;
|
|
|
|
}
|
|
|
|
return visitBoolean(ctx->boolean());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitBoolean(xlangParser::BooleanContext *ctx) {
|
|
|
|
if (auto boolean = ctx->boolean()) {
|
|
|
|
auto type = std::any_cast<Type>(visitBoolean(boolean));
|
|
|
|
|
|
|
|
if (type != Type::Boolean) {
|
|
|
|
errorlistener.typeMismatch(boolean->getStart(), Type::Boolean, type);
|
|
|
|
}
|
|
|
|
return Type::Boolean;
|
|
|
|
}
|
|
|
|
if (ctx->True() || ctx->False()) {
|
|
|
|
return Type::Boolean;
|
|
|
|
}
|
|
|
|
if (ctx->expr().size()) {
|
|
|
|
CHECKOPERATOR(visitExpr, ctx->expr(0), visitExpr, ctx->expr(1),
|
|
|
|
Type::Integer);
|
|
|
|
return Type::Boolean;
|
|
|
|
}
|
|
|
|
if (auto variable = ctx->variable()) {
|
|
|
|
return visitVariable(variable);
|
|
|
|
}
|
|
|
|
return visitCondition(ctx->condition());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitExpr(xlangParser::ExprContext *ctx) {
|
|
|
|
if (auto expr = ctx->expr()) {
|
|
|
|
CHECKOPERATOR(visitExpr, expr, visitTerm, ctx->term(), Type::Integer);
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
return visitTerm(ctx->term());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitTerm(xlangParser::TermContext *ctx) {
|
|
|
|
if (auto term = ctx->term()) {
|
|
|
|
CHECKOPERATOR(visitTerm, term, visitFactor, ctx->factor(), Type::Integer);
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
return visitFactor(ctx->factor());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::any TypeCheckVisitor::visitFactor(xlangParser::FactorContext *ctx) {
|
|
|
|
if (auto factor = ctx->factor()) {
|
|
|
|
auto type = std::any_cast<Type>(visitFactor(factor));
|
|
|
|
|
|
|
|
if (type != Type::Integer) {
|
|
|
|
errorlistener.typeMismatch(factor->getStart(), Type::Integer, type);
|
|
|
|
}
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
if (ctx->Integer()) {
|
|
|
|
return Type::Integer;
|
|
|
|
}
|
|
|
|
if (auto variable = ctx->variable()) {
|
|
|
|
return visitVariable(variable);
|
|
|
|
}
|
|
|
|
return visitExpr(ctx->expr());
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:46:45 +00:00
|
|
|
std::any TypeCheckVisitor::visitVariable(xlangParser::VariableContext *ctx) {
|
|
|
|
auto token = ctx->Identifier()->getSymbol();
|
|
|
|
auto name = token->getText();
|
|
|
|
|
2023-01-07 14:23:02 +00:00
|
|
|
if (ctx->Define()) {
|
|
|
|
auto type = std::any_cast<Type>(visitValue(ctx->value()));
|
|
|
|
|
|
|
|
if (scope.get(name)) {
|
|
|
|
errorlistener.shadowedVariable(token, name);
|
|
|
|
}
|
|
|
|
scope.add(name, type);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
if (ctx->Assign()) {
|
|
|
|
auto type = std::any_cast<Type>(visitValue(ctx->value()));
|
|
|
|
|
|
|
|
if (auto expected = scope.get(name)) {
|
|
|
|
if (type != *expected) {
|
|
|
|
errorlistener.typeMismatch(token, *expected, type);
|
|
|
|
}
|
|
|
|
return *expected;
|
|
|
|
} else {
|
|
|
|
errorlistener.unknownVariable(token, name);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 17:46:45 +00:00
|
|
|
if (ctx->LeftParen()) {
|
2023-01-07 07:54:53 +00:00
|
|
|
auto it = signatures.find(name);
|
2023-01-06 17:46:45 +00:00
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
if (it == signatures.end()) {
|
|
|
|
errorlistener.unknownFunction(token, name);
|
2023-01-07 14:23:02 +00:00
|
|
|
return Type::Invalid;
|
2023-01-06 17:46:45 +00:00
|
|
|
} else {
|
2023-01-07 07:54:53 +00:00
|
|
|
auto signature = it->second;
|
|
|
|
auto arity = signature.parametertypes.size();
|
2023-01-06 17:46:45 +00:00
|
|
|
|
|
|
|
if (auto arg_list = ctx->argumentList()) {
|
|
|
|
auto arg_num = arg_list->value().size();
|
|
|
|
|
|
|
|
if (arity != arg_num) {
|
2023-01-07 07:54:53 +00:00
|
|
|
errorlistener.wrongArgumentNumber(token, name, arity, arg_num);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < arg_num && i < arity; i++) {
|
|
|
|
auto type = std::any_cast<Type>(visitValue(arg_list->value(i)));
|
|
|
|
auto token = arg_list->value(i)->getStart();
|
|
|
|
|
|
|
|
if (type != signature.parametertypes[i]) {
|
|
|
|
errorlistener.typeMismatch(token, signature.parametertypes[i],
|
|
|
|
type);
|
|
|
|
}
|
2023-01-06 03:03:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-06 17:46:45 +00:00
|
|
|
if (arity) {
|
2023-01-07 07:54:53 +00:00
|
|
|
errorlistener.wrongArgumentNumber(token, name, arity, 0);
|
2023-01-06 17:46:45 +00:00
|
|
|
}
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
2023-01-07 07:54:53 +00:00
|
|
|
return signature.returntype;
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
2023-01-07 14:23:02 +00:00
|
|
|
}
|
|
|
|
if (auto type = scope.get(name)) {
|
2023-01-07 07:54:53 +00:00
|
|
|
return *type;
|
2023-01-07 14:23:02 +00:00
|
|
|
} else {
|
|
|
|
errorlistener.unknownVariable(token, name);
|
|
|
|
return Type::Invalid;
|
2023-01-06 02:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xlang
|