-
-
Notifications
You must be signed in to change notification settings - Fork 362
fix: expression evaluation #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HardyNLee
wants to merge
2
commits into
OpenWebGAL:dev
Choose a base branch
from
HardyNLee:fix/expression-evaluation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
packages/webgal/src/Core/controller/gamePlay/expressionEvaluation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import expression from 'angular-expressions'; | ||
| import { stageStateManager } from '@/Core/Modules/stage/stageStateManager'; | ||
| import { webgalStore } from '@/store/store'; | ||
| import { logger } from '@/Core/util/logger'; | ||
| import random from 'lodash/random'; | ||
| import { WebGAL } from '@/Core/WebGAL'; | ||
|
|
||
| /** | ||
| * 提取变量名和表达式 | ||
| * @param expressionString 表达式字符串,例如 "x = a + 5" | ||
| * @returns 包含变量名和表达式,如果无效则返回 undefined | ||
| */ | ||
| export function extractVariableNameAndExpression( | ||
| expressionString: string, | ||
| ): { variableName: string; expression: string } | undefined { | ||
| const equalIndex = expressionString.indexOf('='); | ||
| if (equalIndex === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const variableName = expressionString.substring(0, equalIndex).trim(); | ||
| if (variableName.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const expression = expressionString.substring(equalIndex + 1).trim(); | ||
| if (expression.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { variableName, expression }; | ||
| } | ||
|
|
||
| // 内置函数 | ||
| const builtinFunctions = { | ||
| random: (...args: any[]) => { | ||
| return args.length ? random(...args) : Math.random(); | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * 评估表达式字符串 | ||
| * @param expressionString 表达式字符串 | ||
| * @returns 评估结果,可能是 string、number、boolean 或 undefined | ||
| * 但也有可能返回其他类型,取决于表达式的内容和上下文 | ||
| */ | ||
| export function evaluateExpression(expressionString: string): string | number | boolean | undefined { | ||
| try { | ||
| const evaluate = expression.compile(expressionString); | ||
|
|
||
| const stageState = stageStateManager.getCalculationStageState(); | ||
| const stageVar = stageState.GameVar; | ||
| const userData = webgalStore.getState().userData; | ||
| const globalVar = userData.globalGameVar; | ||
|
|
||
| const scope: any = {}; | ||
| // 先加入内置函数(最低优先级) | ||
| Object.assign(scope, builtinFunctions); | ||
| // 然后按查找链依次覆盖:全局变量 -> 舞台变量 -> 当前调用帧的局部变量(最高优先级) | ||
| Object.assign(scope, globalVar); | ||
| Object.assign(scope, stageVar); | ||
| Object.assign(scope, WebGAL.sceneManager.sceneData.currentLocals); | ||
| // 支持 $ 前缀的特殊值 | ||
| scope['$stage'] = stageState; | ||
| scope['$userData'] = userData; | ||
|
|
||
| const result = evaluate(scope); | ||
| switch (typeof result) { | ||
| case 'string': | ||
| case 'number': | ||
| case 'boolean': | ||
| return result; | ||
| default: | ||
| logger.warn(`不支持的表达式求值类型: ${typeof result},表达式: ${expressionString}`); | ||
| return undefined; | ||
| } | ||
| } catch (error) { | ||
| logger.warn(`表达式求值失败: ${expressionString}`, error); | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此处应有 log