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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -382,7 +382,7 @@ namespace Porygon::Evaluation {
auto opt = func->GetOption(option->GetOptionId()); auto opt = func->GetOption(option->GetOptionId());
if (option -> IsScriptFunction()){ if (option -> IsScriptFunction()){
auto parameterTypes = option->GetParameterTypes(); auto parameterTypes = option->GetParameterTypes();
auto scriptFunctionType = (ScriptFunctionOption*)option; auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option);
auto parameterKeys = scriptFunctionType->GetParameterKeys(); auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope; auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(opt); auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(opt);
@ -413,7 +413,7 @@ namespace Porygon::Evaluation {
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function, const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters) { const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType()); 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 parameterTypes = option->GetParameterTypes();
auto parameterKeys = option->GetParameterKeys(); auto parameterKeys = option->GetParameterKeys();

View File

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

View File

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

View File

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

View File

@ -73,16 +73,13 @@ namespace Porygon::Parser {
class ParsedBlockStatement : public ParsedStatement { class ParsedBlockStatement : public ParsedStatement {
const std::vector<const ParsedStatement *> _statements; const std::vector<const ParsedStatement *> _statements;
public: public:
explicit ParsedBlockStatement(std::vector<const ParsedStatement *> statements) explicit ParsedBlockStatement(const std::vector<const ParsedStatement *>& statements)
: ParsedStatement(statements.front()->GetStartPosition(), : ParsedStatement(statements.front()->GetStartPosition(),
statements.back()->GetEndPosition() - statements.front()->GetStartPosition()), statements.back()->GetEndPosition() - statements.front()->GetStartPosition()),
_statements(statements) {} _statements(statements) {}
ParsedBlockStatement(std::vector<const ParsedStatement *> statements, unsigned int start) : ParsedStatement( ParsedBlockStatement(std::vector<const ParsedStatement *> statements, unsigned int start) : ParsedStatement(
start, 0), start, 0), _statements(std::move(statements)) {
_statements(
std::move(
statements)) {
} }

View File

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

View File

@ -34,7 +34,7 @@ namespace Porygon::Parser {
ParsedStatement *ParseStatement(const IToken *current); ParsedStatement *ParseStatement(const IToken *current);
ParsedStatement *ParseVariableAssignment(const IToken *current); ParsedStatement *ParseVariableAssignment(const IToken *current);
ParsedStatement *ParseIndexAssignment(ParsedExpression *indexer); 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 *ParseFunctionDeclaration(const IToken *current);
ParsedStatement *ParseReturnStatement(const IToken *current); ParsedStatement *ParseReturnStatement(const IToken *current);
ParsedStatement *ParseIfStatement(const IToken *current); ParsedStatement *ParseIfStatement(const IToken *current);
@ -57,10 +57,8 @@ namespace Porygon::Parser {
public: public:
ParsedScriptStatement *Parse(); ParsedScriptStatement *Parse();
explicit Parser(const vector<const IToken *> &tokens, Porygon::Script *scriptData) { explicit Parser(const vector<const IToken *> &tokens, Porygon::Script *scriptData) : _tokens(tokens), _position(0),
_tokens = tokens; ScriptData(scriptData){
_position = 0;
ScriptData = scriptData;
} }
}; };

View File

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

View File

@ -3,11 +3,10 @@
namespace Porygon{ namespace Porygon{
const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{ const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{
// String type is the only simple script type we want to return false;
return _class == TypeClass::String && indexer->_class == TypeClass::Number && !((NumericScriptType*)indexer)->IsFloat();
} }
const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType *indexer) const{ const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType*) const{
if (_class == TypeClass::String){ if (_class == TypeClass::String){
return make_shared<ScriptType>(TypeClass::String); return make_shared<ScriptType>(TypeClass::String);
} }

View File

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

View File

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

View File

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

View File

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