()
| 123 | } |
| 124 | |
| 125 | _factor() { |
| 126 | let expression; |
| 127 | let factor; |
| 128 | let value; |
| 129 | switch (this.token.type) { |
| 130 | case 'num': |
| 131 | value = this.token.value; |
| 132 | this.token = this.lexer.getNextToken(this.decimalCharacter); |
| 133 | |
| 134 | return ASTNode.createLeaf(value); |
| 135 | case '-': |
| 136 | this.token = this.lexer.getNextToken(this.decimalCharacter); |
| 137 | factor = this._factor(); |
| 138 | |
| 139 | return ASTNode.createUnaryNode(factor); |
| 140 | case '(': |
| 141 | this.token = this.lexer.getNextToken(this.decimalCharacter); |
| 142 | expression = this._exp(); |
| 143 | this._match(')'); |
| 144 | |
| 145 | return expression; |
| 146 | default: { |
| 147 | throw new Error(`Unexpected token '${this.token.symbol}' with type '${this.token.type}' at position '${this.token.index}' in the factor function`); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | _match(expected) { |
| 153 | const index = this.lexer.getIndex() - 1; |
no test coverage detected