Overview

The components are intended for Delphi developers and perform high-speed (about 10 million evaluations per second) mathematics and boolean calculations. All components are available for Delphi 6, Delphi 7, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Delphi XE8, Delphi 10 Seattle, Delphi 10.1 Berlin. Please note that the samples do not require components installation (however installation is possible and in case when installed you find the components on "Samples" page of the pallete). You may open the Delphi project file (*.dpr) in the sample folder and simply run it.

Introduction

The best way to understand how parse components work is looking through the demo program source code. Hence the material below contains the basic information. Also, there are a number of samples to download with comprehensive information on parse components. Each sample contains minimum source code and only that one which covers the sample question. All the components are user-friendly and easy for understanding and using them.

First of all, we take a look at CalcUtils unit. This unit contains some simplest methods for making calculations of any type. Then we pay attention to the TCalculator component, which has many features - not only making calculations of any type, but also expression optimization, making new variables, multi-threaded mode and so on. After that we look through the TParser component - the heart of calculation mechanism. TParser has many low-level methods and properties and needed if you are going to extend its existing functionality. However, TParser component is simple in use, according to its philosophy - for example, it implements methods like AsByte, AsInteger, AsDouble, etc., having the singular parameter of string type, which represents the mathematics expression, and returning the number.

Brief

The components are intended for programmers of all levels. It makes possible using the parse components at any time without unnecessary understanding the internal parse mechanisms.

Some features of the parse components:

CalcUtils unit

This unit is for the quick calculation of any expression. You just use the CalcUtils unit and apply its methods. The methods below work at high speed:

The methods calculate mathematics expression (for example: "2 * 2"), which is in Text parameter.

TCalculator component

The component is intended for mathematics and boolean calculations. TCalculator is fully-functional, stand-alone mathematics and multi-threaded shell for the parser. It allows declaring your own variables, managing calculation process by setting up the internal properties and accessing to the internal components. The component philosophy is being easy in use and the primary aim is being fast in calculations - even when parsing a big amount of different expressions.

All calculations are performed by two ways:

TCalculator has such an important thing as optimization mechanism. All expressions are being optimized during the calculation process. Optimization is simplifying the mathematic expression (if possible) at binary level; the result is increase of evaluation speed when calculating by byte-code. TCalculator automatically optimizes all expressions by default.

TCalculator works in multi-threaded mode. Multi-threaded mode provides the best performance due to simultaneous evaluation the elements in an array of expressions. Of course to use the multi-threaded mode we first need to fill the expression array. After that we start the execution in multi-thread mode and wait until all threads are done. TCalculator has properties for managing the amount of threads, adjusting the priority of each thread and much more. It also supports using user-defined (Classes.TThread inheritor) threads.

TCalculator in use

Please take a look at the following samples to see how TCalculator is simple in use:

1) Evaluation of mathematic expression:

2) Evaluation of Boolean expression:

3) Creation of "MyVar" variable, initializing and using it in mathematic expression:

4) Running TCalculator in multi-threaded mode:

It is necessary to say that there is a difference between calling TParser.AsByte and TCalculator.AsByte or other like-named methods, in spite of these methods do the same (from the user's point of view). For example, TCalculator uses internal component TParseManager - one more optimization mechanism, intended for the additional speedup.

TParser component

TParser is intended for mathematics and Boolean calculations and contains many of standard functions, such as Sin (returns the sine of the angle in radians) or Sqrt (returns the square root of the parameter), etc. It also contains standard set of types, such as Byte or Word, which are used for specifying the functions, variables and numbers. It is possible to create your own functions, variables and types (AddFunction, AddVariable, AddType methods). It creates binary representation of formula - script (StringToScript method, TScript = Types.TByteDynArray) and makes the following calculations using the script (Execute method). There are no limitations for the formula; it may have any length and any amount of embedded formulas. The parser implements many features, including methods for simplifying the formula (Optimize method). It supports variables and functions with unlimited number of parameters. It also supports script decompiling (ScriptToString method).

Expressions can be constructed from operands, functions, variables and numbers.

The list below contains all standard functions and its description:

TGraph component

TGraph component is intended for building the graph in rectangular and polar coodrinate systems. Please take a look at some samples showing colorful graphs: