Class ScopeStack

java.lang.Object
org.ek9lang.compiler.common.ScopeStack

public class ScopeStack extends Object
This stack object is only designed to be used when first parsing. The idea is to actually associate the scope with the specific contexts by putting them in a ParseTreeProperty of Scope, this tree is held in the ParsedModule. So why do we need this stack? That is because as we listen/visit the parse tree we need to both create the scope and record it in the parsed module. But also put it on to a stack so that when we return for each level of the parse tree we can get back to the right scope. I don't want to use a 'getEnclosingScope' method on each scope as the very top level has to be thread safe. Plus it exposes information that should only be available to the scope itself - not any other bit of code. This class also has logic and code to be able to traverse back up the 'structure stack'.
  • Constructor Details

    • ScopeStack

      public ScopeStack(IScope base)
  • Method Details

    • getVeryBaseScope

      public IScope getVeryBaseScope()
    • push

      public IScope push(IScope scope)
      Push a scope on to this stack.
    • peek

      public IScope peek()
      Take a look at the top of the stack.
    • pop

      public IScope pop()
      Pop a scope of the stack (exception is empty.
    • isEmpty

      public boolean isEmpty()
      True if stack has no contents.
    • traverseBackUpStack

      public Optional<IScope> traverseBackUpStack(IScope.ScopeType scopeType)
      Navigates back up the scope stack to find the first match of the scope type passed in. This is useful because dynamic classes and functions do not use an enclosing scope of the scope they are in. They get pulled out to module level (by design).
    • traverseBackUpStackToMethodOrFunction

      public Optional<IScopedSymbol> traverseBackUpStackToMethodOrFunction()
      Locates the outer function or method (if possible, not always possible Applications for example).
    • traverseBackUpStackToEnclosingMethod

      public Optional<MethodSymbol> traverseBackUpStackToEnclosingMethod()
      Navigate back up the scope stack to find the first enclosing method (there may not be one). So if this were called in a function or an application for example - there will not be an enclosing method.
      Returns:
      The Optional method located.