Scripting language bundle

From GNUstepWiki
Jump to navigation Jump to search

Scripting language bundle is, well, a bundle that offers a language for StepTalk. The bundle provides mechanisms for understanding the language and interpreting it.

Core of a language bundle is a scripting engine. Each language should have its own engine that executes scripts. The engine should take into account a context, where the script will be executed, and the script itself. After the execution, the engine should provide some feedback, for example a result object or it should throw an exception when something went wrong.

Few steps for creating a custom language bundle:

  1. take a language template from StepTalk sources or create one of your own
  2. create your own language engine as a subclass of the STEngine
  3. implement method executeCode:inEnvironment:
  4. provide an information list about the bundle

Language Template

Template for a language bundle can be found here.

Implementation

Header file for a custom language should look like this:

#import <StepTalk/STEngine.h>

@interface MyLanguageEngine:STEngine
{
    /* ... */
}
@end

And the implementation:

#import "MyLanguageEngine.h"

@implementation MyLanguageEngine
- (id)  executeCode:(NSString *)sourceCode
      inEnvironment:(STEnvironment *)env
{
    id retval = nil;

    /* execute the code sourceCode using environment env */

    /* retval = return value from the interpreter; */
    return retval;
}
@end

You may want to use [environment objectDictionary] to get all environment's named objects.

To translate a selector (for example a symbolic selector) you can use [environment translateSelector:aString forReceiver:anObject] which returns a NSString containing translated selector or nil if the selector should not be sent (because it should be fobidden by the environment). Moreover you should use this method if you want to use restrictions of selectors of the scripting envornment. See STEnvironment and STEnvironmentDescription for more information.

If your language can execute scripts from files, you may specify the file type in the MyLanguageInfo.plist. Then script manager and StepTalk tools can execute files with an appropriate language engine.

{
    /* Array of file types of your language scripts */
    STFileTypes = ( "mylang" );
}

Proposed Changes

- (id)  executeCode:(NSString *)sourceCode
      inEnvironment:(STEnvironment *)env

is going to be renamed to

- (id)interpretScript:(NSString *)sourceCode
            inContext:(STContext *)context

STLanguageManager

New class is going to be added: STLanguageManager

+ defaultManager
- registerLanguage:engineClassName:info:
- registerLanguagesFromBundle:
- availableLanguages
- removeLanguage:

- createEngineForLanguage:
- engineClassForLanguage:

- infoForLanguage:
- bundleForLanguage:
- languageForFileType:

Class STLanguage is going to be removed as well as language related methods from STEngine.