Skip to content

Commit

Permalink
feat: added pointers and references for low level implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 27, 2022
1 parent de99e20 commit dc0569f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/liblesma/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,23 @@ namespace lesma {
class Type : public Expression {
std::string name;
TokenType type;

// Pointer fields
Type *elementType;

// Function fields
std::vector<Type *> params;
Type *ret;

public:
Type(llvm::SMRange Loc, std::string name, TokenType type) : Expression(Loc), name(std::move(name)), type(type) {}
Type(llvm::SMRange Loc, std::string name, TokenType type, Type *elementType) : Expression(Loc), name(std::move(name)), type(type), elementType(elementType) {}
Type(llvm::SMRange Loc, std::string name, TokenType type, std::vector<Type *> params, Type *ret) : Expression(Loc), name(std::move(name)), type(type), params(std::move(params)), ret(ret) {}
~Type() override = default;

[[nodiscard]] [[maybe_unused]] std::string getName() const { return name; }
[[nodiscard]] [[maybe_unused]] TokenType getType() const { return type; }
[[nodiscard]] [[maybe_unused]] Type *getElementType() const { return elementType; }
[[nodiscard]] [[maybe_unused]] std::vector<Type *> getParams() const { return params; }
[[nodiscard]] [[maybe_unused]] Type *getReturnType() const { return ret; }

Expand Down
8 changes: 5 additions & 3 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ llvm::Type *Codegen::visit(lesma::Type *node) {
return Builder->getInt8PtrTy();
else if (node->getType() == TokenType::VOID_TYPE)
return Builder->getVoidTy();
else if (node->getType() == TokenType::PTR_TYPE)
return PointerType::get(visit(node->getElementType()), 0);
else if (node->getType() == TokenType::FUNC_TYPE) {
auto ret_type = visit(node->getReturnType());
std::vector<llvm::Type *> paramsTypes;
Expand Down Expand Up @@ -720,7 +722,7 @@ void Codegen::visit(Class *node) {
for (auto &&[field, elem_type]: zip(node->getFields(), elementTypes))
fields.emplace_back(field->getIdentifier()->getValue(), getType(elem_type));

auto *type = new SymbolType(TY_CLASS, fields);
auto *type = new SymbolType(TY_CLASS, fields, nullptr);
auto *structSymbol = new SymbolTableEntry(node->getIdentifier(), type);
structSymbol->setLLVMType(structType);
Scope->insertType(node->getIdentifier(), type);
Expand Down Expand Up @@ -748,7 +750,7 @@ void Codegen::visit(Enum *node) {
for (const auto &field: node->getValues())
fields.push_back({field, new SymbolType(TY_VOID)});

auto *type = new SymbolType(TY_ENUM, fields);
auto *type = new SymbolType(TY_ENUM, fields, nullptr);
auto *structSymbol = new SymbolTableEntry(node->getIdentifier(), type);
structSymbol->setLLVMType(structType);
Scope->insertType(node->getIdentifier(), type);
Expand Down Expand Up @@ -1056,7 +1058,7 @@ llvm::Value *Codegen::visit(Literal *node) {
else if (node->getType() == TokenType::INTEGER)
return ConstantInt::getSigned(Builder->getInt64Ty(), std::stoi(node->getValue()));
else if (node->getType() == TokenType::BOOL)
return ConstantInt::getBool(*TheContext, node->getValue() == "true");
return node->getValue() == "true" ? Builder->getTrue() : Builder->getFalse();
else if (node->getType() == TokenType::STRING)
return Builder->CreateGlobalStringPtr(node->getValue());
else if (node->getType() == TokenType::NIL)
Expand Down
6 changes: 5 additions & 1 deletion src/liblesma/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ void Parser::Error(Token *token, const std::string &error_message) {

Type *Parser::ParseType() {
auto type = Peek();
if (CheckAny<TokenType::INT_TYPE, TokenType::FLOAT_TYPE, TokenType::STRING_TYPE, TokenType::BOOL_TYPE,
if (Check(TokenType::STAR)) {
Advance();
auto element_type = ParseType();
return new Type({type->getStart(), element_type->getEnd()}, "*" + element_type->getName(), TokenType::PTR_TYPE, element_type);
} else if (CheckAny<TokenType::INT_TYPE, TokenType::FLOAT_TYPE, TokenType::STRING_TYPE, TokenType::BOOL_TYPE,
TokenType::VOID_TYPE>()) {
Advance();
return new Type(type->span, type->lexeme, type->type);
Expand Down
7 changes: 7 additions & 0 deletions src/liblesma/Symbol/SymbolType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ bool SymbolType::isOneOf(const std::vector<SymbolSuperType> &superTypes) const {
*/
SymbolSuperType SymbolType::getSuperType() const { return baseSuperType; }

/**
* Retrieve the super type of the current type
*
* @return Super type
*/
SymbolType *SymbolType::getElementType() const { return elementType; }

/**
* Retrieve the fields of the current type
*
Expand Down
8 changes: 5 additions & 3 deletions src/liblesma/Symbol/SymbolType.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ enum SymbolSuperType {
TY_FLOAT,
TY_STRING,
TY_BOOL,
TY_VOID,
TY_FUNCTION,
TY_CLASS,
TY_ENUM,
TY_VOID,
};

class SymbolType {
SymbolSuperType baseSuperType;
SymbolType *elementType;
std::vector<std::tuple<std::string, SymbolType *>> fields;

public:
// Constructors
explicit SymbolType(SymbolSuperType superType) : baseSuperType(superType), fields({}) {}
explicit SymbolType(SymbolSuperType superType, std::vector<std::tuple<std::string, SymbolType *>> fields) : baseSuperType(superType), fields(std::move(fields)) {}
explicit SymbolType(SymbolSuperType superType) : baseSuperType(superType), elementType(nullptr), fields({}) {}
explicit SymbolType(SymbolSuperType superType, std::vector<std::tuple<std::string, SymbolType *>> fields, SymbolType *elementType) : baseSuperType(superType), elementType(elementType), fields(std::move(fields)) {}

// Public methods
[[nodiscard]] bool is(SymbolSuperType superType) const;
[[nodiscard]] bool isPrimitive() const;
[[nodiscard]] bool isOneOf(const std::vector<SymbolSuperType> &superTypes) const;
[[nodiscard]] SymbolSuperType getSuperType() const;
[[nodiscard]] SymbolType *getElementType() const;
[[nodiscard]] std::vector<std::tuple<std::string, SymbolType *>> getFields() const;
friend bool operator==(const SymbolType &lhs, const SymbolType &rhs);
friend bool operator!=(const SymbolType &lhs, const SymbolType &rhs);
Expand Down
1 change: 1 addition & 0 deletions src/liblesma/Token/TokenType.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace lesma {
BOOL_TYPE,
VOID_TYPE,
FUNC_TYPE,
PTR_TYPE,
CUSTOM_TYPE,

// Keywords.
Expand Down

0 comments on commit dc0569f

Please sign in to comment.