import { AbstractSyntaxTreeBase } from './ast.base' ; import { TokenHelper } from '../token/token.helper' ; import { Token } from '../token/token' ; export class AbstractSyntaxTree extends AbstractSyntaxTreeBase { get expression ( ) : string [ ] { return this . makeExpression ( ) ; } private getParentOperator ( ) { if ( this . isRoot ( ) ) return undefined ; return this . parent . findOperator ( ) ; } private isNeededBracket ( ) { const parentOperator = this . getParentOperator ( ) ; return parentOperator && ( TokenHelper . getPrecedenceDiff ( parentOperator . value , this . value ) > 0 || TokenHelper . getPrecedenceDiff ( parentOperator . value , this . value ) >= 0 && this . parent . rightNode === this ) ; } private findOperator ( ) { if ( this . type === Token . Type . Operator ) return this ; return this . parent . findOperator ( ) ; } private makeExpression ( ) { return this . type === Token . Type . Operator ? this . makeOperatorExpression ( ) : this . makeValueExpression ( ) ; } private makeOperatorClause ( ) { return [ ... this . leftNode ? this . leftNode . expression : [ ] , this . value , ... this . rightNode ? this . rightNode . expression : [ ] ] ; } private makeOperatorExpression ( ) { const expression = this . makeOperatorClause ( ) ; return this . isNeededBracket ( ) ? this . wrapBracket ( expression ) : expression ; } private makeValueExpression ( ) { return [ this . value ] ; } private wrapBracket ( expression ) { return [ Token . literal . BracketOpen , ... expression , Token . literal . BracketClose ] ; } }