diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-14 23:35:23 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-14 23:35:23 -0400 |
commit | de517d06737f77a0cfc818c159b388b4e0c4d6b1 (patch) | |
tree | eb32f4a2cf994ed2f5401bb1e194d1080938d19a /src | |
parent | c5b9b7d03a37b807b212c29a9bdf7afc837c5423 (diff) |
Lex and parse negatives
Diffstat (limited to 'src')
-rw-r--r-- | src/parser.js | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/parser.js b/src/parser.js index 9f2c6ac..5b9b718 100644 --- a/src/parser.js +++ b/src/parser.js @@ -40,7 +40,12 @@ newSymbol('+', null, 20, (left, parser) => { } }) -newSymbol('-', null, 20, (left, parser) => { +newSymbol('-', (parser) => { + return { + type: 'negative', + value: parser.expression(40) + } +}, 20, (left, parser) => { return { type: 'subtract', left: left, @@ -56,10 +61,17 @@ const newParser = (tokens) => { currentToken: 0, token: function() { return this.tokens[this.currentToken] }, advanceToken: function() { this.currentToken++ }, + match: function(token) { + if (this.token().type === token) { + this.advanceToken() + } else { + throw 'error' + } + }, expression: function(rbp) { let symbol = this.token() - let left = symbol.nud() this.advanceToken() + let left = symbol.nud(this) while (rbp < this.token().lbp) { symbol = this.token() |