Compare commits
No commits in common. "57dc0222411f98aa6558563d2655a4e59bf77269" and "1555c12bbdf223c6f606b2c40cad6e206201dd37" have entirely different histories.
57dc022241
...
1555c12bbd
@ -44,7 +44,7 @@ namespace AST {
|
||||
|
||||
std::string CastExpression::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "(" << this->m_ty->formatted() << ")";
|
||||
out << "(" << this->m_ty->formatted() << ") ";
|
||||
out << this->m_expr->formatted();
|
||||
return out.str();
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
|
||||
#include "casting.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace types {
|
||||
std::vector<CastDefinition> create_casts() {
|
||||
@ -22,21 +21,15 @@ namespace types {
|
||||
[](codegen::Builder&, std::shared_ptr<Type>, llvm::Value* value) {
|
||||
return value;
|
||||
} });
|
||||
continue;
|
||||
}
|
||||
|
||||
bool allow_implicit = false;
|
||||
if (target_ty->size() >= source_ty->size())
|
||||
allow_implicit = true;
|
||||
|
||||
if (target_ty->is_signed()) {
|
||||
casts.push_back(CastDefinition{ source_ty, target_ty, allow_implicit,
|
||||
else if (target_ty->is_signed()) {
|
||||
casts.push_back(CastDefinition{ source_ty, target_ty, false,
|
||||
[](codegen::Builder& builder, std::shared_ptr<Type> target, llvm::Value* value) {
|
||||
return builder.builder->CreateSExtOrTrunc(value, target->codegen(builder), "cast");
|
||||
} });
|
||||
}
|
||||
else {
|
||||
casts.push_back(CastDefinition{ source_ty, target_ty, allow_implicit,
|
||||
casts.push_back(CastDefinition{ source_ty, target_ty, false,
|
||||
[](codegen::Builder& builder, std::shared_ptr<Type> target, llvm::Value* value) {
|
||||
return builder.builder->CreateZExtOrTrunc(value, target->codegen(builder), "cast");
|
||||
} });
|
||||
|
||||
@ -117,8 +117,6 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Compile parsed output
|
||||
|
||||
codegen::Scope cg_scope{
|
||||
.binops = typecheck_state.binops,
|
||||
.casts = typecheck_state.casts,
|
||||
@ -126,6 +124,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
||||
.is_lvalue = false,
|
||||
};
|
||||
|
||||
// Compile parsed output
|
||||
try {
|
||||
for (auto& tls : statements) {
|
||||
std::cout << tls->formatted() << std::endl;
|
||||
|
||||
@ -5,31 +5,14 @@
|
||||
#include "result.h"
|
||||
|
||||
namespace {
|
||||
enum class TypecheckResKind {
|
||||
enum class TypecheckRes {
|
||||
Ok,
|
||||
Castable,
|
||||
};
|
||||
|
||||
struct TypecheckRes {
|
||||
TypecheckResKind kind;
|
||||
std::shared_ptr<types::Type> result;
|
||||
};
|
||||
|
||||
Result<TypecheckRes, std::string> check_type(
|
||||
typecheck::State& state,
|
||||
std::shared_ptr<types::Type> checked,
|
||||
std::shared_ptr<types::Type> target) {
|
||||
auto potential_cast = types::find_cast(state.casts, checked, target);
|
||||
|
||||
|
||||
Result<TypecheckRes, std::string> check_type(std::shared_ptr<types::Type> checked, std::shared_ptr<types::Type> target) {
|
||||
if (types::types_equal(checked, target)) {
|
||||
return TypecheckRes{ TypecheckResKind::Ok, target };
|
||||
}
|
||||
else if (potential_cast.has_value()) {
|
||||
if (potential_cast->allow_implicit)
|
||||
return TypecheckRes{ TypecheckResKind::Castable, target };
|
||||
|
||||
return std::string{ "Type " + checked->formatted() + " not implicitly castable to " + target->formatted() };
|
||||
return TypecheckRes::Ok;
|
||||
}
|
||||
|
||||
return std::string{ "Types " + checked->formatted() + " and " + target->formatted() + " incompatible" };
|
||||
@ -41,13 +24,12 @@ namespace {
|
||||
typecheck::State& state) {
|
||||
if (res.ok()) {
|
||||
auto result = res.unwrap();
|
||||
if (result.kind == TypecheckResKind::Ok) {
|
||||
if (result == TypecheckRes::Ok) {
|
||||
return expr;
|
||||
}
|
||||
else {
|
||||
return std::unique_ptr<AST::Expression> {
|
||||
new AST::CastExpression{ expr->m_meta, result.result, std::move(expr) }
|
||||
};
|
||||
state.errors.push_back(CompileError("Casting not yet implemented", expr->m_meta));
|
||||
return expr;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -111,7 +93,7 @@ namespace AST {
|
||||
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
|
||||
typecheck::State& state,
|
||||
typecheck::Scope& scope,
|
||||
std::optional<std::shared_ptr<types::Type>> expected_ty
|
||||
std::optional<std::shared_ptr<types::Type>>
|
||||
) {
|
||||
auto lhs_ty = this->m_lhs->typecheck(state, scope, {});
|
||||
auto rhs_ty = this->m_rhs->typecheck(state, scope, {});
|
||||
@ -119,12 +101,9 @@ namespace AST {
|
||||
if (this->m_binop == types::BinOp::Assignment) {
|
||||
// Re-typecheck rhs to actually match lhs
|
||||
auto rhs_ty = this->m_rhs->typecheck(state, scope, lhs_ty);
|
||||
auto rhs_ty_res = check_type(state, rhs_ty, lhs_ty);
|
||||
this->m_rhs = handle_res(std::move(this->m_rhs), rhs_ty_res, state);
|
||||
return lhs_ty;
|
||||
}
|
||||
|
||||
// Try to find a binop that matches exactly
|
||||
auto binop = types::find_binop(
|
||||
state.binops,
|
||||
lhs_ty,
|
||||
@ -136,53 +115,8 @@ namespace AST {
|
||||
return binop->result;
|
||||
}
|
||||
|
||||
// If that fails, try to find binop that matches on one side perfectly
|
||||
// and is castable on the other side, and would also be perfectly
|
||||
// assignable to the expected value.
|
||||
for (auto& binop : state.binops) {
|
||||
if (expected_ty) {
|
||||
// Skip any binops that would not be immediately assignable to
|
||||
// the expected type
|
||||
if (!types::types_equal(binop.result, *expected_ty)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (types::types_equal(binop.lhs, lhs_ty)) {
|
||||
auto rhs_res = check_type(state, rhs_ty, binop.rhs);
|
||||
if (!rhs_res.ok())
|
||||
// Skip if not implicitly castable to lhs
|
||||
continue;
|
||||
this->m_rhs = handle_res(std::move(this->m_rhs), rhs_res, state);
|
||||
return binop.result;
|
||||
}
|
||||
else if (types::types_equal(binop.rhs, rhs_ty)) {
|
||||
auto lhs_res = check_type(state, lhs_ty, binop.lhs);
|
||||
if (!lhs_res.ok())
|
||||
// Skip if not implicitly castable to rhs
|
||||
continue;
|
||||
this->m_lhs = handle_res(std::move(this->m_lhs), lhs_res, state);
|
||||
return binop.result;
|
||||
}
|
||||
}
|
||||
// TODO check for binops that may be implicitly castable
|
||||
|
||||
// Finally check for any binop that allows the result to be implicitly
|
||||
// casted to the result
|
||||
for (auto& binop : state.binops) {
|
||||
if (expected_ty) {
|
||||
// Skip any binops that would not even be implicitly castable to
|
||||
// the expected result
|
||||
auto result_res = check_type(state, binop.result, *expected_ty);
|
||||
if (!result_res.ok())
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
this->m_rhs = handle_res(std::move(this->m_rhs), lhs_result, state);
|
||||
return binop.result;
|
||||
}
|
||||
|
||||
// No suitable binops found :(
|
||||
state.errors.push_back(CompileError(
|
||||
"No suitable binop between "
|
||||
+ lhs_ty->formatted() + " "
|
||||
@ -222,7 +156,7 @@ namespace AST {
|
||||
auto expected_param_ty = fn_ty->m_param_tys[i];
|
||||
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
|
||||
|
||||
auto check_res = check_type(state, param_ty, expected_param_ty);
|
||||
auto check_res = check_type(param_ty, expected_param_ty);
|
||||
this->m_args[i] = handle_res(std::move(this->m_args[i]), check_res, state);
|
||||
}
|
||||
else {
|
||||
@ -256,7 +190,7 @@ namespace AST {
|
||||
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||
auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty);
|
||||
if (scope.return_ty) {
|
||||
auto check_res = check_type(state, res_ty, *scope.return_ty);
|
||||
auto check_res = check_type(res_ty, *scope.return_ty);
|
||||
this->m_expr = handle_res(std::move(this->m_expr), check_res, state);
|
||||
}
|
||||
}
|
||||
@ -277,7 +211,7 @@ namespace AST {
|
||||
new types::FundamentalType{ types::FundamentalTypeKind::Bool } };
|
||||
auto expr_ty = this->m_condition->typecheck(state, scope, bool_ty);
|
||||
|
||||
auto check_res = check_type(state, expr_ty, bool_ty);
|
||||
auto check_res = check_type(expr_ty, bool_ty);
|
||||
this->m_condition = handle_res(std::move(this->m_condition), check_res, state);
|
||||
|
||||
this->m_then->typecheck(state, scope);
|
||||
|
||||
@ -76,7 +76,7 @@ namespace types {
|
||||
}
|
||||
|
||||
bool Type::is_signed() {
|
||||
return false;
|
||||
false;
|
||||
}
|
||||
|
||||
std::pair<llvm::Value*, std::shared_ptr<Type>> FundamentalType::load(codegen::Builder&, llvm::Value* ptr) {
|
||||
@ -140,19 +140,6 @@ namespace types {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t FundamentalType::size() {
|
||||
switch (this->m_ty) {
|
||||
case FundamentalTypeKind::Int:
|
||||
return 32;
|
||||
case FundamentalTypeKind::Bool:
|
||||
return 1;
|
||||
case FundamentalTypeKind::Char:
|
||||
return 8;
|
||||
default:
|
||||
throw std::runtime_error("Invalid type");
|
||||
}
|
||||
}
|
||||
|
||||
std::string FunctionType::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << "(";
|
||||
@ -183,10 +170,6 @@ namespace types {
|
||||
return std::pair(ptr, self);
|
||||
}
|
||||
|
||||
uint32_t FunctionType::size() {
|
||||
return 64;
|
||||
}
|
||||
|
||||
std::string PointerType::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << this->m_inner->formatted() << "*";
|
||||
@ -200,10 +183,6 @@ namespace types {
|
||||
);
|
||||
}
|
||||
|
||||
uint32_t PointerType::size() {
|
||||
return 64;
|
||||
}
|
||||
|
||||
bool types_equal(std::shared_ptr<types::Type> type1, std::shared_ptr<types::Type> type2) {
|
||||
if (type1->m_kind != type2->m_kind)
|
||||
return false;
|
||||
|
||||
@ -34,7 +34,6 @@ namespace types {
|
||||
virtual llvm::Value* lt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs);
|
||||
virtual llvm::Value* gt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs);
|
||||
virtual bool is_signed();
|
||||
virtual uint32_t size() = 0;
|
||||
};
|
||||
|
||||
class FundamentalType : public Type {
|
||||
@ -50,7 +49,6 @@ namespace types {
|
||||
virtual llvm::Value* lt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) override;
|
||||
virtual llvm::Value* gt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) override;
|
||||
virtual bool is_signed() override;
|
||||
virtual uint32_t size() override;
|
||||
};
|
||||
|
||||
|
||||
@ -70,7 +68,6 @@ namespace types {
|
||||
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
||||
virtual std::pair<llvm::Value*, std::shared_ptr<Type>> load(codegen::Builder& builder, llvm::Value* ptr) override;
|
||||
virtual std::optional<std::shared_ptr<Type>> return_type() override;
|
||||
virtual uint32_t size() override;
|
||||
};
|
||||
|
||||
|
||||
@ -85,7 +82,6 @@ namespace types {
|
||||
virtual std::string formatted() override;
|
||||
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
||||
virtual std::pair<llvm::Value*, std::shared_ptr<Type>> load(codegen::Builder& builder, llvm::Value* ptr) override;
|
||||
virtual uint32_t size() override;
|
||||
};
|
||||
|
||||
bool types_equal(std::shared_ptr<types::Type> type1, std::shared_ptr<types::Type> type2);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user