1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<!-- $Id: ex-script.html 269 2001-10-10 00:38:28Z garland $ -->
<!-- Based on ../tests/t-script.cxx revision 1.6 -->
<html>
<head>
<title>libgfx: Simple Scripted Program</title>
<link rel=stylesheet href="cdoc.css" type="text/css">
<meta name="Author" content="Michael Garland">
<style type="text/css">
<!--
pre { margin-left: 2em }
-->
</style>
</head>
<body>
<h2>Simple Scripted Program</h2>
<p>This program demonstrates some of the basic features provided by the
<tt>libgfx</tt> <a href="script.html">scripting package</a>. Given a list of
file names on the command line, it reads each of them in turn and executes the
scripting commands contained within them.
<h3>Setup and Initialization</h3>
<p>The program begins by including the headers for the <tt>libgfx</tt> modules
which it uses:
<pre>
#include <gfx/gfx.h>
#include <gfx/script.h>
#include <gfx/vec3.h>
</pre>
<p>The <tt>main()</tt> application entry point performs a very simple task.
It creates a scripting environment (<tt>class CmdEnv</tt> and registers a set
of scripting commands. Once these handlers have been installed,
it loops over every file name specified on the command line and executes it as
a script.
<pre>
main(int argc, char *argv[])
{
CmdEnv env;
env.register_command("add", proc_add);
env.register_command("avg", proc_add);
env.register_command("echo", proc_echo);
env.register_command("vec3", proc_vec3);
for(int i=1; i&argc; i++)
script_do_file(argv[i], env);
return 0;
}
</pre>
<h3>Command Procedures</h3>
<p>Every scripting command is handled by some command procedure. The
registration code above binds the actual procedures to the names of their
scripting commands. Then, whenever a given command occurs in a script being
processed, the corresponding handler is invoked.
<p>The first command procedure actually implements two scripting commands:
<tt>add</tt> and <tt>avg</tt>. It determines which command has been invoked
by examining its <tt>name</tt> argument.
It treats its command line as a whitespace-separated sequence of numbers,
either adds or averages all of them, and then prints the result.
<pre>
int proc_add(const CmdLine &cmd)
{
double sum = 0.0;
int count;
std::vector<double> values;
cmd.collect_as_numbers(values);
for(count=0; count<values.size(); count++)
sum += values[count];
if( cmd.opname() == "avg" && count>0 )
sum /= (double)count;
cout << sum << endl;
return SCRIPT_OK;
}
</pre>
<p>This next procedure requires precisely 3 numeric arguments, from which it
constructs a 3-D vector using the <tt>Vec3</tt> class. If the number of
arguments is not 3, it returns a value indicating a syntax error occurred.
<pre>
int proc_vec3(const CmdLine &cmd)
{
if( cmd.argcount() != 3 ) return SCRIPT_ERR_SYNTAX;
Vec3 v;
cmd.collect_as_numbers(v, 3);
cout << v << endl;
return SCRIPT_OK;
}
</pre>
<p>Finally, the <tt>echo</tt> procedure does not interpret its arguments at
all. Instead, it simply prints the entire argument line as is.
<pre>
int proc_echo(const CmdLine &cmd)
{
cout << cmd.argline() << endl;
return SCRIPT_OK;
}
</pre>
<h3>Sample Execution</h3>
<p>To demonstrate the action of this sample program, here is a particularly
simple script.
<pre>
# This is a test script meant to be fed to t-script. It is not meant as an
# exhaustive test, but mainly as a demonstration.
echo The following sum should be 15
add 1 2 3 4 5
echo
echo The following average should be 3.5
avg 3 8 2 1
echo
echo The following is the vector [1 0 0]
vec3 1 0 0
</pre>
<p>When given to the sample program, it produces the following output
<pre>
The following sum should be 15
15
The following average should be 3.5
3.5
The following is the vector [1 0 0]
1 0 0
</pre>
</body>
</html>
|