Skip to content

Commit

Permalink
Completed parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoucy committed Oct 22, 2023
1 parent bc4d92f commit 016318b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 108 deletions.
7 changes: 4 additions & 3 deletions doc/operator-precedence.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ bitshift -> term (( '>>' | '<<' ) term ) *
term -> factor ( ( '-' | '+' ) factor ) *
factor -> unary ( ( '/' | '*' | '%' ) unary ) *
unary -> ( '+' | '-' | '!' | '~' ) unary | call
call -> Identifier '(' (expression ',')* (expression)? ')' | primary
call -> primary ( '(' params? ')' )*
params -> expression ( ',' expression )*
primary -> Integer | Identifier | String | grouping
grouping -> '(' expression ')'
Integer -> ['0'-'9']+
Type -> 'int' | 'str' | 'fnc' FunctionSignature
FunctionSignature -> FunctionParams (Type)? block
FunctionParams -> '(' ( ( TypeIdent ',')* TypeIdent )? ')'
FunctionSignature -> FunctionParams (Type)?
FunctionParams -> '(' ( TypeIdent ( ',' TypeIdent)* )? ')'
TypeIdent -> Identifier Type
Identifier -> [a-zA-Z_][a-zA-Z_0-9]*
String -> "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@

namespace ast
{
class FunctionCallNode final : public ExpressionNode
class CallNode final : public ExpressionNode
{
public:
using ptr = std::shared_ptr<FunctionCallNode>;
using ptr = std::shared_ptr<CallNode>;
static ptr create(
const std::string &identifier,
const ExpressionNode::ptr &callee,
const std::vector<ast::ExpressionNode::ptr> &params
);
explicit FunctionCallNode(
std::string identifier,
explicit CallNode(
ExpressionNode::ptr callee,
const std::vector<ast::ExpressionNode::ptr> &params
);
~FunctionCallNode() final = default;
~CallNode() final = default;

void accept(IVisitor &visitor) final;

[[nodiscard]] const std::string &getIdentifier() const;
[[nodiscard]] const ExpressionNode::ptr &getCallee() const;
[[nodiscard]] const std::vector<ast::ExpressionNode::ptr> &getParams() const;

private:
std::string _identifier;
ExpressionNode::ptr _callee;
std::vector<ast::ExpressionNode::ptr> _params;
};
};
8 changes: 4 additions & 4 deletions inc/ast/visitors/EvalVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "FunctionNode.hpp"
#include "ReturnNode.hpp"
#include "FunctionCallNode.hpp"
#include "CallNode.hpp"

#include "ProgramNode.hpp"

Expand Down Expand Up @@ -56,15 +56,15 @@ namespace ast
void visit(IncrementNode &node) final;

void visit(FunctionNode &node) final;
void visit(FunctionCallNode &node) final;
void visit(CallNode &node) final;
void visit(ReturnNode &node) final;

void visit(ProgramNode &node) final;

const runtime::Object &value() const noexcept;
[[nodiscard]] const runtime::Object &value() const noexcept;
[[nodiscard]] Token::Integer getResult() const;

const runtime::State &getState() const noexcept;
[[nodiscard]] const runtime::State &getState() const noexcept;

void clearState() noexcept;

Expand Down
4 changes: 2 additions & 2 deletions inc/ast/visitors/IVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ast
class IncrementNode;

class FunctionNode;
class FunctionCallNode;
class CallNode;
class ReturnNode;

class ProgramNode;
Expand Down Expand Up @@ -49,7 +49,7 @@ namespace ast
virtual void visit(ForNode &node) = 0;

virtual void visit(FunctionNode &node) = 0;
virtual void visit(FunctionCallNode &node) = 0;
virtual void visit(CallNode &node) = 0;
virtual void visit(ReturnNode &node) = 0;

virtual void visit(ProgramNode &node) = 0;
Expand Down
3 changes: 2 additions & 1 deletion inc/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Parser
ast::ExpressionNode::ptr parseTerm();
ast::ExpressionNode::ptr parseFactor();
ast::ExpressionNode::ptr parseUnary();
ast::ExpressionNode::ptr parseFunctionCall();
ast::ExpressionNode::ptr parseCall();
std::vector<ast::ExpressionNode::ptr> parseParams();
ast::ExpressionNode::ptr parsePrimary();
ast::ExpressionNode::ptr parseInteger();
ast::ExpressionNode::ptr parseString();
Expand Down
34 changes: 34 additions & 0 deletions src/ast/nodes/CallNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "CallNode.hpp"

#include <utility>

ast::CallNode::ptr ast::CallNode::create(
const ast::ExpressionNode::ptr &callee,
const std::vector<ast::ExpressionNode::ptr> &params
)
{
return std::make_shared<CallNode>(callee, params);
}

ast::CallNode::CallNode(
ast::ExpressionNode::ptr callee,
const std::vector<ast::ExpressionNode::ptr> &params
): _callee(std::move(callee)),
_params(params)
{
}

void ast::CallNode::accept(ast::IVisitor &visitor)
{
visitor.visit(*this);
}

const ast::ExpressionNode::ptr &ast::CallNode::getCallee() const
{
return this->_callee;
}

const std::vector<ast::ExpressionNode::ptr> &ast::CallNode::getParams() const
{
return this->_params;
}
33 changes: 0 additions & 33 deletions src/ast/nodes/FunctionCallNode.cpp

This file was deleted.

9 changes: 3 additions & 6 deletions src/ast/visitors/EvalVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,19 @@ void ast::EvalVisitor::visit(ast::IncrementNode &node)
auto &object = this->_state->find(node.getIdentifier());

switch (node.getOperator()) {
default:
throw InternalError("EvalVisitor : invalid operator in IncrementNode");
case Token::INCR: object.assign(object + 1); break;
case Token::DECR: object.assign(object - 1); break;

// default:
// throw InternalError("EvalVisitor : invalid operator in IncrementNode");
default:
throw InternalError("EvalVisitor : invalid operator in IncrementNode");
}
}

void ast::EvalVisitor::visit(ast::FunctionNode &node)
{

}

void ast::EvalVisitor::visit(ast::FunctionCallNode &node)
void ast::EvalVisitor::visit(ast::CallNode &node)
{

}
Expand Down
100 changes: 50 additions & 50 deletions src/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,39 +379,35 @@ ast::StatementNode::ptr Parser::parseFunction()
throw syntaxError("expecting identifier", *fncToken);
this->_tokenItr.advance();

auto token = this->_tokenItr.get();
if (!token || !token->isType(Token::OPEN_PARENTHESIS))
auto open_p = this->_tokenItr.get();
if (!open_p || !open_p->isType(Token::OPEN_PARENTHESIS))
throw syntaxError("expecting '('", *identToken);
this->_tokenItr.advance();

std::clog << "FUNCTION" << std::endl;

std::map<std::string, Token::Type> params;
auto typeIdent = this->parseTypeIdent();

if (typeIdent) {
while (true) {
if (params.find(typeIdent->first) != params.end())
throw syntaxError("parameter name already used", *this->_tokenItr.prev());
while (typeIdent) {

if (params.find(typeIdent->first) != params.end())
throw syntaxError("parameter name already used", *this->_tokenItr.prev());

std::clog << "ident = " << typeIdent->first << std::endl;
params.insert(*typeIdent);
params.insert(*typeIdent);

token = this->_tokenItr.get();
if (!token || !token->isType(Token::COMMA))
break;
this->_tokenItr.advance();
auto token = this->_tokenItr.get();
if (!token || !token->isType(Token::COMMA))
break;
this->_tokenItr.advance();

typeIdent = this->parseTypeIdent();
}
typeIdent = this->parseTypeIdent();
}

token = this->_tokenItr.get();
if (!token || !token->isType(Token::CLOSE_PARENTHESIS))
throw syntaxError("dwqdwqdwqdwqexpecting ')'", *this->_tokenItr.prev());
auto close_p = this->_tokenItr.get();
if (!close_p || !close_p->isType(Token::CLOSE_PARENTHESIS))
throw syntaxError("unmatched '('", *open_p);
this->_tokenItr.advance();

token = this->_tokenItr.get();
auto token = this->_tokenItr.get();
if (!token)
throw syntaxError("expecting return type or block", *this->_tokenItr.prev());

Expand All @@ -422,6 +418,8 @@ ast::StatementNode::ptr Parser::parseFunction()
}

auto block = this->parseBlock();
if (block == nullptr)
throw syntaxError("expecting function body", *identToken);

return ast::FunctionNode::create(
identToken->getLexeme(),
Expand Down Expand Up @@ -573,7 +571,7 @@ ast::ExpressionNode::ptr Parser::parseUnary()
return nullptr;

if (!token->isTypeAnyOf({Token::PLUS, Token::MINUS, Token::NOT, Token::BITWISE_NOT}))
return this->parseFunctionCall();
return this->parseCall();

this->_tokenItr.advance();

Expand All @@ -584,47 +582,49 @@ ast::ExpressionNode::ptr Parser::parseUnary()
return ast::UnaryNode::create(token->getType(), expression);
}

ast::ExpressionNode::ptr Parser::parseFunctionCall()
ast::ExpressionNode::ptr Parser::parseCall()
{
auto token = this->_tokenItr.get();
if (!token)
return nullptr;
auto expr = this->parsePrimary();
ast::ExpressionNode::ptr ret;

if (!token->isType(Token::IDENTIFIER))
return this->parsePrimary();
auto open_p = this->_tokenItr.get();
while (open_p && open_p->isType(Token::OPEN_PARENTHESIS)) {
this->_tokenItr.advance();

std::string ident(token->getLexeme());
auto params = this->parseParams();

auto nextToken = this->_tokenItr.next();
if (!nextToken || !nextToken->isType(Token::OPEN_PARENTHESIS))
return this->parsePrimary();
auto close_p = this->_tokenItr.get();
if (!close_p || !close_p->isType(Token::CLOSE_PARENTHESIS))
throw syntaxError("unmatched parenthesis", *open_p);
this->_tokenItr.advance();

this->_tokenItr.advance().advance();
expr = ast::CallNode::create(expr, params);
open_p = this->_tokenItr.get();
}

return expr;
}

std::vector<ast::ExpressionNode::ptr> Parser::parseParams()
{
auto expr = this->parseExpression();
std::vector<ast::ExpressionNode::ptr> params;
if (!expr)
return {};

if (expr != nullptr) {
while (true) {
params.push_back(std::move(expr));
std::vector<ast::ExpressionNode::ptr> params = {expr};

token = this->_tokenItr.get();
if (!token || !token->isType(Token::COMMA))
break;
this->_tokenItr.advance();
auto comma = this->_tokenItr.get();
while (comma && comma->isType(Token::COMMA)) {
this->_tokenItr.advance();
expr = this->parseExpression();
if (!expr)
throw syntaxError("expecting expression after ','", *comma);

expr = this->parseExpression();
if (expr == nullptr)
throw syntaxError("expecting expression", *token);
}
params.push_back(expr);
comma = this->_tokenItr.get();
}

token = this->_tokenItr.get();
if (!token || !token->isType(Token::CLOSE_PARENTHESIS))
throw syntaxError("expecting ')'", *this->_tokenItr.prev());
this->_tokenItr.advance();

return ast::FunctionCallNode::create(ident, params);
return params;
}

ast::ExpressionNode::ptr Parser::parsePrimary()
Expand Down
2 changes: 1 addition & 1 deletion src/src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ set(SOURCES
${PROJECT_ROOT}/src/ast/nodes/ForNode.cpp
${PROJECT_ROOT}/src/ast/nodes/IncrementNode.cpp
${PROJECT_ROOT}/src/ast/nodes/FunctionNode.cpp
${PROJECT_ROOT}/src/ast/nodes/FunctionCallNode.cpp
${PROJECT_ROOT}/src/ast/nodes/CallNode.cpp
${PROJECT_ROOT}/src/ast/nodes/ReturnNode.cpp

${PROJECT_ROOT}/src/ast/visitors/EvalVisitor.cpp
Expand Down

0 comments on commit 016318b

Please sign in to comment.