(C) 2020 Franck Pommereau <franck.pommereau@univ-evry.fr>
This is free software, see file LICENCE
codanim allows to generate LaTeX/TikZ code to animate source code and data structures for beamer presentations. So far, it is oriented to simulate C code, but there is no conceptual limitation for the simulation of other languages.
codanim is currently highly experimental and neither well documented nor well tested. You've been warned.
There is so far no installation procedure. Just copy directory
codanim
somewhere in your PYTHONPATH
.
File examples/pygments.sty
will be needed to compile the final LaTeX
files. File examples/tpl.tex
shows how the generated LaTeX code may
be included in a beamer presentation.
codanim defines Python classes to simulate the data and control-flow
structures of an imperative language. Data structures are defined in
codanim.data
and include:
Pointer(data)
a pointer to another datadata
Value(value)
an arbitrary value, initialised tovalue
. Ifvalue
isNone
(the default), the value is considered uninitialisedArray(init, index=[])
an array of values, that is zero-indexed, and initialised as follows:- if
init
is anint
, then it is the length of the array whose values are all uninitialised - if
init
is alist
then is holds the initial values of the arrayindex
is a list of variables identifiers that may be used to access the array cells (see below)
- if
Struct(init)
a structure whose fields and initial values are defined byinit
that must be adict
Heap()
a container for dynamically allocated data that provides methodsnew
andfree
to do so
Control-flow structures are used to simulate within Python code that
may potentially be written in any imperative language. Doing so, all
the changes that are made to the data structures defined above are
recorded so that they may be latter animated, consistently with the
animation of the code itself. The idea is that source code in the
simulated language is split into corresponding control-flow
structures, and actual computation is done using equivalent Python
code instead of executing the code in the source language.
Control-flow structures are defined in codanim.flow
and include:
RAW(src='...')
raw code from the simulated language, its simulation is no-op, but it is rendered highlighted in the final animationWS(src='...')
just likeRAW
but should be only white spaces (so that it won't be highlighted)BLOCK(*body)
a group of other structures that are simulated (and rendered) sequentiallySTMT(*steps, src='...')
an arbitrary statement that is simulated by running itssteps
sequentially, each of which being an arbitrary Python statement that isexec
edEXPR(expr, src='...')
an arbitrary Python expressionexpr
that simulate an expression in the simulated languagePY(code)
an arbitrary Python statement that need to be executed but is not rendered in the final animation, just like every structure that expects nosrc='...'
argumentENV(name, value)
a data structure that need to be defined in order to do the simulation (typically: global or external variables), and which is stored into variablename
that can be used from the Python code of statements and expressionDECL(name, init=None, animate=False, src'...')
an actual variable declaration in the simulated language, that will be executed and rendered. The execution consists of evaluatinginit
that should be anEXPR
(orNone
) and assigning its value toname
. Ifanimate
isTrue
then the value of the variable will displayed within a comment just next to the declaration in the final animationXDECL(*names, src='...')
several uninitialised declarationsBREAK
abreak
instruction for loops and switchesRETURN(value, src='...')
a return instruction from a functionIF(cond, then, otherwise=None, src='...')
simulates anif
block with an optionalelse
part (calledotherwise
sinceelse
is a Python keyword. Argumentcond
should be anEXPR
instanceWHILE(cond, body, src='...')
simulates awhile
loopDO(body, cond, src='...')
simulates ado/while
loopFOR(init, cont, step, body)
simulates a C-likefor
loopFUNC(body, src='...')
simulates a function TODO: functions calls is not implemented yet, so basically, a function is so far only aBLOCK
with source codeSWITCH(cond, *cases, src='...')
simulates a C-like switch,cases
may be:CASE(value, body)
to simulate acase
statementDEFAULT()
to simulate adefault
statement
So, all what you need is to defined some data structures, some
control-flow structures with appropriate src
arguments (so that code
is rendered in the simulated language), and with appropriate Python
code to simulate the original statements and expressions.
Writing the control-flow structures may be tedious, so codanim may be
called from the command line to parse actual code to be simulated and
generate the appropriate control-flow structures (in which the Python
code remains to be written). Run python -m codanim
for help.
File examples/Makefile
can be used to build the PDF for all the
examples. For instance use make heap.pdf
to build the first example
described below.
It defines a Heap
instance and add chained Struct
s to it. The
final picture is rendered as TikZ code. There is no animation, this is
just the picture of a chained list.
It uses the same kind of linked lists as above, seen as stacks, to animate a push onto a stack. First the data is defined, then the control-flow structure. The latter is then simulated and finally, both code and data structures are rendered for LaTeX/beamer inclusion.
Note the use of class Box
that can be used to layout several data
structures.
The partitioning and main algorithm of a quick-sort. This shows how to
use Array
, in particular the index
argument. qs-main
also shows
how to define custom layout of arrays, as well as auxiliary Python
functions to simulate full C-functions calls in one step.