Пример #1
0
        public void Translate(ActionSet actionSet)
        {
            // Add the if action.
            Element newIf = Element.If(Expression.Parse(actionSet));

            newIf.Comment = Comment;
            actionSet.AddAction(newIf);

            // Translate the if block.
            Block.Translate(actionSet);

            // Add the else-ifs.
            for (int i = 0; i < ElseIfs.Length; i++)
            {
                // Add the else-if action.
                actionSet.AddAction(Element.ElseIf(ElseIfs[i].Expression.Parse(actionSet)));

                // Translate the else-if block.
                ElseIfs[i].Block.Translate(actionSet);
            }

            // If there is an else block, translate it.
            if (ElseBlock != null)
            {
                actionSet.AddAction(Element.Else());
                ElseBlock.Translate(actionSet);
            }

            // Add the end of the if.
            var end = Element.End();

            end.Comment = EndComment;
            actionSet.AddAction(end);
        }
Пример #2
0
        public void NextCase(IWorkshopTree value)
        {
            // If the state is on a case and the action count was changed, create new skip that will skip to the end of the switch.
            if (AutoBreak && State == SwitchBuilderState.OnCase && actionSet.ActionCount != LastCaseStart)
            {
                // Create the skip and add it to the actionset.
                SkipStartMarker skipToEnd = new SkipStartMarker(actionSet);
                actionSet.AddAction(skipToEnd);

                // Add it to the list of skips that need to skip to the end.
                SkipToEnd.Add(skipToEnd);
            }

            // Update the state.
            State = SwitchBuilderState.OnCase;

            // Mark the start of the case.
            SkipEndMarker startCase = new SkipEndMarker();

            actionSet.AddAction(startCase);

            // Add the skip length to the start of the case to the skipCounts.
            skipCounts.Add(Skipper.GetSkipCount(startCase));

            // Add the skip value.
            skipValues.Add(value);

            // Update the number of actions.
            LastCaseStart = actionSet.ActionCount;
        }
        public static void Assign(ActionSet actionSet, Var var)
        {
            // Get the initial value.
            IWorkshopTree initialValue = new V_Number(0);

            if (var.InitialValue != null)
            {
                initialValue = var.InitialValue.Parse(actionSet);
            }

            // Add the variable to the assigner.
            actionSet.IndexAssigner.Add(actionSet.VarCollection, var, actionSet.IsGlobal, initialValue);

            // Set the initial value.
            if (var.Settable())
            {
                IndexReference reference = (IndexReference)actionSet.IndexAssigner[var];

                if (reference is RecursiveIndexReference recursive)
                {
                    actionSet.InitialSet().AddAction(recursive.Reset());
                    actionSet.AddAction(recursive.Push((Element)initialValue));
                }
                else
                {
                    actionSet.AddAction(reference.SetVariable((Element)initialValue));
                }
            }
        }
Пример #4
0
        public void Translate(ActionSet actionSet)
        {
            // Add the if action.
            actionSet.AddAction(Element.Part <A_If>(Expression.Parse(actionSet)));

            // Translate the if block.
            Block.Translate(actionSet.Indent());

            // Add the else-ifs.
            for (int i = 0; i < ElseIfs.Length; i++)
            {
                // Add the else-if action.
                actionSet.AddAction(Element.Part <A_ElseIf>(ElseIfs[i].Expression.Parse(actionSet)));

                // Translate the else-if block.
                ElseIfs[i].Block.Translate(actionSet.Indent());
            }

            // If there is an else block, translate it.
            if (ElseBlock != null)
            {
                actionSet.AddAction(new A_Else());
                ElseBlock.Translate(actionSet.Indent());
            }

            // Add the end of the if.
            actionSet.AddAction(new A_End());
        }
        /// <summary>The method was already called in the stack.</summary>
        private IWorkshopTree RecursiveCall(ActionSet actionSet, MethodCall methodCall)
        {
            // Push new parameters.
            for (int i = 0; i < method.ParameterVars.Length; i++)
            {
                var varReference = actionSet.IndexAssigner[method.ParameterVars[i]];
                if (varReference is RecursiveIndexReference)
                {
                    actionSet.AddAction(((RecursiveIndexReference)varReference).Push(
                                            (Element)methodCall.ParameterValues[i]
                                            ));
                }
            }

            // Add to the continue skip array.
            V_Number skipLength = new V_Number();

            actionSet.AddAction(continueArray.ModifyVariable(
                                    Operation.AppendToArray,
                                    skipLength
                                    ));

            // Restart the method.
            SkipStartMarker resetSkip = new SkipStartMarker(actionSet);

            resetSkip.SetEndMarker(endOfMethod);
            actionSet.AddAction(resetSkip);

            SkipEndMarker continueAtMarker = new SkipEndMarker();

            actionSet.AddAction(continueAtMarker);
            skipLength.Value = continueAt.NumberOfActionsToMarker(continueAtMarker);

            return(returnHandler.GetReturnedValue());
        }
Пример #6
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));
        }
Пример #7
0
        // Calls single-instance methods.
        private IWorkshopTree ParseSubroutine(ActionSet actionSet, MethodCall methodCall)
        {
            for (int i = 0; i < subroutineInfo.ParameterStores.Length; i++)
            {
                actionSet.AddAction(subroutineInfo.ParameterStores[i].SetVariable((Element)methodCall.ParameterValues[i]));
            }

            if (subroutineInfo.ObjectStore != null)
            {
                actionSet.AddAction(subroutineInfo.ObjectStore.SetVariable(actionSet.CurrentObject));
            }

            switch (methodCall.CallParallel)
            {
            // No parallel, call subroutine normally.
            case CallParallel.NoParallel:
                actionSet.AddAction(Element.Part <A_CallSubroutine>(subroutineInfo.Subroutine));
                return(subroutineInfo.ReturnHandler.GetReturnedValue());

            // Restart the subroutine if it is already running.
            case CallParallel.AlreadyRunning_RestartRule:
                actionSet.AddAction(Element.Part <A_StartRule>(subroutineInfo.Subroutine, EnumData.GetEnumValue(IfAlreadyExecuting.RestartRule)));
                return(null);

            // Do nothing if the subroutine is already running.
            case CallParallel.AlreadyRunning_DoNothing:
                actionSet.AddAction(Element.Part <A_StartRule>(subroutineInfo.Subroutine, EnumData.GetEnumValue(IfAlreadyExecuting.DoNothing)));
                return(null);

            default: throw new NotImplementedException();
            }
        }
        public void Translate(ActionSet actionSet)
        {
            IGettable var;
            Element   target = null;

            Element[] index;
            if (Tree != null)
            {
                ExpressionTreeParseResult treeParseResult = Tree.ParseTree(actionSet, true, true);
                var    = treeParseResult.ResultingVariable;
                target = (Element)treeParseResult.Target;
                index  = treeParseResult.ResultingIndex;
            }
            else
            {
                var   = actionSet.IndexAssigner[SetVariable.Calling];
                index = Array.ConvertAll(SetVariable.Index, index => (Element)index.Parse(actionSet));
            }

            Element value = null;

            if (Value != null)
            {
                value = (Element)Value.Parse(actionSet);
            }

            Elements.Operation?modifyOperation = null;
            switch (Operation)
            {
            case "=": break;

            case "^=": modifyOperation = Elements.Operation.RaiseToPower; break;

            case "*=": modifyOperation = Elements.Operation.Multiply;     break;

            case "/=": modifyOperation = Elements.Operation.Divide;       break;

            case "%=": modifyOperation = Elements.Operation.Modulo;       break;

            case "+=": modifyOperation = Elements.Operation.Add;          break;

            case "-=": modifyOperation = Elements.Operation.Subtract;     break;

            case "++": value = 1; modifyOperation = Elements.Operation.Add;      break;

            case "--": value = 1; modifyOperation = Elements.Operation.Subtract; break;

            default: throw new Exception($"Unknown operation {Operation}.");
            }

            if (modifyOperation == null)
            {
                actionSet.AddAction(((IndexReference)var).SetVariable(value, target, index));
            }
            else
            {
                actionSet.AddAction(((IndexReference)var).ModifyVariable((Elements.Operation)modifyOperation, value, target, index));
            }
        }
Пример #9
0
        public override void Translate(ActionSet actionSet)
        {
            WorkshopVariable variable;
            Element          target;
            Element          start;

            // Existing variable being used in for.
            if (VariableResolve != null)
            {
                VariableElements elements = VariableResolve.ParseElements(actionSet);
                variable = elements.IndexReference.WorkshopVariable;
                target   = elements.Target;
                start    = (Element)InitialResolveValue?.Parse(actionSet) ?? new V_Number(0);
            }
            // New variable being use in for.
            else
            {
                actionSet.IndexAssigner.Add(actionSet.VarCollection, DefinedVariable, actionSet.IsGlobal, null);
                variable = ((IndexReference)actionSet.IndexAssigner[DefinedVariable]).WorkshopVariable;
                target   = new V_EventPlayer();
                start    = (Element)DefinedVariable.InitialValue?.Parse(actionSet) ?? new V_Number(0);
            }

            Element stop = (Element)Stop.Parse(actionSet);
            Element step = (Element)Step.Parse(actionSet);

            // Global
            if (variable.IsGlobal)
            {
                actionSet.AddAction(Element.Part <A_ForGlobalVariable>(
                                        variable,
                                        start, stop, step
                                        ));
            }
            // Player
            else
            {
                actionSet.AddAction(Element.Part <A_ForPlayerVariable>(
                                        target,
                                        variable,
                                        start, stop, step
                                        ));
            }

            // Translate the block.
            Block.Translate(actionSet.Indent());

            // Resolve continues.
            ResolveContinues(actionSet);

            // Cap the for.
            actionSet.AddAction(new A_End());

            // Resolve breaks.
            ResolveBreaks(actionSet);
        }
Пример #10
0
 public void GetClassIndex(int classIdentifier, IndexReference classReference, ActionSet actionSet)
 {
     actionSet.AddAction(classReference.SetVariable(
                             Element.IndexOfArrayValue(ClassIndexes.Get(), 0)
                             ));
     actionSet.AddAction(ClassIndexes.SetVariable(
                             classIdentifier,
                             null,
                             (Element)classReference.GetVariable()
                             ));
 }
Пример #11
0
        public ForeachBuilder(ActionSet actionSet, IWorkshopTree array)
        {
            ActionSet  = actionSet;
            IndexStore = actionSet.VarCollection.Assign("foreachIndex,", actionSet.IsGlobal, true);
            Array      = array;
            Condition  = new V_Compare(IndexStore.GetVariable(), Operators.LessThan, Element.Part <V_CountOf>(Array));
            Index      = (Element)IndexStore.GetVariable();
            IndexValue = Element.Part <V_ValueInArray>(Array, IndexStore.GetVariable());

            actionSet.AddAction(IndexStore.SetVariable(0));
            actionSet.AddAction(Element.Part <A_While>(Condition));
        }
Пример #12
0
 public void AddBreak(ActionSet actionSet)
 {
     if (RawBreak)
     {
         actionSet.AddAction(new A_Break());
     }
     else
     {
         SkipStartMarker breaker = new SkipStartMarker(actionSet);
         actionSet.AddAction(breaker);
         Break.Add(breaker);
     }
 }
Пример #13
0
 public void AddContinue(ActionSet actionSet)
 {
     if (RawContinue)
     {
         actionSet.AddAction(new A_Continue());
     }
     else
     {
         SkipStartMarker continuer = new SkipStartMarker(actionSet);
         actionSet.AddAction(continuer);
         Continue.Add(continuer);
     }
 }
Пример #14
0
        public override void Translate(ActionSet actionSet)
        {
            int actionCountPreCondition = actionSet.ActionCount;

            Element condition    = (Element)Condition.Parse(actionSet);
            bool    actionsAdded = actionSet.ActionCount > actionCountPreCondition;

            if (!actionsAdded)
            {
                // Create a normal while loop.
                actionSet.AddAction(Element.Part <A_While>(condition));

                // Translate the block.
                Block.Translate(actionSet.Indent());

                // Resolve continues.
                ResolveContinues(actionSet);

                // Cap the block.
                actionSet.AddAction(new A_End());

                // Resolve breaks.
                ResolveBreaks(actionSet);
            }
            else
            {
                // The while condition requires actions to get the value.
                actionSet.ActionList.Insert(actionCountPreCondition, new ALAction(Element.Part <A_While>(new V_True())));

                SkipStartMarker whileEndSkip = new SkipStartMarker(actionSet, condition);
                actionSet.Indent().AddAction(whileEndSkip);

                // Translate the block.
                Block.Translate(actionSet.Indent());

                // Resolve continues.
                ResolveContinues(actionSet);

                // Cap the block.
                actionSet.AddAction(new A_End());

                // Skip to the end when the condition is false.
                SkipEndMarker whileEnd = new SkipEndMarker();
                whileEndSkip.SetEndMarker(whileEnd);
                actionSet.AddAction(whileEnd);

                // Resolve breaks.
                ResolveBreaks(actionSet);
            }
        }
 public void AddContinue(ActionSet actionSet, string comment)
 {
     if (RawContinue)
     {
         Element con = Element.Part("Continue");
         con.Comment = comment;
         actionSet.AddAction(con);
     }
     else
     {
         SkipStartMarker continuer = new SkipStartMarker(actionSet, comment);
         actionSet.AddAction(continuer);
         Continue.Add(continuer);
     }
 }
 public void AddBreak(ActionSet actionSet, string comment)
 {
     if (RawBreak)
     {
         Element brk = Element.Part("Break");
         brk.Comment = comment;
         actionSet.AddAction(brk);
     }
     else
     {
         SkipStartMarker breaker = new SkipStartMarker(actionSet, comment);
         actionSet.AddAction(breaker);
         Break.Add(breaker);
     }
 }
        public void Translate(ActionSet actionSet)
        {
            VariableElements elements = _resolve.ParseElements(actionSet);

            // Increment
            if (!_decrement)
            {
                actionSet.AddAction(SetVariableAction.CommentAll(_comment, elements.IndexReference.ModifyVariable(Operation.Add, 1, elements.Target, elements.Index)));
            }
            // Decrement
            else
            {
                actionSet.AddAction(SetVariableAction.CommentAll(_comment, elements.IndexReference.ModifyVariable(Operation.Subtract, 1, elements.Target, elements.Index)));
            }
        }
Пример #18
0
        public override void Translate(ActionSet actionSet)
        {
            if (DefinedVariable != null)
            {
                // Add the defined variable to the index assigner.
                actionSet.IndexAssigner.Add(actionSet.VarCollection, DefinedVariable, actionSet.IsGlobal, null);

                // Set the initial variable.
                if (actionSet.IndexAssigner[DefinedVariable] is IndexReference && DefinedVariable.InitialValue != null)
                {
                    actionSet.AddAction(((IndexReference)actionSet.IndexAssigner[DefinedVariable]).SetVariable(
                                            (Element)DefinedVariable.InitialValue.Parse(actionSet)
                                            ));
                }
            }
            else if (InitialVarSet != null)
            {
                InitialVarSet.Translate(actionSet);
            }

            // Get the condition.
            Element condition;

            if (Condition != null)
            {
                condition = (Element)Condition.Parse(actionSet);                    // User-define condition
            }
            else
            {
                condition = new V_True();  // No condition, just use true.
            }
            actionSet.AddAction(Element.Part <A_While>(condition));

            Block.Translate(actionSet.Indent());

            // Resolve continues.
            ResolveContinues(actionSet);

            if (SetVariableAction != null)
            {
                SetVariableAction.Translate(actionSet.Indent());
            }

            actionSet.AddAction(new A_End());

            // Resolve breaks.
            ResolveBreaks(actionSet);
        }
Пример #19
0
 public void AddBreak(ActionSet actionSet, string comment)
 {
     if (RawBreak)
     {
         actionSet.AddAction(new A_Break()
         {
             Comment = comment
         });
     }
     else
     {
         SkipStartMarker breaker = new SkipStartMarker(actionSet, comment);
         actionSet.AddAction(breaker);
         Break.Add(breaker);
     }
 }
Пример #20
0
 public void AddContinue(ActionSet actionSet, string comment)
 {
     if (RawContinue)
     {
         actionSet.AddAction(new A_Continue()
         {
             Comment = comment
         });
     }
     else
     {
         SkipStartMarker continuer = new SkipStartMarker(actionSet, comment);
         actionSet.AddAction(continuer);
         Continue.Add(continuer);
     }
 }
        public void AddBreak(ActionSet actionSet, string comment)
        {
            SkipStartMarker breaker = new SkipStartMarker(actionSet, comment);

            actionSet.AddAction(breaker);
            switchBuilder.SkipToEnd.Add(breaker);
        }
Пример #22
0
        public void Finish()
        {
            SkipEndMarker endMarker = new SkipEndMarker();

            ActionSet.AddAction(endMarker);
            SkipMarker.SkipCount = SkipMarker.GetSkipCount(endMarker);
        }
        public void Translate(ActionSet actionSet)
        {
            SkipStartMarker breaker = new SkipStartMarker(actionSet);

            actionSet.AddAction(breaker);
            BreakContainer.AddBreak(breaker);
        }
Пример #24
0
        public void Translate(ActionSet actionSet)
        {
            if (DefinedVariable != null)
            {
                // Add the defined variable to the index assigner.
                actionSet.IndexAssigner.Add(actionSet.VarCollection, DefinedVariable, actionSet.IsGlobal, null);

                // Set the initial variable.
                if (actionSet.IndexAssigner[DefinedVariable] is IndexReference && DefinedVariable.InitialValue != null)
                {
                    actionSet.AddAction(((IndexReference)actionSet.IndexAssigner[DefinedVariable]).SetVariable(
                                            (Element)DefinedVariable.InitialValue.Parse(actionSet)
                                            ));
                }
            }
            else if (InitialVarSet != null)
            {
                InitialVarSet.Translate(actionSet);
            }

            WhileBuilder whileBuilder = new WhileBuilder(actionSet, Condition?.Parse(actionSet));

            whileBuilder.Setup();

            Block.Translate(actionSet);

            if (SetVariableAction != null)
            {
                SetVariableAction.Translate(actionSet);
            }

            whileBuilder.Finish();
        }
        public void Translate(ActionSet actionSet)
        {
            SkipStartMarker continuer = new SkipStartMarker(actionSet);

            actionSet.AddAction(continuer);
            Loop.AddContinue(continuer);
        }
Пример #26
0
        public virtual void Return(Scope returningFromScope, ActionSet returningSet)
        {
            if (returningSet.IsRecursive)
            {
                returningFromScope.EndScope(returningSet, true);

                foreach (var recursiveIndexReference in AdditionalPopOnReturn)
                {
                    returningSet.AddAction(recursiveIndexReference.Pop());
                }
            }

            SkipStartMarker returnSkipStart = new SkipStartMarker(returningSet);

            returningSet.AddAction(returnSkipStart);
            _skips.Add(returnSkipStart);
        }
Пример #27
0
        public SwitchBuilder(ActionSet actionSet)
        {
            this.actionSet = actionSet;

            // Create the switch skipper.
            Skipper = new SkipStartMarker(actionSet);
            actionSet.AddAction(Skipper);
        }
Пример #28
0
        public virtual void Return(ActionSet returningSet)
        {
            if (returningSet.IsRecursive)
            {
                returningSet.RecursiveVariableTracker.PopAll();

                foreach (var recursiveIndexReference in AdditionalPopOnReturn)
                {
                    returningSet.AddAction(recursiveIndexReference.Pop());
                }
            }

            SkipStartMarker returnSkipStart = new SkipStartMarker(returningSet);

            returningSet.AddAction(returnSkipStart);
            _skips.Add(returnSkipStart);
        }
Пример #29
0
        public void Translate(ActionSet actionSet)
        {
            VariableElements elements = VariableResolve.ParseElements(actionSet);

            Element value = null;

            if (Value != null)
            {
                value = (Element)Value.Parse(actionSet);
            }

            Elements.Operation?modifyOperation = null;
            switch (Operation)
            {
            case "=": break;

            case "^=": modifyOperation = Elements.Operation.RaiseToPower; break;

            case "*=": modifyOperation = Elements.Operation.Multiply;     break;

            case "/=": modifyOperation = Elements.Operation.Divide;       break;

            case "%=": modifyOperation = Elements.Operation.Modulo;       break;

            case "+=": modifyOperation = Elements.Operation.Add;          break;

            case "-=": modifyOperation = Elements.Operation.Subtract;     break;

            case "++": value = 1; modifyOperation = Elements.Operation.Add;      break;

            case "--": value = 1; modifyOperation = Elements.Operation.Subtract; break;

            default: throw new Exception($"Unknown operation {Operation}.");
            }

            if (modifyOperation == null)
            {
                actionSet.AddAction(elements.IndexReference.SetVariable(value, elements.Target, elements.Index));
            }
            else
            {
                actionSet.AddAction(elements.IndexReference.ModifyVariable((Elements.Operation)modifyOperation, value, elements.Target, elements.Index));
            }
        }
        public void ReturnValue(IWorkshopTree value)
        {
            if (!MultiplePaths && ValueWasReturned)
            {
                throw new Exception("_multiplePaths is set as false and 2 expressions were returned.");
            }
            ValueWasReturned = true;

            // Multiple return paths.
            if (MultiplePaths)
            {
                ActionSet.AddAction(ReturnStore.SetVariable((Element)value));
            }
            // One return path.
            else
            {
                ReturningValue = value;
            }
        }