I am experiencing an issue with the binOp function in my code. The function runs once when called from an expression, but then becomes undefined when called by term. I'm having trouble figuring out why this is happening. I've tried to debug it and review the code several times, but I can't seem to find the root cause. My project is running on localhost using Next.js.
Upon encountering this problem, the following error message is displayed:
TypeError: Cannot read properties of undefined (reading 'binOp')
class Parser {
tokens: Token[]
tokIdx: number
currentTok: Token
constructor(tokens: Token[]) {
this.tokens = tokens
this.tokIdx = -1
this.currentTok = new Token({type:"null"})
this.advance()
}
advance() {
this.tokIdx++
if(this.tokIdx < this.tokens.length) {
this.currentTok = this.tokens[this.tokIdx]
}
return this.currentTok
}
binOp(func:any, ops:string[]) {
console.log("here")
let left: any = func()
while(this.currentTok.type == TT_MUL || this.currentTok.type == TT_DIV) {
let opTok = this.currentTok
this.advance()
let right = func()
left = new BinOpNode({leftNode:left, opTok:opTok, rightNode:right})
}
return left
}
parse() {
return this.expression()
}
factor() {
let tok: Token = this.currentTok
if(tok.type == TT_INT || tok.type == TT_FLOAT) {
this.advance()
return new NumberNode(tok)
}
}
term() {
return this.binOp(this.factor, [TT_MUL, TT_DIV])
}
expression() {
return this.binOp(this.term, [TT_PLUS, TT_MINUS])
}
}