feat: add molang package
1. upgrade bukkit chat 2. fix config update error Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
79
packages/molang/src/parser/expressions/ternary.ts
Normal file
79
packages/molang/src/parser/expressions/ternary.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { Expression, IExpression } from '../expression'
|
||||
import { VoidExpression } from './void'
|
||||
|
||||
export class TernaryExpression extends Expression {
|
||||
type = 'TernaryExpression'
|
||||
protected leftResult: unknown
|
||||
|
||||
constructor(
|
||||
protected leftExpression: IExpression,
|
||||
protected thenExpression: IExpression,
|
||||
protected elseExpression: IExpression
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get allExpressions() {
|
||||
if (this.leftExpression.isStatic())
|
||||
return [
|
||||
this.leftExpression,
|
||||
this.leftExpression.eval()
|
||||
? this.thenExpression
|
||||
: this.elseExpression,
|
||||
]
|
||||
return [this.leftExpression, this.thenExpression, this.elseExpression]
|
||||
}
|
||||
setExpressionAt(index: number, expr: IExpression) {
|
||||
if (index === 0) this.leftExpression = expr
|
||||
else if (index === 1) this.thenExpression = expr
|
||||
else if (index === 2) this.elseExpression = expr
|
||||
}
|
||||
|
||||
get isReturn() {
|
||||
if (this.leftResult === undefined)
|
||||
return this.thenExpression.isReturn && this.elseExpression.isReturn
|
||||
return this.leftResult
|
||||
? this.thenExpression.isReturn
|
||||
: this.elseExpression.isReturn
|
||||
}
|
||||
get hasReturn() {
|
||||
return this.thenExpression.isReturn || this.elseExpression.isReturn
|
||||
}
|
||||
get isContinue() {
|
||||
if (this.leftResult === undefined)
|
||||
return (
|
||||
this.thenExpression.isContinue && this.elseExpression.isContinue
|
||||
)
|
||||
return this.leftResult
|
||||
? this.thenExpression.isContinue
|
||||
: this.elseExpression.isContinue
|
||||
}
|
||||
get isBreak() {
|
||||
if (this.leftResult === undefined)
|
||||
return this.thenExpression.isBreak && this.elseExpression.isBreak
|
||||
return this.leftResult
|
||||
? this.thenExpression.isBreak
|
||||
: this.elseExpression.isBreak
|
||||
}
|
||||
|
||||
isStatic() {
|
||||
return (
|
||||
this.leftExpression.isStatic() &&
|
||||
this.thenExpression.isStatic() &&
|
||||
this.elseExpression.isStatic()
|
||||
)
|
||||
}
|
||||
|
||||
eval() {
|
||||
this.leftResult = this.leftExpression.eval()
|
||||
return this.leftResult
|
||||
? this.thenExpression.eval()
|
||||
: this.elseExpression.eval()
|
||||
}
|
||||
|
||||
toString() {
|
||||
if (this.elseExpression instanceof VoidExpression)
|
||||
return `${this.leftExpression.toString()}?${this.thenExpression.toString()}`
|
||||
return `${this.leftExpression.toString()}?${this.thenExpression.toString()}:${this.elseExpression.toString()}`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user