Class Scope


  • public final class Scope
    extends java.lang.Object
    A script scope, stores the declaration of parameters and local variables as symbols.

    This also acts as the functional scope and variable definition store.

    Since:
    3.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.Integer,​java.lang.Integer> capturedVariables
      The map of local captured variables to parent scope variables, ie closure.
      private static java.lang.String[] EMPTY_STRS
      The empty string array.
      private LexicalScope lexicalVariables
      Let symbols.
      private java.util.Map<java.lang.String,​java.lang.Integer> namedVariables
      The map of named variables aka script parameters and local variables.
      private Scope parent
      The parent scope.
      private int parms
      The number of parameters.
      (package private) static java.lang.Object UNDECLARED
      The value of an as-yet undeclared but variable, for instance: x; before var x;.
      (package private) static java.lang.Object UNDEFINED
      The value of a declared but undefined variable, for instance: var x;.
      private int vars
      The number of local variables.
    • Constructor Summary

      Constructors 
      Constructor Description
      Scope​(Scope scope, java.lang.String... parameters)
      Creates a new scope with a list of parameters.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addLexical​(int s)
      Marks a symbol as a lexical, declared through let or const.
      Frame createFrame​(Frame frame, java.lang.Object... args)
      Creates a frame by copying values up to the number of parameters.
      int declareParameter​(java.lang.String name)
      Declares a parameter.
      int declareVariable​(java.lang.String name)
      Declares a local variable.
      int getArgCount()
      Gets the (maximum) number of arguments this script expects.
      java.lang.Integer getCaptured​(int symbol)
      Gets the captured index of a given symbol, ie the target index of a symbol in a child scope.
      int getCaptureDeclaration​(int symbol)
      Gets the index of a captured symbol, ie the source index of a symbol in a parent scope.
      java.lang.String[] getCapturedVariables()
      Gets this script captured symbols names, i.e.
      java.lang.String[] getLocalVariables()
      Gets this script local variable, i.e.
      java.lang.String[] getParameters()
      Gets this script parameters, i.e.
      (package private) java.lang.String[] getParameters​(int bound)
      Gets this script parameters.
      (package private) Scope getParent()  
      java.lang.Integer getSymbol​(java.lang.String name)
      Checks whether an identifier is a local variable or argument, ie a symbol.
      private java.lang.Integer getSymbol​(java.lang.String name, boolean capture)
      Checks whether an identifier is a local variable or argument, ie a symbol.
      java.lang.String[] getSymbols()
      Gets this script symbols names, i.e.
      boolean isCapturedSymbol​(int symbol)
      Checks whether a given symbol is captured.
      boolean isLexical​(int s)
      Checks whether a symbol is declared through a let or const.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNDECLARED

        static final java.lang.Object UNDECLARED
        The value of an as-yet undeclared but variable, for instance: x; before var x;.
      • UNDEFINED

        static final java.lang.Object UNDEFINED
        The value of a declared but undefined variable, for instance: var x;.
      • EMPTY_STRS

        private static final java.lang.String[] EMPTY_STRS
        The empty string array.
      • parent

        private final Scope parent
        The parent scope.
      • parms

        private int parms
        The number of parameters.
      • vars

        private int vars
        The number of local variables.
      • namedVariables

        private java.util.Map<java.lang.String,​java.lang.Integer> namedVariables
        The map of named variables aka script parameters and local variables. Each parameter is associated to a symbol and is materialized as an offset in the stacked array used during evaluation.
      • capturedVariables

        private java.util.Map<java.lang.Integer,​java.lang.Integer> capturedVariables
        The map of local captured variables to parent scope variables, ie closure.
      • lexicalVariables

        private LexicalScope lexicalVariables
        Let symbols.
    • Constructor Detail

      • Scope

        public Scope​(Scope scope,
                     java.lang.String... parameters)
        Creates a new scope with a list of parameters.
        Parameters:
        scope - the parent scope if any
        parameters - the list of parameters
    • Method Detail

      • addLexical

        public boolean addLexical​(int s)
        Marks a symbol as a lexical, declared through let or const.
        Parameters:
        s - the symbol
        Returns:
        true if the symbol was not already present in the lexical set
      • createFrame

        public Frame createFrame​(Frame frame,
                                 java.lang.Object... args)
        Creates a frame by copying values up to the number of parameters.

        This captures the captured variables values.

        Parameters:
        frame - the caller frame
        args - the arguments
        Returns:
        the arguments array
      • declareParameter

        public int declareParameter​(java.lang.String name)
        Declares a parameter.

        This method creates an new entry in the symbol map.

        Parameters:
        name - the parameter name
        Returns:
        the register index storing this variable
      • declareVariable

        public int declareVariable​(java.lang.String name)
        Declares a local variable.

        This method creates an new entry in the symbol map.

        Parameters:
        name - the variable name
        Returns:
        the register index storing this variable
      • getArgCount

        public int getArgCount()
        Gets the (maximum) number of arguments this script expects.
        Returns:
        the number of parameters
      • getCaptured

        public java.lang.Integer getCaptured​(int symbol)
        Gets the captured index of a given symbol, ie the target index of a symbol in a child scope.
        Parameters:
        symbol - the symbol index
        Returns:
        the target symbol index or null if the symbol is not captured
      • getCaptureDeclaration

        public int getCaptureDeclaration​(int symbol)
        Gets the index of a captured symbol, ie the source index of a symbol in a parent scope.
        Parameters:
        symbol - the symbol index
        Returns:
        the source symbol index or -1 if the symbol is not captured
      • getCapturedVariables

        public java.lang.String[] getCapturedVariables()
        Gets this script captured symbols names, i.e. local variables defined in outer scopes and used by this scope.
        Returns:
        the captured names
      • getLocalVariables

        public java.lang.String[] getLocalVariables()
        Gets this script local variable, i.e. symbols assigned to local variables excluding captured variables.
        Returns:
        the local variable names
      • getParameters

        public java.lang.String[] getParameters()
        Gets this script parameters, i.e. symbols assigned before creating local variables.
        Returns:
        the parameter names
      • getParameters

        java.lang.String[] getParameters​(int bound)
        Gets this script parameters.
        Parameters:
        bound - number of known bound parameters (curry)
        Returns:
        the parameter names
      • getParent

        Scope getParent()
      • getSymbol

        public java.lang.Integer getSymbol​(java.lang.String name)
        Checks whether an identifier is a local variable or argument, ie a symbol. If this fails, look in parents for symbol that can be captured.
        Parameters:
        name - the symbol name
        Returns:
        the symbol index
      • getSymbol

        private java.lang.Integer getSymbol​(java.lang.String name,
                                            boolean capture)
        Checks whether an identifier is a local variable or argument, ie a symbol.
        Parameters:
        name - the symbol name
        capture - whether solving by capturing a parent symbol is allowed
        Returns:
        the symbol index
      • getSymbols

        public java.lang.String[] getSymbols()
        Gets this script symbols names, i.e. parameters and local variables.
        Returns:
        the symbol names
      • isCapturedSymbol

        public boolean isCapturedSymbol​(int symbol)
        Checks whether a given symbol is captured.
        Parameters:
        symbol - the symbol number
        Returns:
        true if captured, false otherwise
      • isLexical

        public boolean isLexical​(int s)
        Checks whether a symbol is declared through a let or const.
        Parameters:
        s - the symbol
        Returns:
        true if symbol was declared through let or const