Add lexical scope and redefining variables in functions (#3015)
* Fix to allow variable shadowing inside functions * Implement closures * Fix KCL test code to not reference future tag definition * Remove tag declarator from function parameters This is an example where the scoping change revealed a subtle issue with TagDeclarators. You cannot bind a new tag using a function parameter. The issue is that evaluating a TagDeclarator like $foo binds an identifier to its corresponding TagIdentifier, but returns the TagDeclarator. If you have a TagDeclarator passed in as a parameter to a function, you can never get its corresponding TagIdentifier. This seems like a case where TagDeclarator evaluation needs to be revisited, especially now that we have scoped tags. * Fix to query return, functions, and tag declarator AST nodes correctly
This commit is contained in:
@ -3,7 +3,7 @@ import { kclManager, engineCommandManager } from 'lib/singletons'
|
||||
import { useKclContext } from 'lang/KclProvider'
|
||||
import { findUniqueName } from 'lang/modifyAst'
|
||||
import { PrevVariable, findAllPreviousVariables } from 'lang/queryAst'
|
||||
import { Value, parse } from 'lang/wasm'
|
||||
import { ProgramMemory, Value, parse } from 'lang/wasm'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { executeAst } from 'lang/langHelpers'
|
||||
import { err, trap } from 'lib/trap'
|
||||
@ -60,9 +60,8 @@ export function useCalculateKclExpression({
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const allVarNames = Object.keys(programMemory.root)
|
||||
if (
|
||||
allVarNames.includes(newVariableName) ||
|
||||
programMemory.has(newVariableName) ||
|
||||
newVariableName === '' ||
|
||||
!isValidVariableName(newVariableName)
|
||||
) {
|
||||
@ -89,17 +88,20 @@ export function useCalculateKclExpression({
|
||||
if (err(ast)) return
|
||||
if (trap(ast, { suppress: true })) return
|
||||
|
||||
const _programMem: any = { root: {}, return: null }
|
||||
availableVarInfo.variables.forEach(({ key, value }) => {
|
||||
_programMem.root[key] = { type: 'userVal', value, __meta: [] }
|
||||
})
|
||||
const _programMem: ProgramMemory = ProgramMemory.empty()
|
||||
for (const { key, value } of availableVarInfo.variables) {
|
||||
const error = _programMem.set(key, {
|
||||
type: 'UserVal',
|
||||
value,
|
||||
__meta: [],
|
||||
})
|
||||
if (trap(error, { suppress: true })) return
|
||||
}
|
||||
const { programMemory } = await executeAst({
|
||||
ast,
|
||||
engineCommandManager,
|
||||
useFakeExecutor: true,
|
||||
programMemoryOverride: JSON.parse(
|
||||
JSON.stringify(kclManager.programMemory)
|
||||
),
|
||||
programMemoryOverride: kclManager.programMemory.clone(),
|
||||
})
|
||||
const resultDeclaration = ast.body.find(
|
||||
(a) =>
|
||||
@ -109,7 +111,7 @@ export function useCalculateKclExpression({
|
||||
const init =
|
||||
resultDeclaration?.type === 'VariableDeclaration' &&
|
||||
resultDeclaration?.declarations?.[0]?.init
|
||||
const result = programMemory?.root?.__result__?.value
|
||||
const result = programMemory?.get('__result__')?.value
|
||||
setCalcResult(typeof result === 'number' ? String(result) : 'NAN')
|
||||
init && setValueNode(init)
|
||||
}
|
||||
|
Reference in New Issue
Block a user