| 1 |
#This file was created by <root> Fri Mar 31 14:16:48 2000 |
| 2 |
#LyX 1.0 (C) 1995-1999 Matthias Ettrich and the LyX Team |
| 3 |
\lyxformat 2.15 |
| 4 |
\textclass article |
| 5 |
\language default |
| 6 |
\inputencoding default |
| 7 |
\fontscheme default |
| 8 |
\graphics default |
| 9 |
\paperfontsize default |
| 10 |
\spacing single |
| 11 |
\papersize Default |
| 12 |
\paperpackage a4wide |
| 13 |
\use_geometry 0 |
| 14 |
\use_amsmath 0 |
| 15 |
\paperorientation portrait |
| 16 |
\secnumdepth 3 |
| 17 |
\tocdepth 3 |
| 18 |
\paragraph_separation indent |
| 19 |
\defskip medskip |
| 20 |
\quotes_language english |
| 21 |
\quotes_times 2 |
| 22 |
\papercolumns 1 |
| 23 |
\papersides 1 |
| 24 |
\paperpagestyle default |
| 25 |
|
| 26 |
\layout Section |
| 27 |
|
| 28 |
The Interpreter |
| 29 |
\layout Subsection |
| 30 |
|
| 31 |
What does the Interpreter do? |
| 32 |
\layout Standard |
| 33 |
|
| 34 |
Unlike the name |
| 35 |
\emph on |
| 36 |
interpreter |
| 37 |
\emph default |
| 38 |
implies, it does not interprete scripts, it merely is the environment for |
| 39 |
scripts to run in. |
| 40 |
That means, in a way scripts interprete themselves. |
| 41 |
I'll explain that shortly, for now lets concentrate on the interpreter. |
| 42 |
\layout Standard |
| 43 |
|
| 44 |
The interpreters purpose is to provide a common environment for self-interpretin |
| 45 |
g scripts. |
| 46 |
That means, it can load scripts from a file, and afterwards execute them. |
| 47 |
It furthermore offers a local storage for temporary data and has a interface |
| 48 |
to access user-defined data as well. |
| 49 |
\layout Subsection |
| 50 |
|
| 51 |
How to use the Interpreter |
| 52 |
\layout Standard |
| 53 |
|
| 54 |
Since there is not much the interpreter does, it is easy and straightforward |
| 55 |
to use: |
| 56 |
\layout Standard |
| 57 |
|
| 58 |
Create a new instance, call |
| 59 |
\family typewriter |
| 60 |
load() |
| 61 |
\family default |
| 62 |
to load the desired script and |
| 63 |
\family typewriter |
| 64 |
run() |
| 65 |
\family default |
| 66 |
to start execution of the script. |
| 67 |
That's all there is to it. |
| 68 |
\layout Subsection |
| 69 |
|
| 70 |
The Interpreter's interface |
| 71 |
\layout Standard |
| 72 |
\noindent |
| 73 |
|
| 74 |
\family typewriter |
| 75 |
interpreter (); |
| 76 |
\layout Standard |
| 77 |
|
| 78 |
Create a new instance of the interpreter |
| 79 |
\newline |
| 80 |
|
| 81 |
\layout Standard |
| 82 |
\noindent |
| 83 |
|
| 84 |
\family typewriter |
| 85 |
interpreter (const char* script, void* data); |
| 86 |
\layout Standard |
| 87 |
|
| 88 |
Creates a new, already initialized, instance of the interpreter |
| 89 |
\newline |
| 90 |
|
| 91 |
\layout Standard |
| 92 |
|
| 93 |
|
| 94 |
\family typewriter |
| 95 |
script |
| 96 |
\family default |
| 97 |
is the filename of the script to load |
| 98 |
\layout Standard |
| 99 |
|
| 100 |
|
| 101 |
\family typewriter |
| 102 |
data |
| 103 |
\family default |
| 104 |
points to a object that is to be manipulated from within the script |
| 105 |
\newline |
| 106 |
|
| 107 |
\layout Standard |
| 108 |
|
| 109 |
The interpreter is now ready to run the script, there is no need to call |
| 110 |
|
| 111 |
\family typewriter |
| 112 |
load() |
| 113 |
\family default |
| 114 |
any more. |
| 115 |
\newline |
| 116 |
|
| 117 |
\layout Standard |
| 118 |
\noindent |
| 119 |
|
| 120 |
\family typewriter |
| 121 |
u_int8 load (const char*); |
| 122 |
\layout Standard |
| 123 |
|
| 124 |
Load a script from file |
| 125 |
\newline |
| 126 |
|
| 127 |
\layout Standard |
| 128 |
|
| 129 |
|
| 130 |
\family typewriter |
| 131 |
script |
| 132 |
\family default |
| 133 |
is the filename of the script to load |
| 134 |
\layout Standard |
| 135 |
|
| 136 |
|
| 137 |
\family typewriter |
| 138 |
returns |
| 139 |
\family default |
| 140 |
1 on success, 0 on failure |
| 141 |
\newline |
| 142 |
|
| 143 |
\layout Standard |
| 144 |
\noindent |
| 145 |
|
| 146 |
\family typewriter |
| 147 |
s_int32 run (); |
| 148 |
\layout Standard |
| 149 |
|
| 150 |
Executes the script. |
| 151 |
|
| 152 |
\newline |
| 153 |
|
| 154 |
\layout Standard |
| 155 |
|
| 156 |
|
| 157 |
\family typewriter |
| 158 |
returns |
| 159 |
\family default |
| 160 |
whatever value the |
| 161 |
\noun on |
| 162 |
Return |
| 163 |
\noun default |
| 164 |
command of your script passes back. |
| 165 |
\newline |
| 166 |
|
| 167 |
\layout Standard |
| 168 |
\noindent |
| 169 |
|
| 170 |
\family typewriter |
| 171 |
void *user_data; |
| 172 |
\layout Standard |
| 173 |
|
| 174 |
Points to an object you can manipulate from within a script. |
| 175 |
\layout Section |
| 176 |
|
| 177 |
The Script |
| 178 |
\layout Subsection |
| 179 |
|
| 180 |
What does a Script do? |
| 181 |
\layout Standard |
| 182 |
|
| 183 |
Nothing and everything. |
| 184 |
A script is a series of |
| 185 |
\emph on |
| 186 |
commands, |
| 187 |
\emph default |
| 188 |
defined by a unique id (a simple integer) followed by their optional parameters. |
| 189 |
On it's own, a script is as useful as the interpreter, that is it's good |
| 190 |
for nothing. |
| 191 |
Only in combination with the interpreter, it becomes useful. |
| 192 |
|
| 193 |
\layout Standard |
| 194 |
|
| 195 |
A bit further up I said scripts would interprete themselves, what do they |
| 196 |
need the interpreter for then? Basically, each command does not only exist |
| 197 |
as a number and some parameters in a script, but also as a class that containes |
| 198 |
the commands execution code. |
| 199 |
You guessed it, commands will be explained in detail a bit later on. |
| 200 |
\layout Subsection |
| 201 |
|
| 202 |
What happens when a Script is loaded? |
| 203 |
\layout Standard |
| 204 |
|
| 205 |
This will answer the question of the interpreters use. |
| 206 |
For each command within the script, a instance of that commands class is |
| 207 |
allocated and initialized with the arguments specified in the script. |
| 208 |
That means the interpreter creates actual code from a mere binary file, |
| 209 |
at the programs runtime. |
| 210 |
Of course it can't create any |
| 211 |
\emph on |
| 212 |
new |
| 213 |
\emph default |
| 214 |
code, only arrange predefined code snippets (the commands) in a way that |
| 215 |
they do something useful. |
| 216 |
That is probably as dynamic as you can make a compiled c++ program. |
| 217 |
\layout Subsection |
| 218 |
|
| 219 |
How is the Script executed? |
| 220 |
\layout Standard |
| 221 |
|
| 222 |
When you run the interpreter, it simply executes the commands, beginning |
| 223 |
with the first, until it hits the already mentioned |
| 224 |
\noun on |
| 225 |
Return |
| 226 |
\noun default |
| 227 |
command. |
| 228 |
That alone isn't very exciting, but there are two things that make the |
| 229 |
whole execution process a bit more interesting. |
| 230 |
\layout Standard |
| 231 |
|
| 232 |
First of all, you can change the order in which commands are executed from |
| 233 |
|
| 234 |
\emph on |
| 235 |
within |
| 236 |
\emph default |
| 237 |
a command. |
| 238 |
More on this later. |
| 239 |
\layout Standard |
| 240 |
|
| 241 |
And second, you can have multiple |
| 242 |
\noun on |
| 243 |
Return |
| 244 |
\noun default |
| 245 |
's in your script. |
| 246 |
That means, once the interpreter has stopped executing commands, you can |
| 247 |
invoke it again, letting it continue with the command following immediately |
| 248 |
after the return. |
| 249 |
\layout Subsection |
| 250 |
|
| 251 |
The Script's file format |
| 252 |
\layout Standard |
| 253 |
\added_space_top 0.3cm \added_space_bottom 0.3cm \align center \LyXTable |
| 254 |
multicol5 |
| 255 |
6 2 0 0 -1 -1 -1 -1 |
| 256 |
1 1 0 0 |
| 257 |
1 0 0 0 |
| 258 |
1 1 0 0 |
| 259 |
1 1 0 0 |
| 260 |
0 1 0 0 |
| 261 |
0 1 0 0 |
| 262 |
2 1 0 "8cm" "" |
| 263 |
4 1 1 "" "" |
| 264 |
0 2 1 0 0 0 0 "" "" |
| 265 |
0 4 1 0 0 0 0 "" "" |
| 266 |
0 4 1 0 0 0 0 "" "" |
| 267 |
0 4 1 0 0 0 0 "" "" |
| 268 |
0 2 1 0 0 0 0 "" "" |
| 269 |
0 4 1 0 0 0 0 "" "" |
| 270 |
0 2 1 0 0 0 0 "" "" |
| 271 |
0 4 1 0 0 0 0 "" "" |
| 272 |
0 2 1 0 0 0 0 "" "" |
| 273 |
0 4 1 0 0 0 0 "" "" |
| 274 |
0 2 1 0 0 0 0 "" "" |
| 275 |
0 4 1 0 0 0 0 "" "" |
| 276 |
|
| 277 |
|
| 278 |
\series bold |
| 279 |
File Header: |
| 280 |
\newline |
| 281 |
8 bytes |
| 282 |
\series default |
| 283 |
|
| 284 |
\newline |
| 285 |
Length of the script in bytes, that is file_length - 8 |
| 286 |
\newline |
| 287 |
4 bytes |
| 288 |
\newline |
| 289 |
Number of commands within the script |
| 290 |
\newline |
| 291 |
4 bytes |
| 292 |
\newline |
| 293 |
|
| 294 |
\series bold |
| 295 |
Commands: |
| 296 |
\series default |
| 297 |
|
| 298 |
\newline |
| 299 |
|
| 300 |
\newline |
| 301 |
Id |
| 302 |
\newline |
| 303 |
4 bytes |
| 304 |
\newline |
| 305 |
[Parameters] |
| 306 |
\newline |
| 307 |
[4*n bytes] |
| 308 |
\layout Standard |
| 309 |
|
| 310 |
A side note on a commands parameters: as we learn later on, each command |
| 311 |
is responsible for extracting it's parameters itself from the script file, |
| 312 |
so the only limitation regarding a commands parameters is that they have |
| 313 |
to be aligned on a 32 bit boundary. |
| 314 |
Apart from that, any number and type of parameters is allowed. |
| 315 |
\layout Section |
| 316 |
|
| 317 |
The Commands |
| 318 |
\layout Subsection |
| 319 |
|
| 320 |
What does a Command do? |
| 321 |
\layout Standard |
| 322 |
|
| 323 |
As we have seen, in the end everything boild down to commands. |
| 324 |
Each command contains code that is invoked from the interpreter. |
| 325 |
What that code does is completely up to you, which means with the right |
| 326 |
commands put together in a script you can do nearly everything, well at |
| 327 |
least nearly everything you can do with c++. |
| 328 |
\layout Standard |
| 329 |
|
| 330 |
To be more precise, each command has a entry point named |
| 331 |
\family typewriter |
| 332 |
run() |
| 333 |
\family default |
| 334 |
, that is called by the interpreter. |
| 335 |
From there on, you are free to call other functions, access the user data |
| 336 |
passed to the interpreter or any other data that is (globally) accessible. |
| 337 |
You can even change the interpreters |
| 338 |
\emph on |
| 339 |
program counter |
| 340 |
\emph default |
| 341 |
(pc), that points to the command to be executed next. |
| 342 |
\layout Subsection |
| 343 |
|
| 344 |
How to implement a Command |
| 345 |
\layout Standard |
| 346 |
|
| 347 |
To make all that work, there are a few requirements any of your commands |
| 348 |
must meet. |
| 349 |
\layout Subsubsection |
| 350 |
|
| 351 |
Command base class |
| 352 |
\layout Standard |
| 353 |
|
| 354 |
Each command has to be derived from a comman base class, namely |
| 355 |
\family typewriter |
| 356 |
command |
| 357 |
\family default |
| 358 |
, defined in |
| 359 |
\emph on |
| 360 |
interpreter.h |
| 361 |
\emph default |
| 362 |
. |
| 363 |
This base class has two pure virtual functions you need to overwrite: |
| 364 |
\newline |
| 365 |
|
| 366 |
\newline |
| 367 |
|
| 368 |
\family typewriter |
| 369 |
virtual void init(s_int32* buffer, u_int32& index, void* data) = 0; |
| 370 |
\layout Standard |
| 371 |
|
| 372 |
This is used to initialize your commands parameters when the interpreter |
| 373 |
loads your script. |
| 374 |
\newline |
| 375 |
|
| 376 |
\layout Standard |
| 377 |
|
| 378 |
|
| 379 |
\family typewriter |
| 380 |
buffer |
| 381 |
\family default |
| 382 |
contains a copy of the script file |
| 383 |
\layout Standard |
| 384 |
|
| 385 |
|
| 386 |
\family typewriter |
| 387 |
index |
| 388 |
\family default |
| 389 |
points to the start of your commands first parameter within |
| 390 |
\family typewriter |
| 391 |
buffer |
| 392 |
\family default |
| 393 |
. |
| 394 |
You have to assure that it points to the start of the next command once |
| 395 |
you finished with reading your commands parameters. |
| 396 |
That might mean a bit more work for you, but since the interpreter knows |
| 397 |
nothing of your command and its parameters, it's the only possible way. |
| 398 |
\layout Standard |
| 399 |
|
| 400 |
|
| 401 |
\family typewriter |
| 402 |
data |
| 403 |
\family default |
| 404 |
points to the userdefined chunk of data you may have passed to the interpreter |
| 405 |
\newline |
| 406 |
|
| 407 |
\newline |
| 408 |
|
| 409 |
\family typewriter |
| 410 |
virtual s_int32 run (u_int32& pc, void* data) = 0; |
| 411 |
\layout Standard |
| 412 |
|
| 413 |
That is the entry point to your commands actual code. |
| 414 |
\newline |
| 415 |
|
| 416 |
\layout Standard |
| 417 |
|
| 418 |
|
| 419 |
\family typewriter |
| 420 |
pc |
| 421 |
\family default |
| 422 |
is a reference to the interpreters program counter. |
| 423 |
Usually you need not touch it, but if you like to continue your script |
| 424 |
with another but the next command, you can change it. |
| 425 |
Note that at the point your command starts executing, |
| 426 |
\family typewriter |
| 427 |
pc |
| 428 |
\family default |
| 429 |
points already to the following command. |
| 430 |
\layout Standard |
| 431 |
|
| 432 |
|
| 433 |
\family typewriter |
| 434 |
data |
| 435 |
\family default |
| 436 |
points to the userdefined chunk of data you may have passed to the interpreter |
| 437 |
\newline |
| 438 |
|
| 439 |
\layout Subsubsection |
| 440 |
|
| 441 |
Register the Command |
| 442 |
\layout Standard |
| 443 |
|
| 444 |
By deriving all commands from one base class, we enable the interpreter |
| 445 |
to remain ignorant of the different commands. |
| 446 |
It needs only to know that base class to run any command derived thereof. |
| 447 |
What it can't do yet is to create an instance of a command when reading |
| 448 |
its Id from the script file. |
| 449 |
Therefore, for each command a function has to be written that returns a |
| 450 |
new instance of that command. |
| 451 |
A pointer to that function is then stored in an array and, what surprise, |
| 452 |
can be accessed with its Id, enabling the interpreter to instanciate commands |
| 453 |
without knowing anything about them, except their Id. |
| 454 |
\the_end |