Implements parsing of function calling

This commit is contained in:
Deukhoofd 2019-06-01 14:56:28 +02:00
parent 1231a77761
commit 8b70eed516
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 59 additions and 10 deletions

View File

@ -1,6 +1,3 @@
#include <memory>
#include "Binder.hpp"
#include <memory>

View File

@ -1,8 +1,8 @@
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#include <memory>
#include <utility>
#include "../../ScriptType.hpp"
#include "EvalValue.hpp"
#include "../../Binder/BoundStatements/BoundStatement.hpp"
@ -13,10 +13,10 @@ class ScriptFunctionEvalValue : public EvalValue{
std::shared_ptr<BoundBlockStatement> _innerBlock;
FunctionScriptType _type;
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, const FunctionScriptType& type)
: _type(type)
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, FunctionScriptType type)
: _type(std::move(type))
{
_innerBlock = innerBlock;
_innerBlock = std::move(innerBlock);
}
EvalValue* Clone() final{

View File

@ -1,7 +1,9 @@
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
#define PORYGONLANG_PARSEDEXPRESSION_HPP
#include <utility>
#include <memory>
#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<ParsedExpression> _function;
vector<std::unique_ptr<ParsedExpression>> _parameters;
public:
FunctionCallExpression(ParsedExpression* function, vector<ParsedExpression*> parameters,
unsigned int start, unsigned int length) : ParsedExpression(start, length){
_function = std::unique_ptr<ParsedExpression>(function);
_parameters.reserve(parameters.size());
for (int i = 0; i < parameters.size(); i++){
_parameters[i] = std::unique_ptr<ParsedExpression>(parameters[i]);
}
}
ParsedExpressionKind GetKind() final{
return ParsedExpressionKind::FunctionCall;
}
ParsedExpression* GetFunction(){
return _function.get();
}
vector<ParsedExpression*> GetParameters(){
auto par = vector<ParsedExpression*>(_parameters.size(), nullptr);
for (int i = 0; i < _parameters.size(); i++){
par[i] = _parameters[i].get();
}
return par;
}
};

View File

@ -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<ParsedExpression*> 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);
}

View File

@ -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<IToken*> tokens, Script* scriptData){