How to write your own interpreter

If you want to use something other than IronPython for your console, such as IronScheme, Lua, or some other script language you've designed yourself, all you have to do is make a class that implements the IInterpreter interface. The interface defines three methods:

public string GetPrompt();

This returns the current prompt. This exists to allow multiple prompts, for things like multiline input, etc.

public string Execute(string str);

The main method. This takes a command and returns the results, to be printed by the console.

public void AddGlobal(string str, object obj);

This adds a global variable, to make it easier to get things done, as opposed to typing in the fully qualified variable name.

Note: this is a method, so it copies the object. Thus, it's only useful for reference types, such as object, and not value types such as int. In theory, you could change it to use the ref modifier, to make it pass by reference, but IronPython doesn't support that, so I didn't bother.

When you're ready to use your interpreter, you can either pass it to the constructor of a new XnaConsole object, or pass it as an argument to the XnaConsole.SetInterpreter() method.

If you need any more help than you got here, you can look in PythonInterpreter.cs for an example.

Last edited Mar 16, 2008 at 7:31 PM by Shader, version 1


No comments yet.