General cleanup
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-07-04 18:24:49 +02:00
parent 0446c1098b
commit bb0a6aba19
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
20 changed files with 90 additions and 92 deletions

View File

@ -187,10 +187,10 @@ namespace Porygon::Binder {
assignmentKey = assignment.GetKey();
}
auto boundBlock = this->BindBlockStatement(functionStatement->GetBlock());
auto boundBlock = dynamic_cast<BoundBlockStatement*>(this->BindBlockStatement(functionStatement->GetBlock()));
this->_scope->GoOuterScope();
this->_currentFunction = nullptr;
return new BoundFunctionDeclarationStatement(type, assignmentKey, (BoundBlockStatement *) boundBlock);
return new BoundFunctionDeclarationStatement(type, assignmentKey, boundBlock);
}
BoundStatement *Binder::BindReturnStatement(const ParsedStatement *statement) {
@ -696,14 +696,13 @@ namespace Porygon::Binder {
auto innerScope = new BoundScope(tableScope);
auto currentScope = this->_scope;
this->_scope = innerScope;
auto block = this->BindBlockStatement(expression->GetBlock());
auto block = dynamic_cast<BoundBlockStatement*>(this->BindBlockStatement(expression->GetBlock()));
this->_scope = currentScope;
auto tableType = std::make_shared<TableScriptType>(tableScope, innerScope->GetLocalVariableCount());
delete innerScope;
return new BoundTableExpression((BoundBlockStatement *) block, tableType, expression->GetStartPosition(),
expression->GetLength());
return new BoundTableExpression(block, tableType, expression->GetStartPosition(), expression->GetLength());
}
}

View File

@ -46,6 +46,12 @@ namespace Porygon::Binder {
BoundExpression *BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter);
public:
Binder(){
_scriptData = nullptr;
_scope = nullptr;
_currentFunction = nullptr;
}
static BoundScriptStatement *
Bind(Porygon::Script *script, const ParsedScriptStatement *s, BoundScope *scriptScope);

View File

@ -18,13 +18,10 @@ namespace Porygon::Diagnostics {
std::vector<std::string> _arguments;
std::string* _message;
public:
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> arguments) {
_severity = severity;
_code = code;
_start = start;
_length = length;
_arguments = std::move(arguments);
_message = nullptr;
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> arguments)
: _severity(severity), _code(code), _start(start), _length(length), _arguments(std::move(arguments)), _message(
nullptr)
{
}
~Diagnostic(){

View File

@ -19,9 +19,7 @@ namespace Porygon::Diagnostics {
vector<size_t> _lineLength;
public:
explicit DiagnosticsHolder(const u16string& str) {
_hasErrors = false;
_lineStarts = vector<size_t>{0};
explicit DiagnosticsHolder(const u16string& str) :_hasErrors(false), _lineStarts(vector<size_t>{0}) {
size_t lineLength = 0;
for (size_t i = 0; i < str.size(); i++){
lineLength++;

View File

@ -69,7 +69,7 @@ namespace Porygon::Evaluation {
const bool NumericEvalValue::operator==(EvalValue *b) const {
if (b->GetTypeClass() != TypeClass::Number)
return false;
auto numVal = (NumericEvalValue *) b;
auto numVal = dynamic_cast<NumericEvalValue*>(b);
if (this->IsFloat() != numVal->IsFloat())
return false;

View File

@ -13,8 +13,7 @@ namespace Porygon::Evaluation {
u16string _value;
size_t _hash;
public:
explicit StringEvalValue(u16string s) {
_value = move(s);
explicit StringEvalValue(u16string s) : _value(move(s)){
_hash = Utilities::HashedString::ConstHash(_value.c_str());
}

View File

@ -5,9 +5,8 @@
namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables,
int localVariableCount) {
_scriptScope = scriptVariables;
_localScope = map<uint64_t, shared_ptr<EvalValue>>();
int localVariableCount) : _scriptScope(scriptVariables),
_localScope(map<uint64_t, shared_ptr<EvalValue>>()) {
}
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) {

View File

@ -33,7 +33,7 @@ namespace Porygon::Evaluation {
case BoundStatementKind::Script:
throw; // Should never happen
case BoundStatementKind::Block:
return this->EvaluateBlockStatement((BoundBlockStatement *) statement);
return this->EvaluateBlockStatement((BoundBlockStatement*)statement);
case BoundStatementKind::Expression:
return this->EvaluateExpressionStatement((BoundExpressionStatement *) statement);
case BoundStatementKind::Assignment:
@ -382,7 +382,7 @@ namespace Porygon::Evaluation {
auto opt = func->GetOption(option->GetOptionId());
if (option -> IsScriptFunction()){
auto parameterTypes = option->GetParameterTypes();
auto scriptFunctionType = (ScriptFunctionOption*)option;
auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option);
auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(opt);
@ -413,7 +413,7 @@ namespace Porygon::Evaluation {
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
auto option = (ScriptFunctionOption*)type-> GetFirstOption();
auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption());
auto parameterTypes = option->GetParameterTypes();
auto parameterKeys = option->GetParameterKeys();

View File

@ -59,12 +59,9 @@ namespace Porygon::Evaluation{
const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression);
public:
explicit Evaluator(map<Utilities::HashedString, shared_ptr<EvalValue>>* scriptVariables){
_scriptVariables = scriptVariables;
_hasReturned = false;
_hasBroken = false;
_returnValue = nullptr;
_evaluationScope = nullptr;
explicit Evaluator(map<Utilities::HashedString, shared_ptr<EvalValue>>* scriptVariables)
: _scriptVariables(scriptVariables), _hasReturned(false), _hasBroken(false), _returnValue(nullptr),
_evaluationScope(nullptr){
}
EvalValue* Evaluate(const BoundScriptStatement* statement);

View File

@ -2,6 +2,7 @@
#ifndef PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
#define PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
#include <utility>
#include "ScriptType.hpp"
namespace Porygon {
@ -10,9 +11,8 @@ namespace Porygon {
vector<shared_ptr<ScriptType>> _parameterTypes;
size_t _option = 0;
public:
GenericFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes){
_returnType = move(returnType);
_parameterTypes = move(parameterTypes);
GenericFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes)
: _returnType(std::move(returnType)), _parameterTypes(std::move(parameterTypes)){
}
virtual ~GenericFunctionOption() = default;
@ -93,8 +93,7 @@ namespace Porygon {
public:
ScriptFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
vector<shared_ptr<Porygon::Binder::BoundVariableKey>> parameterKeys)
: GenericFunctionOption(move(returnType), std::move(parameterTypes)) {
_parameterKeys = move(parameterKeys);
: GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) {
}
const vector<shared_ptr<Porygon::Binder::BoundVariableKey>> GetParameterKeys() const {

View File

@ -72,7 +72,7 @@ namespace Porygon::Parser {
return ParsedExpressionKind::LiteralInteger;
}
explicit LiteralIntegerExpression(IntegerToken *token)
explicit LiteralIntegerExpression(const IntegerToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(token->GetValue()) {
}
@ -89,7 +89,7 @@ namespace Porygon::Parser {
return ParsedExpressionKind::LiteralFloat;
}
explicit LiteralFloatExpression(FloatToken *token)
explicit LiteralFloatExpression(const FloatToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(token->GetValue()) {
}
@ -106,7 +106,7 @@ namespace Porygon::Parser {
return ParsedExpressionKind::LiteralString;
}
explicit LiteralStringExpression(StringToken *token)
explicit LiteralStringExpression(const StringToken *token)
: ParsedExpression(token->GetStartPosition(), token->GetLength()),
_value(std::move(token->GetValue())) {
}
@ -140,7 +140,7 @@ namespace Porygon::Parser {
return ParsedExpressionKind::Variable;
}
explicit VariableExpression(IdentifierToken *token) : ParsedExpression(token->GetStartPosition(),
explicit VariableExpression(const IdentifierToken *token) : ParsedExpression(token->GetStartPosition(),
token->GetLength()),
_value(HashedString(token->GetValue())) {
}
@ -314,12 +314,11 @@ namespace Porygon::Parser {
class ParsedNumericalTableExpression : public ParsedExpression {
vector<const ParsedExpression *> _expressions;
const vector<const ParsedExpression *> _expressions;
public:
ParsedNumericalTableExpression(vector<const ParsedExpression *> expressions, unsigned int start,
unsigned int length)
: ParsedExpression(start, length) {
_expressions = std::move(expressions);
: ParsedExpression(start, length), _expressions(std::move(expressions)) {
}
~ParsedNumericalTableExpression() final {

View File

@ -73,16 +73,13 @@ namespace Porygon::Parser {
class ParsedBlockStatement : public ParsedStatement {
const std::vector<const ParsedStatement *> _statements;
public:
explicit ParsedBlockStatement(std::vector<const ParsedStatement *> statements)
explicit ParsedBlockStatement(const std::vector<const ParsedStatement *>& 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)) {
start, 0), _statements(std::move(statements)) {
}

View File

@ -89,7 +89,7 @@ namespace Porygon::Parser {
}
auto start = current->GetStartPosition();
return new ParsedAssignmentStatement(isLocal, ((IdentifierToken *) identifier)->GetValue(), expression, start,
return new ParsedAssignmentStatement(isLocal, (dynamic_cast<const IdentifierToken*>(identifier))->GetValue(), expression, start,
expression->GetEndPosition() - start);
}
@ -102,7 +102,7 @@ namespace Porygon::Parser {
}
ParsedStatement *
ParsedBlockStatement *
Parser::ParseBlock(const vector<TokenKind> &endTokens, const vector<const ParsedStatement *> &openStatements) {
auto statements = openStatements;
auto start = this->_position;
@ -168,8 +168,8 @@ namespace Porygon::Parser {
hasErrors = true;
continue;
}
auto typeToken = (IdentifierToken *) type;
auto identifierToken = (IdentifierToken *) identifier;
auto typeToken = dynamic_cast<const IdentifierToken*>(type);
auto identifierToken = dynamic_cast<const IdentifierToken*>(identifier);
parameters.push_back(new TypedVariableIdentifier(typeToken->GetValue(), identifierToken->GetValue()));
auto nextKind = next->GetKind();
@ -190,9 +190,9 @@ namespace Porygon::Parser {
if (block->GetKind() == ParsedStatementKind::Bad) {
return new ParsedBadStatement(start, block->GetEndPosition() - start);
}
auto functionIdentifier = ((IdentifierToken *) functionIdentifierToken)->GetValue();
auto functionIdentifier = dynamic_cast<const IdentifierToken*>(functionIdentifierToken)->GetValue();
return new ParsedFunctionDeclarationStatement(HashedString(functionIdentifier), parameters,
(ParsedBlockStatement *) block, start,
block, start,
block->GetEndPosition() - start);
}
@ -239,7 +239,7 @@ namespace Porygon::Parser {
}
ParsedStatement *Parser::ParseNumericForStatement(const IToken *current) {
auto identifier = (IdentifierToken*)current;
auto identifier = dynamic_cast<const IdentifierToken*>(current);
this->Next(); // consume assignment token
bool hasErrors = false;
auto start = this ->ParseExpression(this ->Next());
@ -271,8 +271,8 @@ namespace Porygon::Parser {
}
ParsedStatement *Parser::ParseGenericForStatement(const IToken *current) {
auto keyIdentifier = ((IdentifierToken*) current)->GetValue();
IdentifierToken* valueIdentifierToken = nullptr;
auto keyIdentifier = dynamic_cast<const IdentifierToken*>(current)->GetValue();
const IdentifierToken* valueIdentifierToken = nullptr;
bool hasErrors = false;
auto next = this -> Next();
if (next -> GetKind() == TokenKind::CommaToken){
@ -282,7 +282,7 @@ namespace Porygon::Parser {
this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength());
} else{
valueIdentifierToken = (IdentifierToken*) next;
valueIdentifierToken = dynamic_cast<const IdentifierToken*>(next);
next = this -> Next();
}
}
@ -477,17 +477,17 @@ namespace Porygon::Parser {
ParsedExpression *Parser::ParsePrimaryExpression(const IToken *current) {
switch (current->GetKind()) {
case TokenKind::Integer:
return new LiteralIntegerExpression((IntegerToken *) current);
return new LiteralIntegerExpression(dynamic_cast<const IntegerToken*>(current));
case TokenKind::Float:
return new LiteralFloatExpression((FloatToken *) current);
return new LiteralFloatExpression(dynamic_cast<const FloatToken*>(current));
case TokenKind::String:
return new LiteralStringExpression((StringToken *) current);
return new LiteralStringExpression(dynamic_cast<const StringToken*>(current));
case TokenKind::TrueKeyword:
return new LiteralBoolExpression(current);
case TokenKind::FalseKeyword:
return new LiteralBoolExpression(current);
case TokenKind::Identifier:
return new VariableExpression((IdentifierToken *) current);
return new VariableExpression(dynamic_cast<const IdentifierToken*>(current));
case TokenKind::OpenParenthesis:
return this->ParseParenthesizedExpression(current);
case TokenKind::OpenCurlyBracket:
@ -566,7 +566,7 @@ namespace Porygon::Parser {
identifier->GetEndPosition() - indexingExpression->GetStartPosition());
}
auto start = indexingExpression->GetStartPosition();
return new PeriodIndexExpression(indexingExpression, ((IdentifierToken *) identifier)->GetValue(), start,
return new PeriodIndexExpression(indexingExpression, dynamic_cast<const IdentifierToken*>(identifier)->GetValue(), start,
identifier->GetEndPosition() - start);
}
@ -582,7 +582,7 @@ namespace Porygon::Parser {
// If the first item is an expression, and is followed by a comma, we're dealing with a simple {1, 2, 3} kind of array
if (firstItem->GetKind() == ParsedStatementKind::Expression &&
(this->Peek()->GetKind() == TokenKind::CommaToken)) {
auto statement = ((ParsedExpressionStatement *) firstItem);
auto statement = dynamic_cast<ParsedExpressionStatement*>(firstItem);
auto expr = statement->GetExpression();
statement->NullifyExpression();
delete statement;
@ -607,7 +607,7 @@ namespace Porygon::Parser {
}
// Otherwise we have a more complex table, which can be defined by a block
else {
auto block = (ParsedBlockStatement *) this->ParseBlock({TokenKind::CloseCurlyBracket}, {firstItem});
auto block = this->ParseBlock({TokenKind::CloseCurlyBracket}, {firstItem});
auto closeToken = this->PeekAt(-1);
return new ParsedTableExpression(block, start, closeToken->GetEndPosition() - start);
}

View File

@ -34,7 +34,7 @@ namespace Porygon::Parser {
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 = {});
ParsedBlockStatement *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);
@ -57,10 +57,8 @@ namespace Porygon::Parser {
public:
ParsedScriptStatement *Parse();
explicit Parser(const vector<const IToken *> &tokens, Porygon::Script *scriptData) {
_tokens = tokens;
_position = 0;
ScriptData = scriptData;
explicit Parser(const vector<const IToken *> &tokens, Porygon::Script *scriptData) : _tokens(tokens), _position(0),
ScriptData(scriptData){
}
};

View File

@ -24,13 +24,12 @@ Porygon::Script *Porygon::Script::Create(const string &script) {
return Script::Create(To_UTF16(script));
}
Porygon::Script::Script(const u16string& s) {
Diagnostics = make_shared<Diagnostics::DiagnosticsHolder>(s);
_boundScript = nullptr;
_scriptVariables = new map<Utilities::HashedString, shared_ptr<EvalValue>>();
Porygon::Script::Script(const u16string& s)
: Diagnostics(make_shared<Diagnostics::DiagnosticsHolder>(s)),
_boundScript(nullptr),
_scriptVariables(new map<Utilities::HashedString, shared_ptr<EvalValue>>())
{
_evaluator = new Evaluator(this -> _scriptVariables);
this -> Parse(s);
}
@ -99,10 +98,11 @@ Porygon::Script *Porygon::Script::Clone(const Porygon::Script *script) {
}
Porygon::Script::Script(shared_ptr<BoundScriptStatement> boundScript,
shared_ptr<Porygon::Diagnostics::DiagnosticsHolder> diagnostics) {
_boundScript = std::move(boundScript);
Diagnostics = std::move(diagnostics);
_scriptVariables = new map<Utilities::HashedString, shared_ptr<EvalValue>>();
shared_ptr<Porygon::Diagnostics::DiagnosticsHolder> diagnostics)
: _boundScript(std::move(boundScript)),
Diagnostics(std::move(diagnostics)),
_scriptVariables(new map<Utilities::HashedString, shared_ptr<EvalValue>>())
{
_evaluator = new Evaluator(_scriptVariables);
}

View File

@ -3,11 +3,10 @@
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();
return false;
}
const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType *indexer) const{
const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType*) const{
if (_class == TypeClass::String){
return make_shared<ScriptType>(TypeClass::String);
}

View File

@ -1,3 +1,5 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
#define PORYGONLANG_SCRIPTTYPE_HPP
@ -97,6 +99,17 @@ namespace Porygon{
_hashValue = hashValue;
}
const bool CanBeIndexedWith(ScriptType* indexer) const final{
if (indexer -> GetClass() != TypeClass::Number)
return false;
auto num = dynamic_cast<NumericScriptType*>(indexer);
return !(num->IsAwareOfFloat() && num->IsFloat());
}
const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{
return make_shared<StringScriptType>(false, 0);
}
const bool IsKnownAtBind() const{
return _isKnownAtBind;
}
@ -110,14 +123,14 @@ namespace Porygon{
shared_ptr<ScriptType> _valueType;
// Consider adding a check whether the table actually contains a type if every key is static.
public:
explicit NumericalTableScriptType(shared_ptr<ScriptType> valueType) : ScriptType(TypeClass::Table){
_valueType = std::move(valueType);
explicit NumericalTableScriptType(shared_ptr<ScriptType> valueType)
: ScriptType(TypeClass::Table), _valueType(std::move(valueType)){
}
const bool CanBeIndexedWith(ScriptType* indexer) const final{
if (indexer -> GetClass() != TypeClass::Number)
return false;
auto num =(NumericScriptType*)indexer;
auto num = dynamic_cast<NumericScriptType*>(indexer);
return !(num->IsAwareOfFloat() && num->IsFloat());
}

View File

@ -31,7 +31,7 @@ namespace Porygon{
}
const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{
auto stringKey = (StringScriptType*)indexer;
auto stringKey = dynamic_cast<StringScriptType*>(indexer);
if (stringKey->IsKnownAtBind()){
return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType();
}

View File

@ -12,10 +12,8 @@ namespace Porygon::UserData{
Evaluation::EvalValue* (*_get)(void* obj);
void (*_set)(void* obj, Evaluation::EvalValue* val);
public:
UserDataField(ScriptType* type, Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, Evaluation::EvalValue* val)){
_type = shared_ptr<ScriptType>(type);
_get = getter;
_set = setter;
UserDataField(ScriptType* type, Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, Evaluation::EvalValue* val))
: _type(shared_ptr<ScriptType>(type)), _get(getter), _set(setter){
}
shared_ptr<ScriptType> GetType(){

View File

@ -24,7 +24,7 @@ namespace Porygon::UserData {
if (indexer->GetClass() != TypeClass::String) {
return false;
}
auto str = (StringScriptType *) indexer;
auto str = dynamic_cast<StringScriptType*>(indexer);
if (!str->IsKnownAtBind())
return false;
return _userData->ContainsField(str->GetHashValue());
@ -39,7 +39,7 @@ namespace Porygon::UserData {
}
const shared_ptr<ScriptType> GetIndexedType(ScriptType *indexer) const final {
auto stringKey = (StringScriptType *) indexer;
auto stringKey = dynamic_cast<StringScriptType*>(indexer);
if (stringKey->IsKnownAtBind()) {
return _userData->GetField(stringKey->GetHashValue())->GetType();
}