Large overhaul of pointers to shared_ptrs, implemented function evaluation
This commit is contained in:
parent
8b70eed516
commit
4408cf00cd
|
@ -51,8 +51,8 @@ BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *statement){
|
||||||
auto boundExpression = this->BindExpression(s->GetExpression());
|
auto boundExpression = this->BindExpression(s->GetExpression());
|
||||||
VariableAssignment assignment =
|
VariableAssignment assignment =
|
||||||
s->IsLocal() ?
|
s->IsLocal() ?
|
||||||
this->_scope->CreateExplicitLocal(s->GetIdentifier().GetHash(), *boundExpression->GetType())
|
this->_scope->CreateExplicitLocal(s->GetIdentifier().GetHash(), boundExpression->GetType())
|
||||||
: this->_scope->AssignVariable(s->GetIdentifier().GetHash(), *boundExpression->GetType());
|
: this->_scope->AssignVariable(s->GetIdentifier().GetHash(), boundExpression->GetType());
|
||||||
if (assignment.GetResult() == VariableAssignmentResult::Ok){
|
if (assignment.GetResult() == VariableAssignmentResult::Ok){
|
||||||
auto key = assignment.GetKey();
|
auto key = assignment.GetKey();
|
||||||
return new BoundAssignmentStatement(key, boundExpression);
|
return new BoundAssignmentStatement(key, boundExpression);
|
||||||
|
@ -63,28 +63,28 @@ BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *statement){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptType* ParseTypeIdentifier(HashedString s){
|
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
|
||||||
switch (s.GetHash()){
|
switch (s.GetHash()){
|
||||||
case HashedString::ConstHash("number"): return new NumericScriptType(false, false);
|
case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false);
|
||||||
case HashedString::ConstHash("bool"): return new ScriptType(TypeClass::Bool);
|
case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool);
|
||||||
case HashedString::ConstHash("string"): return new ScriptType(TypeClass::String);
|
case HashedString::ConstHash("string"): return std::make_shared<ScriptType>(TypeClass::String);
|
||||||
default: return new ScriptType(TypeClass::Error); // todo: change to userdata
|
default: return std::make_shared<ScriptType>(TypeClass::Error); // todo: change to userdata
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) {
|
BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) {
|
||||||
auto functionStatement = (ParsedFunctionDeclarationStatement*) statement;
|
auto functionStatement = (ParsedFunctionDeclarationStatement*) statement;
|
||||||
auto parameters = functionStatement->GetParameters();
|
auto parameters = functionStatement->GetParameters();
|
||||||
vector<std::shared_ptr<ScriptType>> parameterTypes = vector<std::shared_ptr<ScriptType>>(parameters.size());
|
auto parameterTypes = new vector<std::shared_ptr<ScriptType>>(parameters.size());
|
||||||
vector<std::shared_ptr<BoundVariableKey>> parameterKeys = vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
|
auto parameterKeys = new vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
|
||||||
this->_scope->GoInnerScope();
|
this->_scope->GoInnerScope();
|
||||||
for (int i = 0; i < parameters.size(); i++){
|
for (int i = 0; i < parameters.size(); i++){
|
||||||
auto var = parameters[i];
|
auto var = parameters[i];
|
||||||
auto parsedType = ParseTypeIdentifier(var->GetType());
|
auto parsedType = ParseTypeIdentifier(var->GetType());
|
||||||
parameterTypes[i] = std::shared_ptr<ScriptType>(parsedType);
|
parameterTypes->at(i) = parsedType;
|
||||||
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), *parsedType);
|
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
|
||||||
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
|
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
|
||||||
parameterKeys[i] = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
|
parameterKeys -> at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
//TODO: log error
|
//TODO: log error
|
||||||
|
@ -95,8 +95,10 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem
|
||||||
this->_scope->GoOuterScope();
|
this->_scope->GoOuterScope();
|
||||||
auto identifier = functionStatement->GetIdentifier();
|
auto identifier = functionStatement->GetIdentifier();
|
||||||
auto returnType = std::make_shared<ScriptType>(TypeClass::Nil);
|
auto returnType = std::make_shared<ScriptType>(TypeClass::Nil);
|
||||||
auto type = new FunctionScriptType(returnType, parameterTypes, parameterKeys);
|
auto parameterTypesPtr = std::shared_ptr<std::vector<std::shared_ptr<ScriptType>>>(parameterTypes);
|
||||||
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), *type);
|
auto parameterKeysPtr = std::shared_ptr<std::vector<std::shared_ptr<BoundVariableKey>>>(parameterKeys);
|
||||||
|
auto type = make_shared<FunctionScriptType>(returnType, parameterTypesPtr, parameterKeysPtr);
|
||||||
|
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
|
||||||
if (assignment.GetResult() == VariableAssignmentResult::Ok){
|
if (assignment.GetResult() == VariableAssignmentResult::Ok){
|
||||||
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);
|
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);
|
||||||
}
|
}
|
||||||
|
@ -123,6 +125,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
|
||||||
|
|
||||||
case ParsedExpressionKind ::Parenthesized:
|
case ParsedExpressionKind ::Parenthesized:
|
||||||
return BindExpression(((ParenthesizedExpression*)expression)->GetInnerExpression());
|
return BindExpression(((ParenthesizedExpression*)expression)->GetInnerExpression());
|
||||||
|
case ParsedExpressionKind ::FunctionCall:
|
||||||
|
return this->BindFunctionCall((FunctionCallExpression*)expression);
|
||||||
|
|
||||||
case ParsedExpressionKind ::Bad:
|
case ParsedExpressionKind ::Bad:
|
||||||
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
|
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
|
||||||
|
@ -151,84 +155,89 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
|
||||||
switch (expression->GetOperatorKind()){
|
switch (expression->GetOperatorKind()){
|
||||||
case BinaryOperatorKind ::Addition:
|
case BinaryOperatorKind ::Addition:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
|
||||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
|
||||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||||
BoundBinaryOperation::Addition, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
BoundBinaryOperation::Addition,
|
||||||
|
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, new NumericScriptType(false, false),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition,
|
||||||
|
std::make_shared<NumericScriptType>(false, false),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
} else if (boundLeftType->GetClass() == TypeClass::String){
|
} else if (boundLeftType->GetClass() == TypeClass::String){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, new ScriptType(TypeClass::String),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared<ScriptType>(TypeClass::String),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BinaryOperatorKind ::Subtraction:
|
case BinaryOperatorKind ::Subtraction:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
|
||||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
|
||||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||||
BoundBinaryOperation::Subtraction,
|
BoundBinaryOperation::Subtraction,
|
||||||
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, new NumericScriptType(false, false),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction,
|
||||||
|
std::make_shared<NumericScriptType>(false, false),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BinaryOperatorKind ::Multiplication:
|
case BinaryOperatorKind ::Multiplication:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
|
||||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
|
||||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||||
BoundBinaryOperation::Multiplication,
|
BoundBinaryOperation::Multiplication,
|
||||||
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, new NumericScriptType(false, false),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication,
|
||||||
|
std::make_shared<NumericScriptType>(false, false),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BinaryOperatorKind ::Division:
|
case BinaryOperatorKind ::Division:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
|
||||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
|
||||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||||
BoundBinaryOperation::Division,
|
BoundBinaryOperation::Division,
|
||||||
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, new NumericScriptType(false, false),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division,
|
||||||
|
std::make_shared<NumericScriptType>(false, false),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BinaryOperatorKind ::Equality:
|
case BinaryOperatorKind ::Equality:
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, std::make_shared<ScriptType>(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
case BinaryOperatorKind ::Inequality:
|
case BinaryOperatorKind ::Inequality:
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, std::make_shared<ScriptType>(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
case BinaryOperatorKind ::LogicalAnd:
|
case BinaryOperatorKind ::LogicalAnd:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, std::make_shared<ScriptType>(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
break;
|
break;
|
||||||
case BinaryOperatorKind ::LogicalOr:
|
case BinaryOperatorKind ::LogicalOr:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, std::make_shared<ScriptType>(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -248,14 +257,16 @@ BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){
|
||||||
break;
|
break;
|
||||||
case UnaryOperatorKind ::Negation:
|
case UnaryOperatorKind ::Negation:
|
||||||
if (operandType->GetClass() == TypeClass::Number){
|
if (operandType->GetClass() == TypeClass::Number){
|
||||||
auto innerType = (NumericScriptType*)operandType;
|
auto innerType = std::dynamic_pointer_cast<NumericScriptType>(operandType);
|
||||||
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, new NumericScriptType(innerType->IsAwareOfFloat(),
|
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
|
||||||
innerType->IsFloat()), expression->GetStartPosition(), expression->GetLength());
|
std::make_shared<NumericScriptType>(innerType.get()->IsAwareOfFloat(), innerType.get()->IsFloat()),
|
||||||
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case UnaryOperatorKind ::LogicalNegation:
|
case UnaryOperatorKind ::LogicalNegation:
|
||||||
if (operandType->GetClass() == TypeClass::Bool){
|
if (operandType->GetClass() == TypeClass::Bool){
|
||||||
return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation, new ScriptType(TypeClass::Bool),
|
return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation,
|
||||||
|
std::make_shared<ScriptType>(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -267,4 +278,34 @@ BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){
|
||||||
|
auto functionExpression = BindExpression(expression->GetFunction());
|
||||||
|
auto type = functionExpression->GetType();
|
||||||
|
if (type->GetClass() != TypeClass::Function){
|
||||||
|
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ExpressionIsNotAFunction, expression->GetStartPosition(),
|
||||||
|
expression->GetLength());
|
||||||
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
||||||
|
}
|
||||||
|
auto functionType = std::dynamic_pointer_cast<FunctionScriptType>(type);
|
||||||
|
auto parameterTypes = functionType->GetParameterTypes();
|
||||||
|
auto givenParameters = expression->GetParameters();
|
||||||
|
if (parameterTypes->size() != givenParameters.size()){
|
||||||
|
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, expression->GetStartPosition(),
|
||||||
|
expression->GetLength());
|
||||||
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
||||||
|
}
|
||||||
|
vector<BoundExpression*> boundParameters = vector<BoundExpression*>(givenParameters.size());
|
||||||
|
for (int i = 0; i < givenParameters.size(); i++){
|
||||||
|
auto parameter = givenParameters[i];
|
||||||
|
auto boundParameter = this -> BindExpression(parameter);
|
||||||
|
if (boundParameter->GetType().get()->operator!=(parameterTypes.get()-> at(i).get())){
|
||||||
|
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, parameter->GetStartPosition(),
|
||||||
|
parameter->GetLength());
|
||||||
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
||||||
|
}
|
||||||
|
boundParameters[i] = boundParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BoundFunctionCallExpression(functionExpression, boundParameters, functionType.get()->GetReturnType(),
|
||||||
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ class Binder {
|
||||||
BoundExpression *BindVariableExpression(VariableExpression *expression);
|
BoundExpression *BindVariableExpression(VariableExpression *expression);
|
||||||
BoundExpression *BindBinaryOperator(BinaryExpression *expression);
|
BoundExpression *BindBinaryOperator(BinaryExpression *expression);
|
||||||
BoundExpression *BindUnaryOperator(UnaryExpression *expression);
|
BoundExpression *BindUnaryOperator(UnaryExpression *expression);
|
||||||
|
BoundExpression *BindFunctionCall(FunctionCallExpression *expression);
|
||||||
public:
|
public:
|
||||||
static BoundScriptStatement* Bind(Script* script, ParsedScriptStatement* s, BoundScope* scriptScope);
|
static BoundScriptStatement* Bind(Script* script, ParsedScriptStatement* s, BoundScope* scriptScope);
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
|
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||||
#define PORYGONLANG_BOUNDEXPRESSION_HPP
|
#define PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
#include "../../ScriptType.hpp"
|
#include "../../ScriptType.hpp"
|
||||||
#include "../BoundOperators.hpp"
|
#include "../BoundOperators.hpp"
|
||||||
#include "../BoundVariables/BoundVariableKey.hpp"
|
#include "../BoundVariables/BoundVariableKey.hpp"
|
||||||
|
@ -22,24 +25,23 @@ enum class BoundExpressionKind{
|
||||||
|
|
||||||
Unary,
|
Unary,
|
||||||
Binary,
|
Binary,
|
||||||
|
FunctionCall,
|
||||||
};
|
};
|
||||||
|
|
||||||
class BoundExpression{
|
class BoundExpression{
|
||||||
unsigned int _start;
|
unsigned int _start;
|
||||||
unsigned int _length;
|
unsigned int _length;
|
||||||
ScriptType* _type;
|
std::shared_ptr<ScriptType> _type;
|
||||||
public:
|
public:
|
||||||
BoundExpression(unsigned int start, unsigned int length, ScriptType* type){
|
BoundExpression(unsigned int start, unsigned int length, std::shared_ptr<ScriptType> type){
|
||||||
_start = start;
|
_start = start;
|
||||||
_length = length;
|
_length = length;
|
||||||
_type = type;
|
_type = type;
|
||||||
}
|
}
|
||||||
virtual ~BoundExpression(){
|
virtual ~BoundExpression() = default;
|
||||||
delete _type;
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual BoundExpressionKind GetKind() = 0;
|
virtual BoundExpressionKind GetKind() = 0;
|
||||||
virtual ScriptType* GetType(){
|
virtual std::shared_ptr<ScriptType> GetType(){
|
||||||
return _type;
|
return _type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -56,7 +58,7 @@ public:
|
||||||
|
|
||||||
class BoundBadExpression : public BoundExpression{
|
class BoundBadExpression : public BoundExpression{
|
||||||
public:
|
public:
|
||||||
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, new ScriptType(TypeClass::Error)){}
|
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Error)){}
|
||||||
|
|
||||||
BoundExpressionKind GetKind() final{
|
BoundExpressionKind GetKind() final{
|
||||||
return BoundExpressionKind ::Bad;
|
return BoundExpressionKind ::Bad;
|
||||||
|
@ -67,7 +69,7 @@ class BoundLiteralIntegerExpression : public BoundExpression{
|
||||||
long _value;
|
long _value;
|
||||||
public:
|
public:
|
||||||
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
|
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, new NumericScriptType(true, false)){
|
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)){
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +86,7 @@ class BoundLiteralFloatExpression : public BoundExpression{
|
||||||
double _value;
|
double _value;
|
||||||
public:
|
public:
|
||||||
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
|
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, new NumericScriptType(true, true)){
|
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)){
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +103,7 @@ class BoundLiteralStringExpression : public BoundExpression{
|
||||||
string _value;
|
string _value;
|
||||||
public:
|
public:
|
||||||
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
|
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, new ScriptType(TypeClass::String)){
|
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::String)){
|
||||||
_value = std::move(value);
|
_value = std::move(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +120,7 @@ class BoundLiteralBoolExpression : public BoundExpression{
|
||||||
bool _value;
|
bool _value;
|
||||||
public:
|
public:
|
||||||
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
|
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, new ScriptType(TypeClass::Bool)){
|
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)){
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,20 +136,15 @@ public:
|
||||||
class BoundVariableExpression : public BoundExpression{
|
class BoundVariableExpression : public BoundExpression{
|
||||||
int _scope;
|
int _scope;
|
||||||
int _id;
|
int _id;
|
||||||
ScriptType _type;
|
|
||||||
public:
|
public:
|
||||||
BoundVariableExpression(int scope, int id, const ScriptType& type, unsigned int start, unsigned int length)
|
BoundVariableExpression(int scope, int id, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, nullptr), _type(type){
|
: BoundExpression(start, length, std::move(type)){
|
||||||
_scope = scope;
|
_scope = scope;
|
||||||
_id = id;
|
_id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
~BoundVariableExpression() override = default;
|
~BoundVariableExpression() override = default;
|
||||||
|
|
||||||
ScriptType* GetType() final{
|
|
||||||
return &_type;
|
|
||||||
};
|
|
||||||
|
|
||||||
BoundExpressionKind GetKind() final{
|
BoundExpressionKind GetKind() final{
|
||||||
return BoundExpressionKind ::Variable;
|
return BoundExpressionKind ::Variable;
|
||||||
}
|
}
|
||||||
|
@ -166,7 +163,7 @@ class BoundBinaryExpression : public BoundExpression {
|
||||||
BoundExpression* _right;
|
BoundExpression* _right;
|
||||||
BoundBinaryOperation _operation;
|
BoundBinaryOperation _operation;
|
||||||
public:
|
public:
|
||||||
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, ScriptType* result,
|
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr<ScriptType> result,
|
||||||
unsigned int start, unsigned int length)
|
unsigned int start, unsigned int length)
|
||||||
: BoundExpression(start, length, result){
|
: BoundExpression(start, length, result){
|
||||||
_left = left;
|
_left = left;
|
||||||
|
@ -199,7 +196,7 @@ class BoundUnaryExpression : public BoundExpression {
|
||||||
BoundExpression* _operand;
|
BoundExpression* _operand;
|
||||||
BoundUnaryOperation _operation;
|
BoundUnaryOperation _operation;
|
||||||
public:
|
public:
|
||||||
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, 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, result){
|
:BoundExpression(start, length, result){
|
||||||
_operand = operand;
|
_operand = operand;
|
||||||
_operation = op;
|
_operation = op;
|
||||||
|
@ -222,6 +219,33 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class BoundFunctionCallExpression : public BoundExpression {
|
||||||
|
BoundExpression* _functionExpression;
|
||||||
|
vector<BoundExpression*> _parameters;
|
||||||
|
public:
|
||||||
|
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result,
|
||||||
|
unsigned int start, unsigned int length)
|
||||||
|
: BoundExpression(start, length, result), _functionExpression(functionExpression), _parameters(std::move(parameters)) {}
|
||||||
|
|
||||||
|
~BoundFunctionCallExpression() final{
|
||||||
|
delete _functionExpression;
|
||||||
|
for (auto p : _parameters){
|
||||||
|
delete p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BoundExpressionKind GetKind() final{
|
||||||
|
return BoundExpressionKind ::FunctionCall;
|
||||||
|
}
|
||||||
|
|
||||||
|
BoundExpression* GetFunctionExpression(){
|
||||||
|
return _functionExpression;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<BoundExpression*> GetParameters(){
|
||||||
|
return _parameters;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP
|
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
class BoundFunctionDeclarationStatement : public BoundStatement{
|
class BoundFunctionDeclarationStatement : public BoundStatement{
|
||||||
BoundVariableKey* _key;
|
BoundVariableKey* _key;
|
||||||
std::shared_ptr<BoundBlockStatement> _block;
|
std::shared_ptr<BoundBlockStatement> _block;
|
||||||
FunctionScriptType* _type;
|
std::shared_ptr<FunctionScriptType> _type;
|
||||||
public:
|
public:
|
||||||
BoundFunctionDeclarationStatement(FunctionScriptType* type, BoundVariableKey* key, BoundBlockStatement* block){
|
BoundFunctionDeclarationStatement(std::shared_ptr<FunctionScriptType> type, BoundVariableKey* key, BoundBlockStatement* block){
|
||||||
_key = key;
|
_key = key;
|
||||||
_block = shared_ptr<BoundBlockStatement>(block);
|
_block = shared_ptr<BoundBlockStatement>(block);
|
||||||
_type = type;
|
_type = type;
|
||||||
|
@ -18,7 +18,6 @@ public:
|
||||||
|
|
||||||
~BoundFunctionDeclarationStatement() final{
|
~BoundFunctionDeclarationStatement() final{
|
||||||
delete _key;
|
delete _key;
|
||||||
delete _type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BoundStatementKind GetKind() final{
|
BoundStatementKind GetKind() final{
|
||||||
|
@ -33,7 +32,7 @@ public:
|
||||||
return _block;
|
return _block;
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionScriptType* GetType(){
|
std::shared_ptr<FunctionScriptType> GetType(){
|
||||||
return _type;
|
return _type;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
#include "BoundScope.hpp"
|
#include "BoundScope.hpp"
|
||||||
|
|
||||||
|
@ -70,16 +72,16 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, const ScriptType& type) {
|
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type) {
|
||||||
auto scope = this->_localScope.at(this->_currentScope - 1);
|
auto scope = this->_localScope.at(this->_currentScope - 1);
|
||||||
if (scope -> find(identifier) != scope -> end()){
|
if (scope -> find(identifier) != scope -> end()){
|
||||||
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
|
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
|
||||||
}
|
}
|
||||||
scope -> insert({identifier, new BoundVariable(type)});
|
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(int identifier, const ScriptType& type) {
|
VariableAssignment BoundScope::AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type) {
|
||||||
int exists = this->Exists(identifier);
|
int exists = this->Exists(identifier);
|
||||||
if (exists == -1){
|
if (exists == -1){
|
||||||
// Creation
|
// Creation
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include "BoundVariable.hpp"
|
#include "BoundVariable.hpp"
|
||||||
#include "BoundVariableKey.hpp"
|
#include "BoundVariableKey.hpp"
|
||||||
#include "VariableAssigmentResult.hpp"
|
#include "VariableAssigmentResult.hpp"
|
||||||
|
@ -26,8 +27,8 @@ public:
|
||||||
|
|
||||||
int Exists(int key);
|
int Exists(int key);
|
||||||
BoundVariable* GetVariable(int scope, int identifier);
|
BoundVariable* GetVariable(int scope, int identifier);
|
||||||
VariableAssignment CreateExplicitLocal(int identifier, const ScriptType& type);
|
VariableAssignment CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type);
|
||||||
VariableAssignment AssignVariable(int identifier, const ScriptType& type);
|
VariableAssignment AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type);
|
||||||
|
|
||||||
int GetDeepestScope(){
|
int GetDeepestScope(){
|
||||||
return _deepestScope;
|
return _deepestScope;
|
||||||
|
|
|
@ -2,17 +2,20 @@
|
||||||
#ifndef PORYGONLANG_BOUNDVARIABLE_HPP
|
#ifndef PORYGONLANG_BOUNDVARIABLE_HPP
|
||||||
#define PORYGONLANG_BOUNDVARIABLE_HPP
|
#define PORYGONLANG_BOUNDVARIABLE_HPP
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include "../../ScriptType.hpp"
|
#include "../../ScriptType.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
class BoundVariable{
|
class BoundVariable{
|
||||||
ScriptType _type;
|
std::shared_ptr<ScriptType> _type;
|
||||||
public:
|
public:
|
||||||
explicit BoundVariable(const ScriptType& type) : _type(type){
|
explicit BoundVariable(std::shared_ptr<ScriptType> type) : _type(type){
|
||||||
}
|
}
|
||||||
~BoundVariable(){
|
~BoundVariable(){
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptType& GetType(){
|
std::shared_ptr<ScriptType> GetType(){
|
||||||
return _type;
|
return _type;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,6 +15,9 @@ enum class DiagnosticCode{
|
||||||
NoUnaryOperationFound,
|
NoUnaryOperationFound,
|
||||||
CantAssignVariable,
|
CantAssignVariable,
|
||||||
VariableNotFound,
|
VariableNotFound,
|
||||||
|
ExpressionIsNotAFunction,
|
||||||
|
ParameterCountMismatch,
|
||||||
|
ParameterTypeMismatch,
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP
|
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP
|
||||||
|
|
|
@ -6,12 +6,13 @@
|
||||||
#include "../EvaluationException.hpp"
|
#include "../EvaluationException.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class EvalValue{
|
class EvalValue{
|
||||||
public:
|
public:
|
||||||
EvalValue() = default;
|
EvalValue() = default;
|
||||||
virtual ~EvalValue() = default;
|
virtual ~EvalValue() = default;
|
||||||
virtual ScriptType* GetType() = 0;
|
virtual std::shared_ptr<ScriptType> GetType() = 0;
|
||||||
|
|
||||||
virtual bool operator ==(EvalValue* b) = 0;
|
virtual bool operator ==(EvalValue* b) = 0;
|
||||||
|
|
||||||
|
@ -37,22 +38,18 @@ public:
|
||||||
|
|
||||||
class BooleanEvalValue : public EvalValue{
|
class BooleanEvalValue : public EvalValue{
|
||||||
bool _value;
|
bool _value;
|
||||||
ScriptType* _type;
|
std::shared_ptr<ScriptType> _type;
|
||||||
public:
|
public:
|
||||||
explicit BooleanEvalValue(bool val){
|
explicit BooleanEvalValue(bool val){
|
||||||
_value = val;
|
_value = val;
|
||||||
_type = new ScriptType(TypeClass::Bool);
|
_type = std::make_shared<ScriptType>(TypeClass::Bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
EvalValue* Clone() final{
|
EvalValue* Clone() final{
|
||||||
return new BooleanEvalValue(_value);
|
return new BooleanEvalValue(_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
~BooleanEvalValue() final{
|
std::shared_ptr<ScriptType> GetType() final{
|
||||||
delete _type;
|
|
||||||
}
|
|
||||||
|
|
||||||
ScriptType* GetType() final{
|
|
||||||
return _type;
|
return _type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,13 +11,10 @@ class NumericEvalValue : public EvalValue{
|
||||||
virtual double GetFloatValue() = 0;
|
virtual double GetFloatValue() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ScriptType* _type;
|
std::shared_ptr<ScriptType> _type;
|
||||||
public:
|
public:
|
||||||
~NumericEvalValue() override{
|
|
||||||
delete _type;
|
|
||||||
};
|
|
||||||
virtual const bool IsFloat() = 0;
|
virtual const bool IsFloat() = 0;
|
||||||
ScriptType* GetType() override {
|
std::shared_ptr<ScriptType> GetType() override {
|
||||||
return _type;
|
return _type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +31,7 @@ class IntegerEvalValue : public NumericEvalValue{
|
||||||
double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
|
double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
|
||||||
public:
|
public:
|
||||||
explicit IntegerEvalValue(long value){
|
explicit IntegerEvalValue(long value){
|
||||||
_type = new NumericScriptType(true, false);
|
_type = std::make_shared<NumericScriptType>(true, false);
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
const bool IsFloat() final{
|
const bool IsFloat() final{
|
||||||
|
@ -62,7 +59,7 @@ class FloatEvalValue : public NumericEvalValue{
|
||||||
double GetFloatValue() final{return _value;}
|
double GetFloatValue() final{return _value;}
|
||||||
public:
|
public:
|
||||||
explicit FloatEvalValue(double value){
|
explicit FloatEvalValue(double value){
|
||||||
_type = new NumericScriptType(true, true);
|
_type = std::make_shared<NumericScriptType>(true, true);
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
const bool IsFloat() final{
|
const bool IsFloat() final{
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
||||||
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
||||||
|
|
||||||
|
@ -11,44 +13,32 @@
|
||||||
|
|
||||||
class ScriptFunctionEvalValue : public EvalValue{
|
class ScriptFunctionEvalValue : public EvalValue{
|
||||||
std::shared_ptr<BoundBlockStatement> _innerBlock;
|
std::shared_ptr<BoundBlockStatement> _innerBlock;
|
||||||
FunctionScriptType _type;
|
std::shared_ptr<FunctionScriptType> _type;
|
||||||
public:
|
public:
|
||||||
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, FunctionScriptType type)
|
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<FunctionScriptType> type)
|
||||||
: _type(std::move(type))
|
: _type(std::move(type))
|
||||||
{
|
{
|
||||||
_innerBlock = std::move(innerBlock);
|
_innerBlock = std::move(innerBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ScriptType> GetType() final{
|
||||||
|
return _type;
|
||||||
|
}
|
||||||
|
|
||||||
EvalValue* Clone() final{
|
EvalValue* Clone() final{
|
||||||
return new ScriptFunctionEvalValue(_innerBlock, _type);
|
return new ScriptFunctionEvalValue(_innerBlock, _type);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptType* GetType() final{
|
|
||||||
return &_type;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool operator ==(EvalValue* b) final{
|
bool operator ==(EvalValue* b) final{
|
||||||
if (b->GetType()->GetClass() != TypeClass::Function)
|
if (b->GetType()->GetClass() != TypeClass::Function)
|
||||||
return false;
|
return false;
|
||||||
return this->_innerBlock == ((ScriptFunctionEvalValue*)b)->_innerBlock;
|
return this->_innerBlock == ((ScriptFunctionEvalValue*)b)->_innerBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
EvalValue* EvaluateFunction(Evaluator* evaluator, const vector<EvalValue*>& parameters){
|
std::shared_ptr<BoundBlockStatement> GetInnerBlock(){
|
||||||
auto parameterTypes = _type.GetParameterTypes();
|
return _innerBlock;
|
||||||
auto parameterKeys = _type.GetParameterKeys();
|
|
||||||
auto scope = evaluator->GetScope();
|
|
||||||
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
|
|
||||||
auto parameter = parameters[i];
|
|
||||||
auto requiredType = parameterTypes[i];
|
|
||||||
if (parameter->GetType() != requiredType.get()){
|
|
||||||
throw EvaluationException("Passed wrong type to function.");
|
|
||||||
}
|
|
||||||
auto key = parameterKeys[i];
|
|
||||||
scope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
|
||||||
}
|
|
||||||
evaluator->EvaluateBlockStatement(_innerBlock.get());
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,17 +9,14 @@ using namespace std;
|
||||||
|
|
||||||
class StringEvalValue : public EvalValue{
|
class StringEvalValue : public EvalValue{
|
||||||
string _value;
|
string _value;
|
||||||
ScriptType* _type;
|
std::shared_ptr<ScriptType> _type;
|
||||||
public:
|
public:
|
||||||
explicit StringEvalValue(string s){
|
explicit StringEvalValue(string s){
|
||||||
_value = move(s);
|
_value = move(s);
|
||||||
_type = new ScriptType(TypeClass::String);
|
_type = std::make_shared<ScriptType>(TypeClass::String);
|
||||||
}
|
|
||||||
~StringEvalValue() final{
|
|
||||||
delete _type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptType* GetType() final{
|
std::shared_ptr<ScriptType> GetType() final{
|
||||||
return _type;
|
return _type;
|
||||||
};
|
};
|
||||||
bool operator ==(EvalValue* b) final{
|
bool operator ==(EvalValue* b) final{
|
||||||
|
|
|
@ -52,7 +52,7 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
|
||||||
auto type = statement->GetType();
|
auto type = statement->GetType();
|
||||||
auto key = statement->GetKey();
|
auto key = statement->GetKey();
|
||||||
auto block = statement->GetBlock();
|
auto block = statement->GetBlock();
|
||||||
auto value = new ScriptFunctionEvalValue(block, *type);
|
auto value = new ScriptFunctionEvalValue(block, type);
|
||||||
if (key->IsCreation()){
|
if (key->IsCreation()){
|
||||||
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
||||||
} else{
|
} else{
|
||||||
|
@ -66,6 +66,8 @@ EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
|
||||||
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
||||||
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
|
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
|
||||||
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
||||||
|
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
|
||||||
|
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
|
||||||
default: throw;
|
default: throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +83,7 @@ NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expressi
|
||||||
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
||||||
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
||||||
case BoundExpressionKind::Variable: return (NumericEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
case BoundExpressionKind::Variable: return (NumericEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||||
|
case BoundExpressionKind ::FunctionCall: return (NumericEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||||
|
|
||||||
case BoundExpressionKind ::LiteralString:
|
case BoundExpressionKind ::LiteralString:
|
||||||
case BoundExpressionKind ::LiteralBool:
|
case BoundExpressionKind ::LiteralBool:
|
||||||
|
@ -95,6 +98,7 @@ BooleanEvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression)
|
||||||
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
||||||
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
||||||
case BoundExpressionKind::Variable: return (BooleanEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
case BoundExpressionKind::Variable: return (BooleanEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||||
|
case BoundExpressionKind ::FunctionCall: return (BooleanEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||||
|
|
||||||
case BoundExpressionKind::Bad:
|
case BoundExpressionKind::Bad:
|
||||||
case BoundExpressionKind::LiteralInteger:
|
case BoundExpressionKind::LiteralInteger:
|
||||||
|
@ -112,7 +116,7 @@ StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression
|
||||||
case BoundExpressionKind::Binary:
|
case BoundExpressionKind::Binary:
|
||||||
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
||||||
case BoundExpressionKind::Variable: return (StringEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
case BoundExpressionKind::Variable: return (StringEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||||
|
case BoundExpressionKind ::FunctionCall: return (StringEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
||||||
|
|
||||||
case BoundExpressionKind::Bad:
|
case BoundExpressionKind::Bad:
|
||||||
case BoundExpressionKind::LiteralInteger:
|
case BoundExpressionKind::LiteralInteger:
|
||||||
|
@ -121,5 +125,47 @@ StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression
|
||||||
case BoundExpressionKind::Unary:
|
case BoundExpressionKind::Unary:
|
||||||
throw;
|
throw;
|
||||||
|
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EvalValue* Evaluator::EvaluateFunctionExpression(BoundExpression * expression){
|
||||||
|
switch (expression->GetKind()){
|
||||||
|
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
||||||
|
default: throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EvalValue* Evaluator::EvaluateNilExpression(BoundExpression * expression){
|
||||||
|
switch (expression->GetKind()){
|
||||||
|
case BoundExpressionKind ::FunctionCall:
|
||||||
|
return this->EvaluateFunctionCallExpression(expression);
|
||||||
|
default:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EvalValue* Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){
|
||||||
|
auto functionCall = (BoundFunctionCallExpression*)expression;
|
||||||
|
auto function = (ScriptFunctionEvalValue*)this->EvaluateExpression(functionCall->GetFunctionExpression());
|
||||||
|
auto boundParameters = functionCall->GetParameters();
|
||||||
|
auto parameters = vector<EvalValue*>(boundParameters.size());
|
||||||
|
for (int i = 0; i < boundParameters.size(); i++){
|
||||||
|
parameters[i] = this->EvaluateExpression(boundParameters[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
||||||
|
auto parameterTypes = type->GetParameterTypes();
|
||||||
|
auto parameterKeys = type->GetParameterKeys();
|
||||||
|
for (int i = 0; i < parameterTypes->size() && i < parameterKeys->size() && i < parameters.size(); i++){
|
||||||
|
auto parameter = parameters[i];
|
||||||
|
auto requiredType = parameterTypes->at(i);
|
||||||
|
if (*parameter->GetType() != requiredType.get()){
|
||||||
|
throw EvaluationException("Passed wrong type to function.");
|
||||||
|
}
|
||||||
|
auto key = parameterKeys->at(i);
|
||||||
|
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
||||||
|
}
|
||||||
|
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
#include "EvalValues/EvalValue.hpp"
|
#include "EvalValues/EvalValue.hpp"
|
||||||
#include "EvalValues/NumericEvalValue.hpp"
|
#include "EvalValues/NumericEvalValue.hpp"
|
||||||
#include "EvalValues/StringEvalValue.hpp"
|
#include "EvalValues/StringEvalValue.hpp"
|
||||||
|
#include "EvalValues/StringEvalValue.hpp"
|
||||||
#include "EvaluationScope/EvaluationScope.hpp"
|
#include "EvaluationScope/EvaluationScope.hpp"
|
||||||
|
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
|
@ -28,6 +29,8 @@ class Evaluator {
|
||||||
NumericEvalValue* EvaluateIntegerExpression(BoundExpression* expression);
|
NumericEvalValue* EvaluateIntegerExpression(BoundExpression* expression);
|
||||||
BooleanEvalValue* EvaluateBoolExpression(BoundExpression* expression);
|
BooleanEvalValue* EvaluateBoolExpression(BoundExpression* expression);
|
||||||
StringEvalValue* EvaluateStringExpression(BoundExpression* expression);
|
StringEvalValue* EvaluateStringExpression(BoundExpression* expression);
|
||||||
|
EvalValue* EvaluateFunctionExpression(BoundExpression *expression);
|
||||||
|
EvalValue *EvaluateNilExpression(BoundExpression *expression);
|
||||||
|
|
||||||
NumericEvalValue* EvaluateIntegerBinary(BoundBinaryExpression* expression);
|
NumericEvalValue* EvaluateIntegerBinary(BoundBinaryExpression* expression);
|
||||||
BooleanEvalValue *EvaluateBooleanBinary(BoundBinaryExpression *expression);
|
BooleanEvalValue *EvaluateBooleanBinary(BoundBinaryExpression *expression);
|
||||||
|
@ -35,6 +38,7 @@ class Evaluator {
|
||||||
|
|
||||||
NumericEvalValue* EvaluateIntegerUnary(BoundUnaryExpression* expression);
|
NumericEvalValue* EvaluateIntegerUnary(BoundUnaryExpression* expression);
|
||||||
BooleanEvalValue *EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
BooleanEvalValue *EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
||||||
|
EvalValue *EvaluateFunctionCallExpression(BoundExpression *expression);
|
||||||
|
|
||||||
EvalValue *GetVariable(BoundVariableExpression *expression);
|
EvalValue *GetVariable(BoundVariableExpression *expression);
|
||||||
public:
|
public:
|
||||||
|
@ -55,6 +59,8 @@ public:
|
||||||
EvaluationScope* GetScope(){
|
EvaluationScope* GetScope(){
|
||||||
return _evaluationScope;
|
return _evaluationScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -225,9 +225,8 @@ public:
|
||||||
FunctionCallExpression(ParsedExpression* function, vector<ParsedExpression*> parameters,
|
FunctionCallExpression(ParsedExpression* function, vector<ParsedExpression*> parameters,
|
||||||
unsigned int start, unsigned int length) : ParsedExpression(start, length){
|
unsigned int start, unsigned int length) : ParsedExpression(start, length){
|
||||||
_function = std::unique_ptr<ParsedExpression>(function);
|
_function = std::unique_ptr<ParsedExpression>(function);
|
||||||
_parameters.reserve(parameters.size());
|
|
||||||
for (int i = 0; i < parameters.size(); i++){
|
for (int i = 0; i < parameters.size(); i++){
|
||||||
_parameters[i] = std::unique_ptr<ParsedExpression>(parameters[i]);
|
_parameters.push_back(std::unique_ptr<ParsedExpression>(parameters[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
|
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
|
||||||
#define PORYGONLANG_SCRIPTTYPE_HPP
|
#define PORYGONLANG_SCRIPTTYPE_HPP
|
||||||
|
|
||||||
|
@ -8,6 +12,8 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "Binder/BoundVariables/BoundVariableKey.hpp"
|
#include "Binder/BoundVariables/BoundVariableKey.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
enum class TypeClass{
|
enum class TypeClass{
|
||||||
Error,
|
Error,
|
||||||
Nil,
|
Nil,
|
||||||
|
@ -32,11 +38,18 @@ public:
|
||||||
return _class;
|
return _class;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool operator ==(ScriptType b){
|
virtual bool operator ==(const ScriptType& b){
|
||||||
return _class == b._class;
|
return _class == b._class;
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual bool operator !=(ScriptType b){
|
virtual bool operator ==(ScriptType* b){
|
||||||
|
return _class == b->_class;
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual bool operator !=(const ScriptType& b){
|
||||||
|
return ! (operator==(b));
|
||||||
|
}
|
||||||
|
virtual bool operator !=(ScriptType* b){
|
||||||
return ! (operator==(b));
|
return ! (operator==(b));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -62,26 +75,26 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
class FunctionScriptType : public ScriptType{
|
class FunctionScriptType : public ScriptType{
|
||||||
std::shared_ptr<ScriptType> _returnType;
|
shared_ptr<ScriptType> _returnType;
|
||||||
std::vector<std::shared_ptr<ScriptType>> _parameterTypes;
|
shared_ptr<vector<shared_ptr<ScriptType>>> _parameterTypes;
|
||||||
std::vector<std::shared_ptr<BoundVariableKey>> _parameterKeys;
|
shared_ptr<vector<shared_ptr<BoundVariableKey>>> _parameterKeys;
|
||||||
public:
|
public:
|
||||||
FunctionScriptType(std::shared_ptr<ScriptType> returnType, std::vector<std::shared_ptr<ScriptType>> parameterTypes,
|
FunctionScriptType(std::shared_ptr<ScriptType> returnType, shared_ptr<vector<shared_ptr<ScriptType>>> parameterTypes,
|
||||||
std::vector<std::shared_ptr<BoundVariableKey>> parameterKeys)
|
shared_ptr<vector<shared_ptr<BoundVariableKey>>> parameterKeys)
|
||||||
: ScriptType(TypeClass::Function){
|
: ScriptType(TypeClass::Function){
|
||||||
_returnType = std::move(returnType);
|
_returnType = std::move(returnType);
|
||||||
_parameterTypes = std::move(parameterTypes);
|
_parameterTypes = std::move(parameterTypes);
|
||||||
_parameterKeys = std::move(parameterKeys);
|
_parameterKeys = std::move(parameterKeys);
|
||||||
}
|
}
|
||||||
ScriptType* GetReturnType(){
|
shared_ptr<ScriptType> GetReturnType(){
|
||||||
return _returnType.get();
|
return _returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<ScriptType>> GetParameterTypes(){
|
shared_ptr<vector<shared_ptr<ScriptType>>> GetParameterTypes(){
|
||||||
return _parameterTypes;
|
return _parameterTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<BoundVariableKey>> GetParameterKeys(){
|
shared_ptr<vector<shared_ptr<BoundVariableKey>>> GetParameterKeys(){
|
||||||
return _parameterKeys;
|
return _parameterKeys;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,4 +12,17 @@ TEST_CASE( "Define script function", "[integration]" ) {
|
||||||
delete script;
|
delete script;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Define script function and call", "[integration]" ) {
|
||||||
|
Script* script = Script::Create("function add(number a, number b) result = a + b end add(1, 2)");
|
||||||
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
||||||
|
script->Evaluate();
|
||||||
|
auto variable = script->GetVariable("add");
|
||||||
|
REQUIRE(variable != nullptr);
|
||||||
|
REQUIRE(variable->GetType()->GetClass() == TypeClass::Function);
|
||||||
|
auto result = script->GetVariable("result");
|
||||||
|
REQUIRE(result->GetType()->GetClass() == TypeClass::Number);
|
||||||
|
REQUIRE(result->EvaluateInteger() == 3);
|
||||||
|
delete script;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue