Class FrameStack

java.lang.Object
org.eclipse.epsilon.eol.execute.context.FrameStack
All Implemented Interfaces:
Cloneable, ConcurrentBaseDelegate<FrameStack>, BaseDelegate<FrameStack>

public class FrameStack extends Object implements 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:
  • Field Details

    • globals

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

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

      protected Map<String,Variable> builtInVariables
    • base

      protected FrameStack base
    • isConcurrent

      protected boolean isConcurrent
  • Constructor Details

    • 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 Details

    • 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
    • enterGlobal

      public Frame enterGlobal(FrameType type, ModuleElement entryPoint, Map<String,?> variables)
      Parameters:
      type -
      entryPoint -
      variables -
      Returns:
      Since:
      1.6
      See Also:
    • enterLocal

      public Frame enterLocal(FrameType type, ModuleElement entryPoint, Map<String,?> variables)
      Parameters:
      type -
      entryPoint -
      variables -
      Returns:
      Since:
      1.6
      See Also:
    • 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(Map<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(Map.Entry<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(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(String name, 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(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(Collection<String> variables)
      Removes the variables from the topmost frame of the scope.
      Parameters:
      variablesThe - variables to remove.
      Since:
      1.6
    • get

      public Variable get(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(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(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(String name)
      Returns true if a variable with the specified name exists in the scope
      Parameters:
      name -
      Returns:
    • containsLocal

      public boolean containsLocal(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(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 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 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 Object
    • toString

      public String toString()
      Overrides:
      toString in class 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
    • getBase

      public FrameStack getBase()
      Specified by:
      getBase in interface BaseDelegate<FrameStack>
      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()
    • isThreadSafe

      public boolean isThreadSafe()
      Specified by:
      isThreadSafe in interface ConcurrentBaseDelegate<FrameStack>
      Since:
      1.6
    • setThreadSafe

      public void setThreadSafe(boolean concurrent)
      Specified by:
      setThreadSafe in interface ConcurrentBaseDelegate<FrameStack>
      Since:
      1.6
    • 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