PyLogo


Interpreter

The interpreter gets tokens (from a reader.TrackingStream) and runs them. It holds the namespace, which is dynamically scoped.

You execute one expression by calling interpreter.expr(tokenizer), where tokenizer may be reader.TrackingStream or other tokenizer instance. It returns the value of the expression.

The RootFrame and Frame subclasses implement the namespace operations (this class is abstract).


Attributes

a prompts

{None: '??? ', 'to': 'to? ', '[': ' [ ? ', 'func': '..? ', '(': ' ( ? '}

a special_forms

{'ask': <function special_tell at 0xb745e3e4>,
 'for': <function special_for at 0xb745e3ac>,
 'local': <function special_local at 0xb745e374>,
 'localmake': <function special_localmake at 0xb745e304>,
 'make': <function special_make at 0xb745e2cc>,
 'makeattr': <function special_makeattr at 0xb745e41c>,
 'tell': <function special_tell at 0xb745e3e4>,
 'to': <function special_to at 0xb745e33c>}

a tokenizer

<property object at 0xb7509e3c>

Methods

f __init__(self, tokenizer=None) ...

f tokenizer__get(self) ...

Gets the current tokenizer.

f push_tokenizer(self, tokenizer) ...

You can stack up multiple tokenizers, as the interpreter goes from evaluating a file to a list to a sublist, etc. New interpreters are created for a new scope.

f pop_tokenizer(self) ...

f expr(self) ...

Top level expression-getter/evaluator (see also expr_top).

f expr_top(self) ...

Unlike expr(), this ignores empty lines; should only be used in top-level expressions (including expressions taken from lists).

f expr_without_error(self) ...

Get a full expression from the tokenizer, execute it, and return the value.

expr ::= exprInner <operator> exprInner
::= exprInner

f expr_inner(self, apply=None, get_function=None, get_variable=None) ...

An 'inner' expression, an expression that does not include infix operators.

exprInner ::= <literal int or float>
          ::= '-' expr
          ::= '+' expr
          ::= ('"' or 'QUOTE') <word>
          ::= ':' <word>
          ::= MAKE (':' or '"') <word> expr
          ::= MAKE <word> expr
          ::= TO <to expression>
          ::= '[' <list expression> ']'
          ::= '(' <word> <expr> ... <expr> ')'
          ::= <word> <expr> ... <expr>

Things to note:

  • MAKE :x 10, MAKE "x 10, and MAKE x 10 all work equivalently (make is a special form, unlike in UCBLogo).
  • <list expression> is a nested list of tokens.
  • <to expression> is TO func_name var1 var2 <int>, where <int> is the default arity (number of variables). Variables, like with make, can be prefixed with : or ", but need not be.
  • () is not used to force precedence, but to force execution with a specific arity. In other words, () works like Lisp.

f special_make(self, greedy) ...

The special MAKE form (special because a variable in the first argument isn't evaluated).

f special_localmake(self, greedy) ...

The special LOCALMAKE form

f special_to(self, greedy) ...

The special TO form.

f special_local(self, greedy) ...

The special LOCAL form (with unevaluated variables). (Should this be generally greedy?)

f special_for(self, greedy) ...

Special FOR form. Again with the variable name.

f special_tell(self, greedy) ...

f special_makeattr(self, greedy) ...

f expr_list(self) ...

Grab a list (the '[' has already been grabbed).

f eval(self, lst) ...

Evaluate a list in this scope.

f apply(self, func, args) ...

Apply the args to the func . If the function has an attribute logo_aware , which is true, then the first argument passed to the function will be this interpreter object. This allows special functions, like IF or WHILE, to manipulate the interpreter.

f import_function(self, import_function, names=None) ...

Inputs the function func into the namespace, using the name it was originally defined with. The special attribute logo_name overrides the function name, and aliases provides abbreviations for the function (like FD for FORWARD).

f import_module(self, mod) ...

Import a module (either a module object, or the string name of the module), moving all of its exported functions into the current namespace (nested namespaces are not supported).

If a file defs/modulename.logodef exists, it will be loaded to find extra information about the module. This file contains one line for each annotated function (# or ; for comment lines). See loadDefs for more.

f load_defs(self, filename) ...

Load function definitions from a file. This file should have one function annotation on a line (lines starting with # or ; are comments).

Each line starts with the function name and a color. A star (*) means that the original function name should not be used. A word like arity:N will set the default arity of the function to N (if the default arity differs from the number of arguments the function takes). Other words are interpreted as aliases for a function. E.g.:

forward: fd
up: * penup pu
; RGB arguments only:
color: arity:3

f import_logo(self, filename) ...

Import a logo file. This executes the file, including any TO statements, putting everything into the normal namespace/scope.

f import_logo_stream(self, stream) ...

f input_loop(self, input, output) ...

Read-Eval-Print-Repeat loop, i.e., the standard prompt.

f push_actor(self, actor) ...

f pop_actor(self, actor=None) ...

f get_variable(self, v) ...

f get_function(self, name) ...

See the source for more information.