Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
ref type
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik committed Aug 29, 2022
1 parent c424195 commit 04d9dc3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
36 changes: 36 additions & 0 deletions Peregrine/ast/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,42 @@ ast::AstNodePtr PointerType::defaultValue() const {
return std::make_shared<ast::NoneLiteral>((Token){.keyword="None",.tkType=tk_none});
}

ReferenceType::ReferenceType(TypePtr baseType) { m_baseType = baseType; }

TypeCategory ReferenceType::category() const { return TypeCategory::Reference; }

TypePtr ReferenceType::baseType() const { return m_baseType; }

bool ReferenceType::isConvertibleTo(const Type& type) const {
return m_baseType->isConvertibleTo(type);
}

bool ReferenceType::isCastableTo(const Type& type) const {
return m_baseType->isCastableTo(type);
}

std::string ReferenceType::stringify() const {
return "&" + m_baseType->stringify();
}

TypePtr ReferenceType::prefixOperatorResult(Token op) const {
return m_baseType->prefixOperatorResult(op);
}
TypePtr ReferenceType::postfixOperatorResult(Token op) const{
return m_baseType->postfixOperatorResult(op);
}
TypePtr ReferenceType::infixOperatorResult(Token op, const TypePtr type) const {
return m_baseType->infixOperatorResult(op, type);
}

ast::AstNodePtr ReferenceType::getTypeAst() const {
return std::make_shared<ast::RefTypeExpr>((Token){}, m_baseType->getTypeAst());
}

ast::AstNodePtr ReferenceType::defaultValue() const {
return NULL;
}

TypeCategory VoidType::category() const { return TypeCategory::Void; }

bool VoidType::isConvertibleTo(const Type& type) const { return false; }
Expand Down
20 changes: 20 additions & 0 deletions Peregrine/ast/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum TypeCategory {
String,
Bool,
Pointer,
Reference,
List,
Dict,
UserDefined,
Expand Down Expand Up @@ -179,6 +180,25 @@ class PointerType : public Type {
TypePtr infixOperatorResult(Token op, const TypePtr type) const;
};

class ReferenceType : public Type {
TypePtr m_baseType;

public:
ReferenceType(TypePtr baseType);

ast::AstNodePtr getTypeAst() const;
TypeCategory category() const;
TypePtr baseType() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
std::string stringify() const;
ast::AstNodePtr defaultValue() const;

TypePtr prefixOperatorResult(Token op) const;
TypePtr postfixOperatorResult(Token op) const;
TypePtr infixOperatorResult(Token op, const TypePtr type) const;
};

class VoidType : public Type {
public:
VoidType() = default;
Expand Down
6 changes: 3 additions & 3 deletions Peregrine/test.pe
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
enum test:
a,b,c
union union_test1:
a:int
a:int
b:int
union union_test:
a:int
a:int
b:union_test1
def may():
c:union_test
Expand All @@ -29,4 +29,4 @@ def main():
#l=f #Should be an error
j=def (x:int):june(x)
a,b,c=m()
n=testing2
n=testing2

0 comments on commit 04d9dc3

Please sign in to comment.