class Parser {
private tokens: Array<ITokenized>
private token_index: number;
private current_token: ITokenized | undefined;
constructor(tokens: Array<ITokenized>) {
// console.log(tokens);
this.tokens = tokens
this.token_index = -1;
this.current_token = undefined
this.next()
}
next() {
this.token_index += 1;
if (this.token_index < this.tokens.length) {
this.current_token = this.tokens[this.token_index]
}
return this.current_token
}
public parse(): any {
let result = this.expression();
return result;
}
private factor() {
let token = this.current_token
if ([TOK_INT, TOK_FLOAT].includes(token?.dataType)) {
this.next();
return new NumberNode(token?.value).represent();
}
}
private term() {
return this.binaryOperation(this.factor, [TOK_MULTI, TOK_DIVI])
}
private expression() {
return this.binaryOperation(this.term, [TOK_PLUS, TOK_MINUS])
}
public binaryOperation(func: Function, operator: Array<string>) {
let leftNode, operationToken, rightNode;
leftNode = func()
while (operator.includes(this.current_token?.dataType)) {
operationToken = this.current_token;
this.next();
rightNode = func()
leftNode = new BinaryOperator(leftNode, operationToken?.dataType, rightNode).represent();
}
return leftNode;
}
}
export default Parser;
F: \ Programación-Files \ hola-mundo \ dev-projects \ puCpp-programación en \ src \ analizador \ Parser.ts: 69 this.binaryOperation retorno (this.factor, [TOK_MULTI, TOK_DIVI]) ^ TypeError: No se puede leer propiedad 'binaryOperation' de la indefinida en Parser.term (F: \ programación-Files \ hola-mundo \ dev-projects \ puCpp-programación en \ src \ analizador \ Parser.ts: 69: 15) en Parser.binaryOperation (F : \ programación-Files \ hola-mundo \ dev-projects \ puCpp-programación en \ src \ analizador \ Parser.ts: 78: 14) en Parser.expression (F: \ programación-Files \ hola-mundo \ dev- proyectos \ puCpp-programación en \ src \ analizador \ Parser.ts: 73: 15) en parser.parse (F: \ programación-Files \ hola-mundo \ dev-projects \ puCpp-programación en \ src \ analizador \ Parser.ts: 56: 21) en Runner.start (F: \ programación-Files \ hola-mundo \ dev-projects \ puCpp-programación en \ src \ lexer \ Runner.ts: 21: 26) en F:\ Programación-Files \ hola-mundo \ dev-projects \ puCpp de programación en idioma \ src \ index.ts: 25: 46 en Interface._onLine (readline.js: 306: 5) en Interface._line (readline.js: 656: 8) en Interface._ttyWrite (readline.js: 937: 14) en Socket.onkeypress (readline.js: 184: 10)
Tratar de forma explícita la unión de sus argumentos de la función:
private term() {
return this.binaryOperation(this.factor.bind(this), [TOK_MULTI, TOK_DIVI])
}
private expression() {
return this.binaryOperation(this.term.bind(this), [TOK_PLUS, TOK_MINUS])
}