Class FrameStack

  • All Implemented Interfaces:
    java.lang.Cloneable, ConcurrentBaseDelegate<FrameStack>, BaseDelegate<FrameStack>

    public class FrameStack
    extends java.lang.Object
    implements java.lang.Cloneable, ConcurrentBaseDelegate<FrameStack>

    A FrameStack is a stack of frames that stores the variables created during the execution of an EOL program.

    A FrameStack is divided into two distinct regions, one for global variables and one for local variables. The global region always contains at least one frame, while the local region can be empty.

    Version:
    1.4
    Author:
    Dimitrios Kolovos, Antonio García-Domínguez, Sina Madani
    See Also:
    Frame
    • Field Detail

      • globals

        protected org.eclipse.epsilon.eol.execute.context.FrameStackRegion globals
      • locals

        protected org.eclipse.epsilon.eol.execute.context.FrameStackRegion locals
      • builtInVariables

        protected java.util.Map<java.lang.String,​Variable> builtInVariables
      • isConcurrent

        protected boolean isConcurrent
    • Constructor Detail

      • FrameStack

        public FrameStack()
      • FrameStack

        public FrameStack​(FrameStack parent)
        Parameters:
        parent -
        Since:
        1.6
      • FrameStack

        public FrameStack​(FrameStack parent,
                          boolean concurrent)
        Parameters:
        parent -
        concurrent -
        Since:
        1.6
    • Method Detail

      • initializeState

        protected void initializeState()
        Called from constructor or clone() method.
      • dispose

        public void dispose()
      • enterGlobal

        public Frame enterGlobal​(FrameType type,
                                 ModuleElement entryPoint,
                                 Variable... variables)
        Enters a new global frame.
        Parameters:
        type - The type of the frame: variables in lower stack frames are visible from an FrameType.UNPROTECTED frame, and invisible from a FrameType.PROTECTED frame.
        entryPoint - The AST from which the entry is performed
        variables - Zero or more variables that will be added to the new frame
        Returns:
        The new Frame
      • enterLocal

        public Frame enterLocal​(FrameType type,
                                ModuleElement entryPoint,
                                Variable... variables)
        Enters a new local frame.
        Parameters:
        type - The type of the frame: variables in lower stack frames are visible from an FrameType.UNPROTECTED frame, and invisible from a FrameType.PROTECTED frame.
        entryPoint - The AST from which the entry is performed
        variables - Zero or more variables that will be added to the new frame
        Returns:
        The new Frame
      • leaveLocal

        public void leaveLocal​(ModuleElement entryPoint,
                               boolean dispose)
        Leaves the current local frame and returns to the previous frame in the stack. This method cannot leave a global stack frame: use #leaveGlobal(AST, boolean) for that.
      • leaveLocal

        public void leaveLocal​(ModuleElement entryPoint)
        Convenience method for {@link #leaveLocal(AST, boolean))} which disposes of the stack frame that was left.
      • leaveGlobal

        public void leaveGlobal​(ModuleElement entryPoint,
                                boolean dispose)
        Leaves the current global stack frame and returns to the previous frame in the stack. This method cannot leave a local stack frame: use #leaveLocal(AST, boolean) for that. This method will not leave the last remaining global stack frame.
      • leaveGlobal

        public void leaveGlobal​(ModuleElement entryPoint)
        Convenience method for #leaveGlobal(AST, boolean) which disposes of the global stack frame that was left.
      • put

        public void put​(java.util.Map<java.lang.String,​?> variables,
                        boolean readOnly)
        Converts the map into Variables and puts them in the topmost frame of the scope.
        Parameters:
        variables - The effective collection of variables.
        readOnly - Whether the Variables should be immutable.
        Since:
        1.6
      • put

        public void put​(java.util.Map.Entry<java.lang.String,​?> variable)
        Puts the Entry as read-only variable into the topmost frame of the scope.
        Parameters:
        variables -
        Since:
        1.6
      • put

        public void put​(java.util.Collection<Variable> variables)
      • put

        public void put​(Variable... variables)
        Puts one or more new variables in the topmost frame of the scope. Note that the topmost frame can be either a local or a global frame, depending on the current state of the FrameStack.
      • put

        public void put​(java.lang.String name,
                        java.lang.Object value)
        Puts a read-only variable into the topmost frame of the scope
        Parameters:
        name - The variable name.
        value - The variable value.
        Since:
        1.6
      • put

        public void put​(Variable variable)
        Puts a new variable in the topmost frame of the scope. Note that the topmost frame can be either a local or a global frame, depending on the current state of the FrameStack.
      • putGlobal

        public void putGlobal​(Variable... variables)
        Puts one or more new variables in the topmost global stack frame.
      • putGlobal

        public void putGlobal​(Variable variable)
        Puts a new variable in the topmost global stack frame.
      • remove

        public void remove​(java.lang.String variable)
        Removes a variable by name from the topmost frame of the scope. Note that the topmost frame can be either a local or a global frame, depending on the current state of the FrameStack.
      • remove

        public void remove​(java.util.Collection<java.lang.String> variables)
        Removes the variables from the topmost frame of the scope.
        Parameters:
        variablesThe - variables to remove.
        Since:
        1.6
      • get

        public Variable get​(java.lang.String name)
        Returns the variable with the specified name and if it does not exist returns null. Note that variables in a higher frame shadow variables with the same name in lower frames. Similarly, local variables shadow global variables with the same name.
        Parameters:
        name - The name of the variable
        Returns:
        The variable with the specified name or null
      • getLocal

        public Variable getLocal​(java.lang.String name)

        Returns the local variable with the specified name If the local variable does not exist, this method returns null.

        Note: this method does not respect the usual shadowing semantics of the FrameStack, and consequently most clients should call get(String) (i.e., only call this method if you really know what you are doing!)

        Parameters:
        name - The name of the local variable
        Returns:
        The local variable with the specified name or null
      • getGlobal

        public Variable getGlobal​(java.lang.String name)

        Returns the global variable with the specified name If the global variable does not exist, this method returns null.

        Note: this method does not respect the usual shadowing semantics of the FrameStack, and consequently most clients should call get(String) (i.e., only call this method if you really know what you are doing!)

        Parameters:
        name - The name of the global variable
        Returns:
        The global variable with the specified name or null
      • isInLoop

        public boolean isInLoop()
      • contains

        public boolean contains​(java.lang.String name)
        Returns true if a variable with the specified name exists in the scope
        Parameters:
        name -
        Returns:
      • containsLocal

        public boolean containsLocal​(java.lang.String name)
        Returns true if a local variable with the specified name exists.

        Note: this method does not respect the usual shadowing semantics of the FrameStack, and consequently most clients should call contains(String) (i.e., only call this method if you really know what you are doing!)

        Parameters:
        name -
        Returns:
      • containsGlobal

        public boolean containsGlobal​(java.lang.String name)
        Returns true if a global variable with the specified name exists.

        Note: this method does not respect the usual shadowing semantics of the FrameStack, and consequently most clients should call contains(String) (i.e., only call this method if you really know what you are doing!)

        Parameters:
        name -
        Returns:
      • getFrames

        public java.util.List<SingleFrame> getFrames()
        Returns a list with all local (from top to bottom) and global (from top to bottom) stack frames, in that order.
      • getFrames

        public java.util.List<SingleFrame> getFrames​(boolean includeBase)
        Parameters:
        includeBase -
        Returns:
        Since:
        1.6
      • getDepth

        public int getDepth()
        WARNING: Do not call if isThreadSafe() is true whilst in use. Failure to comply may result in infinite waiting!
        Returns:
      • clone

        public FrameStack clone()
        Overrides:
        clone in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getTopFrame

        public Frame getTopFrame()
      • getCurrentStatement

        public ModuleElement getCurrentStatement()
      • setCurrentStatement

        public void setCurrentStatement​(ModuleElement ast)
      • countGlobalFrames

        protected int countGlobalFrames()
      • putAll

        public void putAll​(FrameStack other)
        Copies the references of all variables in the given FrameStack into this FrameStack.
        Parameters:
        other - The FrameStack to copy from.
        Since:
        1.6
      • setBase

        public void setBase​(FrameStack parent)
        Parameters:
        parent -
        Since:
        1.6
      • size

        public int size​(boolean includeBase)
        Parameters:
        includeBase -
        Returns:
        Since:
        1.6
      • size

        public int size()
      • merge

        public void merge​(BaseDelegate.MergeMode mode)
        Adds all of this FrameStack's frames into its base FrameStack, or vice-versa.
        Specified by:
        merge in interface BaseDelegate<FrameStack>
        Parameters:
        mode - Whether to merge from base to this, or from this to base.
        Since:
        1.6
      • mergeFrameStacks

        protected static void mergeFrameStacks​(FrameStack from,
                                               FrameStack to)
        Adds all the frames and variables from the first argument to the second one.
        Since:
        1.6