Esempio n. 1
0
 public RecursiveIndexReference(IndexReference reference)
 {
     this.WorkshopVariable = reference.WorkshopVariable;
     this.Index            = reference.Index;
     this.ArrayBuilder     = reference.ArrayBuilder;
 }
Esempio n. 2
0
 public SubroutineInfo(Subroutine routine, FunctionBuildController functionBuilder, IndexReference[] parameterStores, IndexReference objectStore)
 {
     Subroutine      = routine;
     FunctionBuilder = functionBuilder;
     ParameterStores = parameterStores;
     ObjectStore     = objectStore;
 }
Esempio n. 3
0
 public WorkshopArrayBuilder(WorkshopVariable constructor, IndexReference store)
 {
     Constructor = constructor;
     Store       = store;
 }
 public SubroutineInfo(Subroutine routine, ReturnHandler returnHandler, TranslateRule rule, IndexReference[] parameterStores, IndexReference objectStore)
 {
     Subroutine      = routine;
     ReturnHandler   = returnHandler;
     Rule            = rule;
     ParameterStores = parameterStores;
     ObjectStore     = objectStore;
 }
Esempio n. 5
0
 public void Init(DeltinScript deltinScript)
 {
     ClassIndexes = deltinScript.VarCollection.Assign(ClassIndexesTag, true, false);
     deltinScript.InitialGlobal.ActionSet.AddAction(ClassIndexes.SetVariable(0, null, Constants.MAX_ARRAY_LENGTH));
 }
        public ExpressionTreeParseResult ParseTree(ActionSet actionSet, bool expectingValue, bool asElement)
        {
            IGettable        resultingVariable = null;
            IWorkshopTree    target            = null;
            IWorkshopTree    result            = null;
            VarIndexAssigner currentAssigner   = actionSet.IndexAssigner;
            IndexReference   currentObject     = null;

            Element[] resultIndex = new Element[0];

            for (int i = 0; i < Tree.Length; i++)
            {
                bool          isLast  = i == Tree.Length - 1;
                IWorkshopTree current = null;
                if (Tree[i] is CallVariableAction)
                {
                    var callVariableAction = (CallVariableAction)Tree[i];

                    var reference = currentAssigner[callVariableAction.Calling];
                    current = reference.GetVariable((Element)target);

                    resultIndex = new Element[callVariableAction.Index.Length];
                    for (int ai = 0; ai < callVariableAction.Index.Length; ai++)
                    {
                        var workshopIndex = callVariableAction.Index[ai].Parse(actionSet);
                        resultIndex[ai] = (Element)workshopIndex;
                        current         = Element.Part <V_ValueInArray>(current, workshopIndex);
                    }

                    // If this is the last node in the tree, set the resulting variable.
                    if (isLast)
                    {
                        resultingVariable = reference;
                    }
                }
                else
                {
                    var newCurrent = Tree[i].Parse(actionSet.New(currentAssigner).New(currentObject), asElement);
                    if (newCurrent != null)
                    {
                        current     = newCurrent;
                        resultIndex = new Element[0];
                    }
                }

                if (Tree[i].Type() == null)
                {
                    // If this isn't the last in the tree, set it as the target.
                    if (!isLast)
                    {
                        target = current;
                    }
                    currentObject = null;
                }
                else
                {
                    currentObject = Tree[i].Type().GetObjectSource(actionSet.Translate.DeltinScript, current);

                    if (Tree[i].Type() is DefinedType)
                    {
                        currentAssigner = actionSet.IndexAssigner.CreateContained();
                        var definedType = ((DefinedType)Tree[i].Type());

                        // Assign the object variables indexes.
                        definedType.AddObjectVariablesToAssigner(currentObject, currentAssigner);
                    }
                }

                result = current;
            }

            if (result == null && expectingValue)
            {
                throw new Exception("Expression tree result is null");
            }
            return(new ExpressionTreeParseResult(result, resultIndex, target, resultingVariable));
        }
Esempio n. 7
0
        // Sets up single-instance methods for methods with the 'rule' attribute.
        public void SetupSubroutine()
        {
            if (subroutineInfo != null || !IsSubroutine)
            {
                return;
            }

            // Setup the subroutine element.
            Subroutine subroutine = parseInfo.TranslateInfo.SubroutineCollection.NewSubroutine(Name);

            // Create the rule.
            TranslateRule subroutineRule = new TranslateRule(parseInfo.TranslateInfo, subroutine, SubroutineName, subroutineDefaultGlobal);

            // Setup the return handler.
            ReturnHandler returnHandler = new ReturnHandler(subroutineRule.ActionSet, Name, multiplePaths || Attributes.Virtual);
            ActionSet     actionSet     = subroutineRule.ActionSet.New(returnHandler).New(subroutineRule.ActionSet.IndexAssigner.CreateContained());

            // Get the variables that will be used to store the parameters.
            IndexReference[] parameterStores = new IndexReference[ParameterVars.Length];
            for (int i = 0; i < ParameterVars.Length; i++)
            {
                // Create the workshop variable the parameter will be stored as.
                IndexReference indexResult = actionSet.IndexAssigner.AddIndexReference(actionSet.VarCollection, ParameterVars[i], subroutineDefaultGlobal, Attributes.Recursive);
                parameterStores[i] = indexResult;

                // Assign virtual variables to the index reference.
                foreach (Var virtualParameterOption in VirtualVarGroup(i))
                {
                    actionSet.IndexAssigner.Add(virtualParameterOption, indexResult);
                }
            }

            // If the subroutine is an object function inside a class, create a variable to store the class object.
            IndexReference objectStore = null;

            if (Attributes.ContainingType != null && !Static)
            {
                objectStore = actionSet.VarCollection.Assign("_" + Name + "_subroutineStore", true, !Attributes.Recursive);

                // Set the objectStore as an empty array if the subroutine is recursive.
                if (Attributes.Recursive)
                {
                    actionSet.InitialSet().AddAction(objectStore.SetVariable(new V_EmptyArray()));
                    Attributes.ContainingType.AddObjectVariablesToAssigner(Element.Part <V_LastOf>(objectStore.GetVariable()), actionSet.IndexAssigner);
                    actionSet = actionSet.New(Element.Part <V_LastOf>(objectStore.GetVariable())).PackThis();
                }
                else
                {
                    Attributes.ContainingType.AddObjectVariablesToAssigner(objectStore.GetVariable(), actionSet.IndexAssigner);
                    actionSet = actionSet.New(objectStore.GetVariable()).PackThis();
                }
            }

            // Set the subroutine info.
            subroutineInfo = new SubroutineInfo(subroutine, returnHandler, parameterStores, objectStore);

            MethodBuilder builder = new MethodBuilder(this, actionSet, returnHandler);

            builder.BuilderSet = builder.BuilderSet.New(Attributes.Recursive);
            builder.ParseInner();

            // Apply returns.
            returnHandler.ApplyReturnSkips();

            // Pop object array and parameters if recursive.
            if (Attributes.Recursive)
            {
                if (objectStore != null)
                {
                    actionSet.AddAction(objectStore.ModifyVariable(Operation.RemoveFromArrayByIndex, Element.Part <V_CountOf>(objectStore.GetVariable()) - 1));
                }
                RecursiveStack.PopParameterStacks(actionSet, ParameterVars);
            }

            // Add the subroutine.
            Rule translatedRule = subroutineRule.GetRule();

            parseInfo.TranslateInfo.WorkshopRules.Add(translatedRule);

            var codeLens = new ElementCountCodeLens(DefinedAt.range, parseInfo.TranslateInfo.OptimizeOutput);

            parseInfo.Script.AddCodeLensRange(codeLens);
            codeLens.RuleParsed(translatedRule);
        }