MAGES is the official successor to YAMP. It is a very simple, yet powerful, expression parser and interpreter. You can use MAGES to include a sophisticated, easy to customize, and lightweight scripting engine to your application.
Among other applications, MAGES has been used in Microsoft's PowerToys.
2024:
MAGES was just updated (v3.0.0) with object metadata, direct list support, and JSX syntax.
JSX as you know it - stringified via html
:
<div class={"hello" + "," + "there"}><h1>Hi</h1><p>World.</p></div> | html
// result: <div class="hello,there"><h1>Hi</h1><p>World.</p></div>
Object metadata reflected via type
:
new { a: "foo", b: 42 } | type | json
// {
// "name": "Object",
// "create": "[Function]",
// "keys": {
// "0": "a",
// "1": "b"
// }
// }
((x, y, z) => x + y + z) | type | json
// {
// "name": "Function",
// "create": "[Function]",
// "parameters": {
// "0": "x",
// "1": "y",
// "2": "z"
// }
// }
Placeholders for calling functions / specifying what should be curry'ed:
var f = (x, y, z) => x + 2 * y + 3 * z;
5 | f(1, _, 2)
// 17, by computing 1 + 2 * 5 + 3 * 2
2023:
MAGES was updated (v2.0.0) with support for complex numbers. Also, the build target and runtime has been updated to make use of modern possibilities.
2018:
The first stable version has been released. The current version 1.6.0 contains an improved REPL. The library contains everything to perform lightweight scripting operations in C#. A CodeProject article about the library (also containing some background and performance comparisons) is also available.
MAGES itself does not have any dependencies, however, the tests are dependent on NUnit and the benchmarks use BenchmarkDotNet. Usually, MAGES should be installed via the NuGet package source. If this does not work for you, then clone the source and build MAGES yourself. Make sure that all unit tests pass.
The whole library was designed to be consumed from .NET Core 3.0 (or higher) / .NET 5.0 (or higher) applications. This means it is (amongst others) compatible with Unity 2021.2 or Mono 6.4. The NuGet package is available via the official package feed.
In the most simple case you are creating a new engine to hold a global scope (for variables and functions) and launch the interpretation.
var engine = new Mages.Core.Engine();
var result = engine.Interpret("sin(2) * cos(pi / 4)"); // 0.642970376623918
You can also go-ahead and make reusable blocks from snippets.
var expOne = engine.Compile("exp(1)");
var result = expOne(); // 2.71828182845905
Or you can interact with elements created by MAGES.
var func = engine.Interpret("(x, y) => x * y + 3 * sqrt(x)") as Mages.Core.Function;
var result = func.Invoke(new Object[] { 4.0, 3.0 }); // 18.0
Or even simpler (details are explained in the getting started document):
var func = engine.Interpret("(x, y) => x * y + 3 * sqrt(x)") as Mages.Core.Function;
var result = func.Call(4, 3); // 18.0
These are just some of the more basic examples. More information can be found in the documentation.
The documentation is given in form of Markdown documents being placed in the doc folder of this repository. The following links are worth checking out:
If anything is missing, unclear, or wrong then either submit a PR or file an issue. See the following section on contributions for more information.
Contributions in form of feature implementations or bug fixes are highly welcome, but need to be performed in an organized and consistent way. The contribution guidelines should be read before starting any work.
Contributions may also be taken in form of bug reports and feature requests. Long live open-source development!
The rules of semver are our bread and butter. In short this means:
- MAJOR versions at maintainers' discretion following significant changes to the codebase (e.g., breaking API changes)
- MINOR versions for backwards-compatible enhancements (e.g., performance improvements, additional extensions)
- PATCH versions for backwards-compatible bug fixes (e.g., specification compliance bugs, support issues)
Hence: Do not expect any breaking changes within the same major version.
The following companies sponsored part of the development of MAGES.
Thanks for all the support and trust in the project!
This code is released using the MIT license. For more information see the LICENSE file.