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

Commit

Permalink
created some templetes
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik committed Oct 9, 2022
1 parent add3f13 commit 4fbb9f5
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 7 deletions.
148 changes: 148 additions & 0 deletions Peregrine/ast/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,154 @@ ast::AstNodePtr UnionTypeDef::defaultValue() const{
return std::make_shared<ast::NoLiteral>();
}


ExternUnionTypeDef::ExternUnionTypeDef(std::string lib,std::string name,std::map<std::string,TypePtr> items){
m_name=name;
m_items=items;
m_lib=lib;
}

ast::AstNodePtr ExternUnionTypeDef::getTypeAst() const{
auto lib = std::make_shared<ast::IdentifierExpression>(Token{},m_lib);
auto type=std::make_shared<ast::TypeExpression>(Token{},m_name);
return std::make_shared<ast::DotExpression>(Token{},lib,type);
}

TypeCategory ExternUnionTypeDef::category() const{
return TypeCategory::ExternUnion;
}

bool ExternUnionTypeDef::isConvertibleTo(const Type& type) const{
return false;
}

bool ExternUnionTypeDef::isCastableTo(const Type& type) const{
switch(type.category()){
case TypeCategory::Pointer:
return true;
default:
return false;
}
}

std::string ExternUnionTypeDef::stringify() const{
return m_lib+"."+m_name;
}

std::map<std::string,TypePtr> ExternUnionTypeDef::getItem() const{
return m_items;
}

std::string ExternUnionTypeDef::getName() const{
return m_name;
}

std::string ExternUnionTypeDef::getLibName() const{
return m_lib;
}

bool ExternUnionTypeDef::operator==(const Type& type) const{
if(type.category() == TypeCategory::ExternUnion){
auto& unionType=dynamic_cast<const ExternUnionTypeDef&>(type);
auto items=unionType.getItem();
if(m_lib!=unionType.getLibName()){
return false;
}
else if(m_name==unionType.getName()&&items.size()==m_items.size()){
for(auto& item : items){
if(m_items.contains(item.first)){
auto pos = m_items.find(item.first);
if(pos->second!=item.second){
return false;
}
}
else{
return false;
}
}
}
}
return false;
}

ast::AstNodePtr ExternUnionTypeDef::defaultValue() const{
return std::make_shared<ast::NoLiteral>();
}


ExternStructTypeDef::ExternStructTypeDef(std::string lib,std::string name,std::map<std::string,TypePtr> items){
m_name=name;
m_items=items;
m_lib=lib;
}

ast::AstNodePtr ExternStructTypeDef::getTypeAst() const{
auto lib = std::make_shared<ast::IdentifierExpression>(Token{},m_lib);
auto type=std::make_shared<ast::TypeExpression>(Token{},m_name);
return std::make_shared<ast::DotExpression>(Token{},lib,type);
}

TypeCategory ExternStructTypeDef::category() const{
return TypeCategory::ExternStruct;
}

bool ExternStructTypeDef::isConvertibleTo(const Type& type) const{
return false;
}

bool ExternStructTypeDef::isCastableTo(const Type& type) const{
switch(type.category()){
case TypeCategory::Pointer:
return true;
default:
return false;
}
}

std::string ExternStructTypeDef::stringify() const{
return m_lib+"."+m_name;
}

std::map<std::string,TypePtr> ExternStructTypeDef::getItem() const{
return m_items;
}

std::string ExternStructTypeDef::getName() const{
return m_name;
}

std::string ExternStructTypeDef::getLibName() const{
return m_lib;
}

bool ExternStructTypeDef::operator==(const Type& type) const{
if(type.category() == TypeCategory::ExternStruct){
auto& structType=dynamic_cast<const ExternStructTypeDef&>(type);
auto items=structType.getItem();
if(m_lib!=structType.getLibName()){
return false;
}
else if(m_name==structType.getName()&&items.size()==m_items.size()){
for(auto& item : items){
if(m_items.contains(item.first)){
auto pos = m_items.find(item.first);
if(pos->second!=item.second){
return false;
}
}
else{
return false;
}
}
}
}
return false;
}

ast::AstNodePtr ExternStructTypeDef::defaultValue() const{
return std::make_shared<ast::NoLiteral>();
}

std::array<TypePtr, 8> TypeProducer::m_integer = {
std::make_shared<IntType>(IntType::IntSizes::Int8),
std::make_shared<IntType>(IntType::IntSizes::Int16),
Expand Down
48 changes: 47 additions & 1 deletion Peregrine/ast/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ enum TypeCategory {
MultipleReturn,
Enum,
Void,
Union
Union,
ExternUnion,
ExternStruct,
};

class Type;
Expand Down Expand Up @@ -78,6 +80,50 @@ class Type {
bool operator!=(const Type& type) const { return !operator==(type); }
};


class ExternUnionTypeDef : public Type {
public:
ExternUnionTypeDef(std::string lib,std::string name,std::map<std::string,TypePtr> items);

ast::AstNodePtr getTypeAst() const;
TypeCategory category() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
std::string stringify() const;
std::map<std::string,TypePtr> getItem() const;
std::string getName() const;
std::string getLibName() const;
ast::AstNodePtr defaultValue() const;

bool operator==(const Type& type) const;
private:
std::map<std::string,TypePtr> m_items;
std::string m_name;
std::string m_lib;
};


class ExternStructTypeDef : public Type {
public:
ExternStructTypeDef(std::string lib,std::string name,std::map<std::string,TypePtr> items);

ast::AstNodePtr getTypeAst() const;
TypeCategory category() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
std::string stringify() const;
std::map<std::string,TypePtr> getItem() const;
std::string getName() const;
std::string getLibName() const;
ast::AstNodePtr defaultValue() const;

bool operator==(const Type& type) const;
private:
std::map<std::string,TypePtr> m_items;
std::string m_name;
std::string m_lib;
};

class IntType : public Type {
public:
enum IntSizes { Int8, Int16, Int32, Int64 };
Expand Down
5 changes: 0 additions & 5 deletions Peregrine/parser/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ AstNodePtr Parser::parseDict() {
}

AstNodePtr Parser::parseIdentifier() {
//identifier name
if (m_currentToken.tkType!=tk_identifier){
error(m_currentToken,
"Expected identifier but got " + m_currentToken.keyword + " instead");
}
return std::make_shared<IdentifierExpression>(m_currentToken,
m_currentToken.keyword);
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align='center'>
<img id="banner" src="./graphics/banner.png" style="width: 65%; align: center;"/>
<a href="https://peregrine-lang.github.io"><img id="banner" src="./graphics/banner.png" style="width: 65%; align: center;"/></a>
<br/>
<i>A Blazing-Fast Language for the Blazing-Fast world.</i>
<br/><br/>
Expand Down

0 comments on commit 4fbb9f5

Please sign in to comment.