FormScript is an interpreted domain specific language for creating simple interactive calculator programs on the CLI, with emphasis on easy I/O handling.
Suppose we want to calculate the volume and surface area of a cylinder (or many). This is quickly implemented in FormScript:
# cylinder.fsc
info "This program calculates the volume and surface area of a cylinder.";
get "Height of the cylinder" h;
get "Radius of base" r;
curved_surface = 2 * PI * r * h;
base_surface = 2 * (PI * r^2);
put "Volume of the cylinder" h * PI * r^2;
put "Surface area of the cylinder" curved_surface + base_surface;
Running this script with the interpreter yields the output:
$ formscript cylinder.fsc
This program calculates the volume and surface area of a cylinder.
?- Height of the cylinder: 10
?- Radius of base: 4
:- Volume of the cylinder: 502.654
:- Surface area of the cylinder: 351.858
Where lines prefixed by ?-
are interactive prompts asking for user input, making the script reusable for any height or radius arguments.
There are four types of statements:
Prints the given string to the terminal, much like a usual printLn
function, eg:
info "This program calculates the volume and surface area of a cylinder.";
Prompts the user for a number and assigns it to the given variable, eg:
get "Height of the cylinder" h;
... produces the prompt
?- Height of the cylinder:
and awaits user input.
Evaluate the righthand expression and assign its result to the lefthand variable, eg:
curved_surface = 2 * PI * r * h;
Prints the result of an expression or value of a variable alongside a short info text, eg:
put "Surface area of the cylinder" curved_surface + base_surface;
Expressions are composed of operators, variables and constants.
A few examples of valid expressions: 10 * 2
, (5 + PI) * 3
, 2 ^(n + 1)
, a + b
, z
.
Currently available operators are + - * / ^
.
Variable names are alphanumeric and must start with an alphabetic character.
Either a number literal like 10
, 5.0
, 2.0
and so on, or one of the following predefined constants:
Syntax | Value |
---|---|
PI |
Pi: 3.141 |
E |
Eulers number: 2.71828 |
Every script is a semicolon-separated list of the four kinds of statements documented above. Considering statements are executed procedurally in order, the usual (but not stipulated) approach would be:
- Getting input values with
get
- Performing calculations with assignment statements
- Printing output with
put
FormScript is simple by design, this pretty much sums it up. For further reference check the scripts supplied in the `examples` directory.
Have any reasonably recent version of GHC installed, then run
./make.sh
The interpreter executable will appear in the build
directory.