setVariableValue method

void setVariableValue(
  1. String? variableName,
  2. dynamic variableValue
)

设置变量值

Implementation

void setVariableValue(String? variableName, dynamic variableValue) {
  var ref = _variables[variableName];
  if (ref == null) {
    //向上追溯查找变量
    if (parent != null) {
      ref = parent!.findVariable(variableName);
    }
    if (ref != null) {
      //更新变量值
      ref.value = variableValue;
    } else {
      //在当前作用域中添加该变量
      _variables[variableName] = AstReferenceVariable(variableValue);
    }
  } else {
    //更新变量值
    ref.value = variableValue;
  }
}