8 Logix from Python

Logix can easily be used as a component of a regular Python project.

8.1 The logix Module

The logix module is a regular Python module and provides the following methods:

logix.init()

Initialize Logix. The standard languages are created.

logix.imp(logixModuleName)

Import a Logix module (.lx) file. The argument is the module name in standard dotted format. The imported module is returned. E.g.:

config = logix.imp("utils.config")

logix.execfile(filename, [globals])

Parse and execute the file filename, in the (optionally) supplied environment. The parse begins in Base Logix.

logix.parse(language, filename=None, mode='parse',
            execenv=None)

Parse source-code in the target language. Returning either the parsed code-data, or the compiled code (a Python code-object).

language

The language to parse.
(in an active parse, the language may be changed during the parse by a setlang)

src

The source-code. Either a string or a file object. File objects should be opened in universal-newline mode.

filename

Optional filename (string). Used in error messages.

mode

A string to specify the parse mode. All modes except interactive will parse multiple lines. All modes except execmodule return code-data.

exec
Expand macros. Evaluate top-level items and items in deflangs.

execmodule
Like exec but returns a Python codeobject.

interactive
Parses a single expression only. Expands macros, evaluates language expressions in setlang and switchlang. defop, deflang and getops have no effect during the parse.

parse
Parse only. No macro expansion. No evaluation (language extension features have no effect during the parse).

expand
Parse and expand macros. No evaluation (language extension features have no effect during the parse).

execenv

A dict containing the execution environment (name bindings) for code-evaluation during the parse. An execenv is required if the mode is exec, execmodule, or interactive.

logix.eval(codeData, [globals, [locals]])

Evaluate the code-data either in the calling environment, or in the supplied environment. Returns the result. If the code-data contains macro operators, these will be expanded before evaluation.

 

The following languages are available (once you have called logix.init()):

logix.baselang

Base Logix

Logix.stdlang

Standard Logix

logix.syntaxlang

The syntax-rule language.

logix.langlang

The ‘language definition language’ which defines defop, deflang, setlang, getops and a few others. This is the default base language for new languages (when no base language is specified in a deflang).

logix.quotelang

Defines the quasiquote operator and quote-escape operator. The base language for logix.langlang.

8.2 Example

This short example shows how Logix might be used to implement a command-line interface to a Python application. The language commandlang has been defined in commandline.lx. The operator implementation functions have been written to access the application via a global variable app.

We don’t wish to support multiple languages at our command-line, so we do not need to evaluate during parsing. We therefore use the default parse mode ('parse').

The main body of the application might look like:

import logix

commandline = logix.imp("commandline")
commandlang = commandline.commandlang

theApp ...create main application object...

env = dict(app=theApp)

while 1:
    line = raw_input("> ")
    codeData = logix.parse(commandlang, line)
    logix.eval(codeData, env)