Add rest of the integer types
This commit is contained in:
parent
db8de8fc30
commit
2034c6c55f
@ -782,12 +782,24 @@ namespace AST {
|
|||||||
namespace types {
|
namespace types {
|
||||||
llvm::Type* FundamentalType::codegen(codegen::Builder& builder, codegen::TypeMap&) {
|
llvm::Type* FundamentalType::codegen(codegen::Builder& builder, codegen::TypeMap&) {
|
||||||
switch (this->m_ty) {
|
switch (this->m_ty) {
|
||||||
|
case FundamentalTypeKind::ShortInt:
|
||||||
|
case FundamentalTypeKind::UShortInt:
|
||||||
|
return builder.builder->getInt16Ty();
|
||||||
case FundamentalTypeKind::Int:
|
case FundamentalTypeKind::Int:
|
||||||
|
case FundamentalTypeKind::UInt:
|
||||||
return builder.builder->getInt32Ty();
|
return builder.builder->getInt32Ty();
|
||||||
|
case FundamentalTypeKind::LongInt:
|
||||||
|
case FundamentalTypeKind::ULongInt:
|
||||||
|
return builder.builder->getInt64Ty();
|
||||||
|
case FundamentalTypeKind::LongLongInt:
|
||||||
|
case FundamentalTypeKind::ULongLongInt:
|
||||||
|
return builder.builder->getInt128Ty();
|
||||||
case FundamentalTypeKind::Bool:
|
case FundamentalTypeKind::Bool:
|
||||||
return builder.builder->getInt1Ty();
|
return builder.builder->getInt1Ty();
|
||||||
case FundamentalTypeKind::Char:
|
case FundamentalTypeKind::Char:
|
||||||
return builder.builder->getInt8Ty();
|
return builder.builder->getInt8Ty();
|
||||||
|
case FundamentalTypeKind::UChar:
|
||||||
|
return builder.builder->getInt8Ty();
|
||||||
case FundamentalTypeKind::Void:
|
case FundamentalTypeKind::Void:
|
||||||
return builder.builder->getVoidTy();
|
return builder.builder->getVoidTy();
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include "tokens.h"
|
#include "tokens.h"
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
|
||||||
namespace parsing {
|
namespace parsing {
|
||||||
namespace {
|
namespace {
|
||||||
@ -126,16 +127,64 @@ namespace parsing {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// TODO eventually make this be potentially more than one word
|
// TODO eventually make this be potentially more than one word
|
||||||
std::string type_name = token.content;
|
std::string type_name = {};
|
||||||
|
|
||||||
if (type_name == "int") {
|
std::set<std::string> type_parts = { "unsigned", "short", "long", "int", "char", "void" };
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (token.type == token::Type::Ident && type_parts.contains(token.content)) {
|
||||||
|
if (counter > 0) {
|
||||||
|
type_name += " ";
|
||||||
|
inner.next();
|
||||||
|
}
|
||||||
|
type_name += token.content;
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
token = inner.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << type_name << std::endl;
|
||||||
|
|
||||||
|
if (type_name == "short int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::ShortInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "unsigned short int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::UShortInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "int") {
|
||||||
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Int };
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Int };
|
||||||
returned = std::shared_ptr<types::Type>{ ty };
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
}
|
}
|
||||||
|
else if (type_name == "unsigned int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::UInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "long int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::LongInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "unsigned long int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::ULongInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "long long int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::LongLongInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
|
else if (type_name == "unsigned long long int") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::ULongLongInt };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
else if (type_name == "char") {
|
else if (type_name == "char") {
|
||||||
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Char };
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Char };
|
||||||
returned = std::shared_ptr<types::Type>{ ty };
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
}
|
}
|
||||||
|
else if (type_name == "unsigned char") {
|
||||||
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::UChar };
|
||||||
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
}
|
||||||
else if (type_name == "void") {
|
else if (type_name == "void") {
|
||||||
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Void };
|
auto ty = new types::FundamentalType{ is_const, types::FundamentalTypeKind::Void };
|
||||||
returned = std::shared_ptr<types::Type>{ ty };
|
returned = std::shared_ptr<types::Type>{ ty };
|
||||||
|
|||||||
@ -46,12 +46,36 @@ namespace types {
|
|||||||
if (this->m_const)
|
if (this->m_const)
|
||||||
out << "const ";
|
out << "const ";
|
||||||
switch (this->m_ty) {
|
switch (this->m_ty) {
|
||||||
|
case FundamentalTypeKind::ShortInt:
|
||||||
|
out << "ShortInt";
|
||||||
|
break;
|
||||||
case FundamentalTypeKind::Int:
|
case FundamentalTypeKind::Int:
|
||||||
out << "Int";
|
out << "Int";
|
||||||
break;
|
break;
|
||||||
|
case FundamentalTypeKind::LongInt:
|
||||||
|
out << "LongInt";
|
||||||
|
break;
|
||||||
|
case FundamentalTypeKind::LongLongInt:
|
||||||
|
out << "LongLongInt";
|
||||||
|
break;
|
||||||
|
case FundamentalTypeKind::UShortInt:
|
||||||
|
out << "UShortInt";
|
||||||
|
break;
|
||||||
|
case FundamentalTypeKind::UInt:
|
||||||
|
out << "UInt";
|
||||||
|
break;
|
||||||
|
case FundamentalTypeKind::ULongInt:
|
||||||
|
out << "ULongInt";
|
||||||
|
break;
|
||||||
|
case FundamentalTypeKind::ULongLongInt:
|
||||||
|
out << "ULongLongInt";
|
||||||
|
break;
|
||||||
case FundamentalTypeKind::Bool:
|
case FundamentalTypeKind::Bool:
|
||||||
out << "Bool";
|
out << "Bool";
|
||||||
break;
|
break;
|
||||||
|
case FundamentalTypeKind::UChar:
|
||||||
|
out << "UChar";
|
||||||
|
break;
|
||||||
case FundamentalTypeKind::Char:
|
case FundamentalTypeKind::Char:
|
||||||
out << "Char";
|
out << "Char";
|
||||||
break;
|
break;
|
||||||
@ -80,10 +104,21 @@ namespace types {
|
|||||||
|
|
||||||
bool FundamentalType::is_signed() {
|
bool FundamentalType::is_signed() {
|
||||||
switch (this->m_ty) {
|
switch (this->m_ty) {
|
||||||
|
case FundamentalTypeKind::ShortInt:
|
||||||
case FundamentalTypeKind::Int:
|
case FundamentalTypeKind::Int:
|
||||||
|
case FundamentalTypeKind::LongInt:
|
||||||
|
case FundamentalTypeKind::LongLongInt:
|
||||||
return true;
|
return true;
|
||||||
case FundamentalTypeKind::Bool:
|
case FundamentalTypeKind::UShortInt:
|
||||||
|
case FundamentalTypeKind::UInt:
|
||||||
|
case FundamentalTypeKind::ULongInt:
|
||||||
|
case FundamentalTypeKind::ULongLongInt:
|
||||||
|
return false;
|
||||||
case FundamentalTypeKind::Char:
|
case FundamentalTypeKind::Char:
|
||||||
|
return true;
|
||||||
|
case FundamentalTypeKind::UChar:
|
||||||
|
return false;
|
||||||
|
case FundamentalTypeKind::Bool:
|
||||||
return false;
|
return false;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("Invalid type");
|
throw std::runtime_error("Invalid type");
|
||||||
@ -92,11 +127,22 @@ namespace types {
|
|||||||
|
|
||||||
uint32_t FundamentalType::size() {
|
uint32_t FundamentalType::size() {
|
||||||
switch (this->m_ty) {
|
switch (this->m_ty) {
|
||||||
|
case FundamentalTypeKind::ShortInt:
|
||||||
|
case FundamentalTypeKind::UShortInt:
|
||||||
|
return 16;
|
||||||
case FundamentalTypeKind::Int:
|
case FundamentalTypeKind::Int:
|
||||||
|
case FundamentalTypeKind::UInt:
|
||||||
return 32;
|
return 32;
|
||||||
|
case FundamentalTypeKind::LongInt:
|
||||||
|
case FundamentalTypeKind::ULongInt:
|
||||||
|
return 64;
|
||||||
|
case FundamentalTypeKind::LongLongInt:
|
||||||
|
case FundamentalTypeKind::ULongLongInt:
|
||||||
|
return 128;
|
||||||
case FundamentalTypeKind::Bool:
|
case FundamentalTypeKind::Bool:
|
||||||
return 1;
|
return 1;
|
||||||
case FundamentalTypeKind::Char:
|
case FundamentalTypeKind::Char:
|
||||||
|
case FundamentalTypeKind::UChar:
|
||||||
return 8;
|
return 8;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("Invalid type");
|
throw std::runtime_error("Invalid type");
|
||||||
|
|||||||
10
src/types.h
10
src/types.h
@ -17,8 +17,18 @@ namespace types {
|
|||||||
|
|
||||||
enum FundamentalTypeKind {
|
enum FundamentalTypeKind {
|
||||||
Int,
|
Int,
|
||||||
|
ShortInt,
|
||||||
|
LongInt,
|
||||||
|
LongLongInt,
|
||||||
|
|
||||||
|
UInt,
|
||||||
|
UShortInt,
|
||||||
|
ULongInt,
|
||||||
|
ULongLongInt,
|
||||||
|
|
||||||
Bool,
|
Bool,
|
||||||
Char,
|
Char,
|
||||||
|
UChar,
|
||||||
Void,
|
Void,
|
||||||
/// @brief Mainly used for binop resolution
|
/// @brief Mainly used for binop resolution
|
||||||
Any,
|
Any,
|
||||||
|
|||||||
6
test.c
6
test.c
@ -25,7 +25,7 @@ void update_ptr(char* ptr) {
|
|||||||
*ptr = 50;
|
*ptr = 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
long long int main() {
|
||||||
// Test fibonacci sequence
|
// Test fibonacci sequence
|
||||||
char text[30] = "10th fibonacci number is %d!\n";
|
char text[30] = "10th fibonacci number is %d!\n";
|
||||||
printf(text, fibonacci(10));
|
printf(text, fibonacci(10));
|
||||||
@ -71,5 +71,9 @@ int main() {
|
|||||||
printf("while-counter: %d\n", counter++);
|
printf("while-counter: %d\n", counter++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
short int sh = 123;
|
||||||
|
long int lg = 456;
|
||||||
|
long long int longer = 789;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user