______ Making a LOGO Translator in HELM
(__ __) by Scott Nickerson
/ /
/ / he scripting language in Helm has some interesting features that
(_/ are not used very often. Two of these features are the ability to
draw directly on a page, outside of an imagefield, and the ability to
compile a script on the fly. In this tutorial, we will show how you can
use these features to build a simple LOGO language translator that
includes turtle graphics.
Before beginning to construct the actual LOGO translator, you should
become familiar with Helm's paint commands that are described in Chapter
37, Paint Commands, in the Helm Reference Manual. These commands are
unique because they direct Helm's painting tools; they do not have a
painting engine of their own. These means that you can take advantage of
the various painting modes by simply setting the painttype property and
then using one of the draw commands.
In the LOGO application, we will use two of these commands: drawline and
fillbox. Take note of the syntax and arguments of these commands...
drawline <x>, <y>, <x1>, <y1>
This command draws a line on the screen using the current pen color and
the current paint type. The coordinates x-y and x1-y1 are the end points
of the line.
fillbox <x>, <y>, <x1>, <y1>
This command draws a filled rectangle. One corner of the rectangle is
located at x-y. Another corner of the rectangle that is diagonally
opposite of the first corner is located at x1-y1. We will use this
command merely to clear the drawing area.
These commands will paint on the page only if there is no imagefield on
the page. Make sure that there is no imagefield. This will not be a
limitation in future versions of Helm.
LOGO was devised as a simple language to teach children how to program.
The concept of turtle graphics is an important part of LOGO. The child
programmer gives commands to a turtle to go forward, turn left, and so on.
The turtle draws as it moves, leaving a line in its wake. This gives the
child a more concrete way of dealing with graphics, rather than having to
imagine the more abstract Cartesian coordinate system.
The algorithm for the turtle graphics engine is fairly straightforward.
We will merely take advantage of the equations for converting polar
coordinates to Cartesian coordinates. Given our current position, a
distance to move (dis), and an angle in which to move (ang), we can
determine the location of a turtle after movement with the following
equations:
x = cos d * dis + x
y = sin d * dis + y
We must remember to keep the variables x, y, and d in global variables.
Also, Helm's math functions require angles in radians so we must convert
any angle given in degrees to radians using the equation:
ang = ang / 180.0 * 3.1415927
We should also make sure that the angle falls within an acceptable range
of angles by using an if-else construction:
if ang < 0 then ang = 360.0 + ang
else if ang > 360 then ang = ang - 360.0
Pulling these equations all together, we can construct a simple turtle
graphics function.
moveturtle dis
begin
global x, y, ang
if ang < 0 then put 360.0 + ang into ang // Make sure angle
else if ang > 360 then put ang - 360 into ang // is in range.
put ang / 180.0 * 3.1415927 into ang // convert to radians
x1 = cos ang * dis + x // find new location
y1 = sin ang * dis + y
drawline x, y, x1, y1 // draw line
x = x1
y = y1
end //
Now that we have a basic turtle graphics engine, we can easily move the
turtle pen around with other very simple commands. For example, to move
the direction of the turtle 45 degrees to the right, subtract 45 from the
global container dir; to move forward 10, call the moveturtle function
using an argument of 10; to set the position of the turtle pen, just put
new numbers into the global x and y containers.
Helm will translate the LOGO program a line at a time into Helm's script
language, adding each new group of Helm statements to a string. When the
translation is complete, a "set" command will set the script of the form
to the string. Helm will automatically compile the script at this point
(A similar method of dynamically creating scripts is used in the
PlotBook, an example application that comes with the Helm package. It
evaluates mathematical expressions by simply inserting the equation into
a script and having Helm compile and execute the script).
Before we create the translator itself, we need to more clearly define
the LOGO syntax it will recognize. It is easy to translate program
statements if only a single statement can occupy a single line of code.
Also, it is even easier if the commands take the form of a command
keyword followed by an optional argument:
<command> <optional argument>
Consequently, we only have to get the first word of each line to determine
the command. The argument will be the words that follow the first word of
the line. For example, the command to move forward 10 pixels in LOGO is
"Forward 10." To parse this expression in Helm we can write a few simple
commands:
put "Forward 10" into theLine // In the translator, theLine will
// actually be gotten from a line of
// program code.
get the number of words of theLine
if it > 0 then begin
put word 1 of theLine into cmd
put word 1 to it into arg
end
Now, that was pretty easy. A more complex syntax would not be so simple
but, for this application we will follow the format of one statement per
line. Here is the syntax we designed for this very modest implementation of
LOGO:
Command Use
------------------------------ ------------------------------
FORWARD <steps> Move the turtle forward
BACKWARD <steps> Move the turtle backward
RIGHT <degrees> Turn the turtle to the right
LEFT <degrees> Turn the turtle to the left
PENUP The turtle stops drawing as it moves
PENDOWN The turtle starts drawing as it moves
HOME Move the turtle to the center position
COLOR <color number: 0 - 7> Sets the color of the drawing pen
MAKE <variable> = <expression> Set the value of a variable
TO ... END The start of a function
REPEAT ... END Repeats the statement block
To parse an entire LOGO program, we simply put the parser that we
developed into a for-loop that loops as many times as there are lines in
the LOGO program. Given that the program is kept in textfield "Program,"
we can construct an example of this loop...
on SelectUp begin
global x, y, ang
put "LOGOfunc begin\n" into theCode
put textfield "Program" into theProgram // Putting the contents of
// a textfield that would other
// be frequently referenced, will
// greatly speed script execution.
put the number of line of theProgram into numlines
for n = 1 to numline begin
get the number of words of theLine
if it > 0 then begin
put word 1 of theLine into cmd
put word 1 to it into arg
end
if cmd = "Forward" begin
put "moveturtle(arg)\n" after theCode
end
else if cmd = "Right" begin
put "subtract arg from ang\n" after theCode
end
else if cmd = "Left" begin
put "add arg to ang\n" after theCode
end
else begin
answer "Cannot understand keyword."
exit
end
end
put "end\n" after theCode
set the script of this form to theCode
LOGOfunc()
end
As you can see from this function, Helm stores the new script in a
container called "theCode". It consists of a dynamically created function
called LOGOfunc() After it is constructed, the translator attaches the
script to the form and Helm compiles it. Then this newly compiled
function is called. As it runs, you should see the artwork created by the
movement of the turtle.
VPLOGO is only a very tiny subset of LOGO. However, you can enhance it by
translating additional statements or you can use the principles of this
application to create translators for other languages. Your language will
run quickly because even the HelmBrowser includes the entire Helm
compiler and interpreter. You simply let Helm do the compiling for you.
The ability to compile scripts on the fly also has implications for
future versions of Helm.
The next major version of Helm will include capabilities for communications
using the serial and parallel ports. You could create a custom script
language that is used for programs that are transmitted from one computer
to another. These custom programs can be instantly compiled and executed
when they arrive at their destination sites. For instance, it would be
possible to create a special language for presentation graphics. A Helm
application will create the presentation at one site and update remote
sites by sending programs written in the custom language. The destination
Helm book will receive the program and instantly translate it into a Helm
script and run it. The presentation will change seamlessly as the new
program is executed.
Previous Article Table of Contents Next Article
HTML Conversion by AG2HTML.pl & witbrock@cs.cmu.edu