Fun with Smalltalk script
Par Hilaire Fernandes le lundi, août 4 2008, 20:10 - Lien permanent
In DrGeo 1.1, I implemented a Scheme script plugged into a geometric figure. These scripts are written with the Scheme language and they are used to do user computation over geometric items. They are plugged into the area and they display the computed script value. See examples. I took a fresh view on how to implement it in DrGeoII, and of course I want take advantage of what Smalltalk can offer for these user script systems.
How does it work?
DrGeo handles user scripts just like any other geometric item, or more exactly as any value item inserted into the geometric figure. A script depends on:
- a set of items in the geometric figure (its parents item). This parent collection is used as arguments for the script. To specify its parent items, the user selects them in the drawing area.
- a script definition (or code source). The returned value of the script is printed in the drawing area, the script value.
- a position in the drawing area is where to print the script value.
DrGeoII script revisited
Regarding DrGeoII script implementation (as of 04/08/2008), as follows:
- the script language is Smalltalk, a script is defined as an instance method of the
DrGeoUserScriptsclass. - a script owns an arbitrary number of arguments, starting with zero. Script name uses the Smalltalk convention: unary message or keyword message.
- To create and to edit a script, the user operates with a Browser narrowed to the
DrGeoUserScriptsclass
Step by step process to work on a script
We will give two examples, in this article one script without any arguments, and in a next article a script with two arguments.
From the background menu of DrGeoII, go to numerics>edit script. A class browser window opens on the DrGeoUserScripts class. We can create and edit the script from there. You are encouraged to experiment the class browser to get your own feeling with the tool.
The browser is opened by default on the examples message category. The pane on the bottom is the place to write code. Edited code must be confirmed with the Alt+s key stroke. When confirming the code change, the method is compiled and DrGeoII is informed about the change, so the drawing area is updated.
A script without arguments
Let's say we want a random integer in ]-10 ; 10]. The method could looks like:

Now you want to use this script in your drawing area. From the DrGeoII background menu, go to numerics>use script. In the newly opened wizard dialog, you can select the script:

You may have noted the script description is the comment at the beginning of the message source code, between the double quotes.
Next, as this script owns no argument, click anywhere in the drawing area, the script value will be plugged in at this place. Now when you move this value with the mouse, you will note the script value is changing. Obviously it is a random number!
Of course the script value can be used as any other item value: to define a point by coordinates, or as an argument to another script.
Conclusion
In this article, we have exposed the overall structure of the DrGeoII Smalltalk script system and how to use it. We have shown a simple script example without argument to generate random integer.
The DrGeoII Smalltalk script system completely fit in the OLPC philosophy regarding introducing programming to kid. Not only with DrGeoII kids can write code snippet but they can also do mathematical calculus. Moreover the Smalltalk environment provide an extremely friendly environment to write code: the class browser, the debugger and many more friendly tools.
