Esempio n. 1
0
        public ForeachBuilder(ActionSet actionSet, IWorkshopTree array, bool recursive = false)
        {
            ActionSet  = actionSet;
            IndexStore = actionSet.VarCollection.Assign("foreachIndex,", actionSet.IsGlobal, !recursive);
            Recursive  = recursive;

            if (recursive)
            {
                RecursiveIndexReference recursiveStore = new RecursiveIndexReference(IndexStore);
                IndexStore = recursiveStore;

                actionSet.InitialSet().AddAction(recursiveStore.Reset());
                actionSet.AddAction(recursiveStore.Push(0));
                actionSet.ReturnHandler.AdditionalPopOnReturn.Add(recursiveStore);
            }
            else
            {
                actionSet.AddAction(IndexStore.SetVariable(0));
            }

            Array      = array;
            Condition  = Element.Compare(IndexStore.GetVariable(), Operator.LessThan, Element.CountOf(Array));
            Index      = (Element)IndexStore.GetVariable();
            IndexValue = Element.ValueInArray(Array, IndexStore.GetVariable());

            actionSet.AddAction(Element.While(Condition));
        }
        public GettableAssignerResult GetResult(GettableAssignerValueInfo info)
        {
            var inline = _attributes.StoreType == StoreType.None || info.Inline;

            // Get the initial value.
            IWorkshopTree initialValue    = Element.Num(0);
            bool          hasDefaultValue = true;

            // Set the initial value to the override if it exists.
            if (info.InitialValueOverride != null)
            {
                initialValue = info.InitialValueOverride;
            }

            // Otherwise, use the var's initial value.
            else if (_attributes.DefaultValue != null)
            {
                initialValue = _attributes.DefaultValue.Parse(info.ActionSet);
            }

            // No default value
            else
            {
                hasDefaultValue = false;
            }

            // Inline
            if (inline)
            {
                return(new GettableAssignerResult(new WorkshopElementReference(initialValue), initialValue));
            }

            // Assign the index reference
            var value = info.IndexReferenceCreator.Create(_attributes);

            if (info.IsRecursive)
            {
                value = new RecursiveIndexReference(value);
            }

            // Set the initial value.
            if (info.SetInitialValue == SetInitialValue.SetAndFallbackTo0 || (info.SetInitialValue == SetInitialValue.SetIfExists && hasDefaultValue))
            {
                if (value is RecursiveIndexReference recursive)
                {
                    info.ActionSet.InitialSet().AddAction(recursive.Reset());
                    info.ActionSet.AddAction(recursive.Push((Element)initialValue));
                }
                else
                {
                    info.ActionSet.AddAction(value.SetVariable((Element)initialValue));
                }
            }

            return(new GettableAssignerResult(value, initialValue));
        }
Esempio n. 3
0
        public IGettable Add(VarCollection varCollection, Var var, bool isGlobal, IWorkshopTree referenceValue, bool recursive = false)
        {
            if (varCollection == null)
            {
                throw new ArgumentNullException(nameof(varCollection));
            }
            if (var == null)
            {
                throw new ArgumentNullException(nameof(var));
            }

            if (references.ContainsKey(var))
            {
                throw new Exception(var.Name + " was already added into the variable index assigner.");
            }

            IGettable assigned;

            // A gettable/settable variable
            if (var.Settable())
            {
                assigned = varCollection.Assign(var, isGlobal);
                if (recursive)
                {
                    assigned = new RecursiveIndexReference((IndexReference)assigned);
                }
                references.Add(var, assigned);
            }

            // Element reference
            else if (var.VariableType == VariableType.ElementReference)
            {
                if (referenceValue == null)
                {
                    throw new ArgumentNullException(nameof(referenceValue));
                }
                assigned = new WorkshopElementReference(referenceValue);
                references.Add(var, assigned);
            }

            else
            {
                throw new NotImplementedException();
            }

            return(assigned);
        }
        public IGettable Add(VarCollection varCollection, IIndexReferencer var, bool isGlobal, IWorkshopTree referenceValue, bool recursive = false)
        {
            if (varCollection == null)
            {
                throw new ArgumentNullException(nameof(varCollection));
            }
            if (var == null)
            {
                throw new ArgumentNullException(nameof(var));
            }
            CheckIfAdded(var);

            IGettable assigned;

            // A gettable/settable variable
            if (var.Settable())
            {
                assigned = varCollection.Assign(var, isGlobal);
                if (recursive || var.Recursive)
                {
                    assigned = new RecursiveIndexReference((IndexReference)assigned);
                }
                references.Add(var, assigned);
            }

            // Element reference
            else if (var.VariableType == VariableType.ElementReference)
            {
                if (referenceValue == null)
                {
                    throw new ArgumentNullException(nameof(referenceValue));
                }
                assigned = new WorkshopElementReference(referenceValue);
                references.Add(var, assigned);
            }

            else
            {
                throw new NotImplementedException();
            }

            return(assigned);
        }
        public IndexReference AddIndexReference(VarCollection varCollection, Var var, bool isGlobal, bool recursive = false)
        {
            if (varCollection == null)
            {
                throw new ArgumentNullException(nameof(varCollection));
            }
            if (var == null)
            {
                throw new ArgumentNullException(nameof(var));
            }
            CheckIfAdded(var);

            IndexReference assigned = varCollection.Assign(var, isGlobal);

            if (recursive)
            {
                assigned = new RecursiveIndexReference((IndexReference)assigned);
            }
            references.Add(var, assigned);

            return(assigned);
        }
Esempio n. 6
0
        public void Add(VarCollection varCollection, Var var, bool isGlobal, IWorkshopTree referenceValue, bool recursive = false)
        {
            if (varCollection == null)
            {
                throw new ArgumentNullException(nameof(varCollection));
            }
            if (var == null)
            {
                throw new ArgumentNullException(nameof(var));
            }

            // A gettable/settable variable
            if (var.Settable())
            {
                var assigned = varCollection.Assign(var, isGlobal);
                if (recursive)
                {
                    assigned = new RecursiveIndexReference(assigned);
                }
                references.Add(var, assigned);
            }

            // Element reference
            else if (var.VariableType == VariableType.ElementReference)
            {
                if (referenceValue == null)
                {
                    throw new ArgumentNullException(nameof(referenceValue));
                }
                references.Add(var, new WorkshopElementReference(referenceValue));
            }

            else
            {
                throw new NotImplementedException();
            }
        }