Compare commits
5 Commits
792a689005
...
dbd5627fe6
Author | SHA1 | Date | |
---|---|---|---|
dbd5627fe6 | |||
26a8953060 | |||
c52492f6c0 | |||
dc85a8b296 | |||
734537a528 |
@ -1,5 +1,5 @@
|
|||||||
const { exit } from @'core/os';
|
const { exit } from @'core/os';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
const io from @'core/io';
|
const io from @'core/io';
|
||||||
const { open } from @'core/fs';
|
const { open } from @'core/fs';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
io.out('What is your name? ');
|
io.out('What is your name? ');
|
||||||
let name = io.in();
|
let name = io.in();
|
||||||
let file = open('name.txt');
|
let file = open('name.txt');
|
||||||
|
@ -2,7 +2,7 @@ const io from @'core/io';
|
|||||||
const { concat } from @'core/string';
|
const { concat } from @'core/string';
|
||||||
const { toString } from @'core/float_utils';
|
const { toString } from @'core/float_utils';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
let float1 : f128 = 234.6;
|
let float1 : f128 = 234.6;
|
||||||
f128 float2 : f128 = 2934748348291930404.5;
|
f128 float2 : f128 = 2934748348291930404.5;
|
||||||
io.outln(concat('Result is: ',toString(float1 + float2)));
|
io.outln(concat('Result is: ',toString(float1 + float2)));
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
const io from @'core/io';
|
const io from @'core/io';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
io.outln('Hello world!');
|
io.outln('Hello world!');
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
const io from @'core/io';
|
const io from @'core/io';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
io.out('What\'s your name? ');
|
io.out('What\'s your name? ');
|
||||||
let name = io.in();
|
let name = io.in();
|
||||||
io.out('Hello, ');
|
io.out('Hello, ');
|
||||||
|
@ -2,7 +2,7 @@ const { io } from @'core/io';
|
|||||||
|
|
||||||
const good_food : String = 'watermelon';
|
const good_food : String = 'watermelon';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
|
|
||||||
io.outln('Hello world!');
|
io.outln('Hello world!');
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const io from @'core/io';
|
const io from @'core/io';
|
||||||
|
|
||||||
let @main in {
|
let @main : i32 in {
|
||||||
let age : i32 = 64;
|
let age : i32 = 64;
|
||||||
io.out('I am ');
|
io.out('I am ');
|
||||||
io.out(age);
|
io.out(age);
|
||||||
|
@ -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))
|
||||||
|
@ -37,6 +37,11 @@ void IRBuilder::create_program(std::shared_ptr<ProgramNode> program)
|
|||||||
{
|
{
|
||||||
Error::throw_error_without_location(format_string("Missing entry point: %s", Arguments::values["entry"]));
|
Error::throw_error_without_location(format_string("Missing entry point: %s", Arguments::values["entry"]));
|
||||||
}
|
}
|
||||||
|
if (module->getFunction(Arguments::values["entry"])->empty())
|
||||||
|
{
|
||||||
|
Error::throw_error_without_location(
|
||||||
|
format_string("Entry point %s has no body", Arguments::values["entry"]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,8 @@
|
|||||||
"file": "declare-main.sp",
|
"file": "declare-main.sp",
|
||||||
"compile": {
|
"compile": {
|
||||||
"flags": [],
|
"flags": [],
|
||||||
"exit-code": 0,
|
"exit-code": 1,
|
||||||
"stdout": "",
|
"stdout": "",
|
||||||
"stderr": ""
|
"stderr": "\u001b[1;1m\u001b[31;49merror: \u001b[0;0mEntry point main has no body\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
9
tests/top-level-math.json
Normal file
9
tests/top-level-math.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"file": "top-level-math.sp",
|
||||||
|
"compile": {
|
||||||
|
"flags": [],
|
||||||
|
"exit-code": 1,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": "\u001b[1;1mtests/top-level-math.sp:1:1: \u001b[31;49merror: \u001b[0;0mExpected let\n1 \n \u001b[31;49m^\u001b[0;0m\n"
|
||||||
|
}
|
||||||
|
}
|
1
tests/top-level-math.sp
Normal file
1
tests/top-level-math.sp
Normal file
@ -0,0 +1 @@
|
|||||||
|
4 + 5 - 6
|
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,6 +5,14 @@ if [ "$1" == "" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -f $1.cpp ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f $1.h ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
touch $1.cpp
|
touch $1.cpp
|
||||||
touch $1.h
|
touch $1.h
|
||||||
|
|
||||||
@ -39,4 +47,4 @@ llvm::Value* $1::codegen(IRBuilder* generator)
|
|||||||
return llvm::ConstantInt::getSigned(llvm::IntegerType::getInt32Ty(generator->getBuilder()->getContext()),
|
return llvm::ConstantInt::getSigned(llvm::IntegerType::getInt32Ty(generator->getBuilder()->getContext()),
|
||||||
0);
|
0);
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
Loading…
Reference in New Issue
Block a user