Interface Environment

  • All Known Implementing Classes:
    DefaultEnvironment

    public interface Environment

    Blocks of code associated with an identifier are processed by their environment. An example of an environment is the subroutine environment. To declare a subroutine in sleep you use:

    sub identifier { commands; }

    When sleep encounters this code it looks for the environment bound to the keyword "sub". It passes the environment for "sub" a copy of the script instance, the identifier, and the block of executable code. The environment can do anything it wants with this information. The subroutine environment simply creates a Function object with the block of code and installs it into the environment. Thus allowing scripts to declare custom subroutines.

    In general a block of code is associated with an environment using the following syntax:

    keyword identifier { commands; } # sleep code

    Script environment bridge keywords should be registered with the script parser before any scripts are loaded. This can be accomplished as follows:

    ParserConfig.addKeyword("keyword");

    To install a new environment into the script environment:

     ScriptInstance script;              // assume
     Environment    myEnvironmentBridge; // assume
     
     Hashtable environment = script.getScriptEnvironment().getEnvironment();
     environment.put("keyword", myEnvironmentBridge);
     

    The Block object passed to the environment can be executed using:

    SleepUtils.runCode(commands, instance.getScriptEnvironment());

    Environment bridges are great for implementing different types of paradigms. I've used this feature to add everything from event driven scripting to popup menu structures to my application. Environments are a very powerful way to get the most out of integrating your application with the sleep language.

    See Also:
    ParserConfig.addKeyword(String)
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void bindFunction​(ScriptInstance si, java.lang.String typeKeyword, java.lang.String functionName, Block functionBody)
      binds a function (functionName) of a certain type (typeKeyword) to the defined functionBody.
    • Method Detail

      • bindFunction

        void bindFunction​(ScriptInstance si,
                          java.lang.String typeKeyword,
                          java.lang.String functionName,
                          Block functionBody)
        binds a function (functionName) of a certain type (typeKeyword) to the defined functionBody.
        Parameters:
        typeKeyword - the keyword for the function. (i.e. sub)
        functionName - the function name (i.e. add)
        functionBody - the compiled body of the function (i.e. code to add 2 numbers)