September, 2018
I didn't really seriously started coding until college, but I do remember fondly some classes I had in elementary school that taught us Logo.
Some days ago I got melancholic while using turtledemo and decided to create a project that has no practical applications whatsoever – a transpiler called chelodina1.
At the moment it can turn Logo code like this:
TO triangle :length
REPEAT 3 [ FORWARD :length RIGHT 120 ]
END
TO flower :length :count
REPEAT 150 [
triangle :length
RIGHT 360 / :count
]
END
TO web
REPEAT 6 [ flower 150 18 ]
END
web
Into Python like this:
import turtle
def triangle(p_length):
for _ in range(3):
turtle.forward(p_length)
turtle.right(120.0)
def flower(p_length, p_count):
for _ in range(150):
triangle(p_length)
turtle.right(360.0 / p_count)
def web():
for _ in range(6):
flower(150.0, 18.0)
web()
turtle.done()
Or compile the python code directly with chelodina --input file --run
to look at the result:
You will find more details about its usage on Github.
For chelodina I wanted to use a Yacc-like parser in Python, so I went with the sure-fire solution that is PLY – David Beazley has been maintaining this project since 20012.
Positive notes:
Things I didn't enjoy:
t_
in the lexer and p_
in the parser.[-1]
behaves differently.For a future project, I would probably look into a more pythonic library like Lark. It's harder to develop when the library is not being explicit, as you're always second-guessing if it was a problem with your code or you just broke another magic rule you didn't know about.
This is the current BNF grammar. I have only focused on making the examples work so it still has ways to go for a complete implementation.
program : statements
statements : statements statement
| statement
statement : expression
| funcdef
| repeat
expression : NAME terms
| NAME
terms : terms PLUS terms
| terms MINUS terms
| terms MULT terms
| terms DIV terms
| terms NUMBER
| terms PARAM
| NUMBER
| PARAM
funcdef : TO NAME terms statements END
| TO NAME statements END
repeat : REPEAT NUMBER LBRACKET expression RBRACKET
I plan to work on it sparingly –with UCBLogo as the target implementation– since it's been a fun project.
That's funny because snake-necked turtles!
And actively maintaining it, not just letting it sit pretty.