Recent

Author Topic: RunFormula: math expression parser and evaluator  (Read 1266 times)

stormray

  • Newbie
  • Posts: 6
RunFormula: math expression parser and evaluator
« on: April 15, 2026, 01:16:24 pm »
Hello!

I would like to announce you RunFormula, my math expression parser and scripting engine for FreePascal and Lazarus.

Key features:

Supported
    integers in decimal, hexadecimal and binary formats;
    floating-point numbers in decimal and scientific notation;
    complex numbers and operations on them;
    intervals and operations on them;
    strings and ASCII characters;

Arithmetic and logical operations: + - * / or and xor not shl (<<) shr (>>) mod (%) div == <> < <= >= > & (string concatenation) ** (integer exponentiation).
Variables and runtime initialization of variables via external callback function.
A set of built-in functions and the ability to register and use additional user-defined functions.
Control flow functions: if() repeat() exit() result() continue() break()
Ability to use inline function definitions directly within the formula (subroutines).
Support for the define directive.
Compiling the source formula into bytecode for multiple execution.
Minimal dependencies (only SysUtils in the base configuration).

Source code, Demo project, Help, Examples on GitHub:

https://github.com/runnfla/RunFormula

Any feedback are appreciated.
« Last Edit: May 09, 2026, 02:55:34 pm by stormray »

Handoko

  • Hero Member
  • *****
  • Posts: 5558
  • My goal: build my own game engine using Lazarus
Re: RunFormula: math expression parser and evaluator
« Reply #1 on: April 15, 2026, 01:24:30 pm »
Hello stormray,
Welcome to the forum.

Nice, I like. I may need it someday, bookmarked.
Thank you for sharing it.

stormray

  • Newbie
  • Posts: 6
Re: RunFormula: math expression parser and evaluator
« Reply #2 on: May 09, 2026, 03:05:54 pm »
Version 0.0 is released. Added experimental support for physical dimensions, including unit consistency checks, automatic result dimensioning and automatic conversion between unit systems.

LeP

  • Sr. Member
  • ****
  • Posts: 441
Re: RunFormula: math expression parser and evaluator
« Reply #3 on: May 09, 2026, 03:39:53 pm »
I just want to make a note: when generating commits, give them a meaningful name that helps understand the story (not a generic UPD)
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1285
Re: RunFormula: math expression parser and evaluator
« Reply #4 on: May 10, 2026, 10:49:20 pm »
Hello!

I would like to announce you RunFormula, my math expression parser and scripting engine for FreePascal and Lazarus.

Key features:

Supported
    integers in decimal, hexadecimal and binary formats;
    floating-point numbers in decimal and scientific notation;
    complex numbers and operations on them;
    intervals and operations on them;
    strings and ASCII characters;

Arithmetic and logical operations: + - * / or and xor not shl (<<) shr (>>) mod (%) div == <> < <= >= > & (string concatenation) ** (integer exponentiation).
Variables and runtime initialization of variables via external callback function.
A set of built-in functions and the ability to register and use additional user-defined functions.
Control flow functions: if() repeat() exit() result() continue() break()
Ability to use inline function definitions directly within the formula (subroutines).
Support for the define directive.
Compiling the source formula into bytecode for multiple execution.
Minimal dependencies (only SysUtils in the base configuration).

Source code, Demo project, Help, Examples on GitHub:

https://github.com/runnfla/RunFormula

Any feedback are appreciated.


Version 0.0 is released. Added experimental support for physical dimensions, including unit consistency checks, automatic result dimensioning and automatic conversion between unit systems.

Welcome to the forum.

Interesting. Thanks for sharing.

stormray

  • Newbie
  • Posts: 6
Re: RunFormula: math expression parser and evaluator
« Reply #5 on: June 15, 2026, 03:30:10 pm »
GaleFuncs - extension for LibreOffice Calc and MS Excel based on the RunFormula scripting engine.

https://github.com/runnfla/GaleFuncs
https://extensions.libreoffice.org/en/extensions/show/99599

You can now add meters to seconds directly within Calc cells  :)

« Last Edit: June 15, 2026, 07:33:01 pm by stormray »

CM630

  • Hero Member
  • *****
  • Posts: 1743
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: RunFormula: math expression parser and evaluator
« Reply #6 on: June 16, 2026, 12:17:34 pm »
I tried the demo (RFDemo.lpi).

1. 1,23+4,56   resulted in ... 56  :o

I could be 42 or something more common.
2. 5/0 resulted in a crash.
« Last Edit: June 16, 2026, 12:38:12 pm by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

stormray

  • Newbie
  • Posts: 6
Re: RunFormula: math expression parser and evaluator
« Reply #7 on: June 16, 2026, 06:20:13 pm »
Hi CM630, thank you for your interest in RunFormula.

1. 1,23+4,56   resulted in ... 56  :o

The result 56 is correct. You used a comma as a decimal separator, but RunFormula uses a dot. If you type 1.23+4.56 you will get 5.79. But due to regional settings, the result might be displayed as 5,79. This will be fixed soon.

Commas are used to separate expressions. So, 1,23+4,56 means: evaluate 1, then evaluate 23+4, and then evaluate 56. The system returns the last result, which is 56.

Upd: To resolve this issue, I plan to disallow digits immediately following a comma. Writing 1,23+4,56 will trigger an error. The correct format will be 1, 23+4, 56

2. 5/0 resulted in a crash.

You need to turn off debugging in Project Options. Please uncheck everything on the Compiler Options -> Debugging tab, and then you'll see a proper error message.
« Last Edit: June 16, 2026, 10:37:26 pm by stormray »

CM630

  • Hero Member
  • *****
  • Posts: 1743
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: RunFormula: math expression parser and evaluator
« Reply #8 on: June 17, 2026, 08:21:49 am »
Hi CM630, thank you for your interest in RunFormula.

1. 1,23+4,56   resulted in ... 56  :o

The result 56 is correct. You used a comma as a decimal separator, but RunFormula uses a dot. If you type 1.23+4.56 you will get 5.79. But due to regional settings, the result might be displayed as 5,79. This will be fixed soon.

Commas are used to separate expressions. So, 1,23+4,56 means: evaluate 1, then evaluate 23+4, and then evaluate 56. The system returns the last result, which is 56.

Upd: To resolve this issue, I plan to disallow digits immediately following a comma. Writing 1,23+4,56 will trigger an error. The correct format will be 1, 23+4, 56
Hardcoding the decimal delimiter and the parameter separator will result in nothing good.
I would not like dots as decimal separators in my output, no matter what my regional settings are: they will look pretty odd in an ISO/IEC compliant report and will be perfectly wrong in a ГОСТ document.
On the other hands for India I would like dots, probably.
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

Jorg3000

  • Jr. Member
  • **
  • Posts: 86
Re: RunFormula: math expression parser and evaluator
« Reply #9 on: June 17, 2026, 02:01:37 pm »
Upd: To resolve this issue, I plan to disallow digits immediately following a comma. Writing 1,23+4,56 will trigger an error. The correct format will be 1, 23+4, 56

Hi!
I would like to suggest that the function RunFlaParse() gets an optional parameter that allows you to specify how points/commas should be interpreted, for example: has decimal point, or decimal comma, or use default setting, or use the operating system setting.

The advantage would be that you could define individually for each call exactly what this string contains.
Then you could pass either user input or program-defined strings, without having to change any global settings for this call.
Jörg
« Last Edit: June 17, 2026, 02:05:10 pm by Jorg3000 »

stormray

  • Newbie
  • Posts: 6
Re: RunFormula: math expression parser and evaluator
« Reply #10 on: June 17, 2026, 03:45:25 pm »
Hi Jörg,

I would like to suggest that the function RunFlaParse() gets an optional parameter that allows you to specify how points/commas should be interpreted, for example: has decimal point, or decimal comma, or use default setting

I'm afraid that this will just cause confusion, and the scripts will become non-portable across different systems.

Yes, it’s a dilemma: whether to use the comma as a decimal mark or as an expression/variable separator. By choosing the dot as the decimal separator and the comma for expressions, I was following the convention of most programming languages, as well as the mathematical practice of separating expressions with commas. I’ll think about this issue some more. Alternatively, both the dot and the comma could be allowed as decimal separators, while restricting the expression separator strictly to the semicolon. However, that would make the syntax feel unconventional—for example, a function call would look like func(X; 1; 2), an array element like A[10; 20], and a complex number like (1;2).

CM630

  • Hero Member
  • *****
  • Posts: 1743
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: RunFormula: math expression parser and evaluator
« Reply #11 on: June 17, 2026, 09:04:39 pm »
When the regional decimal delimiter is a dot, Excel separates the parameters with a comma.
But when the decimal seprator is a comma, they are separarted with a ;
One never knows what data (file) will come as an input, so autoswicthing only will cause troubles.

... However, that would make the syntax feel unconventional—for example, a function call would look like func(X; 1; 2), an array element like A[10; 20], and a complex number like (1;2).
The syntax might feel unconvetional to programmers, but software is also often used by humans.


Btw:
procedure Calculate(a : Double; b : Double);
but
Calculate (5,6);
« Last Edit: June 17, 2026, 09:08:33 pm by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

stormray

  • Newbie
  • Posts: 6
Re: RunFormula: math expression parser and evaluator
« Reply #12 on: June 17, 2026, 10:32:05 pm »
When the regional decimal delimiter is a dot, Excel separates the parameters with a comma.
But when the decimal seprator is a comma, they are separarted with a ;

You are absolutely right about how Excel handles this. However, this automatic switching often causes major problems when users with different regional settings collaborate. I want to avoid this completely in RunFormula scripts. A script shouldn't depend on a user's locale.
For now, I'm leaning toward keeping things as they are: a dot as the strict decimal separator, and a comma for expressions. In the future, I plan to add dedicated functions for input and output formatting to accommodate different regional views.

 

TinyPortal © 2005-2018