This calculator is using the ANTLR4 JavaScript target to calculate results from formulas that are passed in as string. Both JavaScript and TypeScript is supported.
It's a Visual Studio solution project and requires TypeScript >= 2.0.2 to run. Output is generated by doing first npm install
and then running the npm run build
command in the project directory (no Visual Studio required). The JavaScript bundle.js
is gzipped just a bit over 50 kb.
Whenever a calculation is performed, a CalculationResult
is returned with the following properties:
Property | Type | |
---|---|---|
isValid | boolean | true if the formula could be parsed and calculated, else false |
errorPosition | number | Position of the offending symbol in the line, 0 based index, for invalid results, else null |
errorMessage | string | ANTLR error message for invalid formulas, else null |
result | number | NaN for invalid formulas, else the actual result |
Unit tests are performed using Chutzpah (via console in CI and with the Visual Studio extension while developing).
Since this is mostly a learning project, expect bugs😉
You can check out the live demo here!
Clone this repository or just go with npm install antlr-calculator
.
Just reference dist/bundle.js
and the global variable antlrCalc
is available.
var result = antlrCalc.Calculator.calculate('4*5');
console.log(JSON.stringify(result, null, 2));
// {
// "isValid": true,
// "errorPosition": null,
// "errorMessage": null,
// "result": 20
// }
Import the Calculator
class and use the static calculate(formula: string)
method to evaluate formulas.
import { Calculator } from 'antlr-calculator';
var result = Calculator.calculate('4*5');
console.log(JSON.stringify(result, null, 2));
// {
// "isValid": true,
// "errorPosition": null,
// "errorMessage": null,
// "result": 20
// }
The FileStream
class in the antlr4
library has a call to require('fs')
if the
current environment is detected to be node.js. You might get errors indicating that
the fs
module cannot be resolved. When running in the browser, it's safe to ignore this module.
Add the following to your webpack.config
:
node: {
fs: 'empty'
}
Expression | |
---|---|
FLOOR expression |
Round down to zero accuracy |
CEIL expression |
Round up to zero accuracy |
ABS expression |
Absolute value |
ROUNDK '(' expression ';' expression ')' |
Round expr_1 with expr_2 accuracy |
ROUND expression |
Round with zero accuracy |
TRUNC expression |
Trim decimal digits |
SIN expression |
Sinus |
COS expression |
Cosinus |
TAN expression |
Tangens |
COT expression |
Cotangens |
SINH expression |
Sinus Hypererbolicus |
COSH expression |
Cosinus Hyperbolicus |
TANH expression |
Tangens Hyperbolicus |
ARCSIN expression |
Inverse Sinus |
ARCCOS expression |
Inverse Cosinus |
ARCTAN expression |
Inverse Tangens |
ARCTAN2 '(' expression ';' expression ')' |
Atan2 |
ARCCOT expression |
Inverse Cotangens |
EXP expression |
e ^ expr |
LN expression |
Logarithm to e |
EEX expression |
10 ^ expr |
LOG expression |
Logarithm to 10 |
RAD expression |
Angle to radians (360° base) |
DEG expression |
Radians to angle (360° base) |
SQRT expression |
Square root |
SQR expression |
Square product |
`expression op = ('^' | '**') expression` |
`expression (MOD | '%' ) expression` |
expression DIV expression |
Whole part of division rest |
`expression op = ('~' | '//') expression` |
`expression op = ('*' | '/') expression` |
`expression op = ('+' | '-') expression` |
NUMBER |
Single integer or float number |
'(' expression ')' |
Expression within parentheses |
PI '()'? |
Mathematical constant pi = 3,141593 |
expression E+ expression |
Exponent, e.g. 10e+43 |
expression E- expression |
Inverted Exponent, e.g. 10e-43 |
EULER |
Mathematical constant e = 2,718282 |
'-' expression |
Unary minus sign (negative numbers) |
'+' expression |
Unary plus sign (positive numbers) |
expression may be any expression as functions can be nested. Example: DEG(2*PI)
or LOG(10^3)
.
Formulas can be case invariant, e.g. SIN
, sin
and siN
are all considered the same.
Comments in Formulas are supported by encapsulating them either in /*...*/
, '...'
or "..."
quote styles. Examples:
4/*Length*/*3/*Width*/
resolves to 12
4'Length'*3'Width'
resolves to 12
4"Length"*3"Width"
resolves to 12