Aloha,
Jonathan Fine wrote:
I'm writing some routines for handling dvi files.
In case you didn't know, these are TeX's typeset output.
These are binary files containing opcodes.
I wish to write one or more dvi opcode interpreters.
Are there any tools or good examples to follow for
writing a bytecode interpreter?
As far as i know, dvi is a very straight forward format, commands
followed by parameters, no conditionals, no loops.
For similar designs i used something like the following approach:
s = file('a.dvi','r').read() # read complete file to string
while s:
command = ord(s[0])
if command < 128:
#typeset command
s = s[1:]
elif command = 139:
#bop command
param = s[:40]
#interpret param
c = struct.unpack('D',param[:3])
#consume s
s = s[41:]
else:
#undefined command
s = s[1:]
You can work directly on strings, or convert to a list. If you don't
want long if/elif lists, you can use a dict as a dispatcher (python
cookbook has an example?). For most of the commands you can use
a lookup table for the parameter list length.
TeX §591 claims, that dvi is stricly interpretable from front to end.
The description in Tex§585++ can be transcripted to struct definitions
easily.
Wishing a happy day
LOBI