Little Logo

Simple PHP interpreter for a restricted version of Logo.

View the Project on GitHub felixplesoianu/little-logo

Little Logo

Little Logo is a simplified dialect of the Logo programming language. Specifically, it is modeled after UCB Logo, the de facto reference implementation nowadays. Unfortunately, UCB Logo suffers from an irregular syntax with lots of exceptions. As Little Logo is made for fun, I went for a simpler, uniform language. It loses to the original in many respects, but wins in others.


Little Logo is free form. If you were to feed this source code to UCB Logo:


You would receive two error messages in return:

not enough inputs to print
You don't say what to do with 3

Little Logo, on the other hand, will do what you expect.

Expressions are statements. In Lisp, Python and most (all?) curly brace languages, a stand-alone expression is a valid statement. That means you can call a function for its side effects, ignoring the return value, and docstrings don't need to be special-cased. To top it all, it makes for simpler syntax. Little Logo follows this model, unlike its illustrious predecessor.

In addition, Little Logo takes advantage of this by making several procedures return a meaningful value that don't do so in UCB Logo, most importantly MAKE.

Clean, portable semantics. While Little Logo is very close to the host language, the interpreter architecture is simple and very general; it should be trivial to port to any reasonably high-level language.


No TO ... END. That may be a staple of Logo, but it's a line-oriented construct that has to be special-cased, and it's confusing to boot, as it doesn't follow the same rules as the rest of the language (as the UCB Logo manual admits in the introduction). You do get user-defined words, though -- see below.

No infix arithmetic. Unfortunately, that would be difficult to implement with the current interpreter architecture. To compensate, Little Logo has sane aliases for certain operators. (GREATEREQUALP? seriously? What's wrong with GTE?)

No optional function arguments either, also due to the simplistic architecture.


As of 2011-05-18, the PHP implementation largely works, implementing over 60 primitives, not counting aliases. As it stands, it is able to run this simple script:

; Compute a square root through the Newton-Raphson method.
; Not a good show of Little Logo's capabilities, but I couldn't come
; up with anything more interesting. By the way, SQRT is implemented.

; This won't work in UCBLogo due to its lack of an ABS function.

make "a 42 ; Number to extract square root from.
make "precision 0.000001
make "x quotient :a 2
make "prev quotient :a :x

while [lessp :precision  abs difference :x :prev] [
    make "prev :x
    make "x quotient   sum :x  quotient :a :x   2

pr :x

The choice of primitives is a bit haphazard, but does include most (all?) control structures, including the "question mark" form of template-based iteration, most list manipulation primitives, some logic and arithmetic.

As of 2011-11-12, Little Logo also supports user-defined words, in a form inspired by the "named slot" form of template iteration, with a hint or two taken from Scheme:

make "hypot [[a b] sqrt sum product :a :a product :b :b]

; Works in both UCB Logo and Little Logo
pr apply :hypot [3 4]
; Works only in Little Logo
pr hypot 3 4

Unlike in UCB Logo, those are real functions, that can contain any number of expressions, and return the value of the last one. Nested (dynamic) scopes work as you might expect:

make "test [[]
    make "a 3
    localmake "b 5
    pr list :a :b
make "a 1
make "b 2
pr list :a :b
pr list :a :b

LOCAL turned out to be a little can of worms, so it's not implemented. You'll have to settle for LOCALMAKE.

Implementation details

The prototype and so far only implementation of Little Logo is written in PHP. Why? It looked like a fun thing to try. And it was! Moreover, it turns out several Logo primitives map very well onto PHP's, which made the implementation especially clean. FIRST, for example, was straightforward. Same for ISEQ and most array operations.

That said, some Logo operations make no sense in this implementation, for example LISTTOARRAY (since Little Logo uses native PHP arrays throughout); others are redundant -- LISTP and ARRAYP do the same thing.

As of 2012-09-25, Little Logo features an instruction count limit parameter designed to prevent DDoS attacks in case you plan to run the interpreter over the Web -- it kicks in much sooner than PHP's max execution time. The default value is 20000, and you can (re)set it manually before running a piece of Logo code. Also pay attention to avenues for code injection.


GOTO (?)