-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement type checker for strong typing
- Loading branch information
Showing
15 changed files
with
360 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "include/checker.hpp" | ||
#include "../common/include/errors.hpp" | ||
|
||
TypeChecker::TypeChecker(RuntimeEnvironment& env) : env(env) {}; | ||
|
||
ASTValueType TypeChecker::determineType(ASTBase* expression) { | ||
switch (expression->type) { | ||
case ASTType::Value: { | ||
return static_cast<ASTValue*>(expression)->type; | ||
} | ||
case ASTType::Variable: { | ||
ASTVariable* var = static_cast<ASTVariable*>(expression); | ||
ASTVariableDeclaration* declaration = env.getVariable(var->name); | ||
return determineType(declaration->value.get()); | ||
} | ||
case ASTType::BinaryOperation: { | ||
ASTBinaryOperation* operation = static_cast<ASTBinaryOperation*>(expression); | ||
ASTValueType leftType = determineType(operation->left.get()); | ||
ASTValueType rightType = determineType(operation->right.get()); | ||
|
||
if (!isNumber(leftType)) return leftType; | ||
if (!isNumber(rightType)) return rightType; | ||
if (leftType == ASTValueType::Float || rightType == ASTValueType::Float) { | ||
return ASTValueType::Float; | ||
} | ||
return ASTValueType::Integer; | ||
} | ||
default: | ||
throw ZynkError{ ZynkErrorType::TypeError, "Cannot determine type for given AST type." }; | ||
} | ||
} | ||
|
||
void TypeChecker::checkType(const ASTValueType& declared, ASTBase* value) { | ||
ASTValueType valueType = determineType(value); | ||
if (declared != valueType) { | ||
throw ZynkError{ | ||
ZynkErrorType::TypeError, | ||
"Type mismatch. Declared type is " + typeToString(declared) + | ||
", but assigned value is of type " + typeToString(valueType) + "." | ||
}; | ||
} | ||
} | ||
|
||
bool TypeChecker::isNumber(const ASTValueType& type) { | ||
return type == ASTValueType::Float || type == ASTValueType::Integer; | ||
} | ||
|
||
std::string TypeChecker::typeToString(const ASTValueType& type) { | ||
switch (type) { | ||
case ASTValueType::String: | ||
return "string"; | ||
case ASTValueType::Integer: | ||
return "int"; | ||
case ASTValueType::Float: | ||
return "float"; | ||
case ASTValueType::Bool: | ||
return "bool"; | ||
default: | ||
return "Unknown Type"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CHECKER_HPP | ||
#define CHECKER_HPP | ||
|
||
#include "../../parsing/include/ast.hpp" | ||
#include "../../execution/include/runtime.hpp" | ||
|
||
class TypeChecker { | ||
public: | ||
TypeChecker(RuntimeEnvironment& env); | ||
void checkType(const ASTValueType& declared, ASTBase* value); | ||
ASTValueType determineType(ASTBase* expression); | ||
private: | ||
RuntimeEnvironment& env; | ||
std::string typeToString(const ASTValueType& type); | ||
bool isNumber(const ASTValueType& type); | ||
}; | ||
|
||
#endif // CHECKER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.