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

Commit

Permalink
typechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik committed Jun 11, 2022
1 parent 4feb9b5 commit ba331bb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
13 changes: 8 additions & 5 deletions Peregrine/analyzer/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ bool TypeChecker::visit(const ast::VariableStatement& node) {
node.value()->accept(*this);
nonConstNode.setProcessedType(m_result,defined_before);
varType = m_result;
} else {
check(node.value(), *varType);
nonConstNode.setProcessedType(varType,defined_before);
} else{
if(node.value()->type()!=ast::KAstNoLiteral){
check(node.value(), *varType);
}
nonConstNode.setProcessedType(varType,true);
}

//TODO:Check if it is an identifier
Expand All @@ -159,11 +161,12 @@ bool TypeChecker::visit(const ast::ConstDeclaration& node) {
node.value()->accept(*this);
nonConstNode.setProcessedType(m_result);
constType = m_result;
} else {
} else{
check(node.value(), *constType);
nonConstNode.setProcessedType(constType);
nonConstNode.setProcessedType(NULL);
}

//TODO:Check if it is an identifier
m_env->set(identifierName(node.name()), constType);
return true;
}
Expand Down
15 changes: 9 additions & 6 deletions Peregrine/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,13 @@ types::TypePtr VariableStatement::processedType() const {
}

void VariableStatement::setProcessedType(types::TypePtr processedType,bool defined_before) {
m_processedType = processedType;
m_processedType = processedType;
if(!defined_before&&m_processedType!=NULL){
m_type=m_processedType->getTypeAst();
}
if (m_value->type()==KAstNoLiteral&&m_processedType!=NULL) {
m_value=m_processedType->defaultValue();
}
}

Token VariableStatement::token() const { return m_token; }
Expand Down Expand Up @@ -517,8 +520,8 @@ types::TypePtr ConstDeclaration::processedType() const {
}

void ConstDeclaration::setProcessedType(types::TypePtr processedType) {
m_processedType = processedType;
if(m_processedType!=NULL){
m_processedType = processedType;
m_type=m_processedType->getTypeAst();
}
}
Expand All @@ -530,13 +533,13 @@ AstKind ConstDeclaration::type() const { return KAstConstDecl; }
std::string ConstDeclaration::stringify() const {
std::string res = "const ";

if (m_type->type() != KAstNoLiteral) {
res += m_type->stringify();
res += " ";
}

res += m_name->stringify();

if (m_type->type() != KAstNoLiteral) {
res += ":";
res += m_type->stringify();
}
res += " = ";
res += m_value->stringify();

Expand Down
2 changes: 1 addition & 1 deletion Peregrine/ast/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class ListLiteral : public AstNode {
std::vector<AstNodePtr> m_elements;

public:
ListLiteral(Token tok, std::vector<AstNodePtr> elements);
ListLiteral(Token tok, std::vector<AstNodePtr> elements={});

std::vector<AstNodePtr> elements() const;

Expand Down
38 changes: 31 additions & 7 deletions Peregrine/ast/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ ast::AstNodePtr IntType::getTypeAst() const {
return std::make_shared<ast::TypeExpression>((Token){}, res);
}

ast::AstNodePtr IntType::defaultValue() const {
return std::make_shared<ast::IntegerLiteral>((Token){.keyword="0",.tkType=tk_integer}, "0");
}

DecimalType::DecimalType(DecimalSize decimalSize) {
m_decimalSize = decimalSize;
}
Expand All @@ -167,7 +171,8 @@ bool DecimalType::isConvertibleTo(const Type& type) const {

if (!isFloat() && typeDecimal.isFloat())
return false;

if(m_decimalSize>typeDecimal.size())
return false;
return true;
}

Expand Down Expand Up @@ -269,7 +274,9 @@ bool DecimalType::operator==(const Type& type) const {

return false;
}

ast::AstNodePtr DecimalType::defaultValue() const {
return std::make_shared<ast::DecimalLiteral>((Token){.keyword="0",.tkType=tk_decimal}, "0");
}
TypeCategory StringType::category() const { return TypeCategory::String; }

bool StringType::isConvertibleTo(const Type& type) const {
Expand Down Expand Up @@ -332,7 +339,9 @@ TypePtr StringType::infixOperatorResult(Token op, const TypePtr type) const {
ast::AstNodePtr StringType::getTypeAst() const {
return std::make_shared<ast::TypeExpression>((Token){}, "str");
}

ast::AstNodePtr StringType::defaultValue() const {
return std::make_shared<ast::StringLiteral>((Token){.keyword="\"\"",.tkType=tk_string}, "\"\"",false);
}
TypeCategory BoolType::category() const { return TypeCategory::Bool; }

bool BoolType::isConvertibleTo(const Type& type) const {
Expand Down Expand Up @@ -365,6 +374,10 @@ ast::AstNodePtr BoolType::getTypeAst() const {
return std::make_shared<ast::TypeExpression>((Token){}, "bool");
}

ast::AstNodePtr BoolType::defaultValue() const {
return std::make_shared<ast::BoolLiteral>((Token){.keyword="False",.tkType=tk_false}, "False");
}

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

TypeCategory PointerType::category() const { return TypeCategory::Pointer; }
Expand Down Expand Up @@ -444,6 +457,10 @@ ast::AstNodePtr PointerType::getTypeAst() const {
return std::make_shared<ast::PointerTypeExpr>((Token){}, m_baseType->getTypeAst());
}

ast::AstNodePtr PointerType::defaultValue() const {
return std::make_shared<ast::NoneLiteral>((Token){.keyword="None",.tkType=tk_none});
}

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

bool VoidType::isConvertibleTo(const Type& type) const { return false; }
Expand All @@ -455,7 +472,9 @@ std::string VoidType::stringify() const { return "void"; }
ast::AstNodePtr VoidType::getTypeAst() const {
return std::make_shared<ast::TypeExpression>((Token){}, "void");
}

ast::AstNodePtr VoidType::defaultValue() const {
return std::make_shared<ast::NoLiteral>();
}
ListType::ListType(TypePtr elemType, std::string size) {
m_elemType = elemType;
m_size = size;
Expand Down Expand Up @@ -524,7 +543,9 @@ ast::AstNodePtr ListType::getTypeAst() const {
}
return std::make_shared<ast::ListTypeExpr>((Token){}, m_elemType->getTypeAst(),size);
}

ast::AstNodePtr ListType::defaultValue() const {
return std::make_shared<ast::ListLiteral>((Token){});
}
UserDefinedType::UserDefinedType(TypePtr baseType) { m_baseType = baseType; }

TypeCategory UserDefinedType::category() const {
Expand All @@ -540,7 +561,9 @@ bool UserDefinedType::isConvertibleTo(const Type& type) const {
bool UserDefinedType::isCastableTo(const Type& type) const {
return m_baseType->isCastableTo(type);
}

ast::AstNodePtr UserDefinedType::defaultValue() const {
return m_baseType->defaultValue();
}
// TODO
std::string UserDefinedType::stringify() const { return m_baseType->stringify(); }

Expand Down Expand Up @@ -592,7 +615,8 @@ bool FunctionType::isConvertibleTo(const Type& type) const {

// TODO
bool FunctionType::isCastableTo(const Type& type) const { return false; }

// TODO
ast::AstNodePtr FunctionType::defaultValue() const {return std::make_shared<ast::NoLiteral>();}
std::string FunctionType::stringify() const { return "function"; }

ast::AstNodePtr FunctionType::getTypeAst() const {
Expand Down
12 changes: 12 additions & 0 deletions Peregrine/ast/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Type {
// using cast)
virtual bool isCastableTo(const Type& type) const = 0;

//some default value for the type
virtual ast::AstNodePtr defaultValue() const = 0;

virtual std::string stringify() const { return ""; }

// returns the type obtained after applying the given operator to this type
Expand Down Expand Up @@ -86,6 +89,7 @@ class IntType : public Type {
Modifier modifier() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
std::string stringify() const;

TypePtr prefixOperatorResult(Token op) const;
Expand All @@ -110,6 +114,7 @@ class DecimalType : public Type {
DecimalSize size() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
std::string stringify() const;

bool isFloat() const;
Expand All @@ -133,6 +138,7 @@ class StringType : public Type {
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;
Expand All @@ -147,6 +153,7 @@ class BoolType : public Type {
TypeCategory category() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
std::string stringify() const;
};

Expand All @@ -162,6 +169,7 @@ class PointerType : public Type {
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;
Expand All @@ -175,6 +183,7 @@ class VoidType : public Type {
ast::AstNodePtr getTypeAst() const;
TypeCategory category() const;
bool isConvertibleTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
bool isCastableTo(const Type& type) const;
std::string stringify() const;
};
Expand All @@ -187,6 +196,7 @@ class ListType : public Type {
ListType(TypePtr elemType, std::string size);

ast::AstNodePtr getTypeAst() const;
ast::AstNodePtr defaultValue() const;
TypeCategory category() const;
TypePtr elemType() const;
std::string size() const;
Expand All @@ -207,6 +217,7 @@ class UserDefinedType : public Type {
TypeCategory category() const;
TypePtr baseType() const;
bool isConvertibleTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
bool isCastableTo(const Type& type) const;
std::string stringify() const;

Expand All @@ -226,6 +237,7 @@ class FunctionType : public Type {
TypePtr returnType() const;
bool isConvertibleTo(const Type& type) const;
bool isCastableTo(const Type& type) const;
ast::AstNodePtr defaultValue() const;
std::string stringify() const;

bool operator==(const Type& type) const;
Expand Down

0 comments on commit ba331bb

Please sign in to comment.