Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (81 sloc) 3.496 kB
2cb0dd3 make links from README work on github
darius authored
1 expr
2 ====
3
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
4 This package parses and evaluates mathematical expressions over
0eb7736 changed README to Markdown
darius authored
5 floating-point numbers, like `2 + 2` or `cos(x/(2*pi)) * cos(y/(2*pi))`.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
6
7 The design priorities were ease of use with helpful error messages,
8 ease of integration into applets, and good performance: fast
9 evaluation and short download times. Features and flexibility were
10 not high priorities, but the code is simple enough that it shouldn't
11 be hard to change to your taste.
12
13 Javadoc comments in the source files give full documentation, and
59996cf reformat for 80 columns
darius authored
14 [user-doc.html](expr/blob/master/doc/user-doc.html) gives a user's-eye
15 view.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
16
17
0eb7736 changed README to Markdown
darius authored
18 Installing it
19 =============
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
20
e4de456 how to build expr.jar
darius authored
21 To install, put `expr.jar` in your classpath. (You'll need to create
22 it first by running `make`, if you downloaded this from GitHub.)
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
23
0eb7736 changed README to Markdown
darius authored
24 To try it out, put the `expr` directory in your classpath. Then
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
25
26 java expr.Example '3.14159 * x^2' 0 4 1
27
59996cf reformat for 80 columns
darius authored
28 should write the output given in
29 [Example.java](expr/blob/master/expr/Example.java).
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
30
31 To incorporate this code into an applet, put expr.jar somewhere
32 accessible to your webserver, and reference it with the ARCHIVE
33 attribute in your HTML:
34
35 <APPLET ARCHIVE="/path/to/expr.jar"
36 CODE="MyApplet.class"
37 ...>
38 </APPLET>
39
0eb7736 changed README to Markdown
darius authored
40 To provide documentation for your users, put
2f78fef try again to make links from README work on github
darius authored
41 [user-doc.html](expr/blob/master/doc/user-doc.html) where they can read it.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
42
43
0eb7736 changed README to Markdown
darius authored
44 Using it
45 ========
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
46
47 To get started quickly without reading the documentation, see the
59996cf reformat for 80 columns
darius authored
48 example code in [Example.java](expr/blob/master/expr/Example.java).
49 Here are some excerpts from it, with each bit preceded by an
50 explanation:
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
51
0eb7736 changed README to Markdown
darius authored
52 `expr` is an object representing a parsed expression.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
53
54 Expr expr;
55
0eb7736 changed README to Markdown
darius authored
56 Parse the string in `args[0]` and set `expr` to the representation of
57 the result. We only parse the string once, so that later on we won't
58 have to parse it each time we evaluate it.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
59
60 try { expr = Parser.parse(args[0]); }
61
0eb7736 changed README to Markdown
darius authored
62 If the string couldn't be parsed, complain and abort. The `e.explain()`
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
63 tries to describe exactly what went wrong.
64
65 catch (SyntaxException e) {
66 System.err.println(e.explain())
67 return;
68 }
69
0eb7736 changed README to Markdown
darius authored
70 Create a variable-object for `x`, so that we can control the value that
71 `x` takes each time we evaluate the expression. For example, if the
72 expression is parsed from `1 + x * x`, its value will depend on what
73 we set `x` to.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
74
75 Variable x = Variable.make("x");
76
0eb7736 changed README to Markdown
darius authored
77 For values of `x` in the range from `low` to `high`, increasing by
78 `step`, print out the value of the expression.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
79
80 for (double xval = low; xval <= high; xval += step) {
81 x.setValue(xval);
82 System.out.println(expr.value());
83 }
84
0eb7736 changed README to Markdown
darius authored
85 There's another included example: the graphing applet in
2f78fef try again to make links from README work on github
darius authored
86 [example.html](expr/blob/master/example/example.html).
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
87
88
0eb7736 changed README to Markdown
darius authored
89 Other features
90 ==============
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
91
92 The above is the simplest code you can write to get going. With a few
93 more lines, you can help the parser catch more errors. By default, it
94 allows any variable to be in the input expression, even variables you
95 haven't defined. Here's how to tell it what's allowed:
96
97 Variable x = Variable.make("x");
98 Parser parser = new Parser();
99 parser.allow(x);
100
101 Expr expr;
102 try {
103 expr = parser.parseString(args[0]);
104 } catch (SyntaxException e) {
105 // the rest is the same as before
106 }
107
108 You can disallow all variables, if for some reason you want to, by
0eb7736 changed README to Markdown
darius authored
109 changing `parser.allow(x)` to `parser.allow(null)`.
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
110
111
0eb7736 changed README to Markdown
darius authored
112 Contact
113 =======
289ac9a initial import from http://www.accesscom.com/~darius/software/expr.zip
darius authored
114
59996cf reformat for 80 columns
darius authored
115 See the file [COPYING](expr/blob/master/COPYING) for copyright info.
116 Send questions and bug reports to Darius Bacon <darius@wry.me>.
Something went wrong with that request. Please try again.