Added namespaces to most classes, general cleanup
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2019-06-17 18:35:12 +02:00
parent 21d3329c55
commit fde102d954
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
66 changed files with 4301 additions and 3909 deletions

View File

@ -6,6 +6,9 @@
#include "../UserData/UserDataScriptType.hpp"
#include <memory>
using namespace Porygon::Parser;
namespace Porygon::Binder {
BoundScriptStatement *Binder::Bind(Script *script, const ParsedScriptStatement *s, BoundScope *scriptScope) {
auto binder = Binder();
binder._scriptData = script;
@ -25,16 +28,25 @@ Binder::~Binder() {
BoundStatement *Binder::BindStatement(const ParsedStatement *statement) {
switch (statement->GetKind()) {
case ParsedStatementKind ::Script: throw; // This shouldn't happen.
case ParsedStatementKind ::Block: return this -> BindBlockStatement(statement);
case ParsedStatementKind ::Expression: return this -> BindExpressionStatement(statement);
case ParsedStatementKind::Assignment: return this -> BindAssignmentStatement(statement);
case ParsedStatementKind::IndexAssignment: return this -> BindIndexAssignmentStatement(statement);
case ParsedStatementKind ::FunctionDeclaration: return this->BindFunctionDeclarationStatement(statement);
case ParsedStatementKind::Return: return this -> BindReturnStatement(statement);
case ParsedStatementKind::Conditional: return this -> BindConditionalStatement(statement);
case ParsedStatementKind::Script:
throw; // This shouldn't happen.
case ParsedStatementKind::Block:
return this->BindBlockStatement(statement);
case ParsedStatementKind::Expression:
return this->BindExpressionStatement(statement);
case ParsedStatementKind::Assignment:
return this->BindAssignmentStatement(statement);
case ParsedStatementKind::IndexAssignment:
return this->BindIndexAssignmentStatement(statement);
case ParsedStatementKind::FunctionDeclaration:
return this->BindFunctionDeclarationStatement(statement);
case ParsedStatementKind::Return:
return this->BindReturnStatement(statement);
case ParsedStatementKind::Conditional:
return this->BindConditionalStatement(statement);
case ParsedStatementKind::Bad: return new BoundBadStatement();
case ParsedStatementKind::Bad:
return new BoundBadStatement();
}
}
@ -64,9 +76,9 @@ BoundStatement* Binder::BindAssignmentStatement(const ParsedStatement *statement
if (assignment.GetResult() == VariableAssignmentResult::Ok) {
auto key = assignment.GetKey();
return new BoundAssignmentStatement(key, boundExpression);
}
else{
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength());
} else {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
}
@ -82,8 +94,10 @@ BoundStatement *Binder::BindIndexAssignmentStatement(const ParsedStatement *stat
}
auto valueExpression = this->BindExpression(s->GetValueExpression());
auto boundIndexType = indexable->GetType();
if (boundIndexType ->GetClass() != TypeClass ::Error && boundIndexType->operator!=(valueExpression->GetType().get())){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidTableValueType, statement->GetStartPosition(), statement->GetLength());
if (boundIndexType->GetClass() != TypeClass::Error &&
boundIndexType->operator!=(valueExpression->GetType().get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType,
statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
}
@ -93,14 +107,17 @@ BoundStatement *Binder::BindIndexAssignmentStatement(const ParsedStatement *stat
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s) {
auto hash = s.GetHash();
switch (hash) {
case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false);
case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool);
case HashedString::ConstHash("string"): return std::make_shared<StringScriptType>(false, 0);
case HashedString::ConstHash("number"):
return std::make_shared<NumericScriptType>(false, false);
case HashedString::ConstHash("bool"):
return std::make_shared<ScriptType>(TypeClass::Bool);
case HashedString::ConstHash("string"):
return std::make_shared<StringScriptType>(false, 0);
default:
if (!UserDataStorage::HasUserDataType(hash)){
if (!UserData::UserDataStorage::HasUserDataType(hash)) {
return nullptr;
}
return std::make_shared<UserDataScriptType>(hash);
return std::make_shared<UserData::UserDataScriptType>(hash);
}
}
@ -116,15 +133,15 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *
auto var = parameters->at(i);
auto parsedType = ParseTypeIdentifier(var->GetType());
if (parsedType == nullptr) {
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidTypeName, statement->GetStartPosition(), statement->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTypeName, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
parameterTypes.at(i) = parsedType;
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok) {
parameterKeys.at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
}
else{
} else {
//TODO: log error
continue;
}
@ -137,7 +154,8 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() != VariableAssignmentResult::Ok) {
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
auto boundBlock = this->BindBlockStatement(functionStatement->GetBlock());
@ -155,7 +173,8 @@ BoundStatement *Binder::BindReturnStatement(const ParsedStatement* statement){
currentReturnType = this->_currentFunction->GetReturnType();
}
if (expression == nullptr && currentReturnType != nullptr) {
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
auto boundExpression = this->BindExpression(expression);
@ -169,7 +188,8 @@ BoundStatement *Binder::BindReturnStatement(const ParsedStatement* statement){
return new BoundReturnStatement(boundExpression);
}
if (currentReturnType.get()->operator!=(expresionType.get())) {
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
return new BoundReturnStatement(boundExpression);
@ -179,7 +199,8 @@ BoundStatement *Binder::BindConditionalStatement(const ParsedStatement* statemen
auto conditionalStatement = (ParsedConditionalStatement *) statement;
auto boundCondition = this->BindExpression(conditionalStatement->GetCondition());
if (boundCondition->GetType()->GetClass() != TypeClass::Bool) {
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::ConditionNotABool, statement->GetStartPosition(), statement->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ConditionNotABool, statement->GetStartPosition(),
statement->GetLength());
return new BoundBadStatement();
}
auto boundBlock = this->BindStatement(conditionalStatement->GetBlock());
@ -193,13 +214,17 @@ BoundStatement *Binder::BindConditionalStatement(const ParsedStatement* statemen
BoundExpression *Binder::BindExpression(const ParsedExpression *expression) {
switch (expression->GetKind()) {
case ParsedExpressionKind::LiteralInteger:
return new BoundLiteralIntegerExpression(((LiteralIntegerExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
return new BoundLiteralIntegerExpression(((LiteralIntegerExpression *) expression)->GetValue(),
expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind::LiteralFloat:
return new BoundLiteralFloatExpression(((LiteralFloatExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
return new BoundLiteralFloatExpression(((LiteralFloatExpression *) expression)->GetValue(),
expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind::LiteralString:
return new BoundLiteralStringExpression(((LiteralStringExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
return new BoundLiteralStringExpression(((LiteralStringExpression *) expression)->GetValue(),
expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind::LiteralBool:
return new BoundLiteralBoolExpression(((LiteralBoolExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
return new BoundLiteralBoolExpression(((LiteralBoolExpression *) expression)->GetValue(),
expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind::Variable:
return this->BindVariableExpression((VariableExpression *) expression);
@ -231,12 +256,14 @@ BoundExpression* Binder::BindVariableExpression(const VariableExpression* expres
auto key = expression->GetValue();
auto scope = this->_scope->Exists(key.GetHash());
if (scope == -1) {
this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::VariableNotFound, expression->GetStartPosition(), expression->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::VariableNotFound, expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
auto var = this->_scope->GetVariable(scope, key.GetHash());
auto type = var->GetType();
return new BoundVariableExpression(new BoundVariableKey(key.GetHash(), scope, false), type, expression->GetStartPosition(), expression->GetLength());
return new BoundVariableExpression(new BoundVariableKey(key.GetHash(), scope, false), type,
expression->GetStartPosition(), expression->GetLength());
}
BoundExpression *Binder::BindBinaryOperator(const BinaryExpression *expression) {
@ -254,16 +281,18 @@ BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
}
} else if (boundLeftType->GetClass() == TypeClass::String) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared<StringScriptType>(false,
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation,
std::make_shared<StringScriptType>(false,
0),
expression->GetStartPosition(), expression->GetLength());
}
@ -275,10 +304,11 @@ BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Subtraction,
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
@ -292,10 +322,11 @@ BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Multiplication,
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
@ -309,10 +340,11 @@ BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Division,
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
@ -320,43 +352,56 @@ BoundExpression* Binder::BindBinaryOperator(const BinaryExpression* expression){
}
break;
case BinaryOperatorKind::Equality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, std::make_shared<ScriptType>(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
case BinaryOperatorKind::Inequality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, std::make_shared<ScriptType>(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
case BinaryOperatorKind::Less:
if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThan, std::make_shared<ScriptType>(TypeClass::Bool),
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThan,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::LessOrEquals:
if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThanEquals, std::make_shared<ScriptType>(TypeClass::Bool),
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThanEquals,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::Greater:
if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThan, std::make_shared<ScriptType>(TypeClass::Bool),
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThan,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::GreaterOrEquals:
if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThanEquals, std::make_shared<ScriptType>(TypeClass::Bool),
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThanEquals,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::LogicalAnd:
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, std::make_shared<ScriptType>(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
break;
case BinaryOperatorKind::LogicalOr:
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, std::make_shared<ScriptType>(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
break;
}
this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(), expression->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -374,7 +419,9 @@ BoundExpression* Binder::BindUnaryOperator(const UnaryExpression* expression){
if (operandType->GetClass() == TypeClass::Number) {
auto innerType = std::dynamic_pointer_cast<NumericScriptType>(operandType);
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
std::make_shared<NumericScriptType>(innerType.get()->IsAwareOfFloat(), innerType.get()->IsFloat()),
std::make_shared<NumericScriptType>(
innerType.get()->IsAwareOfFloat(),
innerType.get()->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
break;
@ -388,7 +435,8 @@ BoundExpression* Binder::BindUnaryOperator(const UnaryExpression* expression){
default:
break;
}
this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::NoUnaryOperationFound, expression->GetStartPosition(), expression->GetLength());
this->_scriptData->Diagnostics->LogError(DiagnosticCode::NoUnaryOperationFound, expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -397,7 +445,8 @@ BoundExpression* Binder::BindFunctionCall(const FunctionCallExpression* expressi
auto functionExpression = BindExpression(expression->GetFunction());
auto type = functionExpression->GetType();
if (type->GetClass() != TypeClass::Function) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ExpressionIsNotAFunction, expression->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ExpressionIsNotAFunction,
expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -405,7 +454,8 @@ BoundExpression* Binder::BindFunctionCall(const FunctionCallExpression* expressi
auto parameterTypes = functionType->GetParameterTypes();
auto givenParameters = expression->GetParameters();
if (parameterTypes.size() != givenParameters->size()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, expression->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch,
expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -414,7 +464,8 @@ BoundExpression* Binder::BindFunctionCall(const FunctionCallExpression* expressi
auto parameter = givenParameters->at(i);
auto boundParameter = this->BindExpression(parameter);
if (boundParameter->GetType().get()->operator!=(parameterTypes.at(i).get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, parameter->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch,
parameter->GetStartPosition(),
parameter->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -437,16 +488,18 @@ BoundExpression *Binder::BindIndexExpression(const IndexExpression *expression,
}
if (indexerType->GetClass() == TypeClass::UserData) {
auto stringKey = dynamic_pointer_cast<StringScriptType>(index->GetType());
auto field = dynamic_pointer_cast<UserDataScriptType>(indexerType) -> GetField(stringKey->GetHashValue());
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue());
if (!setter) {
if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter, index->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter,
index->GetStartPosition(),
index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
} else {
if (!field->HasSetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter, index->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter,
index->GetStartPosition(),
index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -454,7 +507,8 @@ BoundExpression *Binder::BindIndexExpression(const IndexExpression *expression,
}
auto resultType = indexer->GetType()->GetIndexedType(index->GetType().get());
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength());
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(),
expression->GetLength());
}
BoundExpression *Binder::BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter) {
@ -467,16 +521,18 @@ BoundExpression* Binder::BindPeriodIndexExpression(const PeriodIndexExpression*
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
if (indexerType->GetClass() == TypeClass::UserData) {
auto field = dynamic_pointer_cast<UserDataScriptType>(indexerType) -> GetField(identifier . GetHash());
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash());
if (!setter) {
if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter, expression->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter,
expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
} else {
if (!field->HasSetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter, expression->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter,
expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
@ -484,7 +540,8 @@ BoundExpression* Binder::BindPeriodIndexExpression(const PeriodIndexExpression*
}
auto resultType = indexer->GetType()->GetIndexedType(identifier.GetHash());
return new BoundPeriodIndexExpression(indexer, identifier, resultType, expression->GetStartPosition(), expression->GetLength());
return new BoundPeriodIndexExpression(indexer, identifier, resultType, expression->GetStartPosition(),
expression->GetLength());
}
@ -498,7 +555,8 @@ BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTable
for (int i = 1; i < expressions->size(); i++) {
boundExpressions[i] = this->BindExpression(expressions->at(i));
if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType, boundExpressions[i]->GetStartPosition(),
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType,
boundExpressions[i]->GetStartPosition(),
boundExpressions[i]->GetLength());
}
}
@ -507,7 +565,8 @@ BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTable
valueType = std::make_shared<ScriptType>(TypeClass::Nil);
}
auto tableType = std::make_shared<NumericalTableScriptType>(valueType);
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), expression->GetLength());
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(),
expression->GetLength());
}
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
@ -521,6 +580,7 @@ BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expres
auto tableType = std::make_shared<TableScriptType>(tableScope, innerScope->GetLocalVariableCount());
delete innerScope;
return new BoundTableExpression((BoundBlockStatement*)block, tableType, expression->GetStartPosition(), expression->GetLength());
return new BoundTableExpression((BoundBlockStatement *) block, tableType, expression->GetStartPosition(),
expression->GetLength());
}
}

View File

@ -8,36 +8,54 @@
#include "../Parser/ParsedExpressions/ParsedTableExpression.hpp"
using namespace std;
using namespace Porygon::Parser;
namespace Porygon::Binder {
class Binder {
Script* _scriptData;
Porygon::Script *_scriptData;
BoundScope *_scope;
shared_ptr<FunctionScriptType> _currentFunction;
~Binder();
BoundStatement *BindStatement(const ParsedStatement *statement);
BoundStatement *BindBlockStatement(const ParsedStatement *statement);
BoundStatement *BindExpressionStatement(const ParsedStatement *statement);
BoundStatement *BindAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindIndexAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindFunctionDeclarationStatement(const ParsedStatement *statement);
BoundStatement *BindReturnStatement(const ParsedStatement *statement);
BoundStatement *BindConditionalStatement(const ParsedStatement *statement);
BoundExpression *BindExpression(const ParsedExpression *expression);
BoundExpression *BindVariableExpression(const VariableExpression *expression);
BoundExpression *BindBinaryOperator(const BinaryExpression *expression);
BoundExpression *BindUnaryOperator(const UnaryExpression *expression);
BoundExpression *BindFunctionCall(const FunctionCallExpression *expression);
BoundExpression *BindIndexExpression(const IndexExpression *expression, bool setter);
BoundExpression *BindNumericalTableExpression(const ParsedNumericalTableExpression *expression);
BoundExpression *BindTableExpression(const ParsedTableExpression *expression);
public:
static BoundScriptStatement* Bind(Script* script, const ParsedScriptStatement* s, BoundScope* scriptScope);
static BoundScriptStatement *
Bind(Porygon::Script *script, const ParsedScriptStatement *s, BoundScope *scriptScope);
BoundExpression *BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter);
};
}
#endif //PORYGONLANG_BINDER_HPP

View File

@ -10,6 +10,7 @@
using namespace std;
namespace Porygon::Binder {
enum class BoundExpressionKind {
Bad,
@ -36,13 +37,13 @@ public:
BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type)
: _start(start),
_length(length),
_type(std::move(type))
{
_type(std::move(type)) {
}
virtual ~BoundExpression() = default;
virtual const BoundExpressionKind GetKind() const = 0;
virtual const std::shared_ptr<ScriptType> &GetType() const {
return _type;
};
@ -50,9 +51,11 @@ public:
const unsigned int GetStartPosition() const {
return _start;
}
const unsigned int GetLength() const {
return _length;
}
const unsigned int GetEndPosition() const {
return _start + _length - 1;
}
@ -60,7 +63,9 @@ public:
class BoundBadExpression : public BoundExpression {
public:
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Error)){}
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length,
make_shared<ScriptType>(
TypeClass::Error)) {}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Bad;
@ -72,8 +77,7 @@ class BoundLiteralIntegerExpression : public BoundExpression{
public:
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)),
_value(value)
{
_value(value) {
}
const BoundExpressionKind GetKind() const final {
@ -90,8 +94,7 @@ class BoundLiteralFloatExpression : public BoundExpression{
public:
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)),
_value(value)
{
_value(value) {
}
const BoundExpressionKind GetKind() const final {
@ -107,9 +110,9 @@ class BoundLiteralStringExpression : public BoundExpression{
const u16string _value;
public:
BoundLiteralStringExpression(const u16string &value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<StringScriptType>(true, HashedString::ConstHash(value.c_str()))),
_value(value)
{
: BoundExpression(start, length,
make_shared<StringScriptType>(true, Utilities::HashedString::ConstHash(value.c_str()))),
_value(value) {
}
const BoundExpressionKind GetKind() const final {
@ -126,8 +129,7 @@ class BoundLiteralBoolExpression : public BoundExpression{
public:
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)),
_value(value)
{
_value(value) {
}
const BoundExpressionKind GetKind() const final {
@ -142,10 +144,10 @@ public:
class BoundVariableExpression : public BoundExpression {
const BoundVariableKey *_key;
public:
BoundVariableExpression(BoundVariableKey* key, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
BoundVariableExpression(BoundVariableKey *key, shared_ptr<ScriptType> type, unsigned int start,
unsigned int length)
: BoundExpression(start, length, std::move(type)),
_key(key)
{
_key(key) {
}
~BoundVariableExpression() override {
@ -166,13 +168,13 @@ class BoundBinaryExpression : public BoundExpression {
const BoundExpression *_right;
const BoundBinaryOperation _operation;
public:
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr<ScriptType> result,
BoundBinaryExpression(BoundExpression *left, BoundExpression *right, BoundBinaryOperation op,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_left(left),
_right(right),
_operation(op)
{
_operation(op) {
}
~BoundBinaryExpression() final {
@ -201,11 +203,11 @@ class BoundUnaryExpression : public BoundExpression {
const BoundExpression *_operand;
const BoundUnaryOperation _operation;
public:
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length)
BoundUnaryExpression(BoundExpression *operand, BoundUnaryOperation op, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_operand(operand),
_operation(op)
{
_operation(op) {
}
~BoundUnaryExpression() final {
@ -229,9 +231,11 @@ class BoundFunctionCallExpression : public BoundExpression {
const BoundExpression *_functionExpression;
const vector<BoundExpression *> _parameters;
public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result,
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _functionExpression(functionExpression), _parameters(std::move(parameters)) {}
: BoundExpression(start, length, std::move(result)), _functionExpression(functionExpression),
_parameters(std::move(parameters)) {}
~BoundFunctionCallExpression() final {
delete _functionExpression;
@ -257,9 +261,11 @@ class BoundIndexExpression : public BoundExpression {
const BoundExpression *_indexableExpression;
const BoundExpression *_indexExpression;
public:
BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result,
BoundIndexExpression(BoundExpression *indexableExpression, BoundExpression *indexExpression,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {}
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_indexExpression(indexExpression) {}
~BoundIndexExpression() final {
delete _indexableExpression;
@ -281,11 +287,13 @@ public:
class BoundPeriodIndexExpression : public BoundExpression {
const BoundExpression *_indexableExpression;
const HashedString _index;
const Utilities::HashedString _index;
public:
BoundPeriodIndexExpression(BoundExpression* indexableExpression, HashedString index, shared_ptr<ScriptType> result,
BoundPeriodIndexExpression(BoundExpression *indexableExpression, Utilities::HashedString index,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _index(index) {}
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_index(index) {}
~BoundPeriodIndexExpression() final {
delete _indexableExpression;
@ -299,7 +307,7 @@ public:
return _indexableExpression;
}
const HashedString GetIndex() const{
const Utilities::HashedString GetIndex() const {
return _index;
}
};
@ -307,10 +315,10 @@ public:
class BoundNumericalTableExpression : public BoundExpression {
const vector<const BoundExpression *> _expressions;
public:
BoundNumericalTableExpression(vector<const BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
BoundNumericalTableExpression(vector<const BoundExpression *> expressions, shared_ptr<ScriptType> type,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)),
_expressions(std::move(expressions))
{}
_expressions(std::move(expressions)) {}
~BoundNumericalTableExpression() final {
for (auto e: _expressions) {
@ -326,6 +334,6 @@ public:
return &_expressions;
}
};
}
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP

View File

@ -6,10 +6,12 @@
#include "../BoundStatements/BoundStatement.hpp"
namespace Porygon::Binder {
class BoundTableExpression : public BoundExpression {
const BoundBlockStatement *_block;
public:
BoundTableExpression(BoundBlockStatement* block, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
BoundTableExpression(BoundBlockStatement *block, shared_ptr<ScriptType> type, unsigned int start,
unsigned int length)
: BoundExpression(start, length, std::move(type)) {
_block = block;
}
@ -26,6 +28,7 @@ public:
return _block;
}
};
}
#include "BoundExpression.hpp"

View File

@ -2,6 +2,7 @@
#ifndef PORYGONLANG_BOUNDOPERATORS_HPP
#define PORYGONLANG_BOUNDOPERATORS_HPP
namespace Porygon::Binder {
enum class BoundBinaryOperation {
Addition,
Subtraction,
@ -23,5 +24,6 @@ enum class BoundUnaryOperation{
Negation,
LogicalNegation,
};
}
#endif //PORYGONLANG_BOUNDOPERATORS_HPP

View File

@ -7,14 +7,15 @@
#include <memory>
#include "BoundStatement.hpp"
namespace Porygon::Binder {
class BoundFunctionDeclarationStatement : public BoundStatement {
const BoundVariableKey *_key;
const std::shared_ptr<BoundBlockStatement> _block;
const std::shared_ptr<FunctionScriptType> _type;
public:
BoundFunctionDeclarationStatement(std::shared_ptr<FunctionScriptType> type, BoundVariableKey* key, BoundBlockStatement* block)
:_key(key), _block(block), _type(std::move(type))
{
BoundFunctionDeclarationStatement(std::shared_ptr<FunctionScriptType> type, BoundVariableKey *key,
BoundBlockStatement *block)
: _key(key), _block(block), _type(std::move(type)) {
}
~BoundFunctionDeclarationStatement() final {
@ -37,6 +38,7 @@ public:
return _type;
}
};
}
#include "BoundStatement.hpp"

View File

@ -12,6 +12,7 @@
using namespace std;
namespace Porygon::Binder {
enum class BoundStatementKind {
Bad,
Script,
@ -27,6 +28,7 @@ enum class BoundStatementKind{
class BoundStatement {
public:
virtual const BoundStatementKind GetKind() const = 0;
virtual ~BoundStatement() = default;
};
@ -41,9 +43,9 @@ class BoundBlockStatement : public BoundStatement{
const vector<BoundStatement *> _statements;
public:
explicit BoundBlockStatement(vector<BoundStatement *> statements)
: _statements(std::move(statements))
{
: _statements(std::move(statements)) {
}
~BoundBlockStatement() override {
for (auto s : _statements) {
delete s;
@ -64,8 +66,7 @@ class BoundScriptStatement : public BoundBlockStatement{
public:
explicit BoundScriptStatement(vector<BoundStatement *> statements, int localVariableCount)
: BoundBlockStatement(std::move(statements)),
_localVariableCount(localVariableCount)
{
_localVariableCount(localVariableCount) {
}
const BoundStatementKind GetKind() const final {
@ -81,10 +82,10 @@ class BoundExpressionStatement : public BoundStatement{
const BoundExpression *_expression;
public:
explicit BoundExpressionStatement(BoundExpression *expression)
: _expression(expression)
{
: _expression(expression) {
_expression = expression;
}
~BoundExpressionStatement() final {
delete _expression;
}
@ -103,8 +104,7 @@ class BoundAssignmentStatement : public BoundStatement{
const BoundExpression *_expression;
public:
BoundAssignmentStatement(BoundVariableKey *key, BoundExpression *expression)
: _key(key), _expression(expression)
{
: _key(key), _expression(expression) {
}
~BoundAssignmentStatement() final {
@ -130,8 +130,7 @@ class BoundIndexAssignmentStatement : public BoundStatement{
const BoundExpression *_valueExpression;
public:
BoundIndexAssignmentStatement(const BoundExpression *index, BoundExpression *value)
: _indexExpression(index), _valueExpression(value)
{
: _indexExpression(index), _valueExpression(value) {
}
~BoundIndexAssignmentStatement() final {
@ -156,9 +155,9 @@ class BoundReturnStatement : public BoundStatement{
const BoundExpression *_expression;
public:
explicit BoundReturnStatement(BoundExpression *expression)
: _expression(expression)
{
: _expression(expression) {
}
~BoundReturnStatement() final {
delete _expression;
}
@ -178,8 +177,7 @@ class BoundConditionalStatement : public BoundStatement{
const BoundStatement *_elseStatement;
public:
explicit BoundConditionalStatement(BoundExpression *condition, BoundStatement *block, BoundStatement *next)
:_condition(condition), _block(block), _elseStatement(next)
{
: _condition(condition), _block(block), _elseStatement(next) {
}
~BoundConditionalStatement() final {
@ -204,6 +202,7 @@ public:
return _elseStatement;
}
};
}
#include "BoundFunctionDeclarationStatement.hpp"

View File

@ -3,6 +3,7 @@
#include "BoundScope.hpp"
namespace Porygon::Binder {
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
@ -76,7 +77,8 @@ VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::sha
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
}
scope->insert({identifier, new BoundVariable(std::move(type))});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
return VariableAssignment(VariableAssignmentResult::Ok,
new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type) {
@ -94,6 +96,7 @@ VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::sh
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false));
}
}
}

View File

@ -13,6 +13,7 @@
using namespace std;
namespace Porygon::Binder {
class BoundScope {
unordered_map<uint32_t, BoundVariable *> *_tableScope;
vector<unordered_map<uint32_t, BoundVariable *> *> _localScope;
@ -20,14 +21,19 @@ class BoundScope {
int _lastCreatedScope;
public:
explicit BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope);
~BoundScope();
void GoInnerScope();
void GoOuterScope();
int Exists(int key);
BoundVariable *GetVariable(uint32_t scope, uint32_t identifier);
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type);
size_t GetLocalVariableCount() {
@ -38,6 +44,7 @@ public:
return _currentScope;
}
};
}
#endif //PORYGONLANG_BOUNDSCOPE_HPP

View File

@ -9,6 +9,7 @@
using namespace std;
namespace Porygon::Binder {
class BoundVariable {
std::shared_ptr<ScriptType> _type;
public:
@ -19,5 +20,6 @@ public:
return _type;
}
};
}
#endif //PORYGONLANG_BOUNDVARIABLE_HPP

View File

@ -4,25 +4,25 @@
#include <string>
namespace Porygon::Binder {
class BoundVariableKey {
const int _identifier;
const unsigned int _scopeId;
const bool _isCreation;
const uint64_t _hash;
static uint64_t KnuthsHash(unsigned int i1, unsigned int i2)
{
static uint64_t KnuthsHash(unsigned int i1, unsigned int i2) {
uint64_t ret = i1;
ret *= 2654435761U;
return ret ^ i2;
}
public:
BoundVariableKey(int id, unsigned int scope, bool creation)
: _identifier(id),
_scopeId(scope),
_isCreation(creation),
_hash(KnuthsHash(id, scope))
{}
_hash(KnuthsHash(id, scope)) {}
const int GetIdentifier() const {
@ -41,5 +41,6 @@ public:
return _hash;
}
};
}
#endif //PORYGONLANG_BOUNDVARIABLEKEY_HPP

View File

@ -4,6 +4,7 @@
#include "BoundVariableKey.hpp"
namespace Porygon::Binder {
enum class VariableAssignmentResult {
Ok,
ExplicitLocalVariableExists,
@ -27,5 +28,6 @@ public:
return _key;
}
};
}
#endif //PORYGONLANG_VARIABLEASSIGMENTRESULT_HPP

View File

@ -5,6 +5,9 @@
#include "EvalValues/NumericEvalValue.hpp"
#include "EvalValues/StringEvalValue.hpp"
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
@ -25,54 +28,46 @@ const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundB
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundBinaryOperation::Equality:
{
case BoundBinaryOperation::Equality: {
auto leftValue = this->EvaluateExpression(expression->GetLeft());
auto rightValue = this->EvaluateExpression(expression->GetRight());
bool equals = leftValue.get()->operator==(rightValue.get());
return make_shared<BooleanEvalValue>(equals);
}
case BoundBinaryOperation::Inequality:
{
case BoundBinaryOperation::Inequality: {
auto leftValue = this->EvaluateExpression(expression->GetLeft());
auto rightValue = this->EvaluateExpression(expression->GetRight());
bool equals = leftValue.get()->operator!=(rightValue.get());
return make_shared<BooleanEvalValue>(equals);
}
case BoundBinaryOperation ::LessThan:
{
case BoundBinaryOperation::LessThan: {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
return leftValue->operator<(rightValue);
}
case BoundBinaryOperation ::LessThanEquals:
{
case BoundBinaryOperation::LessThanEquals: {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
return leftValue->operator<=(rightValue);
}
case BoundBinaryOperation ::GreaterThan:
{
case BoundBinaryOperation::GreaterThan: {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
return leftValue->operator>(rightValue);
}
case BoundBinaryOperation ::GreaterThanEquals:
{
case BoundBinaryOperation::GreaterThanEquals: {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
return leftValue->operator>=(rightValue);
}
case BoundBinaryOperation::LogicalAnd:
{
case BoundBinaryOperation::LogicalAnd: {
auto leftValue = this->EvaluateBoolExpression(expression->GetLeft());
if (!leftValue->EvaluateBool()) return leftValue;
auto rightValue = this->EvaluateBoolExpression(expression->GetRight());
return rightValue;
}
case BoundBinaryOperation::LogicalOr:
{
case BoundBinaryOperation::LogicalOr: {
auto leftValue = this->EvaluateBoolExpression(expression->GetLeft());
if (leftValue->EvaluateBool()) return leftValue;
auto rightValue = this->EvaluateBoolExpression(expression->GetRight());
@ -93,3 +88,4 @@ const shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBin
stringStream << *right->EvaluateString();
return make_shared<StringEvalValue>(stringStream.str());
}
}

View File

@ -3,8 +3,10 @@
#include "StringEvalValue.hpp"
#include <cstring>
namespace Porygon::Evaluation {
extern "C" {
TypeClass GetEvalValueTypeClass(EvalValue* v){
Porygon::TypeClass GetEvalValueTypeClass(EvalValue *v) {
return v->GetTypeClass();
}
@ -40,6 +42,7 @@ extern "C" {
return new StringEvalValue(s);
}
}
}
#ifdef TESTS_BUILD
#include <catch.hpp>
@ -47,7 +50,7 @@ extern "C" {
TEST_CASE( "Evaluate String", "[integration]" ) {
auto script = Script::Create(u"\"foo bar\"");
auto script = Porygon::Script::Create(u"\"foo bar\"");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto lastValue = script->GetLastValue();

View File

@ -8,10 +8,13 @@
#include <sstream>
#include <memory>
namespace Porygon::Evaluation {
class EvalValue {
public:
EvalValue() = default;
virtual ~EvalValue() = default;
virtual const TypeClass GetTypeClass() const = 0;
virtual const bool operator==(EvalValue *b) const = 0;
@ -25,12 +28,15 @@ public:
virtual const long EvaluateInteger() const {
throw EvaluationException("Can't evaluate this EvalValue as integer.");
}
virtual const double EvaluateFloat() const {
throw EvaluationException("Can't evaluate this EvalValue as float.");
}
virtual const bool EvaluateBool() const {
throw EvaluationException("Can't evaluate this EvalValue as bool.");
}
virtual const std::u16string *EvaluateString() const {
throw EvaluationException("Can't evaluate this EvalValue as string.");
}
@ -54,8 +60,7 @@ class BooleanEvalValue : public EvalValue{
const bool _value;
public:
explicit BooleanEvalValue(bool val)
: _value(val)
{
: _value(val) {
}
const shared_ptr<EvalValue> Clone() const final {
@ -80,5 +85,6 @@ public:
return _value;
}
};
}
#endif //PORYGONLANG_EVALVALUE_HPP

View File

@ -1,6 +1,7 @@
#include "NumericEvalValue.hpp"
namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<NumericEvalValue> &b) const {
if (this->IsFloat()) {
if (b->IsFloat()) {
@ -142,3 +143,4 @@ const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr
}
}
}
}

View File

@ -4,10 +4,11 @@
#include <sstream>
#include "EvalValue.hpp"
namespace Porygon::Evaluation {
class NumericEvalValue : public EvalValue {
virtual const long GetIntegerValue() const = 0;
virtual const double GetFloatValue() const = 0;
public:
@ -18,23 +19,37 @@ public:
}
const shared_ptr<NumericEvalValue> operator+(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator-(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator*(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator/(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator<(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator<=(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator>(const shared_ptr<NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator>=(const shared_ptr<NumericEvalValue> &b) const;
const bool operator==(EvalValue *b) const final;
};
class IntegerEvalValue : public NumericEvalValue {
const long _value;
const long GetIntegerValue() const final { return _value; }
const double GetFloatValue() const final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
const double GetFloatValue() const final {
throw EvaluationException("Attempting to retrieve float from int eval value.");
}
public:
explicit IntegerEvalValue(long value) : _value(value) {
}
const bool IsFloat() const final {
return false;
}
@ -54,11 +69,17 @@ public:
class FloatEvalValue : public NumericEvalValue {
const double _value;
const long GetIntegerValue() const final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
const long GetIntegerValue() const final {
throw EvaluationException("Attempting to retrieve float from int eval value.");
}
const double GetFloatValue() const final { return _value; }
public:
explicit FloatEvalValue(double value) : _value(value) {
}
const bool IsFloat() const final {
return true;
}
@ -75,5 +96,6 @@ public:
return std::hash<double>{}(_value);
}
};
}
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP

View File

@ -11,29 +11,30 @@
#include "../Evaluator.hpp"
#include "../EvaluationScope/EvaluationScope.hpp"
namespace Porygon::Evaluation {
class ScriptFunctionEvalValue : public EvalValue {
const std::shared_ptr<BoundBlockStatement> _innerBlock;
const std::shared_ptr<FunctionScriptType> _type;
const std::shared_ptr<EvaluationScope> _scope;
const std::size_t _hash;
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock,
std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type, size_t hash)
: _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(hash)
{
_hash(hash) {
}
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<EvaluationScope> scope,
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock,
std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type)
: _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(rand())
{
_hash(rand()) {
}
const std::shared_ptr<ScriptType> GetType() const {
@ -68,6 +69,7 @@ public:
return _scope;
}
};
}
#endif //PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP

View File

@ -8,13 +8,14 @@
using namespace std;
namespace Porygon::Evaluation {
class StringEvalValue : public EvalValue {
u16string _value;
size_t _hash;
public:
explicit StringEvalValue(u16string s) {
_value = move(s);
_hash = HashedString::ConstHash (_value.c_str());
_hash = Utilities::HashedString::ConstHash(_value.c_str());
}
const TypeClass GetTypeClass() const final {
@ -45,6 +46,6 @@ public:
return _hash;
}
};
}
#endif //PORYGONLANG_STRINGEVALVALUE_HPP

View File

@ -6,6 +6,7 @@
using namespace std;
namespace Porygon::Evaluation {
class TableEvalValue : public EvalValue {
shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> _table;
size_t _hash;
@ -14,6 +15,7 @@ class TableEvalValue : public EvalValue {
_table = std::move(table);
_hash = hash;
}
public:
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table) {
_table = std::move(table);
@ -46,7 +48,7 @@ public:
}
const shared_ptr<EvalValue> IndexValue(const char *val) const {
auto hash = HashedString::ConstHash(val);
auto hash = Utilities::HashedString::ConstHash(val);
return this->_table->at(hash);
}
@ -55,6 +57,6 @@ public:
this->_table->at(hash) = value;
}
};
}
#endif //PORYGONLANG_TABLEEVALVALUE_HPP

View File

@ -7,6 +7,7 @@
#include <string>
using namespace std;
namespace Porygon::Evaluation {
class EvaluationException : public std::exception {
string _message;
public:
@ -15,10 +16,11 @@ public:
}
const string defaultErrorText = "An evaluation exception occurred: ";
const char *what() const noexcept final {
return (defaultErrorText + _message).c_str();
}
};
}
#endif //PORYGONLANG_EVALUATIONEXCEPTION_HPP

View File

@ -2,7 +2,9 @@
#include "EvaluationScope.hpp"
#include <memory>
EvaluationScope::EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables,
int localVariableCount) {
_scriptScope = scriptVariables;
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
}
@ -33,3 +35,4 @@ shared_ptr<EvalValue> EvaluationScope::GetVariable(const BoundVariableKey* key)
return _localScope[key->GetHash()];
}
}
}

View File

@ -5,18 +5,24 @@
#include <unordered_map>
#include <vector>
#include "../EvalValues/EvalValue.hpp"
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
class EvaluationScope {
unordered_map<uint32_t, shared_ptr<EvalValue>> *_scriptScope;
unordered_map<uint64_t, shared_ptr<EvalValue>> _localScope;
public:
explicit EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables, int deepestScope);
~EvaluationScope() = default;
void CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value);
void SetVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value);
shared_ptr<EvalValue> GetVariable(const BoundVariableKey *key);
};
}
#endif //PORYGONLANG_EVALUATIONSCOPE_HPP

View File

@ -11,9 +11,12 @@
#include "../TableScriptType.hpp"
using namespace std;
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount());
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables,
statement->GetLocalVariableCount());
EvaluateBlockStatement(statement);
return this->_returnValue.get();
}
@ -22,15 +25,22 @@ void Evaluator::EvaluateStatement(const BoundStatement *statement) {
if (this->_hasReturned)
return;
switch (statement->GetKind()) {
case BoundStatementKind ::Script: throw; // Should never happen
case BoundStatementKind ::Block: return this->EvaluateBlockStatement((BoundBlockStatement *) statement);
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
case BoundStatementKind::Script:
throw; // Should never happen
case BoundStatementKind::Block:
return this->EvaluateBlockStatement((BoundBlockStatement *) statement);
case BoundStatementKind::Expression:
return this->EvaluateExpressionStatement((BoundExpressionStatement *) statement);
case BoundStatementKind::Assignment:
return this->EvaluateAssignmentStatement((BoundAssignmentStatement *) statement);
case BoundStatementKind::IndexAssignment:
return this->EvaluateIndexAssignmentStatement((BoundIndexAssignmentStatement *) statement);
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
case BoundStatementKind::Return: return this -> EvaluateReturnStatement((BoundReturnStatement*)statement);
case BoundStatementKind::Conditional: return this -> EvaluateConditionalStatement((BoundConditionalStatement*)statement);
case BoundStatementKind::FunctionDeclaration:
return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement *) statement);
case BoundStatementKind::Return:
return this->EvaluateReturnStatement((BoundReturnStatement *) statement);
case BoundStatementKind::Conditional:
return this->EvaluateConditionalStatement((BoundConditionalStatement *) statement);
case BoundStatementKind::Bad:
throw;
@ -107,14 +117,22 @@ void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *st
const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType();
switch (type->GetClass()) {
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
case TypeClass ::Table: return this-> EvaluateTableExpression(expression);
case TypeClass ::UserData: return this -> EvaluateUserDataExpression(expression);
default: throw;
case TypeClass::Number:
return this->EvaluateIntegerExpression(expression);
case TypeClass::Bool:
return this->EvaluateBoolExpression(expression);
case TypeClass::String:
return this->EvaluateStringExpression(expression);
case TypeClass::Function:
return this->EvaluateFunctionExpression(expression);
case TypeClass::Nil:
return this->EvaluateNilExpression(expression);
case TypeClass::Table:
return this->EvaluateTableExpression(expression);
case TypeClass::UserData:
return this->EvaluateUserDataExpression(expression);
default:
throw;
}
}
@ -128,14 +146,23 @@ const shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)expression)->GetValue());
case BoundExpressionKind ::LiteralFloat: return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression*)expression)->GetValue());
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
case BoundExpressionKind::Variable: return dynamic_pointer_cast<NumericEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind ::Index: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind ::PeriodIndex: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::LiteralInteger:
return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue());
case BoundExpressionKind::LiteralFloat:
return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression *) expression)->GetValue());
case BoundExpressionKind::Unary:
return this->EvaluateIntegerUnary((BoundUnaryExpression *) expression);
case BoundExpressionKind::Binary:
return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<NumericEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::LiteralString:
case BoundExpressionKind::LiteralBool:
@ -148,13 +175,21 @@ const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const Bo
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue());
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
case BoundExpressionKind::Variable: return dynamic_pointer_cast<BooleanEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind ::Index: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind ::PeriodIndex: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::LiteralBool:
return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression *) expression)->GetValue());
case BoundExpressionKind::Unary:
return this->EvaluateBooleanUnary((BoundUnaryExpression *) expression);
case BoundExpressionKind::Binary:
return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<BooleanEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::Bad:
case BoundExpressionKind::LiteralInteger:
@ -173,10 +208,14 @@ const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const Boun
return make_shared<StringEvalValue>(((BoundLiteralStringExpression *) expression)->GetValue());
case BoundExpressionKind::Binary:
return this->EvaluateStringBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable: return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind ::Index: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind ::PeriodIndex: return dynamic_pointer_cast<StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
case BoundExpressionKind::Bad:
case BoundExpressionKind::LiteralInteger:
@ -192,12 +231,17 @@ const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const Boun
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
case BoundExpressionKind ::PeriodIndex: return this->EvaluatePeriodIndexExpression(expression);
default: throw;
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
case BoundExpressionKind::PeriodIndex:
return this->EvaluatePeriodIndexExpression(expression);
default:
throw;
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
@ -206,26 +250,31 @@ const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpressi
return nullptr;
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
case BoundExpressionKind ::NumericalTable: return this-> EvaluateNumericTableExpression(expression);
case BoundExpressionKind ::Table: return this -> EvaluateComplexTableExpression(expression);
case BoundExpressionKind ::PeriodIndex: return this->EvaluatePeriodIndexExpression(expression);
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
case BoundExpressionKind::NumericalTable:
return this->EvaluateNumericTableExpression(expression);
case BoundExpressionKind::Table:
return this->EvaluateComplexTableExpression(expression);
case BoundExpressionKind::PeriodIndex:
return this->EvaluatePeriodIndexExpression(expression);
default:
throw;
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
auto functionCall = (BoundFunctionCallExpression *) expression;
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(
this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto boundParameters = functionCall->GetParameters();
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters->size());
@ -319,9 +368,12 @@ const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const Boun
const shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this -> EvaluateIndexExpression(expression);
default: throw;
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
default:
throw;
}
}
}

View File

@ -4,7 +4,6 @@
#include <string>
#include "../Binder/BoundStatements/BoundStatement.hpp"
#include "../Script.hpp"
#include "EvalValues/EvalValue.hpp"
#include "EvalValues/NumericEvalValue.hpp"
#include "EvalValues/StringEvalValue.hpp"
@ -14,12 +13,14 @@
using namespace std;
namespace Porygon::Evaluation{
class Evaluator {
shared_ptr<EvalValue> _returnValue;
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptVariables;
bool _hasReturned;
shared_ptr<EvalValue> _lastValue;
Script* _scriptData;
//Porygon::Script* _scriptData;
shared_ptr<EvaluationScope> _evaluationScope;
void EvaluateStatement(const BoundStatement* statement);
@ -54,8 +55,8 @@ class Evaluator {
const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
public:
explicit Evaluator(Script* script){
_scriptData = script;
explicit Evaluator(unordered_map<uint32_t, shared_ptr<EvalValue>>* scriptVariables){
_scriptVariables = scriptVariables;
_hasReturned = false;
_returnValue = nullptr;
_evaluationScope = nullptr;
@ -70,6 +71,7 @@ public:
}
};
}

View File

@ -4,10 +4,10 @@
#include "EvaluationException.hpp"
#include "../Script.hpp"
namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundUnaryOperation::Negation:
{
case BoundUnaryOperation::Negation: {
auto operandValue = EvaluateIntegerExpression(expression->GetOperand());
if (operandValue->IsFloat()) {
double f = operandValue->EvaluateFloat();
@ -24,8 +24,7 @@ const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUn
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) {
case BoundUnaryOperation::LogicalNegation:
{
case BoundUnaryOperation::LogicalNegation: {
auto val = EvaluateBoolExpression(expression->GetOperand());
bool b = val->EvaluateBool();
return make_shared<BooleanEvalValue>(!b);
@ -34,3 +33,4 @@ const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUn
throw;
}
}
}

View File

@ -1,6 +1,8 @@
#ifndef PORYGONLANG_BINARYOPERATORKIND_HPP
#define PORYGONLANG_BINARYOPERATORKIND_HPP
namespace Porygon::Parser {
enum class BinaryOperatorKind {
// Math
Addition,
@ -20,4 +22,5 @@ enum class BinaryOperatorKind{
LogicalAnd,
LogicalOr,
};
}
#endif //PORYGONLANG_BINARYOPERATORKIND_HPP

View File

@ -5,9 +5,9 @@
#include "Lexer.hpp"
Lexer::Lexer(const u16string& scriptString, class Script* script)
: _scriptString(scriptString)
{
namespace Porygon::Parser {
Lexer::Lexer(const u16string &scriptString, Porygon::Script *script)
: _scriptString(scriptString) {
this->_scriptSize = scriptString.size();
this->ScriptData = script;
this->_position = 0;
@ -45,7 +45,12 @@ IToken* Lexer::LexNext(char16_t c){
switch (c) {
case '\0':
return new SimpleToken(TokenKind::EndOfFile, this->_position - 1, 1);
case ' ': case '\t': case '\n': case '\r': case '\v': case '\f':
case ' ':
case '\t':
case '\n':
case '\r':
case '\v':
case '\f':
return new SimpleToken(TokenKind::WhiteSpace, this->_position - 1, 1);
case '+':
return new SimpleToken(TokenKind::PlusToken, this->_position - 1, 1);
@ -96,7 +101,16 @@ IToken* Lexer::LexNext(char16_t c){
}
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1);
return new SimpleToken(TokenKind::BadToken, this->_position - 1, 1);
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return LexNumber(c);
case '"':
return LexString(c);
@ -115,17 +129,28 @@ IToken* Lexer::LexNext(char16_t c){
int CharToInt(char16_t c) {
switch (c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: return -1;
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
}
@ -157,15 +182,13 @@ IToken* Lexer::LexNumber(char16_t c){
is_searching = false;
continue;
}
}
else{
} else {
this->Next();
length++;
if (has_point) {
decimal_index++;
float_value += next_val / pow(10, decimal_index);
}
else {
} else {
int_value *= 10;
int_value += next_val;
}
@ -173,8 +196,7 @@ IToken* Lexer::LexNumber(char16_t c){
}
if (has_point) {
return new FloatToken(float_value, start, length);
}
else{
} else {
return new IntegerToken(int_value, start, length);
}
}
@ -188,34 +210,53 @@ IToken * Lexer::LexIdentifierOrKeyword() {
if (isalpha(next) || next == '_' || next > 255) {
this->Next();
end++;
}
else{
} else {
break;
}
}
u16string s = this->_scriptString.substr(start, end - start + 1);
switch (HashedString::ConstHash(s.c_str())) {
case HashedString::ConstHash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3);
case HashedString::ConstHash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5);
case HashedString::ConstHash("do"): return new SimpleToken(TokenKind::DoKeyword, start, 2);
case HashedString::ConstHash("else"): return new SimpleToken(TokenKind::ElseKeyword, start, 4);
case HashedString::ConstHash("elseif"): return new SimpleToken(TokenKind::ElseIfKeyword, start, 6);
case HashedString::ConstHash("end"): return new SimpleToken(TokenKind::EndKeyword, start, 3);
case HashedString::ConstHash("false"): return new SimpleToken(TokenKind::FalseKeyword, start, 5);
case HashedString::ConstHash("for"): return new SimpleToken(TokenKind::ForKeyword, start, 3);
case HashedString::ConstHash("function"): return new SimpleToken(TokenKind::FunctionKeyword, start, 8);
case HashedString::ConstHash("if"): return new SimpleToken(TokenKind::IfKeyword, start, 2);
case HashedString::ConstHash("in"): return new SimpleToken(TokenKind::InKeyword, start, 2);
case HashedString::ConstHash("local"): return new SimpleToken(TokenKind::LocalKeyword, start, 5);
case HashedString::ConstHash("nil"): return new SimpleToken(TokenKind::NilKeyword, start, 3);
case HashedString::ConstHash("not"): return new SimpleToken(TokenKind::NotKeyword, start, 3);
case HashedString::ConstHash("or"): return new SimpleToken(TokenKind::OrKeyword, start, 2);
case HashedString::ConstHash("return"): return new SimpleToken(TokenKind::ReturnKeyword, start, 6);
case HashedString::ConstHash("then"): return new SimpleToken(TokenKind::ThenKeyword, start, 4);
case HashedString::ConstHash("true"): return new SimpleToken(TokenKind::TrueKeyword, start, 4);
case HashedString::ConstHash("while"): return new SimpleToken(TokenKind::WhileKeyword, start, 5);
default: return new IdentifierToken(HashedString(s), start, s.length());
case HashedString::ConstHash("and"):
return new SimpleToken(TokenKind::AndKeyword, start, 3);
case HashedString::ConstHash("break"):
return new SimpleToken(TokenKind::BreakKeyword, start, 5);
case HashedString::ConstHash("do"):
return new SimpleToken(TokenKind::DoKeyword, start, 2);
case HashedString::ConstHash("else"):
return new SimpleToken(TokenKind::ElseKeyword, start, 4);
case HashedString::ConstHash("elseif"):
return new SimpleToken(TokenKind::ElseIfKeyword, start, 6);
case HashedString::ConstHash("end"):
return new SimpleToken(TokenKind::EndKeyword, start, 3);
case HashedString::ConstHash("false"):
return new SimpleToken(TokenKind::FalseKeyword, start, 5);
case HashedString::ConstHash("for"):
return new SimpleToken(TokenKind::ForKeyword, start, 3);
case HashedString::ConstHash("function"):
return new SimpleToken(TokenKind::FunctionKeyword, start, 8);
case HashedString::ConstHash("if"):
return new SimpleToken(TokenKind::IfKeyword, start, 2);
case HashedString::ConstHash("in"):
return new SimpleToken(TokenKind::InKeyword, start, 2);
case HashedString::ConstHash("local"):
return new SimpleToken(TokenKind::LocalKeyword, start, 5);
case HashedString::ConstHash("nil"):
return new SimpleToken(TokenKind::NilKeyword, start, 3);
case HashedString::ConstHash("not"):
return new SimpleToken(TokenKind::NotKeyword, start, 3);
case HashedString::ConstHash("or"):
return new SimpleToken(TokenKind::OrKeyword, start, 2);
case HashedString::ConstHash("return"):
return new SimpleToken(TokenKind::ReturnKeyword, start, 6);
case HashedString::ConstHash("then"):
return new SimpleToken(TokenKind::ThenKeyword, start, 4);
case HashedString::ConstHash("true"):
return new SimpleToken(TokenKind::TrueKeyword, start, 4);
case HashedString::ConstHash("while"):
return new SimpleToken(TokenKind::WhileKeyword, start, 5);
default:
return new IdentifierToken(HashedString(s), start, s.length());
}
}
@ -262,7 +303,8 @@ IToken* Lexer::LexString(char16_t c){
if (ControlCharacters.find(c) != ControlCharacters.end()) {
stream << ControlCharacters.at(c);
} else {
this -> ScriptData->Diagnostics->LogError(DiagnosticCode::InvalidStringControlCharacter, start + 1 + i, 1);
this->ScriptData->Diagnostics->LogError(DiagnosticCode::InvalidStringControlCharacter,
start + 1 + i, 1);
stream << c;
}
} else {
@ -272,3 +314,4 @@ IToken* Lexer::LexString(char16_t c){
return new StringToken(stream.str(), start, end - start);
}
}

View File

@ -7,6 +7,7 @@
using namespace std;
namespace Porygon::Parser{
class Lexer {
const u16string& _scriptString;
#ifdef TESTS_BUILD
@ -21,12 +22,13 @@ public:
IToken* LexIdentifierOrKeyword();
IToken* LexString(char16_t c);
public:
Script* ScriptData;
Porygon::Script* ScriptData;
vector<const IToken*> Lex();
explicit Lexer(const u16string& scriptString, class Script* script);
explicit Lexer(const u16string& scriptString, Porygon::Script* script);
};
}
#endif //PORYGONLANG_LEXER_HPP

View File

@ -11,6 +11,7 @@
#include "../BinaryOperatorKind.hpp"
#include "../../Utilities/HashedString.hpp"
namespace Porygon::Parser {
enum class ParsedExpressionKind {
Bad,
@ -36,8 +37,7 @@ class ParsedExpression {
public:
ParsedExpression(unsigned int position, unsigned int length)
: _position(position),
_length(length)
{
_length(length) {
}
virtual ~ParsedExpression() = default;
@ -72,10 +72,10 @@ public:
const ParsedExpressionKind GetKind() const final {
return ParsedExpressionKind::LiteralInteger;
}
explicit LiteralIntegerExpression(IntegerToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(token -> GetValue())
{
_value(token->GetValue()) {
}
const long GetValue() const {
@ -89,10 +89,10 @@ public:
const ParsedExpressionKind GetKind() const final {
return ParsedExpressionKind::LiteralFloat;
}
explicit LiteralFloatExpression(FloatToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(token -> GetValue())
{
_value(token->GetValue()) {
}
const double GetValue() const {
@ -106,10 +106,10 @@ public:
const ParsedExpressionKind GetKind() const final {
return ParsedExpressionKind::LiteralString;
}
explicit LiteralStringExpression(StringToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(std::move(token -> GetValue()))
{
_value(std::move(token->GetValue())) {
}
const u16string &GetValue() const {
@ -123,10 +123,10 @@ public:
const ParsedExpressionKind GetKind() const final {
return ParsedExpressionKind::LiteralBool;
}
explicit LiteralBoolExpression(const IToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(token -> GetKind() == TokenKind::TrueKeyword)
{
_value(token->GetKind() == TokenKind::TrueKeyword) {
}
const bool GetValue() const {
@ -140,9 +140,10 @@ public:
const ParsedExpressionKind GetKind() const final {
return ParsedExpressionKind::Variable;
}
explicit VariableExpression(IdentifierToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength())
, _value(HashedString(token -> GetValue()))
{
explicit VariableExpression(IdentifierToken *token) : ParsedExpression(token->GetStartPosition(),
token->GetLength()),
_value(HashedString(token->GetValue())) {
}
const HashedString GetValue() const {
@ -185,8 +186,7 @@ public:
UnaryExpression(UnaryOperatorKind kind, ParsedExpression *operand, unsigned int start, unsigned int length)
: ParsedExpression(start, length),
_kind(kind), _operand(operand)
{
_kind(kind), _operand(operand) {
}
const UnaryOperatorKind GetOperatorKind() const {
@ -215,8 +215,7 @@ public:
BinaryExpression(BinaryOperatorKind kind, ParsedExpression *left, ParsedExpression *right, unsigned int start,
unsigned int length)
: ParsedExpression(start, length),
_kind(kind), _left(left), _right(right)
{
_kind(kind), _left(left), _right(right) {
}
const BinaryOperatorKind GetOperatorKind() const {
@ -236,10 +235,10 @@ class FunctionCallExpression : public ParsedExpression{
const std::unique_ptr<ParsedExpression> _function;
const vector<const ParsedExpression *> _parameters;
public:
FunctionCallExpression(ParsedExpression* function, vector<const ParsedExpression*> parameters, unsigned int start, unsigned int length)
FunctionCallExpression(ParsedExpression *function, vector<const ParsedExpression *> parameters,
unsigned int start, unsigned int length)
: ParsedExpression(start, length),
_function(function), _parameters(std::move(parameters))
{
_function(function), _parameters(std::move(parameters)) {
}
~FunctionCallExpression() final {
@ -255,6 +254,7 @@ public:
const ParsedExpression *GetFunction() const {
return _function.get();
}
const vector<const ParsedExpression *> *GetParameters() const {
return &_parameters;
}
@ -266,8 +266,7 @@ class IndexExpression : public ParsedExpression{
public:
IndexExpression(ParsedExpression *indexer, ParsedExpression *index, unsigned int start, unsigned int length)
: ParsedExpression(start, length),
_indexerExpression(indexer), _indexExpression(index)
{
_indexerExpression(indexer), _indexExpression(index) {
}
~IndexExpression() final {
@ -294,8 +293,7 @@ class PeriodIndexExpression : public ParsedExpression{
public:
PeriodIndexExpression(ParsedExpression *indexer, HashedString index, unsigned int start, unsigned int length)
: ParsedExpression(start, length),
_indexerExpression(indexer), _index(index)
{
_indexerExpression(indexer), _index(index) {
}
~PeriodIndexExpression() final {
@ -319,7 +317,8 @@ public:
class ParsedNumericalTableExpression : public ParsedExpression {
vector<const ParsedExpression *> _expressions;
public:
ParsedNumericalTableExpression(vector<const ParsedExpression*> expressions, unsigned int start, unsigned int length)
ParsedNumericalTableExpression(vector<const ParsedExpression *> expressions, unsigned int start,
unsigned int length)
: ParsedExpression(start, length) {
_expressions = std::move(expressions);
}
@ -338,6 +337,6 @@ public:
return ParsedExpressionKind::NumericalTable;
}
};
}
#endif //PORYGONLANG_PARSEDEXPRESSION_HPP

View File

@ -5,12 +5,12 @@
#include "ParsedExpression.hpp"
#include "../ParsedStatements/ParsedStatement.hpp"
namespace Porygon::Parser {
class ParsedTableExpression : public ParsedExpression {
const ParsedBlockStatement *_block;
public:
ParsedTableExpression(ParsedBlockStatement *block, unsigned int start, unsigned int length)
: ParsedExpression(start, length), _block(block)
{
: ParsedExpression(start, length), _block(block) {
}
~ParsedTableExpression() final {
@ -25,7 +25,7 @@ public:
return ParsedExpressionKind::Table;
}
};
}
#include "ParsedExpression.hpp"
#endif //PORYGONLANG_PARSEDTABLEEXPRESSION_HPP

View File

@ -13,6 +13,7 @@
#include "../../Utilities/HashedString.hpp"
#include "../TypedVariableIdentifier.hpp"
namespace Porygon::Parser {
enum class ParsedStatementKind {
Bad,
Script,
@ -30,10 +31,11 @@ class ParsedStatement {
const unsigned int _length;
public:
ParsedStatement(unsigned int start, unsigned int length)
:_start(start), _length(length)
{
: _start(start), _length(length) {
}
virtual ~ParsedStatement() = default;
virtual const ParsedStatementKind GetKind() const = 0;
const unsigned int GetStartPosition() const {
@ -52,6 +54,7 @@ public:
class ParsedBadStatement : public ParsedStatement {
public:
ParsedBadStatement(unsigned int start, unsigned int length) : ParsedStatement(start, length) {};
const ParsedStatementKind GetKind() const final {
return ParsedStatementKind::Bad;
}
@ -61,13 +64,15 @@ class ParsedBlockStatement : public ParsedStatement{
const std::vector<const ParsedStatement *> _statements;
public:
explicit ParsedBlockStatement(std::vector<const ParsedStatement *> statements)
: ParsedStatement(statements.front()->GetStartPosition(), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()),
_statements(statements)
{}
: ParsedStatement(statements.front()->GetStartPosition(),
statements.back()->GetEndPosition() - statements.front()->GetStartPosition()),
_statements(statements) {}
ParsedBlockStatement(std::vector<const ParsedStatement*> statements, unsigned int start) : ParsedStatement(start, 0),
_statements(std::move(statements))
{
ParsedBlockStatement(std::vector<const ParsedStatement *> statements, unsigned int start) : ParsedStatement(
start, 0),
_statements(
std::move(
statements)) {
}
@ -88,7 +93,8 @@ public:
class ParsedScriptStatement : public ParsedBlockStatement {
public:
explicit ParsedScriptStatement(vector<const ParsedStatement*> statements) : ParsedBlockStatement(move(statements)){}
explicit ParsedScriptStatement(vector<const ParsedStatement *> statements) : ParsedBlockStatement(
move(statements)) {}
const ParsedStatementKind GetKind() const final {
return ParsedStatementKind::Script;
@ -100,8 +106,7 @@ class ParsedExpressionStatement : public ParsedStatement{
public:
explicit ParsedExpressionStatement(ParsedExpression *expression)
: ParsedStatement(expression->GetStartPosition(), expression->GetLength()),
_expression(expression)
{
_expression(expression) {
}
~ParsedExpressionStatement() override {
@ -122,9 +127,11 @@ class ParsedFunctionDeclarationStatement : public ParsedStatement{
const vector<TypedVariableIdentifier *> _parameters;
const ParsedBlockStatement *_block;
public:
ParsedFunctionDeclarationStatement(HashedString identifier, vector<TypedVariableIdentifier*> parameters, ParsedBlockStatement* block,
ParsedFunctionDeclarationStatement(HashedString identifier, vector<TypedVariableIdentifier *> parameters,
ParsedBlockStatement *block,
unsigned int start, unsigned int length)
: ParsedStatement(start, length), _identifier(identifier), _parameters(std::move(parameters)), _block(block){};
: ParsedStatement(start, length), _identifier(identifier), _parameters(std::move(parameters)),
_block(block) {};
~ParsedFunctionDeclarationStatement() override {
for (auto v : _parameters) {
@ -155,9 +162,9 @@ class ParsedAssignmentStatement : public ParsedStatement{
const HashedString _identifier;
const ParsedExpression *_expression;
public:
ParsedAssignmentStatement(bool local, const HashedString identifier, ParsedExpression* expression, unsigned int start, unsigned int length)
: ParsedStatement(start, length), _local(local), _identifier(identifier), _expression(expression)
{
ParsedAssignmentStatement(bool local, const HashedString identifier, ParsedExpression *expression,
unsigned int start, unsigned int length)
: ParsedStatement(start, length), _local(local), _identifier(identifier), _expression(expression) {
}
~ParsedAssignmentStatement() final {
@ -185,7 +192,8 @@ class ParsedIndexAssignmentStatement : public ParsedStatement{
const ParsedExpression *_indexExpression;
const ParsedExpression *_valueExpression;
public:
ParsedIndexAssignmentStatement(ParsedExpression* indexExpression, ParsedExpression* valueExpression, unsigned int start,
ParsedIndexAssignmentStatement(ParsedExpression *indexExpression, ParsedExpression *valueExpression,
unsigned int start,
unsigned int length)
: ParsedStatement(start, length),
_indexExpression(indexExpression), _valueExpression(valueExpression) {}
@ -213,8 +221,7 @@ class ParsedReturnStatement : public ParsedStatement{
public:
ParsedReturnStatement(ParsedExpression *expression, unsigned int start, unsigned int length)
: ParsedStatement(start, length),
_expression(expression)
{
_expression(expression) {
}
~ParsedReturnStatement() final {
@ -236,14 +243,14 @@ class ParsedConditionalStatement : public ParsedStatement{
// This can be either else if or else
const ParsedStatement *_elseStatement;
public:
ParsedConditionalStatement(ParsedExpression* condition, ParsedStatement* block, unsigned int start, unsigned int length)
: ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nullptr)
{
ParsedConditionalStatement(ParsedExpression *condition, ParsedStatement *block, unsigned int start,
unsigned int length)
: ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nullptr) {
}
ParsedConditionalStatement(ParsedExpression* condition, ParsedStatement* block, ParsedStatement* nextStatement, unsigned int start, unsigned int length)
: ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nextStatement)
{
ParsedConditionalStatement(ParsedExpression *condition, ParsedStatement *block, ParsedStatement *nextStatement,
unsigned int start, unsigned int length)
: ParsedStatement(start, length), _condition(condition), _block(block), _elseStatement(nextStatement) {
_condition = condition;
_block = block;
_elseStatement = nextStatement;
@ -271,5 +278,5 @@ public:
return _elseStatement;
}
};
}
#endif //PORYGONLANG_PARSEDSTATEMENT_HPP

View File

@ -7,7 +7,7 @@
#include "TypedVariableIdentifier.hpp"
#include "ParsedExpressions/ParsedTableExpression.hpp"
namespace Porygon::Parser {
ParsedScriptStatement *Parser::Parse() {
vector<const ParsedStatement *> statements;
while (this->_position < this->_tokens.size()) {
@ -36,11 +36,16 @@ const IToken *Parser::Next() {
ParsedStatement *Parser::ParseStatement(const IToken *current) {
auto currentKind = current->GetKind();
switch (currentKind) {
case TokenKind ::LocalKeyword: return this->ParseVariableAssignment(current);
case TokenKind ::FunctionKeyword: return this -> ParseFunctionDeclaration(current);
case TokenKind ::ReturnKeyword: return this->ParseReturnStatement(current);
case TokenKind ::IfKeyword: return this -> ParseIfStatement(current);
default: break;
case TokenKind::LocalKeyword:
return this->ParseVariableAssignment(current);
case TokenKind::FunctionKeyword:
return this->ParseFunctionDeclaration(current);
case TokenKind::ReturnKeyword:
return this->ParseReturnStatement(current);
case TokenKind::IfKeyword:
return this->ParseIfStatement(current);
default:
break;
}
if (this->Peek()->GetKind() == TokenKind::AssignmentToken) {
return ParseVariableAssignment(current);
@ -67,27 +72,32 @@ ParsedStatement *Parser::ParseVariableAssignment(const IToken *current) {
auto expression = this->ParseExpression(this->Next());
if (identifier->GetKind() != TokenKind::Identifier) {
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), identifier->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength());
return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength());
}
if (assignmentToken->GetKind() != TokenKind::AssignmentToken) {
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), identifier->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength());
return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength());
}
auto start = current->GetStartPosition();
return new ParsedAssignmentStatement(isLocal, ((IdentifierToken*)identifier) -> GetValue(), expression, start, expression->GetEndPosition() - start);
return new ParsedAssignmentStatement(isLocal, ((IdentifierToken *) identifier)->GetValue(), expression, start,
expression->GetEndPosition() - start);
}
ParsedStatement *Parser::ParseIndexAssignment(ParsedExpression *indexer) {
this->Next(); // Consume assignment token
auto valueExpression = this->ParseExpression(this->Next());
auto start = indexer->GetStartPosition();
return new ParsedIndexAssignmentStatement(indexer, valueExpression, start, valueExpression->GetEndPosition() - start);
return new ParsedIndexAssignmentStatement(indexer, valueExpression, start,
valueExpression->GetEndPosition() - start);
}
ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens, const vector<const ParsedStatement*>& openStatements) {
ParsedStatement *
Parser::ParseBlock(const vector<TokenKind> &endTokens, const vector<const ParsedStatement *> &openStatements) {
auto statements = openStatements;
auto start = this->_position;
while (this->_position < this->_tokens.size()) {
@ -97,7 +107,8 @@ ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens, const ve
break;
}
if (nextKind == TokenKind::EndOfFile) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), next->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength());
break;
}
statements.push_back(this->ParseStatement(next));
@ -114,11 +125,14 @@ ParsedStatement *Parser::ParseFunctionDeclaration(const IToken *current) {
vector<TypedVariableIdentifier *> parameters;
bool hasErrors = false;
if (functionIdentifierToken->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, functionIdentifierToken->GetStartPosition(), functionIdentifierToken->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken,
functionIdentifierToken->GetStartPosition(),
functionIdentifierToken->GetLength());
hasErrors = true;
}
if (openParenthesis->GetKind() != TokenKind::OpenParenthesis && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, openParenthesis->GetStartPosition(), openParenthesis->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken,
openParenthesis->GetStartPosition(), openParenthesis->GetLength());
hasErrors = true;
}
@ -130,18 +144,21 @@ ParsedStatement *Parser::ParseFunctionDeclaration(const IToken *current) {
auto identifier = this->Next();
auto next = this->Next();
if (type->GetKind() != TokenKind::Identifier && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(), type->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(),
type->GetLength());
hasErrors = true;
continue;
}
if (identifier->GetKind() != TokenKind::Identifier && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), identifier->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength());
hasErrors = true;
continue;
}
if (type->GetKind() != TokenKind::Identifier || identifier->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(), type->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(),
type->GetLength());
hasErrors = true;
continue;
}
@ -153,7 +170,8 @@ ParsedStatement *Parser::ParseFunctionDeclaration(const IToken *current) {
if (nextKind == TokenKind::CloseParenthesis || nextKind == TokenKind::EndOfFile) {
break;
} else if (nextKind != TokenKind::CommaToken && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), next->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength());
hasErrors = true;
}
}
@ -167,7 +185,9 @@ ParsedStatement *Parser::ParseFunctionDeclaration(const IToken *current) {
return new ParsedBadStatement(start, block->GetEndPosition() - start);
}
auto functionIdentifier = ((IdentifierToken *) functionIdentifierToken)->GetValue();
return new ParsedFunctionDeclarationStatement(HashedString(functionIdentifier), parameters, (ParsedBlockStatement*)block, start, block->GetEndPosition() - start);
return new ParsedFunctionDeclarationStatement(HashedString(functionIdentifier), parameters,
(ParsedBlockStatement *) block, start,
block->GetEndPosition() - start);
}
ParsedStatement *Parser::ParseReturnStatement(const IToken *current) {
@ -181,7 +201,8 @@ ParsedStatement* Parser::ParseIfStatement(const IToken* current){
auto condition = this->ParseExpression(this->Next());
auto next = this->Next();
if (next->GetKind() != TokenKind::ThenKeyword) {
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), next->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength());
return new ParsedBadStatement(next->GetStartPosition(), next->GetLength());
}
auto block = this->ParseBlock({TokenKind::EndKeyword, TokenKind::ElseKeyword, TokenKind::ElseIfKeyword});
@ -189,10 +210,12 @@ ParsedStatement* Parser::ParseIfStatement(const IToken* current){
auto start = current->GetStartPosition();
if (closeToken->GetKind() == TokenKind::ElseIfKeyword) {
auto elseIfStatement = this->ParseIfStatement(closeToken);
return new ParsedConditionalStatement(condition, block, elseIfStatement, start, elseIfStatement->GetEndPosition() - start);
return new ParsedConditionalStatement(condition, block, elseIfStatement, start,
elseIfStatement->GetEndPosition() - start);
} else if (closeToken->GetKind() == TokenKind::ElseKeyword) {
auto elseStatement = this->ParseBlock({TokenKind::EndKeyword});
return new ParsedConditionalStatement(condition, block, elseStatement, start, elseStatement->GetEndPosition() - start);
return new ParsedConditionalStatement(condition, block, elseStatement, start,
elseStatement->GetEndPosition() - start);
}
return new ParsedConditionalStatement(condition, block, start, block->GetEndPosition() - start);
}
@ -230,9 +253,12 @@ OperatorPrecedence GetUnaryPrecedence(TokenKind kind){
UnaryOperatorKind GetUnaryOperatorKind(TokenKind kind) {
switch (kind) {
case TokenKind::PlusToken: return UnaryOperatorKind::Identity;
case TokenKind::MinusToken: return UnaryOperatorKind::Negation;
case TokenKind::NotKeyword: return UnaryOperatorKind::LogicalNegation;
case TokenKind::PlusToken:
return UnaryOperatorKind::Identity;
case TokenKind::MinusToken:
return UnaryOperatorKind::Negation;
case TokenKind::NotKeyword:
return UnaryOperatorKind::LogicalNegation;
default: // This should never trigger, so throw.
throw;
}
@ -241,22 +267,34 @@ UnaryOperatorKind GetUnaryOperatorKind(TokenKind kind){
BinaryOperatorKind GetBinaryOperatorKind(TokenKind kind) {
switch (kind) {
// Math operators
case TokenKind::PlusToken: return BinaryOperatorKind ::Addition;
case TokenKind::MinusToken: return BinaryOperatorKind ::Subtraction;
case TokenKind::StarToken: return BinaryOperatorKind ::Multiplication;
case TokenKind::SlashToken: return BinaryOperatorKind ::Division;
case TokenKind::PlusToken:
return BinaryOperatorKind::Addition;
case TokenKind::MinusToken:
return BinaryOperatorKind::Subtraction;
case TokenKind::StarToken:
return BinaryOperatorKind::Multiplication;
case TokenKind::SlashToken:
return BinaryOperatorKind::Division;
// Equality operators
case TokenKind::EqualityToken: return BinaryOperatorKind ::Equality;
case TokenKind::InequalityToken: return BinaryOperatorKind ::Inequality;
case TokenKind ::Less: return BinaryOperatorKind ::Less;
case TokenKind ::LessEquals: return BinaryOperatorKind ::LessOrEquals;
case TokenKind ::Greater: return BinaryOperatorKind ::Greater;
case TokenKind ::GreaterEquals: return BinaryOperatorKind ::GreaterOrEquals;
case TokenKind::EqualityToken:
return BinaryOperatorKind::Equality;
case TokenKind::InequalityToken:
return BinaryOperatorKind::Inequality;
case TokenKind::Less:
return BinaryOperatorKind::Less;
case TokenKind::LessEquals:
return BinaryOperatorKind::LessOrEquals;
case TokenKind::Greater:
return BinaryOperatorKind::Greater;
case TokenKind::GreaterEquals:
return BinaryOperatorKind::GreaterOrEquals;
// logical operators
case TokenKind::AndKeyword: return BinaryOperatorKind ::LogicalAnd;
case TokenKind::OrKeyword: return BinaryOperatorKind ::LogicalOr;
case TokenKind::AndKeyword:
return BinaryOperatorKind::LogicalAnd;
case TokenKind::OrKeyword:
return BinaryOperatorKind::LogicalOr;
default: // This should never trigger, so throw.
throw;
}
@ -265,22 +303,34 @@ BinaryOperatorKind GetBinaryOperatorKind(TokenKind kind){
OperatorPrecedence GetBinaryPrecedence(TokenKind kind) {
switch (kind) {
// Math
case TokenKind::PlusToken: return OperatorPrecedence ::Additive;
case TokenKind::MinusToken: return OperatorPrecedence ::Additive;
case TokenKind::StarToken: return OperatorPrecedence ::Multiplication;
case TokenKind::SlashToken: return OperatorPrecedence ::Multiplication;
case TokenKind::PlusToken:
return OperatorPrecedence::Additive;
case TokenKind::MinusToken:
return OperatorPrecedence::Additive;
case TokenKind::StarToken:
return OperatorPrecedence::Multiplication;
case TokenKind::SlashToken:
return OperatorPrecedence::Multiplication;
// Equality
case TokenKind::EqualityToken: return OperatorPrecedence ::Equality;
case TokenKind::InequalityToken: return OperatorPrecedence ::Equality;
case TokenKind ::Less: return OperatorPrecedence ::Equality;
case TokenKind ::LessEquals: return OperatorPrecedence ::Equality;
case TokenKind ::Greater: return OperatorPrecedence ::Equality;
case TokenKind ::GreaterEquals: return OperatorPrecedence ::Equality;
case TokenKind::EqualityToken:
return OperatorPrecedence::Equality;
case TokenKind::InequalityToken:
return OperatorPrecedence::Equality;
case TokenKind::Less:
return OperatorPrecedence::Equality;
case TokenKind::LessEquals:
return OperatorPrecedence::Equality;
case TokenKind::Greater:
return OperatorPrecedence::Equality;
case TokenKind::GreaterEquals:
return OperatorPrecedence::Equality;
// Logical
case TokenKind::AndKeyword: return OperatorPrecedence ::LogicalAnd;
case TokenKind::OrKeyword: return OperatorPrecedence ::LogicalOr;
case TokenKind::AndKeyword:
return OperatorPrecedence::LogicalAnd;
case TokenKind::OrKeyword:
return OperatorPrecedence::LogicalOr;
default:
return OperatorPrecedence::No;
}
@ -315,18 +365,28 @@ ParsedExpression* Parser::ParseBinaryExpression(const IToken* current, OperatorP
ParsedExpression *Parser::ParsePrimaryExpression(const IToken *current) {
switch (current->GetKind()) {
case TokenKind ::Integer: return new LiteralIntegerExpression((IntegerToken*)current);
case TokenKind ::Float: return new LiteralFloatExpression((FloatToken*)current);
case TokenKind ::String: return new LiteralStringExpression((StringToken*)current);
case TokenKind ::TrueKeyword: return new LiteralBoolExpression(current);
case TokenKind ::FalseKeyword: return new LiteralBoolExpression(current);
case TokenKind ::Identifier: return new VariableExpression((IdentifierToken*)current);
case TokenKind ::OpenParenthesis: return this -> ParseParenthesizedExpression(current);
case TokenKind ::OpenCurlyBracket: return this -> ParseTableExpression(current);
case TokenKind::Integer:
return new LiteralIntegerExpression((IntegerToken *) current);
case TokenKind::Float:
return new LiteralFloatExpression((FloatToken *) current);
case TokenKind::String:
return new LiteralStringExpression((StringToken *) current);
case TokenKind::TrueKeyword:
return new LiteralBoolExpression(current);
case TokenKind::FalseKeyword:
return new LiteralBoolExpression(current);
case TokenKind::Identifier:
return new VariableExpression((IdentifierToken *) current);
case TokenKind::OpenParenthesis:
return this->ParseParenthesizedExpression(current);
case TokenKind::OpenCurlyBracket:
return this->ParseTableExpression(current);
// If we find a bad token here, we should have already logged it in the lexer, so don't log another error.
case TokenKind ::BadToken: return new BadExpression(current->GetStartPosition(), current->GetLength());
case TokenKind::BadToken:
return new BadExpression(current->GetStartPosition(), current->GetLength());
default:
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, current->GetStartPosition(), current->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, current->GetStartPosition(),
current->GetLength());
return new BadExpression(current->GetStartPosition(), current->GetLength());
}
}
@ -336,7 +396,8 @@ ParsedExpression *Parser::ParseParenthesizedExpression(const IToken *current) {
auto expression = this->ParseExpression(next);
auto closeToken = this->Next();
if (closeToken->GetKind() != TokenKind::CloseParenthesis) {
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, closeToken->GetStartPosition(), closeToken->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeToken->GetStartPosition(),
closeToken->GetLength());
return new BadExpression(closeToken->GetStartPosition(), closeToken->GetLength());
}
auto start = current->GetStartPosition();
@ -353,14 +414,16 @@ ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* function
} else {
while (peekedKind != TokenKind::CloseParenthesis) {
if (peekedKind == TokenKind::EndOfFile) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), peeked->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(),
peeked->GetLength());
return new BadExpression(peeked->GetStartPosition(), peeked->GetLength());
}
parameters.push_back(this->ParseExpression(this->Next()));
peeked = this->Next();
peekedKind = peeked->GetKind();
if (peekedKind != TokenKind::CloseParenthesis && peekedKind != TokenKind::CommaToken) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), peeked->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(),
peeked->GetLength());
return new BadExpression(peeked->GetStartPosition(), peeked->GetLength());
}
}
@ -374,7 +437,8 @@ ParsedExpression* Parser::ParseIndexExpression(ParsedExpression* indexingExpress
auto indexExpression = this->ParseExpression(this->Next());
auto closeBracket = this->Next();
if (closeBracket->GetKind() != TokenKind::CloseSquareBracket) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(), closeBracket->GetLength());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(),
closeBracket->GetLength());
return new BadExpression(closeBracket->GetStartPosition(), closeBracket->GetLength());
}
auto start = indexingExpression->GetStartPosition();
@ -385,11 +449,14 @@ ParsedExpression* Parser::ParsePeriodIndexExpression(ParsedExpression* indexingE
this->Next(); // consume '.' token
auto identifier = this->Next();
if (identifier->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), identifier->GetLength());
return new BadExpression(indexingExpression->GetStartPosition(), identifier->GetEndPosition() - indexingExpression->GetStartPosition());
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength());
return new BadExpression(indexingExpression->GetStartPosition(),
identifier->GetEndPosition() - indexingExpression->GetStartPosition());
}
auto start = indexingExpression->GetStartPosition();
return new PeriodIndexExpression(indexingExpression, ((IdentifierToken*)identifier)->GetValue(), start, identifier->GetEndPosition() - start);
return new PeriodIndexExpression(indexingExpression, ((IdentifierToken *) identifier)->GetValue(), start,
identifier->GetEndPosition() - start);
}
@ -412,8 +479,10 @@ ParsedExpression* Parser::ParseTableExpression(const IToken* current){
auto expression = this->ParseExpression(this->Next());
expressions.push_back(expression);
n = this->Next();
if (n->GetKind() != TokenKind::CommaToken && n->GetKind() != TokenKind ::CloseCurlyBracket && !hasErrors){
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, n->GetStartPosition(), n->GetLength());
if (n->GetKind() != TokenKind::CommaToken && n->GetKind() != TokenKind::CloseCurlyBracket &&
!hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, n->GetStartPosition(),
n->GetLength());
hasErrors = true;
}
}
@ -430,3 +499,4 @@ ParsedExpression* Parser::ParseTableExpression(const IToken* current){
}
}
}

View File

@ -7,6 +7,7 @@
#include "ParsedStatements/ParsedStatement.hpp"
#include "../Script.hpp"
namespace Porygon::Parser {
enum class OperatorPrecedence {
No,
LogicalOr,
@ -20,38 +21,55 @@ enum class OperatorPrecedence {
class Parser {
vector<const IToken *> _tokens;
unsigned int _position;
Script* ScriptData;
Porygon::Script *ScriptData;
const IToken *Peek();
const IToken *PeekAt(int offset);
const IToken *Next();
ParsedStatement *ParseStatement(const IToken *current);
ParsedStatement *ParseVariableAssignment(const IToken *current);
ParsedStatement *ParseIndexAssignment(ParsedExpression *indexer);
ParsedStatement *ParseBlock(const vector<TokenKind>& endTokens, const vector<const ParsedStatement*>& openStatements = {});
ParsedStatement *
ParseBlock(const vector<TokenKind> &endTokens, const vector<const ParsedStatement *> &openStatements = {});
ParsedStatement *ParseFunctionDeclaration(const IToken *current);
ParsedStatement *ParseReturnStatement(const IToken *current);
ParsedStatement *ParseIfStatement(const IToken *current);
ParsedExpression *ParseExpression(const IToken *current);
ParsedExpression *ParseBinaryExpression(const IToken *current, OperatorPrecedence parentPrecedence);
ParsedExpression *ParsePrimaryExpression(const IToken *current);
ParsedExpression *ParseParenthesizedExpression(const IToken *current);
ParsedExpression *ParseFunctionCallExpression(ParsedExpression *functionExpression);
ParsedExpression *ParseIndexExpression(ParsedExpression *indexingExpression);
ParsedExpression *ParsePeriodIndexExpression(ParsedExpression *indexingExpression);
ParsedExpression *ParseTableExpression(const IToken *current);
public:
ParsedScriptStatement *Parse();
explicit Parser(const vector<const IToken*>& tokens, Script* scriptData){
explicit Parser(const vector<const IToken *> &tokens, Porygon::Script *scriptData) {
_tokens = tokens;
_position = 0;
ScriptData = scriptData;
}
};
}
#endif //PORYGONLANG_PARSER_HPP

View File

@ -10,7 +10,9 @@
#include "../Utilities/HashedString.hpp"
using namespace std;
using namespace Porygon::Utilities;
namespace Porygon::Parser {
class IToken {
const unsigned int _position;
const unsigned int _length;
@ -18,8 +20,7 @@ public:
virtual const TokenKind GetKind() const = 0;
IToken(unsigned int position, unsigned int length)
: _position(position), _length(length)
{
: _position(position), _length(length) {
}
const unsigned int GetStartPosition() const {
@ -43,8 +44,7 @@ public:
explicit SimpleToken(TokenKind kind, unsigned int position, unsigned int length)
: IToken(position, length),
_kind(kind)
{
_kind(kind) {
}
const TokenKind GetKind() const final {
@ -58,8 +58,7 @@ public:
explicit IntegerToken(long value, unsigned int position, unsigned int length)
: IToken(position, length),
_value(value)
{
_value(value) {
}
const TokenKind GetKind() const final {
@ -77,8 +76,7 @@ public:
explicit FloatToken(double value, unsigned int position, unsigned int length)
: IToken(position, length),
_value(value)
{
_value(value) {
}
const TokenKind GetKind() const final {
@ -96,8 +94,7 @@ public:
explicit StringToken(u16string value, unsigned int position, unsigned int length)
: IToken(position, length),
_value(std::move(value))
{
_value(std::move(value)) {
}
const TokenKind GetKind() const final {
@ -110,13 +107,12 @@ public:
};
class IdentifierToken : public IToken {
const HashedString _value;
const Utilities::HashedString _value;
public:
explicit IdentifierToken(const HashedString value, unsigned int position, unsigned int length)
: IToken(position, length),
_value(value)
{
_value(value) {
}
const TokenKind GetKind() const final {
@ -127,5 +123,6 @@ public:
return _value;
}
};
}
#endif //PORYGONLANG_TOKEN_HPP

View File

@ -1,6 +1,7 @@
#ifndef PORYGONLANG_TOKENKIND_HPP
#define PORYGONLANG_TOKENKIND_HPP
namespace Porygon::Parser {
enum class TokenKind {
EndOfFile,
BadToken,
@ -54,6 +55,6 @@ enum class TokenKind{
TrueKeyword,
WhileKeyword,
};
}
#endif //PORYGONLANG_TOKENKIND_HPP

View File

@ -4,13 +4,13 @@
#include "../Utilities/HashedString.hpp"
namespace Porygon::Parser {
class TypedVariableIdentifier {
HashedString _type;
HashedString _identifier;
public:
TypedVariableIdentifier(HashedString type, HashedString identifier)
: _type(type), _identifier(identifier)
{
: _type(type), _identifier(identifier) {
}
HashedString GetType() {
@ -21,5 +21,6 @@ public:
return _identifier;
}
};
}
#endif //PORYGONLANG_TYPEDVARIABLEIDENTIFIER_HPP

View File

@ -2,9 +2,11 @@
#ifndef PORYGONLANG_UNARYOPERATORKIND_HPP
#define PORYGONLANG_UNARYOPERATORKIND_HPP
namespace Porygon::Parser {
enum class UnaryOperatorKind {
Identity,
Negation,
LogicalNegation,
};
}
#endif //PORYGONLANG_UNARYOPERATORKIND_HPP

View File

@ -9,7 +9,7 @@
#include "Parser/Parser.hpp"
#include "Binder/Binder.hpp"
Script* Script::Create(const u16string& script) {
Porygon::Script* Porygon::Script::Create(const u16string& script) {
auto s = new Script();
s -> Parse(script);
return s;
@ -20,22 +20,22 @@ std::u16string To_UTF16(const string &s)
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return conv.from_bytes(s);
}
Script *Script::Create(const string &script) {
Porygon::Script *Porygon::Script::Create(const string &script) {
return Script::Create(To_UTF16(script));
}
Script::Script() {
Porygon::Script::Script() {
Diagnostics = new DiagnosticsHolder();
_evaluator = new Evaluator(this);
_boundScript = nullptr;
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
_evaluator = new Evaluator(this -> _scriptVariables);
}
EvalValue* Script::Evaluate() {
EvalValue* Porygon::Script::Evaluate() {
return _evaluator->Evaluate(_boundScript);
}
Script::~Script() {
Porygon::Script::~Script() {
delete this -> Diagnostics;
delete this -> _boundScript;
delete this -> _evaluator;
@ -43,10 +43,10 @@ Script::~Script() {
delete this->_scriptVariables;
}
void Script::Parse(const u16string& script) {
void Porygon::Script::Parse(const u16string& script) {
auto lexer = Lexer(script, this);
auto lexResult = lexer.Lex();
auto parser = Parser(lexResult, this);
auto parser = Parser::Parser(lexResult, this);
auto parseResult = parser.Parse();
for (auto token : lexResult){
delete token;
@ -55,7 +55,7 @@ void Script::Parse(const u16string& script) {
if (!Diagnostics->HasErrors()){
unordered_map<uint32_t, BoundVariable*> scriptScope;
auto bindScope = new BoundScope(&scriptScope);
this->_boundScript = Binder::Bind(this, parseResult, bindScope);
this->_boundScript = Binder::Binder::Bind(this, parseResult, bindScope);
for (const auto& v : scriptScope){
this->_scriptVariables -> insert({v.first, nullptr});
delete v.second;
@ -65,59 +65,57 @@ void Script::Parse(const u16string& script) {
delete parseResult;
}
EvalValue *Script::GetVariable(const u16string &key) {
EvalValue *Porygon::Script::GetVariable(const u16string &key) {
return _scriptVariables -> at(HashedString(key).GetHash()).get();
}
bool Script::HasVariable(const u16string &key) {
bool Porygon::Script::HasVariable(const u16string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end();
}
EvalValue *Script::GetLastValue() {
EvalValue *Porygon::Script::GetLastValue() {
return _evaluator->GetLastValue();
}
bool Script::HasFunction(const u16string &key) {
bool Porygon::Script::HasFunction(const u16string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
}
shared_ptr<EvalValue> Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
shared_ptr<EvalValue> Porygon::Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, variables);
}
extern "C" {
Script* CreateScript(char16_t * s){
return Script::Create(s);
Porygon::Script* CreateScript(char16_t * s){
return Porygon::Script::Create(s);
}
void EvaluateScript(Script* script){
void EvaluateScript(Porygon::Script* script){
script->Evaluate();
}
EvalValue* GetLastValue(Script* script){
EvalValue* GetLastValue(Porygon::Script* script){
return script->GetLastValue();
}
bool HasVariable(Script* script, const char16_t* key){
bool HasVariable(Porygon::Script* script, const char16_t* key){
return script->HasVariable(key);
}
EvalValue* GetVariable(Script* script, const char16_t* key){
EvalValue* GetVariable(Porygon::Script* script, const char16_t* key){
return script->GetVariable(key);
}
bool HasFunction(Script* script, const char16_t* key){
bool HasFunction(Porygon::Script* script, const char16_t* key){
return script->HasFunction(key);
}
EvalValue* CallFunction(Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
EvalValue* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
std::vector<EvalValue*> v(parameters, parameters + parameterCount);
return script->CallFunction(key, v).get();
}
}

View File

@ -1,3 +1,5 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPT_HPP
#define PORYGONLANG_SCRIPT_HPP
@ -6,20 +8,18 @@
#include <unordered_map>
#include "Diagnostics/DiagnosticsHolder.hpp"
#include "Binder/BoundStatements/BoundStatement.hpp"
class Script;
class Evaluator;
#include "Evaluator/Evaluator.hpp"
#include "Evaluator/EvalValues/EvalValue.hpp"
#include "Utilities/HashedString.hpp"
using namespace std;
using namespace Porygon::Evaluation;
namespace Porygon{
class Script {
friend class Evaluator;
Evaluator* _evaluator;
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptVariables;
BoundScriptStatement* _boundScript;
Binder::BoundScriptStatement* _boundScript;
shared_ptr<ScriptType> _returnType;
explicit Script();
@ -36,7 +36,7 @@ public:
}
void SetReturnType(shared_ptr<ScriptType> t){
_returnType = t;
_returnType = std::move(t);
}
EvalValue* Evaluate();
@ -50,5 +50,7 @@ public:
bool HasFunction(const u16string& key);
};
}
#endif //PORYGONLANG_SCRIPT_HPP

View File

@ -1,5 +1,6 @@
#include "Script.hpp"
namespace Porygon{
const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{
// String type is the only simple script type we want to
return _class == TypeClass::String && indexer->_class == TypeClass::Number && !((NumericScriptType*)indexer)->IsFloat();
@ -13,7 +14,7 @@ const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType *indexer) con
}
extern "C"{
ScriptType* CreateScriptType(TypeClass t){
ScriptType* CreateScriptType(Porygon::TypeClass t){
return new ScriptType(t);
}
@ -25,3 +26,5 @@ extern "C"{
return new StringScriptType(knownAtBind, hash);
}
}
}

View File

@ -12,6 +12,7 @@
using namespace std;
namespace Porygon{
enum class TypeClass{
Error,
Nil,
@ -103,11 +104,11 @@ public:
class FunctionScriptType : public ScriptType{
shared_ptr<ScriptType> _returnType;
vector<shared_ptr<ScriptType>> _parameterTypes;
vector<shared_ptr<BoundVariableKey>> _parameterKeys;
vector<shared_ptr<Binder::BoundVariableKey>> _parameterKeys;
int _scopeIndex;
public:
FunctionScriptType(std::shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
vector<shared_ptr<BoundVariableKey>> parameterKeys, int scopeIndex)
vector<shared_ptr<Binder::BoundVariableKey>> parameterKeys, int scopeIndex)
: ScriptType(TypeClass::Function){
_returnType = std::move(returnType);
_parameterTypes = std::move(parameterTypes);
@ -126,7 +127,7 @@ public:
return _parameterTypes;
}
const vector<shared_ptr<BoundVariableKey>> GetParameterKeys() const{
const vector<shared_ptr<Binder::BoundVariableKey>> GetParameterKeys() const{
return _parameterKeys;
}
@ -151,6 +152,7 @@ public:
return _valueType;
}
};
}
#endif //PORYGONLANG_SCRIPTTYPE_HPP

View File

@ -4,6 +4,7 @@
#include <unordered_map>
#include "Binder/BoundVariables/BoundVariable.hpp"
namespace Porygon{
class TableScriptType : public ScriptType{
const unordered_map<uint32_t, BoundVariable*>* _values;
const int _localVariableCount;
@ -50,6 +51,7 @@ public:
}
};
}
#include "ScriptType.hpp"

View File

@ -2,6 +2,7 @@
#include "UserData.hpp"
#include "UserDataStorage.hpp"
namespace Porygon::UserData {
extern "C" {
void RegisterUserDataType(uint32_t id) {
auto ud = new UserData({});
@ -18,3 +19,4 @@ extern "C"{
return ud->GetFieldCount();
}
}
}

View File

@ -5,6 +5,7 @@
#include <unordered_map>
#include "UserDataField.hpp"
namespace Porygon::UserData {
class UserData {
std::unordered_map<uint32_t, UserDataField *> _fields;
public:
@ -28,6 +29,7 @@ public:
return _fields.size();
}
};
}
#endif //PORYGONLANG_USERDATA_HPP

View File

@ -1,8 +1,11 @@
#include "UserDataField.hpp"
namespace Porygon::UserData {
extern "C" {
UserDataField* CreateUserDataField(ScriptType* type, EvalValue* (*getter)(void* obj), void (*setter)(void* obj, EvalValue* val)){
UserDataField *
CreateUserDataField(ScriptType *type, Evaluation::EvalValue *(*getter)(void *obj), void (*setter)(void *obj, Evaluation::EvalValue *val)) {
return new UserDataField(type, getter, setter);
}
}
}

View File

@ -6,12 +6,13 @@
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "../Evaluator/EvalValues/NumericEvalValue.hpp"
namespace Porygon::UserData{
class UserDataField {
shared_ptr<ScriptType> _type;
EvalValue* (*_get)(void* obj);
void (*_set)(void* obj, EvalValue* val);
Evaluation::EvalValue* (*_get)(void* obj);
void (*_set)(void* obj, Evaluation::EvalValue* val);
public:
UserDataField(ScriptType* type, EvalValue* (*getter)(void* obj), void (*setter)(void* obj, EvalValue* val)){
UserDataField(ScriptType* type, Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, Evaluation::EvalValue* val)){
_type = shared_ptr<ScriptType>(type);
_get = getter;
_set = setter;
@ -25,7 +26,7 @@ public:
return _get != nullptr;
}
EvalValue* Get(void* obj){
Evaluation::EvalValue* Get(void* obj){
return this ->_get(obj);
}
@ -33,10 +34,11 @@ public:
return _set != nullptr;
}
void Set(void* obj, EvalValue* val){
void Set(void* obj, Evaluation::EvalValue* val){
this->_set(obj, val);
}
};
}
#endif //PORYGONLANG_USERDATAFIELD_HPP

View File

@ -8,12 +8,14 @@
#include "UserData.hpp"
#include "UserDataStorage.hpp"
namespace Porygon::UserData {
class UserDataScriptType : public ScriptType {
shared_ptr<UserData> _userData;
public:
explicit UserDataScriptType(uint32_t id) : ScriptType(TypeClass::UserData) {
_userData = UserDataStorage::GetUserDataType(id);
}
explicit UserDataScriptType(shared_ptr<UserData> ud) : ScriptType(TypeClass::UserData) {
_userData = std::move(ud);
}
@ -48,6 +50,7 @@ public:
return _userData->GetField(hash)->GetType();
}
};
}
#endif //PORYGONLANG_USERDATASCRIPTTYPE_HPP

View File

@ -1,4 +1,6 @@
#include "UserDataStorage.hpp"
namespace Porygon::UserData {
UserDataStorage::_internalDataStorage UserDataStorage::_internal = UserDataStorage::_internalDataStorage();
}

View File

@ -5,11 +5,13 @@
#include <unordered_map>
#include "UserData.hpp"
namespace Porygon::UserData {
class UserDataStorage {
class _internalDataStorage {
public:
std::unordered_map<uint32_t, shared_ptr<UserData>> _userData;
};
static _internalDataStorage _internal;
public:
@ -25,6 +27,6 @@ public:
return UserDataStorage::_internal._userData[i];
}
};
}
#endif //PORYGONLANG_USERDATASTORAGE_HPP

View File

@ -1,8 +1,10 @@
#include "UserDataValue.hpp"
namespace Porygon::UserData {
extern "C" {
UserDataValue *CreateUserDataEvalValue(uint32_t typeHash, void *obj) {
return new UserDataValue(typeHash, obj);
}
}
}

View File

@ -7,19 +7,18 @@
#include "UserData.hpp"
#include "UserDataStorage.hpp"
class UserDataValue : public EvalValue{
namespace Porygon::UserData {
class UserDataValue : public Evaluation::EvalValue {
const shared_ptr<UserData> _userData;
void *_obj;
public:
UserDataValue(shared_ptr<UserData> userData, void *obj)
: _userData(std::move(userData))
{
: _userData(std::move(userData)) {
_obj = obj;
}
UserDataValue(uint32_t userDataId, void *obj)
: _userData(UserDataStorage::GetUserDataType(userDataId))
{
: _userData(UserDataStorage::GetUserDataType(userDataId)) {
_obj = obj;
}
@ -58,6 +57,6 @@ public:
field->Set(_obj, value.get());
}
};
}
#endif //PORYGONLANG_USERDATAVALUE_HPP

View File

@ -4,6 +4,7 @@
#include <string>
namespace Porygon::Utilities{
class HashedString{
const uint32_t _hash;
public:
@ -40,5 +41,6 @@ public:
return _hash != b._hash;
}
};
}
#endif //PORYGONLANG_HASHEDSTRING_HPP

View File

@ -2,6 +2,8 @@
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Basic conditional", "[integration]" ) {
Script* script = Script::Create("if true then foo = true end");
REQUIRE(!script->Diagnostics -> HasErrors());

View File

@ -3,6 +3,7 @@
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Diagnostic invalid character", "[integration]" ) {
auto script = Script::Create("1 + 1 @");
REQUIRE(script->Diagnostics -> HasErrors());

View File

@ -3,6 +3,7 @@
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "True Equals True", "[integration]" ) {
auto script = Script::Create("true == true");
REQUIRE(!script->Diagnostics -> HasErrors());

View File

@ -2,6 +2,8 @@
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Define script function", "[integration]" ) {
Script* script = Script::Create("function add(number a, number b) a + b end");
REQUIRE(!script->Diagnostics -> HasErrors());

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "String indexing", "[integration]" ) {
auto script = Script::Create("'foobar'[4]");

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Basic True", "[integration]" ) {
auto script = Script::Create("true");

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Integer Negation", "[integration]" ) {
auto script = Script::Create("-60");

View File

@ -3,6 +3,7 @@
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Simple String", "[integration]" ) {
auto script = Script::Create("\"foo bar\"");

View File

@ -2,6 +2,7 @@
#include <catch.hpp>
#include "../src/Script.hpp"
#include "../../src/Evaluator/EvalValues/TableEvalValue.hpp"
using namespace Porygon;
TEST_CASE( "Create empty table", "[integration]" ) {
Script* script = Script::Create("table = {}");

View File

@ -5,6 +5,9 @@
#include "../../src/UserData/UserData.hpp"
#include "../../src/UserData/UserDataStorage.hpp"
#include "../../src/UserData/UserDataValue.hpp"
using namespace Porygon;
using namespace Porygon::UserData;
using namespace Porygon::Utilities;
class UserDataTestObject{
public:
@ -18,8 +21,8 @@ public:
((UserDataTestObject*)obj)->foo = val->EvaluateInteger();
}
static UserData* CreateData(){
return new UserData({
static Porygon::UserData::UserData* CreateData(){
return new Porygon::UserData::UserData({
{
HashedString::ConstHash("foo"),
new UserDataField(new NumericScriptType(true, false), GetFoo, SetFoo)

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Create script variable", "[integration]" ) {
Script* script = Script::Create("foo = true");

View File

@ -3,6 +3,7 @@
#include <codecvt>
#include <locale>
#include "../../src/Parser/Lexer.hpp"
using namespace Porygon::Parser;
TEST_CASE( "When at end of script return terminator", "[lexer]" ) {
Lexer lexer = Lexer(u"", nullptr);

View File

@ -3,6 +3,7 @@
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../../src/Parser/Parser.hpp"
using namespace Porygon::Parser;
TEST_CASE( "Parse single true keyword", "[parser]" ) {
vector<const IToken*> v {new SimpleToken(TokenKind::TrueKeyword,0,0), new SimpleToken(TokenKind::EndOfFile,0,0)};