-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME
251 lines (214 loc) · 7.42 KB
/
README
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
This program interprets C-like mathematical expressions and prints the result.
In Script mode, you can specify a multi-line script that contains variables,
then set the variable values and run the script multiple times (changing the
variable values between each run if you want to.
In simple mode, each line you type is interpreted as a simple equation and the
result is printed when you press return.
The program was written during free time in my universities days (in 2000) and
was cleaned in 2013 when I decided to share it. If you somehow stumble onto
this code and find it useful, or merely interesting, please drop me an email
to let me know (although I doubt it will ever happen, if I receive even only
one email that will make my day :) ). Also fill free to open pull requests
on github.com/criezy/ for any improvement you make.
1) Usage and options
2) Simple Mode
3) Script Mode
4) Compilation and contributions
1) Usage and options
--------------------
Usage: script [options]
When no option is given it starts the program in interactive script mode.
Options can be:\n");
--help Print this help.
--simple-mode Interactive simple equation mode.
-e Same as --simple-mode
--script='command' Start in script mode and set the script to the given one.
-s 'command' Same as --script='command'
--file=path Start in script mode and load the script from the given file.
-f path Same as --file=path
2) Simple Mode
--------------
Each line you type is interpreted as a mathematical expression and the result is
printed on the next line. Type 'help' to get a list of recognised constant names,
functions and operators. Type 'exit' or 'quit' to quit the program.
Example of expression syntax:
$ ./script -e
Starting simple equation mode.
Type 'help' to get some help.
> (3 + 12) / 5
3
> 3 + 12 / 5
5.4
> if (cos(3 + 12 / 5) < 0, ceil(fabs(cos(3 + 12 / 5))), sqrt(3 + 12 / 5))
2.32379000772
> cos(3 + 12 / 5)
0.634692875943
> sqrt(3 + 12 / 5)
2.32379000772
You can also use variables. Use 'define VAR' to define a variable and 'undefine VAR'
to undefine a previously defined variable. You can also use 'variables' to list the
defined variables.
For example:
$ ./script -e
Starting simple equation mode.
Type 'help' to get some help.
> define a
> define b cos(PI / 3)
0.5
> variables
There are 2 variables defined:
a = 0.000000
b = 0.500000
> a + b
0.5
> undefine a
> variables
There is 1 variable defined:
b = 0.500000
If you enabled the tree debug feature at compile time, you can use the
tree <equation> command to print the parser tree for the given equation.
For example:
$ ./script -e
Starting simple equation mode.
Type 'help' to get some help.
> tree (3 + 12) / 5
DivideOperator
PlusOperator
Constant: 3.000000
Constant: 12.000000
Constant: 5.000000
3
3) Script Mode
--------------
In script mode, you enter a multi-line script that may contain variables. Then
you can set the variable values and run the script multiple times. To define
the script you can:
- give it to the program on the command line (--script= option)
- give it to the program as a text file (--file= option)
- type it after starting the program
To type the script in the program, type 'START'. Then everything you type until
you type 'END' will define the script. If you start typing a script it will
throw away any script that might already have been defined (either passed to
the command line or previously typed in the program).
When not typing the script, the following commands are recognised:
- 'start [name]' Start defining the script with the given name.
- 'help [topic]' Print a help message.
- 'scripts' List defined scripts.
- 'clear [name]' Undefined the script with the given name.
- 'run [name]' Run the script previously defined with the given name.
- 'run [name] > file' Run the script previously defined with the given name and redirect output
to file.
- 'script [name]' Print the script previously defined with the given name.
- 'script [name] < file' Initialise the script with the given name to the content of the given file.
- 'script [name] > file' Save the script previously defined with the given name to the given file.
- 'variables' Print the list of variables in the previously defined script.
- 'quit' Quit the program ('exit' also works).
- Everything else will be interpreted as a one line script and run immediately.
This is usually used to set variable values (e.g. 'foo = 12.5').
For all the commands above that take a script name, the name is optional. Using
names allows to defined several scripts that will coexist.
Consider the following script.
$ ./script
Starting script mode.
Type 'help' to get some help.
> start
if (z == 0) {
x = y;
} else {
x = y / (2. * z);
}
print(x);
print(y);
print(z);
z = z + 1;
end
>
You can then set the initial value for the variables y and z:
> y = 48;
> z = 0;
And then run the script several times:
> run
x = 48
y = 48
z = 0
> run
x = 24
y = 48
z = 1
You can also use external text file for scripts and variable values. External
variable files contain the variable name on the first line and values on
following lines. The script is then run once for each line. And you can
specify external files to which print() outputs are written.
> script init < examples/elastic_init.txt
> script compute < examples/elastic_compute.txt
> script stats < examples/elastic_stats.txt
> run init > result.txt
> run compute < examples/elastic_data.txt > result.txt
> run stats
Total number of samples: 12
Number of invalid samples: 1
You can use the 'tree' command to print the parser tree for a script. This is
only available if you enabled the tree debugging feature when compiling.
$ ./script
Starting script mode.
Type 'help' to get some help.
> start
if (z == 0) {
x = y;
} else {
x = y / (2. * z);
}
print(x);
z = z + 1;
end
> tree
Script
If
Condition
Is equal
Variable: z
Constant: 0.000000
Then
Assign
Variable: x
Variable: y
Else
Assign
Variable: x
Divide
Variable: y
Multiply
Constant: 2.000000
Variable: z
Print
Variable: x
Assign
Variable: z
Add
Variable: z
Constant: 1.000000
You can also provide a script name for a previously defined script:
> start foo
[...]
end
> tree foo
And you can redirect the output to a file:
> tree foo > foo_tree.txt
4) Compilation and contributions
--------------------------------
Compilation should be straightforward: type 'make' and that's it.
You can compile with support for the GNU deadline library for improved
text inout (including history). To do so you need to edit two lines in the
Makefile.
You can also enable some debugging features by editing the corresponding
line in the Makefile.
The source code was originally written on SunOS, IRIX and HP-UX. I have not
tested compilation on those systems when cleaning the code as I don't have
access to those anymore. I can however confirm it compiles and works on
Debian and Mac OS X. And I don't see why it would not work on any other
Linux and Unix systems.
Part of the code is documented as I was experimenting with doxygen, but
overall there is little comments or documentation (now you have been warned
feel free to look at the code anyway).
And if you do any modifications you deem worthwhile, please open a pull
request on github.com/criezy/ where the code is hosted.