Instead of simply building and evaluating expressions of integer values as in Arithmetica, you should allow the user to create expressions that evaluate to images. In this program, images are represented as an MxN grid of numeric color values within a specific range (i.e., -1 to 1, 0 to 255, etc.). So, in the simplest sense, this is just building expressions that evaluate grids of numbers instead of a single number.
The result of evaluating an expression in this application is an image. Thus, it will not be practical to print the image to the console after each command. Instead, you should save the result of the expression in a temporary file and use a program (like xv, irfanview, or gimp) to display the results.
Your program should recognize the following expressions:
expression |
|
semantics |
|
---|---|---|---|
Constant |
[0-1]+(.[0-9]+)? |
an image of constant color |
87
1.0 0.4 |
String |
between quotes> |
an image of the given file name is read, if the image cannot be read, an all black image is produced |
"foo.ppm"
"images/mickey.ppm" |
Variable |
[a-zA-z0-9]+ |
an expression represented by a word |
a
bugs q45 |
Assignment |
|
assigns an expression to a variable |
a = 1.0
bugs = "foo/ppm" a = bugs |
Unary Operator |
|
prefixes an expression
~ // invert an image |
~a
~(t + a * 0.1) |
Binary Operator |
|
combines two expressions into a single expression (in precedence order)
* // times / // divide + // plus - // minus |
a + b
a / 2 a + 10 * c |
Unary Functions |
|
a function that takes an expression as its single argument
sin // sine cos // cosine tan // tangent abs // absolute value |
sin(a * b)
abs(x) - y / 2 |
Multi-Argument Functions | a function that takes one or more arguments; the first of
which is an expression and the second is shown below
color(const, const, const) // image of single rgb color color(string) // image of single "#RRGGBB" color filter(expr, string) // apply kernel from file to expr |
color(0.1, 0.5, 1)
color("#ff00ff") filter(a, "blur") |
|
Parentheses |
|
raises an expression's precedence |
(a + b) * 3
~(bugs - 0.1) |
Operators have the following precedence from left to right (listed from
highest to lowest):
() | parentheses |
! | unary operators |
*, / | multiplicative operators |
+, - | additive operators |
= | assignment |
New Functions | define functions with the following semantics
random() // create random image expression mutate(expr) // create new image from old by introducing random expressions mate(expr, expr) // create new image from "parents" by randomly combining |