Skip to content

Commit 3fe7afc

Browse files
Added Unary operators and parenthetical precedence
1 parent e335394 commit 3fe7afc

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

BarkScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "lexer.h"
55
#include "parser.h"
66

7-
const std::string bsversion = "0.0.5.1";
7+
const std::string bsversion = "0.0.6";
88

99
int main() {
1010
std::cout << "BarkScript version " << bsversion << std::endl;

Parser.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ ParseResult Parser::factor() {
3131
if (token.type == tokens::NUMBER) {
3232
nextToken();
3333
return pr.success(makeSharedNode(NumberNode(token)));
34+
} else if (in_array(token.type, { tokens::PLUS, tokens::MINUS })) {
35+
nextToken();
36+
spNode factorRes = pr.registerPR(factor());
37+
if (pr.hasError()) return pr;
38+
return pr.success(makeSharedNode(UnaryOperatorNode(token, factorRes)));
39+
} else if (token.type == tokens::OPEN_PAREN) {
40+
nextToken();
41+
spNode exprRes = pr.registerPR(expr());
42+
if (pr.hasError()) return pr;
43+
if (currentToken.type == tokens::CLOSE_PAREN) {
44+
nextToken();
45+
return pr.success(exprRes);
46+
}
47+
return pr.failure(makeSharedError(InvalidSyntaxError(currentToken.positionStart, currentToken.positionEnd, "Expected a ')'")));
3448
} else {
3549
pr.registerPR(makeSharedNode(ErrorNode(token)));
3650
return pr.failure(makeSharedError(InvalidSyntaxError(token.positionStart, token.positionEnd, "Expected a number")));

ast.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ struct BinaryOperatorNode : Node {
6262
}
6363
};
6464

65+
struct UnaryOperatorNode : Node {
66+
UnaryOperatorNode(Token token, spNode rightNode) {
67+
this->nodeType = "UNOP";
68+
this->token = token;
69+
this->rightNode = rightNode;
70+
}
71+
72+
std::string to_string() override {
73+
return "(" + token.value + ", " + rightNode->to_string() + ")";
74+
}
75+
};
76+
6577
struct ErrorNode : Node {
6678
ErrorNode(Token token) {
6779
this->nodeType = "ERROR";

syntax.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ expr : term ((PLUS|MINUS) term)*
33
term : factor ((ASTERISK|F_SLASH) factor)*
44

55
factor : NUMBER
6-
7-
6+
: (PLUS|MINUS) factor
7+
: OPEN_PAREN expr CLOSE_PAREN

0 commit comments

Comments
 (0)