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:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語は、\ SRC \パーサ\ Parser.ts:69リターンthis.binaryOperation(this.factor、[TOK_MULTI、TOK_DIVI])^ TypeError例外:読み取ることができません。 Parser.termで未定義のプロパティ 'binaryOperation'(F:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \パーサ\ Parser.ts:69:15)Parser.binaryOperationで(F :\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \パーサ\ Parser.ts:78:14)Parser.expressionで(F:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \パーサ\ Parser.ts:73:15)Parser.parse(F時:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \パーサ\ Parser.ts:56:21)Runner.startで(F:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \レクサー\ Runner.ts:21:26)Fで:\プログラミング・ファイルを\こんにちは世界\ DEV-プロジェクト\ puCppプログラミング言語\ SRC \ index.ts:25:Interface._lineで(readline.js Interface._onLine(:306 5 readline.js)の46: Interface._ttyWrite(readline.jsで8):656 937:14)Socket.onkeypressで(readline.js:184:10)
あなたの関数の引数を結合明示的に試してみてください。
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])
}