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