Пример #1
0
        public override void Execute(ScriptRuntimeContext context)
        {
            if (context.VariableExists(identifier))
            {
                //You cannot declare a local variable to shadow an existing global
                throw new ScriptRuntimeException($"Variable already declared in this context: {identifier}");
            }

            object defaultValue;

            if (initializer == null)
            {
                defaultValue = valueType.GetDefaultValue();
            }
            else
            {
                defaultValue = initializer.GetAs <object>(context);

                if (!valueType.IsAssignableFrom(initializer.GetValueType()))
                {
                    defaultValue = Convert.ChangeType(defaultValue, valueType);
                }
            }

            context.DeclareVariable(identifier, valueType, defaultValue);
        }
        public override void Execute(ScriptRuntimeContext context)
        {
            if (context.VariableExists(identifier))
            {
                throw new ScriptRuntimeException($"Variable already declared in this context: {identifier}");
            }

            if (context.GlobalVariableExists(identifier))
            {
                //Adding an indirect reference to the existing variable
                context.DeclareExistingGlobal(identifier, valueType);

                //Not running initialization because it exists
                return;
            }

            if (IsExtern)
            {
                throw new ScriptRuntimeException($"Extern Variable does not already exist: {identifier}");
            }

            object defaultValue;

            if (initializer == null)
            {
                defaultValue = valueType.GetDefaultValue();
            }
            else
            {
                defaultValue = initializer.GetAs <object>(context);

                if (!valueType.IsAssignableFrom(initializer.GetValueType()))
                {
                    defaultValue = Convert.ChangeType(defaultValue, valueType);
                }
            }

            context.DeclareNewGlobal(identifier, valueType, defaultValue);
        }