Skip to content

Commit

Permalink
Added Unary operators and parenthetical precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
Samathingamajig committed Feb 13, 2021
1 parent e335394 commit 3fe7afc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BarkScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "lexer.h"
#include "parser.h"

const std::string bsversion = "0.0.5.1";
const std::string bsversion = "0.0.6";

int main() {
std::cout << "BarkScript version " << bsversion << std::endl;
Expand Down
14 changes: 14 additions & 0 deletions Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ ParseResult Parser::factor() {
if (token.type == tokens::NUMBER) {
nextToken();
return pr.success(makeSharedNode(NumberNode(token)));
} else if (in_array(token.type, { tokens::PLUS, tokens::MINUS })) {
nextToken();
spNode factorRes = pr.registerPR(factor());
if (pr.hasError()) return pr;
return pr.success(makeSharedNode(UnaryOperatorNode(token, factorRes)));
} else if (token.type == tokens::OPEN_PAREN) {
nextToken();
spNode exprRes = pr.registerPR(expr());
if (pr.hasError()) return pr;
if (currentToken.type == tokens::CLOSE_PAREN) {
nextToken();
return pr.success(exprRes);
}
return pr.failure(makeSharedError(InvalidSyntaxError(currentToken.positionStart, currentToken.positionEnd, "Expected a ')'")));
} else {
pr.registerPR(makeSharedNode(ErrorNode(token)));
return pr.failure(makeSharedError(InvalidSyntaxError(token.positionStart, token.positionEnd, "Expected a number")));
Expand Down
12 changes: 12 additions & 0 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ struct BinaryOperatorNode : Node {
}
};

struct UnaryOperatorNode : Node {
UnaryOperatorNode(Token token, spNode rightNode) {
this->nodeType = "UNOP";
this->token = token;
this->rightNode = rightNode;
}

std::string to_string() override {
return "(" + token.value + ", " + rightNode->to_string() + ")";
}
};

struct ErrorNode : Node {
ErrorNode(Token token) {
this->nodeType = "ERROR";
Expand Down
4 changes: 2 additions & 2 deletions syntax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ expr : term ((PLUS|MINUS) term)*
term : factor ((ASTERISK|F_SLASH) factor)*

factor : NUMBER


: (PLUS|MINUS) factor
: OPEN_PAREN expr CLOSE_PAREN

0 comments on commit 3fe7afc

Please sign in to comment.