Class BasicLocalVariableMap

java.lang.Object
com.scriptbasic.memory.BasicLocalVariableMap
All Implemented Interfaces:
LocalVariableMap, VariableMap
Direct Known Subclasses:
MixedBasicVariableMap

public class BasicLocalVariableMap
extends java.lang.Object
implements LocalVariableMap
Handle the local variable in a hierarchical stack of Maps
Author:
Peter Verhas date June 22, 2012
  • Constructor Summary

    Constructors 
    Constructor Description
    BasicLocalVariableMap()  
  • Method Summary

    Modifier and Type Method Description
    protected java.lang.String converted​(java.lang.String name)
    Converts the name of a variable in a way that if the name 'a' and name 'b' results the same string converted then 'a' and 'b' are treated from some aspects as the same variable name.
    boolean currentScopeIsGlobal()
    Returns true if the current scope is global, there no no any local stack frame open.
    void dropFrame()
    Drop the current frame and pop off the map from the top of the stack.
    java.util.Set<java.lang.String> getVariableNameSet()
    Get the set of the variables stored in the variable map so that the caller can iterate through the variables.
    RightValue getVariableValue​(java.lang.String variableName)
    Get the value of a variable.
    boolean isGlobal​(java.lang.String variableName)  
    boolean isLocal​(java.lang.String variableName)  
    void newFrame()
    Push the actual Map onto the stack of maps and create a new empty stack.
    void registerGlobalVariable​(java.lang.String variableName)
    Register a variable as a global variable.
    void registerLocalVariable​(java.lang.String variableName)
    Define the variable as a local variable in a local environment.
    void setCaseFreak()
    Tell the object that the variable names has to be cased only one way.
    void setCaseIgnorant()
    Tell the object that the variable handling is case insensitive.
    void setCaseSensitive()
    Tell the object that the variable names are handled in a case sensitive way.
    void setVariable​(java.lang.String variableName, RightValue rightValue)
    Set the value of the variable.
    java.lang.Boolean variableDefined​(java.lang.String variableName)
    Checks that a variable exists and has a defined value in this map.
    java.lang.Boolean variableExists​(java.lang.String variableName)
    Checks that a variable exists in this map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.scriptbasic.interfaces.VariableMap

    getVariableNameSet
  • Constructor Details

  • Method Details

    • getVariableValue

      public RightValue getVariableValue​(java.lang.String variableName) throws ScriptBasicException
      Description copied from interface: VariableMap
      Get the value of a variable.
      Specified by:
      getVariableValue in interface VariableMap
      Parameters:
      variableName - parameter
      Returns:
      the right value the variable has or null if the variable is not defined or does not exists.
      Throws:
      ScriptBasicException - in case there is an exception
    • variableExists

      public java.lang.Boolean variableExists​(java.lang.String variableName) throws ScriptBasicException
      Description copied from interface: VariableMap
      Checks that a variable exists in this map.
      Specified by:
      variableExists in interface VariableMap
      Parameters:
      variableName - parameter
      Returns:
      true if the variable exists, even if the variable has undefined value. false if the variable does not exist in this map.
      Throws:
      ScriptBasicException - in case there is an exception
    • variableDefined

      public java.lang.Boolean variableDefined​(java.lang.String variableName) throws ScriptBasicException
      Description copied from interface: VariableMap
      Checks that a variable exists and has a defined value in this map. Undefined value is represented by null.
      Specified by:
      variableDefined in interface VariableMap
      Parameters:
      variableName - parameter
      Returns:
      true if the variable exists and has a defined value.
      Throws:
      ScriptBasicException - in case there is an exception
    • setVariable

      public void setVariable​(java.lang.String variableName, RightValue rightValue) throws ScriptBasicException
      Description copied from interface: VariableMap
      Set the value of the variable. If the variable did not exist then the variable is automatically created. If the variable had undefined value then the new value will be the one defined by the argument.

      You can set a variable to undefined passing null to this method as rightValue.

      Specified by:
      setVariable in interface VariableMap
      Parameters:
      variableName - parameter
      rightValue - the new value of the variable
      Throws:
      ScriptBasicException - in case there is an exception
    • newFrame

      public void newFrame()
      Description copied from interface: LocalVariableMap
      Push the actual Map onto the stack of maps and create a new empty stack. This is what a program has to do when a function is called. The local variables (if any) are pushed on the stack and a new stack frame is opened.
      Specified by:
      newFrame in interface LocalVariableMap
    • dropFrame

      public void dropFrame()
      Description copied from interface: LocalVariableMap
      Drop the current frame and pop off the map from the top of the stack. This is what programs have to do when a RETURN is executed from a function.
      Specified by:
      dropFrame in interface LocalVariableMap
    • currentScopeIsGlobal

      public boolean currentScopeIsGlobal()
      Returns true if the current scope is global, there no no any local stack frame open.
      Returns:
      return value
    • registerGlobalVariable

      public void registerGlobalVariable​(java.lang.String variableName) throws ScriptBasicException
      Description copied from interface: LocalVariableMap
      Register a variable as a global variable. This is needed when an implementing class manages the global as well as the local variables.

      Interpreters may handle variables differently. When a variable is not defined using some keyword as 'LOCAL' or 'GLOBAL' then the interpreter may treat the variable default local, may treat default global or may raise error. This is configurable in ScriptBasic.

      This method can be used to declare that a variable is used in the local environment referring to the global variable.

      Specified by:
      registerGlobalVariable in interface LocalVariableMap
      Parameters:
      variableName - parameter
      Throws:
      ScriptBasicException - in case of exception
    • registerLocalVariable

      public void registerLocalVariable​(java.lang.String variableName) throws ScriptBasicException
      Description copied from interface: LocalVariableMap
      Define the variable as a local variable in a local environment. For more information see the documentation of the method LocalVariableMap.registerGlobalVariable(String)
      Specified by:
      registerLocalVariable in interface LocalVariableMap
      Parameters:
      variableName - parameter
      Throws:
      ScriptBasicException - in case of exception
    • isGlobal

      public boolean isGlobal​(java.lang.String variableName) throws ScriptBasicException
      Throws:
      ScriptBasicException
    • isLocal

      public boolean isLocal​(java.lang.String variableName) throws ScriptBasicException
      Throws:
      ScriptBasicException
    • getVariableNameSet

      public java.util.Set<java.lang.String> getVariableNameSet()
      Description copied from interface: VariableMap
      Get the set of the variables stored in the variable map so that the caller can iterate through the variables.
      Specified by:
      getVariableNameSet in interface VariableMap
      Returns:
      return value
    • setCaseSensitive

      public void setCaseSensitive()
      Tell the object that the variable names are handled in a case sensitive way. It means that the variables 'vaRIable' and 'variABLE' are two different variables and can live in the global or the actual local variable name space along with each other in peace.

      It is not recommended to use this mode, however most modern programming languages do this.

    • setCaseFreak

      public void setCaseFreak()
      Tell the object that the variable names has to be cased only one way. It means that there can not be a variable named "vaRIable' and 'variABLE' at the same time.

      It is the recommended behavior, however BASIC implementations rarely exhibit such variable name handling this is not the default.

    • setCaseIgnorant

      public void setCaseIgnorant()
      Tell the object that the variable handling is case insensitive. It means that the variables 'vaRIable' and 'variABLE' denote the same variable.

      It is not recommended to use this mode, however most BASIC implementation do this. This is the default behavior.

    • converted

      protected java.lang.String converted​(java.lang.String name)
      Converts the name of a variable in a way that if the name 'a' and name 'b' results the same string converted then 'a' and 'b' are treated from some aspects as the same variable name. The generic approach is simply upper casing.
      Parameters:
      name - parameter
      Returns:
      return value