Improve typechecking for binops

This commit is contained in:
Sofia 2026-05-10 18:50:46 +03:00
parent adc74333ff
commit e064b3f04d
8 changed files with 52 additions and 4 deletions

View File

@ -6,6 +6,7 @@ namespace AST {
std::string IntLiteralExpression::formatted() {
std::stringstream out{ "" };
out << this->m_value;
out << this->m_ty->formatted();
return out.str();
}

View File

@ -55,7 +55,7 @@ namespace AST {
: Expression{ meta }
, m_value{ value }
, m_ty{ { std::shared_ptr<types::Type>{
new types::FundamentalType{true, types::FundamentalTypeKind::Int}
new types::FundamentalType{true, types::FundamentalTypeKind::AnyInt}
} } } {
}
virtual ~IntLiteralExpression() override = default;

View File

@ -107,13 +107,32 @@ namespace types {
auto int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::Int } };
auto uint_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::UInt } };
auto char_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::Char } };
auto uchar_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::UChar } };
auto short_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::ShortInt } };
auto ushort_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::UShortInt } };
auto long_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::LongInt } };
auto ulong_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::ULongInt } };
auto long_long_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::LongLongInt } };
auto ulong_long_int_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::ULongLongInt } };
auto bool_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
// Integer Increment/Decrement unaries
for (auto& ty : { int_ty, char_ty }) {
for (auto& ty : {
short_int_ty, int_ty, long_int_ty, long_long_int_ty, char_ty,
ushort_int_ty, uint_ty, ulong_int_ty, ulong_long_int_ty, uchar_ty,
}) {
definitions.push_back(UnopDefinition{
ty, types::Unary::AddPostfix, ty,
[](codegen::Builder& builder, std::shared_ptr<Type> ty, llvm::Value* ptr) {
@ -168,7 +187,11 @@ namespace types {
}
// Not & Negation
for (auto& ty : { int_ty, char_ty, bool_ty }) {
for (auto& ty : {
short_int_ty, int_ty, long_int_ty, long_long_int_ty, char_ty,
ushort_int_ty, uint_ty, ulong_int_ty, ulong_long_int_ty, uchar_ty,
bool_ty
}) {
definitions.push_back(UnopDefinition{
ty, types::Unary::Not, ty,
[](codegen::Builder& builder, std::shared_ptr<Type> ty, llvm::Value* value) {

View File

@ -28,6 +28,8 @@ namespace types {
new FundamentalType{ false, FundamentalTypeKind::ULongLongInt } };
auto bool_ty = std::shared_ptr<Type>{
new FundamentalType{ false, FundamentalTypeKind::Bool } };
auto any_int_ty = std::shared_ptr<Type>{
new FundamentalType{ false, FundamentalTypeKind::AnyInt } };
auto numerical_types = {
short_int_ty, int_ty, long_int_ty, long_long_int_ty, char_ty,
@ -35,6 +37,13 @@ namespace types {
bool_ty
};
for (auto& target_ty : numerical_types) {
casts.push_back(CastDefinition{ any_int_ty, target_ty, true,
[](codegen::Builder&, std::shared_ptr<Type>, llvm::Value* value) {
return value;
} });
}
for (auto& source_ty : numerical_types) {
for (auto& target_ty : numerical_types) {
if (types::types_equal(source_ty, target_ty)) {

View File

@ -224,6 +224,10 @@ namespace AST {
if (!rhs_res.ok())
// Skip if not implicitly castable to lhs
continue;
rhs_ty = this->m_rhs->typecheck(state, scope, binop.rhs).type;
rhs_res = check_type(state, rhs_ty, binop.rhs);
this->m_rhs = handle_res(std::move(this->m_rhs), rhs_res, state);
return { binop.result(binop, lhs_ty, rhs_ty), false, false };
}
@ -232,6 +236,9 @@ namespace AST {
if (!lhs_res.ok())
// Skip if not implicitly castable to rhs
continue;
lhs_ty = this->m_lhs->typecheck(state, scope, binop.lhs).type;
lhs_res = check_type(state, lhs_ty, binop.lhs);
this->m_lhs = handle_res(std::move(this->m_lhs), lhs_res, state);
return { binop.result(binop, lhs_ty, rhs_ty), false, false };
}
@ -247,6 +254,8 @@ namespace AST {
if (!result_res.ok())
continue;
}
lhs_ty = this->m_lhs->typecheck(state, scope, binop.lhs).type;
rhs_ty = this->m_rhs->typecheck(state, scope, binop.rhs).type;
auto lhs_result = check_type(state, lhs_ty, binop.lhs);
auto rhs_result = check_type(state, rhs_ty, binop.rhs);
this->m_lhs = handle_res(std::move(this->m_lhs), lhs_result, state);

View File

@ -70,6 +70,9 @@ namespace types {
case FundamentalTypeKind::ULongLongInt:
out << "ULongLongInt";
break;
case FundamentalTypeKind::AnyInt:
out << "AnyInt";
break;
case FundamentalTypeKind::Bool:
out << "Bool";
break;

View File

@ -26,6 +26,9 @@ namespace types {
ULongInt,
ULongLongInt,
/// @brief stand-in type for integer literals
AnyInt,
Bool,
Char,
UChar,

2
test.c
View File

@ -71,7 +71,7 @@ long long int main() {
printf("while-counter: %d\n", counter++);
}
short int sh = 123;
short int sh = 123 + 5;
long int lg = 456;
long long int longer = 789;