implicit casts between integer/floating point types yay!!
This commit is contained in:
parent
792a689005
commit
734537a528
@ -40,6 +40,30 @@ void FunctionNode::codegen(IRBuilder* builder, llvm::Module* module)
|
|||||||
|
|
||||||
if (llvm::Value* retVal = body->codegen(builder))
|
if (llvm::Value* retVal = body->codegen(builder))
|
||||||
{
|
{
|
||||||
|
if (retVal->getType() != prototype.returnType)
|
||||||
|
{
|
||||||
|
if (retVal->getType()->isIntegerTy() && prototype.returnType->isIntegerTy())
|
||||||
|
{
|
||||||
|
retVal = builder->getBuilder()->CreateIntCast(retVal, prototype.returnType, true);
|
||||||
|
}
|
||||||
|
else if (retVal->getType()->isFloatingPointTy() && prototype.returnType->isFloatingPointTy())
|
||||||
|
{
|
||||||
|
retVal = builder->getBuilder()->CreateFPCast(retVal, prototype.returnType);
|
||||||
|
}
|
||||||
|
else if (retVal->getType()->isIntegerTy() && prototype.returnType->isFloatingPointTy())
|
||||||
|
{
|
||||||
|
retVal = builder->getBuilder()->CreateCast(llvm::Instruction::SIToFP, retVal, prototype.returnType);
|
||||||
|
}
|
||||||
|
else if (retVal->getType()->isFloatingPointTy() && prototype.returnType->isIntegerTy())
|
||||||
|
{
|
||||||
|
retVal = builder->getBuilder()->CreateCast(llvm::Instruction::FPToSI, retVal, prototype.returnType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Error::throw_error_without_location(
|
||||||
|
format_string("The %s function's body type does not match its return type", prototype.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
builder->getBuilder()->CreateRet(retVal);
|
builder->getBuilder()->CreateRet(retVal);
|
||||||
|
|
||||||
if (llvm::verifyFunction(*Function))
|
if (llvm::verifyFunction(*Function))
|
||||||
|
@ -10,7 +10,8 @@ Parser::Parser(const TokenStream& tokens) : tokens(tokens)
|
|||||||
{
|
{
|
||||||
m_type_map = {{"void", llvm::Type::getVoidTy(*globalContext)}, {"bool", llvm::Type::getInt1Ty(*globalContext)},
|
m_type_map = {{"void", llvm::Type::getVoidTy(*globalContext)}, {"bool", llvm::Type::getInt1Ty(*globalContext)},
|
||||||
{"i8", llvm::Type::getInt8Ty(*globalContext)}, {"i16", llvm::Type::getInt16Ty(*globalContext)},
|
{"i8", llvm::Type::getInt8Ty(*globalContext)}, {"i16", llvm::Type::getInt16Ty(*globalContext)},
|
||||||
{"i32", llvm::Type::getInt32Ty(*globalContext)}, {"i64", llvm::Type::getInt64Ty(*globalContext)}};
|
{"i32", llvm::Type::getInt32Ty(*globalContext)}, {"i64", llvm::Type::getInt64Ty(*globalContext)},
|
||||||
|
{"f32", llvm::Type::getFloatTy(*globalContext)}, {"f64", llvm::Type::getDoubleTy(*globalContext)}};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Parser> Parser::new_parser(const TokenStream& tokens)
|
std::shared_ptr<Parser> Parser::new_parser(const TokenStream& tokens)
|
||||||
|
@ -2,8 +2,13 @@
|
|||||||
"file": "float-expr.sp",
|
"file": "float-expr.sp",
|
||||||
"compile": {
|
"compile": {
|
||||||
"flags": [],
|
"flags": [],
|
||||||
"exit-code": 1,
|
"exit-code": 0,
|
||||||
"stdout": "",
|
"stdout": "",
|
||||||
"stderr": "\u001b[1;1m\u001b[31;49merror: \u001b[0;0mInvalid function main\n"
|
"stderr": ""
|
||||||
|
},
|
||||||
|
"run": {
|
||||||
|
"exit-code": 3,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
14
tests/unmatched-integer-type.json
Normal file
14
tests/unmatched-integer-type.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"file": "unmatched-integer-type.sp",
|
||||||
|
"compile": {
|
||||||
|
"flags": [],
|
||||||
|
"exit-code": 0,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": ""
|
||||||
|
},
|
||||||
|
"run": {
|
||||||
|
"exit-code": 0,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": ""
|
||||||
|
}
|
||||||
|
}
|
3
tests/unmatched-integer-type.sp
Normal file
3
tests/unmatched-integer-type.sp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
let @main : i64 in {
|
||||||
|
0
|
||||||
|
}
|
@ -4,6 +4,6 @@
|
|||||||
"flags": [],
|
"flags": [],
|
||||||
"exit-code": 1,
|
"exit-code": 1,
|
||||||
"stdout": "",
|
"stdout": "",
|
||||||
"stderr": "\u001b[1;1m\u001b[31;49merror: \u001b[0;0mInvalid function main\n"
|
"stderr": "\u001b[1;1m\u001b[31;49merror: \u001b[0;0mThe main function's body type does not match its return type\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user