File tree Expand file tree Collapse file tree 4 files changed +29
-3
lines changed
Expand file tree Collapse file tree 4 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 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
99int main () {
1010 std::cout << " BarkScript version " << bsversion << std::endl;
Original file line number Diff line number Diff 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" )));
Original file line number Diff line number Diff 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+
6577struct ErrorNode : Node {
6678 ErrorNode (Token token) {
6779 this ->nodeType = " ERROR" ;
Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ expr : term ((PLUS|MINUS) term)*
33term : factor ((ASTERISK|F_SLASH) factor)*
44
55factor : NUMBER
6-
7-
6+ : (PLUS|MINUS) factor
7+ : OPEN_PAREN expr CLOSE_PAREN
You can’t perform that action at this time.
0 commit comments