Recently I have been taking some time to work on a personal project. A sub-project of it is an expression evaluator. I just finished it today. It was written in C++ with STL, without 3rd-party software. (download “my expression evaluator”)

This small tool work as a command line application, it take an argument as the expression. After evaluating the expression, it writes the result to stdout and returns 0. When error occurs, it writes error message to stderr, and returns 1.

The expression should be written in BASIC syntax. algebra, string, comparison and logic calculations are supported. The spec of it is as follows:

**Data types**

- boolean
- integer
- double
- string

**Operators**

- math: ^, +(unary), -(unary), *, /, \, MOD, +, –
- string: +
- relational: =, <>, <, <=, >, >=
- logic: NOT, AND, OR
- others: (, ) (to change precedences of operators)

**Math Functions**

abs, sign, int, sqrt, exp, log, log10, rad (change degrees to radians), deg (change radians to degrees), sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, sec, csc, pi (return value of Pi), random (return random number in [0, 1])

**String Functions:**

str(num) (convert number to string), space(n), tab(n), ltrim(str), rtrim(str), trim(str), len(str), ucase(str), lcase(str), val(str) (parse string to number), isNumeric(str), left(str, n), right(str, n), mid(str, from, n), instr(str1, str2) (return index starting from 0; if not found, return -1)

Note that here are some differences with the traditional BASIC language:

- some function names are different, for example, “sqrt” instead of “sqr”
- indexes in a string starts from 0 instead of 1
- BASIC type characters (such as “2#” for 2 of double type) not supported

This software is licensed under “HSL“, you can use it freely. When there are bugs, please let me know, I will try to fix them.