Implements parsing of function calling
This commit is contained in:
parent
1231a77761
commit
8b70eed516
|
@ -1,6 +1,3 @@
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
|
|
||||||
#include "Binder.hpp"
|
#include "Binder.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
||||||
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
#include "../../ScriptType.hpp"
|
#include "../../ScriptType.hpp"
|
||||||
#include "EvalValue.hpp"
|
#include "EvalValue.hpp"
|
||||||
#include "../../Binder/BoundStatements/BoundStatement.hpp"
|
#include "../../Binder/BoundStatements/BoundStatement.hpp"
|
||||||
|
@ -13,10 +13,10 @@ class ScriptFunctionEvalValue : public EvalValue{
|
||||||
std::shared_ptr<BoundBlockStatement> _innerBlock;
|
std::shared_ptr<BoundBlockStatement> _innerBlock;
|
||||||
FunctionScriptType _type;
|
FunctionScriptType _type;
|
||||||
public:
|
public:
|
||||||
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, const FunctionScriptType& type)
|
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, FunctionScriptType type)
|
||||||
: _type(type)
|
: _type(std::move(type))
|
||||||
{
|
{
|
||||||
_innerBlock = innerBlock;
|
_innerBlock = std::move(innerBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
EvalValue* Clone() final{
|
EvalValue* Clone() final{
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
|
||||||
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
|
#ifndef PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||||
#define PORYGONLANG_PARSEDEXPRESSION_HPP
|
#define PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "../Token.hpp"
|
#include "../Token.hpp"
|
||||||
#include "../UnaryOperatorKind.hpp"
|
#include "../UnaryOperatorKind.hpp"
|
||||||
#include "../BinaryOperatorKind.hpp"
|
#include "../BinaryOperatorKind.hpp"
|
||||||
|
@ -19,6 +21,7 @@ enum class ParsedExpressionKind{
|
||||||
Unary,
|
Unary,
|
||||||
Binary,
|
Binary,
|
||||||
Parenthesized,
|
Parenthesized,
|
||||||
|
FunctionCall,
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParsedExpression {
|
class ParsedExpression {
|
||||||
|
@ -216,8 +219,32 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
class FunctionCallExpression : public ParsedExpression{
|
class FunctionCallExpression : public ParsedExpression{
|
||||||
|
std::unique_ptr<ParsedExpression> _function;
|
||||||
|
vector<std::unique_ptr<ParsedExpression>> _parameters;
|
||||||
public:
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
#include "Parser.hpp"
|
#include "Parser.hpp"
|
||||||
|
#include "ParsedStatements/ParsedStatement.hpp"
|
||||||
#include "UnaryOperatorKind.hpp"
|
#include "UnaryOperatorKind.hpp"
|
||||||
#include "BinaryOperatorKind.hpp"
|
#include "BinaryOperatorKind.hpp"
|
||||||
#include "TypedVariableIdentifier.hpp"
|
#include "TypedVariableIdentifier.hpp"
|
||||||
|
@ -137,7 +138,8 @@ ParsedStatement *Parser::ParseFunctionDeclaration(IToken *current) {
|
||||||
|
|
||||||
ParsedExpression* Parser::ParseExpression(IToken* current){
|
ParsedExpression* Parser::ParseExpression(IToken* current){
|
||||||
auto expression = this -> ParseBinaryExpression(current, OperatorPrecedence::No);
|
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
|
//TODO: Function Evaluation
|
||||||
}
|
}
|
||||||
return expression;
|
return expression;
|
||||||
|
@ -250,6 +252,28 @@ ParsedExpression *Parser::ParseParenthesizedExpression(IToken *current) {
|
||||||
return new ParenthesizedExpression(expression, start, closeToken->GetEndPosition() - start);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Parser {
|
||||||
ParsedExpression* ParseBinaryExpression(IToken* current, OperatorPrecedence parentPrecedence);
|
ParsedExpression* ParseBinaryExpression(IToken* current, OperatorPrecedence parentPrecedence);
|
||||||
ParsedExpression* ParsePrimaryExpression(IToken* current);
|
ParsedExpression* ParsePrimaryExpression(IToken* current);
|
||||||
ParsedExpression* ParseParenthesizedExpression(IToken *current);
|
ParsedExpression* ParseParenthesizedExpression(IToken *current);
|
||||||
|
ParsedExpression* ParseFunctionCallExpression(ParsedExpression* functionExpression);
|
||||||
public:
|
public:
|
||||||
ParsedScriptStatement* Parse();
|
ParsedScriptStatement* Parse();
|
||||||
explicit Parser(vector<IToken*> tokens, Script* scriptData){
|
explicit Parser(vector<IToken*> tokens, Script* scriptData){
|
||||||
|
|
Loading…
Reference in New Issue