Exemplo n.º 1
0
        public T FindVariableValue <T>(string name, bool includeParent, VariableSearchMode mode) where T : SerializableValue
        {
            var variable = FindVariable(name, includeParent, mode);

            if (variable == null || variable.ReferenceTarget.GetType() != typeof(T))
            {
                return(null);
            }
            return((T)variable.ReferenceTarget);
        }
Exemplo n.º 2
0
        private void SetVariable(VariableSearchMode mode)
        {
            var name  = PopString();
            var value = MemoryStack.Pop();

            value = value is ReferenceValue referenceValue ? referenceValue.ReferenceTarget : value;
            if (string.IsNullOrEmpty(name))
            {
                throw new RuntimeException(_callStack, $"Unable to set variable: expected name {name} is not string value");
            }
            var variable = ActiveScope?.FindVariableAndScope(name, true, mode);

            if (name.Equals("true", StringComparison.InvariantCultureIgnoreCase) || name.Equals("false", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new RuntimeException(_callStack, "Unable to set variable: system value true/false is readonly");
            }
            if (variable.HasValue)
            {
                try {
                    if (value == null || value is NullValue)
                    {
                        variable.Value.Scope.LocalVariables.Remove(name);
                    }
                    else
                    {
                        variable.Value.Target.ReferenceTarget = value;
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(_callStack, ex);
                }
            }
            else if (value != null && !(value is NullValue))
            {
                ActiveScope?.LocalVariables.Add(name, new ReferenceValue {
                    ReferenceTarget = value, IsConstant = mode == VariableSearchMode.OnlyConstant
                });
            }
            MemoryStack.Push(value);
        }
Exemplo n.º 3
0
        private void LoadVariable(VariableSearchMode mode)
        {
            string name;

            if (MemoryStack.Peek() is ReferenceValue referenceValue)
            {
                name = PopString(referenceValue.ReferenceTarget);
                MemoryStack.Pop();
            }
            else
            {
                name = PopString();
            }
            if (name.Equals("true", StringComparison.InvariantCultureIgnoreCase))
            {
                MemoryStack.Push(new BooleanValue {
                    value = true
                });
            }
            else if (name.Equals("false", StringComparison.InvariantCultureIgnoreCase))
            {
                MemoryStack.Push(new BooleanValue {
                    value = false
                });
            }
            else
            {
                var target = ActiveScope?.FindVariable(name, true, mode);
                if (target == null)
                {
                    LoadNull();
                }
                else
                {
                    MemoryStack.Push(target.ReferenceTarget);
                }
            }
        }
Exemplo n.º 4
0
 public ReferenceValue FindVariable(string name, bool includeParent, VariableSearchMode mode)
 {
     return(FindVariableAndScope(name, includeParent, mode)?.Target);
 }
Exemplo n.º 5
0
        public (ReferenceValue Target, ScopeValue Scope)? FindVariableAndScope(string name, bool includeParent, VariableSearchMode mode)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }
            IEnumerable <KeyValuePair <string, ReferenceValue> > items;

            switch (mode)
            {
            case VariableSearchMode.All:
                items = LocalVariables;
                break;

            case VariableSearchMode.OnlyConstant:
                items = LocalVariables.Where(e => e.Value.IsConstant);
                break;

            case VariableSearchMode.OnlyNonConstant:
                items = LocalVariables.Where(e => !e.Value.IsConstant);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, $"Unknown VariableSearchMode {mode}");
            }
            var result = items.Where(e => e.Key == name).ToList();

            if (result.Any())
            {
                return(result.First().Value, this);
            }
            return(includeParent ? parentScope?.FindVariableAndScope(name, true, mode) : null);
        }
Exemplo n.º 6
0
 public (ReferenceValue Target, ScopeValue Scope)? FindVariableAndScope(string name, bool includeParent, VariableSearchMode mode)
 {
     return(Runtime.ActiveScope?.FindVariableAndScope(name, includeParent, mode));
 }