Allow implicitly converting IntLiteralExpr to other int types
This commit is contained in:
parent
2695a83ac8
commit
810dd3595e
@ -42,8 +42,15 @@ namespace AST {
|
|||||||
class IntLiteralExpression : public Expression {
|
class IntLiteralExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
int m_value;
|
int m_value;
|
||||||
|
std::shared_ptr<types::Type> m_ty;
|
||||||
public:
|
public:
|
||||||
IntLiteralExpression(token::Metadata meta, int value) : Expression{ meta }, m_value{ value } {}
|
IntLiteralExpression(token::Metadata meta, int value)
|
||||||
|
: Expression{ meta }
|
||||||
|
, m_value{ value }
|
||||||
|
, m_ty{ { std::shared_ptr<types::Type>{
|
||||||
|
new types::FundamentalType{types::FundamentalTypeKind::Int}
|
||||||
|
} } } {
|
||||||
|
}
|
||||||
virtual ~IntLiteralExpression() override = default;
|
virtual ~IntLiteralExpression() override = default;
|
||||||
virtual std::string formatted() override;
|
virtual std::string formatted() override;
|
||||||
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||||
|
|||||||
@ -19,11 +19,10 @@ namespace AST {
|
|||||||
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) {
|
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) {
|
||||||
auto ty = builder.builder->getInt32Ty();
|
auto ty = builder.builder->getInt32Ty();
|
||||||
|
|
||||||
auto stack_type = new types::FundamentalType{ types::FundamentalTypeKind::Int };
|
|
||||||
|
|
||||||
return codegen::StackValue{
|
return codegen::StackValue{
|
||||||
llvm::ConstantInt::get(ty, this->m_value),
|
llvm::ConstantInt::get(ty, this->m_value),
|
||||||
std::unique_ptr<types::Type>{stack_type}
|
this->m_ty,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -43,11 +43,24 @@ namespace AST {
|
|||||||
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
||||||
typecheck::State&,
|
typecheck::State&,
|
||||||
typecheck::Scope&,
|
typecheck::Scope&,
|
||||||
std::optional<std::shared_ptr<types::Type>>
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
||||||
) {
|
) {
|
||||||
return std::shared_ptr<types::Type>{
|
// Allow implicitly converting IntLiteralExpression to other types
|
||||||
new types::FundamentalType{ types::FundamentalTypeKind::Int }
|
// representable by integers.
|
||||||
};
|
if (expected_ty) {
|
||||||
|
if ((*expected_ty)->m_kind == types::TypeKind::Fundamental) {
|
||||||
|
auto ty = dynamic_cast<types::FundamentalType*>((*expected_ty).get());
|
||||||
|
if (
|
||||||
|
ty->m_ty == types::FundamentalTypeKind::Bool
|
||||||
|
|| ty->m_ty == types::FundamentalTypeKind::Char
|
||||||
|
|| ty->m_ty == types::FundamentalTypeKind::Int
|
||||||
|
) {
|
||||||
|
this->m_ty = *expected_ty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this->m_ty;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
|
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
|
||||||
|
|||||||
2
test.c
2
test.c
@ -6,7 +6,7 @@ int fibonacci(int n) {
|
|||||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
char main() {
|
||||||
printf("10th fibonacci number is %d!", fibonacci(10));
|
printf("10th fibonacci number is %d!", fibonacci(10));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user