From 8b70eed516eec14bba17efc495355a872b4d6395 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 1 Jun 2019 14:56:28 +0200 Subject: [PATCH] Implements parsing of function calling --- src/Binder/Binder.cpp | 3 -- .../EvalValues/ScriptFunctionEvalValue.hpp | 8 ++--- .../ParsedExpressions/ParsedExpression.hpp | 31 +++++++++++++++++-- src/Parser/Parser.cpp | 26 +++++++++++++++- src/Parser/Parser.hpp | 1 + 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index eed6ac0..7ecd1b8 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -1,6 +1,3 @@ -#include - - #include "Binder.hpp" #include diff --git a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp index bee9b26..56976e6 100644 --- a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp +++ b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp @@ -1,8 +1,8 @@ - #ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP #include +#include #include "../../ScriptType.hpp" #include "EvalValue.hpp" #include "../../Binder/BoundStatements/BoundStatement.hpp" @@ -13,10 +13,10 @@ class ScriptFunctionEvalValue : public EvalValue{ std::shared_ptr _innerBlock; FunctionScriptType _type; public: - explicit ScriptFunctionEvalValue(std::shared_ptr innerBlock, const FunctionScriptType& type) - : _type(type) + explicit ScriptFunctionEvalValue(std::shared_ptr innerBlock, FunctionScriptType type) + : _type(std::move(type)) { - _innerBlock = innerBlock; + _innerBlock = std::move(innerBlock); } EvalValue* Clone() final{ diff --git a/src/Parser/ParsedExpressions/ParsedExpression.hpp b/src/Parser/ParsedExpressions/ParsedExpression.hpp index 933ff2e..8e863fc 100644 --- a/src/Parser/ParsedExpressions/ParsedExpression.hpp +++ b/src/Parser/ParsedExpressions/ParsedExpression.hpp @@ -1,7 +1,9 @@ - #ifndef PORYGONLANG_PARSEDEXPRESSION_HPP #define PORYGONLANG_PARSEDEXPRESSION_HPP +#include +#include + #include "../Token.hpp" #include "../UnaryOperatorKind.hpp" #include "../BinaryOperatorKind.hpp" @@ -19,6 +21,7 @@ enum class ParsedExpressionKind{ Unary, Binary, Parenthesized, + FunctionCall, }; class ParsedExpression { @@ -216,8 +219,32 @@ public: }; class FunctionCallExpression : public ParsedExpression{ - + std::unique_ptr _function; + vector> _parameters; public: + FunctionCallExpression(ParsedExpression* function, vector parameters, + unsigned int start, unsigned int length) : ParsedExpression(start, length){ + _function = std::unique_ptr(function); + _parameters.reserve(parameters.size()); + for (int i = 0; i < parameters.size(); i++){ + _parameters[i] = std::unique_ptr(parameters[i]); + } + } + + ParsedExpressionKind GetKind() final{ + return ParsedExpressionKind::FunctionCall; + } + + ParsedExpression* GetFunction(){ + return _function.get(); + } + vector GetParameters(){ + auto par = vector(_parameters.size(), nullptr); + for (int i = 0; i < _parameters.size(); i++){ + par[i] = _parameters[i].get(); + } + return par; + } }; diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index e241f75..b9e4bbd 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -1,5 +1,6 @@ #include "Parser.hpp" +#include "ParsedStatements/ParsedStatement.hpp" #include "UnaryOperatorKind.hpp" #include "BinaryOperatorKind.hpp" #include "TypedVariableIdentifier.hpp" @@ -137,7 +138,8 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) { ParsedExpression* Parser::ParseExpression(IToken* current){ auto expression = this -> ParseBinaryExpression(current, OperatorPrecedence::No); - if (this -> Peek() -> GetKind() == TokenKind::OpenParenthesis){ + while (this -> Peek() -> GetKind() == TokenKind::OpenParenthesis){ + expression = this->ParseFunctionCallExpression(expression); //TODO: Function Evaluation } return expression; @@ -250,6 +252,28 @@ ParsedExpression *Parser::ParseParenthesizedExpression(IToken *current) { return new ParenthesizedExpression(expression, start, closeToken->GetEndPosition() - start); } +ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* functionExpression) { + this -> Next(); // consume the open parenthesis + vector parameters; + auto peeked = this -> Peek(); + auto peekedKind = peeked->GetKind(); + while (peekedKind != TokenKind::CloseParenthesis){ + if (peekedKind == TokenKind ::EndOfFile){ + this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), peeked->GetLength()); + return new BadExpression(peeked->GetStartPosition(), peeked->GetLength()); + } + parameters.push_back(this->ParseExpression(this->Next())); + peeked = this -> Next() ; + peekedKind = peeked->GetKind(); + if (peekedKind != TokenKind::CloseParenthesis && peekedKind != TokenKind::CommaToken){ + this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), peeked->GetLength()); + return new BadExpression(peeked->GetStartPosition(), peeked->GetLength()); + } + } + auto start = functionExpression->GetStartPosition(); + return new FunctionCallExpression(functionExpression, parameters, start, peeked->GetEndPosition() - start); +} + diff --git a/src/Parser/Parser.hpp b/src/Parser/Parser.hpp index 2c497ab..8a51bb6 100644 --- a/src/Parser/Parser.hpp +++ b/src/Parser/Parser.hpp @@ -34,6 +34,7 @@ class Parser { ParsedExpression* ParseBinaryExpression(IToken* current, OperatorPrecedence parentPrecedence); ParsedExpression* ParsePrimaryExpression(IToken* current); ParsedExpression* ParseParenthesizedExpression(IToken *current); + ParsedExpression* ParseFunctionCallExpression(ParsedExpression* functionExpression); public: ParsedScriptStatement* Parse(); explicit Parser(vector tokens, Script* scriptData){