uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
uCalc = [uCalc]
Property
Product:
Class:
Retrieves the parent uCalc instance that owns the function or operator currently executing the callback.
Remarks
The uCalc() method provides access to the parent uCalc instance from within a callback function. This is a crucial feature that elevates callbacks from simple, isolated calculations to powerful, interactive components that can query and modify the engine's state.
🎯 Core Use Cases
Accessing the parent instance allows your callback to:
- Evaluate Expressions: Use methods like EvalStr or Parse to perform secondary calculations within the same context.
- Access Engine Services: Retrieve type definitions with DataTypeOf, look up other items with ItemOf, or inspect instance settings.
- Modify State: Dynamically create new symbols with DefineVariable or DefineFunction based on the callback's logic.
- Access Variables: Read or write to other variables that exist within the instance's scope.
💡 Comparative Analysis
In many programming environments (like C# or C++), a callback function is typically stateless and unaware of its invocation context unless that context is explicitly passed as an argument. The uCalc() method provides this context automatically.
This is akin to having a built-in dependency injection system for every callback. Instead of a function being a simple black box that takes inputs and produces an output, it becomes an agent that can interact with the entire parsing environment it lives in. This enables highly dynamic and stateful behavior that is difficult to achieve in other systems without significant boilerplate code.
Examples
How to handle and retrieve various data types (including pointers) within a callback.
using uCalcSoftware;
var uc = new uCalc();
static void MyFunction(uCalc.Callback cb) {
var uc = cb.uCalc;
Console.WriteLine("------ MyFunc ------");
// Retrieve standard 32-bit and 64-bit integer arguments directly
Console.WriteLine(cb.ArgInt32(1));
Console.WriteLine(cb.ArgInt64(2));
// Retrieve the value of a pointer argument by referencing its exact data type.
Console.WriteLine(uc.ItemOf("Int8").DataType.ToString(cb.ArgAddr(3)));
// The Item object correctly identifies the type before conversion
Console.WriteLine(uc.ItemOf("Int").DataType.ToString(cb.ArgPtr(4)));
}
static void MyFunction2(uCalc.Callback cb) {
var uc = cb.uCalc;
Console.WriteLine("------ MyFunc2 ------");
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString(cb.ArgPtr(1)));
}
static void MyFunction3(uCalc.Callback cb) {
var uc = cb.uCalc;
Console.WriteLine("------ MyFunc3 ------");
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16).ToString(cb.ArgPtr(1)));
}
uc.DefineVariable("x As Int = 123"); // Int32
uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)");
uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", MyFunction);
uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)");
uc.DefineVariable("x2 As Int8 = -123");
uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)");
uc.DefineFunction("MyFunc2(d As Int8 Ptr)", MyFunction2);
uc.Eval("MyFunc2(xPtr2)");
uc.DefineVariable("x3 As Int16 = 1234");
uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)");
uc.DefineFunction("MyFunc3(d As Int16 Ptr)", MyFunction3);
uc.Eval("MyFunc3(xPtr3)");
------ MyFunc ------
1230
2
-1
123
------ MyFunc2 ------
-123
------ MyFunc3 ------
1234 using uCalcSoftware; var uc = new uCalc(); static void MyFunction(uCalc.Callback cb) { var uc = cb.uCalc; Console.WriteLine("------ MyFunc ------"); // Retrieve standard 32-bit and 64-bit integer arguments directly Console.WriteLine(cb.ArgInt32(1)); Console.WriteLine(cb.ArgInt64(2)); // Retrieve the value of a pointer argument by referencing its exact data type. Console.WriteLine(uc.ItemOf("Int8").DataType.ToString(cb.ArgAddr(3))); // The Item object correctly identifies the type before conversion Console.WriteLine(uc.ItemOf("Int").DataType.ToString(cb.ArgPtr(4))); } static void MyFunction2(uCalc.Callback cb) { var uc = cb.uCalc; Console.WriteLine("------ MyFunc2 ------"); Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString(cb.ArgPtr(1))); } static void MyFunction3(uCalc.Callback cb) { var uc = cb.uCalc; Console.WriteLine("------ MyFunc3 ------"); Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16).ToString(cb.ArgPtr(1))); } uc.DefineVariable("x As Int = 123"); // Int32 uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)"); uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", MyFunction); uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)"); uc.DefineVariable("x2 As Int8 = -123"); uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)"); uc.DefineFunction("MyFunc2(d As Int8 Ptr)", MyFunction2); uc.Eval("MyFunc2(xPtr2)"); uc.DefineVariable("x3 As Int16 = 1234"); uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)"); uc.DefineFunction("MyFunc3(d As Int16 Ptr)", MyFunction3); uc.Eval("MyFunc3(xPtr3)");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyFunction(uCalcBase::Callback cb) {
auto uc = cb.uCalc();
cout << "------ MyFunc ------" << endl;
// Retrieve standard 32-bit and 64-bit integer arguments directly
cout << cb.ArgInt32(1) << endl;
cout << cb.ArgInt64(2) << endl;
// Retrieve the value of a pointer argument by referencing its exact data type.
cout << uc.ItemOf("Int8").DataType().ToString(cb.ArgAddr(3)) << endl;
// The Item object correctly identifies the type before conversion
cout << uc.ItemOf("Int").DataType().ToString(cb.ArgPtr(4)) << endl;
}
void ucalc_call MyFunction2(uCalcBase::Callback cb) {
auto uc = cb.uCalc();
cout << "------ MyFunc2 ------" << endl;
cout << uc.DataTypeOf(BuiltInType::Integer_8).ToString(cb.ArgPtr(1)) << endl;
}
void ucalc_call MyFunction3(uCalcBase::Callback cb) {
auto uc = cb.uCalc();
cout << "------ MyFunc3 ------" << endl;
cout << uc.DataTypeOf(BuiltInType::Integer_16).ToString(cb.ArgPtr(1)) << endl;
}
int main() {
uCalc uc;
uc.DefineVariable("x As Int = 123"); // Int32
uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)");
uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", MyFunction);
uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)");
uc.DefineVariable("x2 As Int8 = -123");
uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)");
uc.DefineFunction("MyFunc2(d As Int8 Ptr)", MyFunction2);
uc.Eval("MyFunc2(xPtr2)");
uc.DefineVariable("x3 As Int16 = 1234");
uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)");
uc.DefineFunction("MyFunc3(d As Int16 Ptr)", MyFunction3);
uc.Eval("MyFunc3(xPtr3)");
}
------ MyFunc ------
1230
2
-1
123
------ MyFunc2 ------
-123
------ MyFunc3 ------
1234 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyFunction(uCalcBase::Callback cb) { auto uc = cb.uCalc(); cout << "------ MyFunc ------" << endl; // Retrieve standard 32-bit and 64-bit integer arguments directly cout << cb.ArgInt32(1) << endl; cout << cb.ArgInt64(2) << endl; // Retrieve the value of a pointer argument by referencing its exact data type. cout << uc.ItemOf("Int8").DataType().ToString(cb.ArgAddr(3)) << endl; // The Item object correctly identifies the type before conversion cout << uc.ItemOf("Int").DataType().ToString(cb.ArgPtr(4)) << endl; } void ucalc_call MyFunction2(uCalcBase::Callback cb) { auto uc = cb.uCalc(); cout << "------ MyFunc2 ------" << endl; cout << uc.DataTypeOf(BuiltInType::Integer_8).ToString(cb.ArgPtr(1)) << endl; } void ucalc_call MyFunction3(uCalcBase::Callback cb) { auto uc = cb.uCalc(); cout << "------ MyFunc3 ------" << endl; cout << uc.DataTypeOf(BuiltInType::Integer_16).ToString(cb.ArgPtr(1)) << endl; } int main() { uCalc uc; uc.DefineVariable("x As Int = 123"); // Int32 uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)"); uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", MyFunction); uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)"); uc.DefineVariable("x2 As Int8 = -123"); uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)"); uc.DefineFunction("MyFunc2(d As Int8 Ptr)", MyFunction2); uc.Eval("MyFunc2(xPtr2)"); uc.DefineVariable("x3 As Int16 = 1234"); uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)"); uc.DefineFunction("MyFunc3(d As Int16 Ptr)", MyFunction3); uc.Eval("MyFunc3(xPtr3)"); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyFunction(ByVal cb As uCalc.Callback)
Dim uc = cb.uCalc
Console.WriteLine("------ MyFunc ------")
'// Retrieve standard 32-bit and 64-bit integer arguments directly
Console.WriteLine(cb.ArgInt32(1))
Console.WriteLine(cb.ArgInt64(2))
'// Retrieve the value of a pointer argument by referencing its exact data type.
Console.WriteLine(uc.ItemOf("Int8").DataType.ToString(cb.ArgAddr(3)))
'// The Item object correctly identifies the type before conversion
Console.WriteLine(uc.ItemOf("Int").DataType.ToString(cb.ArgPtr(4)))
End Sub
Public Sub MyFunction2(ByVal cb As uCalc.Callback)
Dim uc = cb.uCalc
Console.WriteLine("------ MyFunc2 ------")
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString(cb.ArgPtr(1)))
End Sub
Public Sub MyFunction3(ByVal cb As uCalc.Callback)
Dim uc = cb.uCalc
Console.WriteLine("------ MyFunc3 ------")
Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16).ToString(cb.ArgPtr(1)))
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("x As Int = 123") '// Int32
uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)")
uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", AddressOf MyFunction)
uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)")
uc.DefineVariable("x2 As Int8 = -123")
uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)")
uc.DefineFunction("MyFunc2(d As Int8 Ptr)", AddressOf MyFunction2)
uc.Eval("MyFunc2(xPtr2)")
uc.DefineVariable("x3 As Int16 = 1234")
uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)")
uc.DefineFunction("MyFunc3(d As Int16 Ptr)", AddressOf MyFunction3)
uc.Eval("MyFunc3(xPtr3)")
End Sub
End Module
------ MyFunc ------
1230
2
-1
123
------ MyFunc2 ------
-123
------ MyFunc3 ------
1234 Imports System Imports uCalcSoftware Public Module Program Public Sub MyFunction(ByVal cb As uCalc.Callback) Dim uc = cb.uCalc Console.WriteLine("------ MyFunc ------") '// Retrieve standard 32-bit and 64-bit integer arguments directly Console.WriteLine(cb.ArgInt32(1)) Console.WriteLine(cb.ArgInt64(2)) '// Retrieve the value of a pointer argument by referencing its exact data type. Console.WriteLine(uc.ItemOf("Int8").DataType.ToString(cb.ArgAddr(3))) '// The Item object correctly identifies the type before conversion Console.WriteLine(uc.ItemOf("Int").DataType.ToString(cb.ArgPtr(4))) End Sub Public Sub MyFunction2(ByVal cb As uCalc.Callback) Dim uc = cb.uCalc Console.WriteLine("------ MyFunc2 ------") Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_8).ToString(cb.ArgPtr(1))) End Sub Public Sub MyFunction3(ByVal cb As uCalc.Callback) Dim uc = cb.uCalc Console.WriteLine("------ MyFunc3 ------") Console.WriteLine(uc.DataTypeOf(BuiltInType.Integer_16).ToString(cb.ArgPtr(1))) End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("x As Int = 123") '// Int32 uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)") uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", AddressOf MyFunction) uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)") uc.DefineVariable("x2 As Int8 = -123") uc.DefineVariable("xPtr2 As Int8 Ptr = AddressOf(x2)") uc.DefineFunction("MyFunc2(d As Int8 Ptr)", AddressOf MyFunction2) uc.Eval("MyFunc2(xPtr2)") uc.DefineVariable("x3 As Int16 = 1234") uc.DefineVariable("xPtr3 As Int16 Ptr = AddressOf(x3)") uc.DefineFunction("MyFunc3(d As Int16 Ptr)", AddressOf MyFunction3) uc.Eval("MyFunc3(xPtr3)") End Sub End Module