uCalc SDK Interactive Examples

Creating a variable in the default instance and retrieving its value.

ID: 607

				
					using uCalcSoftware;

var uc = new uCalc();
using (var myVar = new uCalc.Item("Variable: x = 42")) {
   Console.WriteLine($"Created variable '{myVar.Name}' with value: {myVar.Value()}");
}
				
			
Created variable 'x' with value: 42
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   {
      uCalc::Item myVar("Variable: x = 42");
      myVar.Owned(); // Causes myVar to be released when it goes out of scope
      cout << "Created variable '" << myVar.Name() << "' with value: " << myVar.Value() << endl;
   }
}
				
			
Created variable 'x' with value: 42
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Using myVar As New uCalc.Item("Variable: x = 42")
         Console.WriteLine($"Created variable '{myVar.Name}' with value: {myVar.Value()}")
      End Using
   End Sub
End Module
				
			
Created variable 'x' with value: 42
Creating alternative names for Define commands

ID: 58

				
					using uCalcSoftware;

var uc = new uCalc();
uc.CreateAlias("VariableDefinition", "Variable", true);
uc.CreateAlias("Method", "Function", true);

uc.Define("VariableDefinition: MyVar = 123");
uc.Define("Method: MyFunc(x) = x * 10");

Console.WriteLine(uc.Eval("MyVar"));
Console.WriteLine(uc.Eval("MyFunc(5)"));
				
			
123
50
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.CreateAlias("VariableDefinition", "Variable", true);
   uc.CreateAlias("Method", "Function", true);

   uc.Define("VariableDefinition: MyVar = 123");
   uc.Define("Method: MyFunc(x) = x * 10");

   cout << uc.Eval("MyVar") << endl;
   cout << uc.Eval("MyFunc(5)") << endl;
}
				
			
123
50
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.CreateAlias("VariableDefinition", "Variable", true)
      uc.CreateAlias("Method", "Function", true)
      
      uc.Define("VariableDefinition: MyVar = 123")
      uc.Define("Method: MyFunc(x) = x * 10")
      
      Console.WriteLine(uc.Eval("MyVar"))
      Console.WriteLine(uc.Eval("MyFunc(5)"))
   End Sub
End Module
				
			
123
50
Creating an error handler that automatically defines variables on the fly by checking for an 'Undefined Identifier' error.

ID: 326

				
					using uCalcSoftware;

var uc = new uCalc();

static void AutoDefineHandler(Handle_uCalc h) {
   var uc = new uCalc(h);
   // Check if the error is specifically an undefined identifier
   if (uc.Error.Code == ErrorCode.Undefined_Identifier) {
      // If so, define the missing variable and instruct uCalc to resume
      Console.WriteLine($"Auto-defining variable: '{uc.Error.Symbol}'");
      uc.DefineVariable(uc.Error.Symbol);
      uc.Error.Response = ErrorHandlerResponse.Resume;
   }
}


uc.Error.AddHandler(AutoDefineHandler);

// 'x' doesn't exist, but the handler will intercept the error and create it.
var Result = uc.EvalStr("x = 10; x * 5");
Console.WriteLine($"Result: {Result}");
				
			
Auto-defining variable: 'x'
Result: 50
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call AutoDefineHandler(Handle_uCalc h) {
   auto uc = uCalc(h);
   // Check if the error is specifically an undefined identifier
   if (uc.Error().Code() == ErrorCode::Undefined_Identifier) {
      // If so, define the missing variable and instruct uCalc to resume
      cout << "Auto-defining variable: '" << uc.Error().Symbol() << "'" << endl;
      uc.DefineVariable(uc.Error().Symbol());
      uc.Error().Response(ErrorHandlerResponse::Resume);
   }
}

int main() {
   uCalc uc;
   uc.Error().AddHandler(AutoDefineHandler);

   // 'x' doesn't exist, but the handler will intercept the error and create it.
   auto Result = uc.EvalStr("x = 10; x * 5");
   cout << "Result: " << Result << endl;
}
				
			
Auto-defining variable: 'x'
Result: 50
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub AutoDefineHandler(ByVal h As Handle_uCalc)
      Dim uc As New uCalc(h)
      '// Check if the error is specifically an undefined identifier
      If uc.Error.Code = ErrorCode.Undefined_Identifier Then
         '// If so, define the missing variable and instruct uCalc to resume
         Console.WriteLine($"Auto-defining variable: '{uc.Error.Symbol}'")
         uc.DefineVariable(uc.Error.Symbol)
         uc.Error.Response = ErrorHandlerResponse.Resume
      End If
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      uc.Error.AddHandler(AddressOf AutoDefineHandler)
      
      '// 'x' doesn't exist, but the handler will intercept the error and create it.
      Dim Result = uc.EvalStr("x = 10; x * 5")
      Console.WriteLine($"Result: {Result}")
   End Sub
End Module
				
			
Auto-defining variable: 'x'
Result: 50
Creating isolated evaluation contexts.

ID: 256

				
					using uCalcSoftware;

var uc = new uCalc();
var main = new uCalc();
main.DefineVariable("rate = 0.05");

var scenarioA = main.Clone();
var scenarioB = main.Clone();

scenarioA.DefineVariable("rate = 0.10");
scenarioB.DefineVariable("rate = 0.20");

Console.WriteLine($"A: {scenarioA.Eval("1000 * rate")}");
Console.WriteLine($"B: {scenarioB.Eval("1000 * rate")}");
Console.WriteLine($"Main: {main.Eval("1000 * rate")}");
main.Release();
scenarioA.Release();
scenarioB.Release();

				
			
A: 100
B: 200
Main: 50
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uCalc main;
   main.DefineVariable("rate = 0.05");

   auto scenarioA = main.Clone();
   auto scenarioB = main.Clone();

   scenarioA.DefineVariable("rate = 0.10");
   scenarioB.DefineVariable("rate = 0.20");

   cout << "A: " << scenarioA.Eval("1000 * rate") << endl;
   cout << "B: " << scenarioB.Eval("1000 * rate") << endl;
   cout << "Main: " << main.Eval("1000 * rate") << endl;
   main.Release();
   scenarioA.Release();
   scenarioB.Release();

}
				
			
A: 100
B: 200
Main: 50
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim main As New uCalc()
      main.DefineVariable("rate = 0.05")
      
      Dim scenarioA = main.Clone()
      Dim scenarioB = main.Clone()
      
      scenarioA.DefineVariable("rate = 0.10")
      scenarioB.DefineVariable("rate = 0.20")
      
      Console.WriteLine($"A: {scenarioA.Eval("1000 * rate")}")
      Console.WriteLine($"B: {scenarioB.Eval("1000 * rate")}")
      Console.WriteLine($"Main: {main.Eval("1000 * rate")}")
      main.Release()
      scenarioA.Release()
      scenarioB.Release()
      
   End Sub
End Module
				
			
A: 100
B: 200
Main: 50
Creating uCalc instances

ID: 78

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineVariable("x = 123");

var uc1 = new uCalc(); // Creates a new instance
Console.WriteLine(uc1.EvalStr("x")); // uc1 does not have a variable named x
uc1.Release(); // Releases uc1 if it is no longer needed

var uc2 = uc.Clone(); // Creates new instance that is a clone of uc
Console.WriteLine(uc2.EvalStr("x")); // starts with the value of x obtained from uc
uc2.Eval("x = 456"); // Changes the value of x in uc1 but not uc
Console.WriteLine(uc2.EvalStr("x"));
Console.WriteLine(uc.EvalStr("x")); // The original x in uc remains unchanged
uc2.Release();

// Language specific - auto-releasing uCalc object

{ // Instances pointed to by neither uCalc1 nor uCalc2 will be released when they go out of scope
   var uCalc1 = new uCalc();
   var uCalc2 = uc.Clone();
   // Call uCalc1.Release() and uCalc2.Release() explicitly if want to release them
}

{ // The instances that both uCalc1 and uCalc2 point to will be released when uCalc1 and uCalc2 go out of scope
   using var uCalc1 = new uCalc();
   using var uCalc2 = uc.Clone();

   // No need for uCalc1.Release() or uCalc2.Release(), they will automatically be released
}

				
			
Undefined identifier
123
456
123
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineVariable("x = 123");

   uCalc uc1; // Creates a new instance
   cout << uc1.EvalStr("x") << endl; // uc1 does not have a variable named x
   uc1.Release(); // Releases uc1 if it is no longer needed

   auto uc2 = uc.Clone(); // Creates new instance that is a clone of uc
   cout << uc2.EvalStr("x") << endl; // starts with the value of x obtained from uc
   uc2.Eval("x = 456"); // Changes the value of x in uc1 but not uc
   cout << uc2.EvalStr("x") << endl;
   cout << uc.EvalStr("x") << endl; // The original x in uc remains unchanged
   uc2.Release();

   // Language specific - auto-releasing uCalc object

   { // Instances pointed to by neither uCalc1 nor uCalc2 will be released when they go out of scope
      auto uCalc1 = new uCalc();
      auto uCalc2 = uc.Clone();
      // Call uCalc1.Release() and uCalc2.Release() explicitly if want to release them
   }

   { // The instances that both uCalc1 and uCalc2 point to will be released when uCalc1 and uCalc2 go out of scope
      
      uCalc uCalc1;
      uCalc uCalc2(uc.Clone());
      // No need for uCalc1.Release() or uCalc2.Release(), they will automatically be released
   }

}
				
			
Undefined identifier
123
456
123
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("x = 123")
      
      Dim uc1 As New uCalc() '// Creates a new instance
      Console.WriteLine(uc1.EvalStr("x")) '// uc1 does not have a variable named x
      uc1.Release() '// Releases uc1 if it is no longer needed
      
      Dim uc2 = uc.Clone() '// Creates new instance that is a clone of uc
      Console.WriteLine(uc2.EvalStr("x")) '// starts with the value of x obtained from uc
      uc2.Eval("x = 456") '// Changes the value of x in uc1 but not uc
      Console.WriteLine(uc2.EvalStr("x"))
      Console.WriteLine(uc.EvalStr("x")) '// The original x in uc remains unchanged
      uc2.Release()
      
      '// Language specific - auto-releasing uCalc object
      #If False
         { '// Instances pointed to by neither uCalc1 nor uCalc2 will be released when they go out of scope
         Dim uCalc1 = new uCalc()
         Dim uCalc2 = uc.Clone()
         '// Call uCalc1.Release() and uCalc2.Release() explicitly if want to release them
         }
         
         { '// The instances that both uCalc1 and uCalc2 point to will be released when uCalc1 and uCalc2 go out of scope
         
         
         '// No need for uCalc1.Release() or uCalc2.Release(), they will automatically be released
         }
         #End If
      End Sub
   End Module
				
			
Undefined identifier
123
456
123
Customizing token definitions (e.g., treating hyphens as part of a word).

ID: 246

See: Tokens
				
					using uCalcSoftware;

var uc = new uCalc();
var t = uc.NewTransformer();
// Now capture it using the {@Alpha} category
t.FromTo("{@Alpha:w}", "<{w}>");

Console.WriteLine("Before:");
Console.WriteLine(t.Transform("1. Start-Up 'big ideas' well-knwon."));

// Define a new token pattern for hyphenated words
// We assign it to the 'AlphaNumeric' category so it behaves like a word
t.Tokens.Add("[a-zA-Z-]+", TokenType.AlphaNumeric);
Console.WriteLine("After:");
Console.WriteLine(t.Transform("1. Start-Up 'big ideas' well-knwon."));
				
			
Before:
1. <Start>-<Up> 'big ideas' <well>-<knwon>.
After:
1. <Start-Up> 'big ideas' <well-knwon>.
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto t = uc.NewTransformer();
   // Now capture it using the {@Alpha} category
   t.FromTo("{@Alpha:w}", "<{w}>");

   cout << "Before:" << endl;
   cout << t.Transform("1. Start-Up 'big ideas' well-knwon.") << endl;

   // Define a new token pattern for hyphenated words
   // We assign it to the 'AlphaNumeric' category so it behaves like a word
   t.Tokens().Add("[a-zA-Z-]+", TokenType::AlphaNumeric);
   cout << "After:" << endl;
   cout << t.Transform("1. Start-Up 'big ideas' well-knwon.") << endl;
}
				
			
Before:
1. <Start>-<Up> 'big ideas' <well>-<knwon>.
After:
1. <Start-Up> 'big ideas' <well-knwon>.
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t = uc.NewTransformer()
      '// Now capture it using the {@Alpha} category
      t.FromTo("{@Alpha:w}", "<{w}>")
      
      Console.WriteLine("Before:")
      Console.WriteLine(t.Transform("1. Start-Up 'big ideas' well-knwon."))
      
      '// Define a new token pattern for hyphenated words
      '// We assign it to the 'AlphaNumeric' category so it behaves like a word
      t.Tokens.Add("[a-zA-Z-]+", TokenType.AlphaNumeric)
      Console.WriteLine("After:")
      Console.WriteLine(t.Transform("1. Start-Up 'big ideas' well-knwon."))
   End Sub
End Module
				
			
Before:
1. <Start>-<Up> 'big ideas' <well>-<knwon>.
After:
1. <Start-Up> 'big ideas' <well-knwon>.
Data type Reset

ID: 88

See: Reset
				
					using uCalcSoftware;

var uc = new uCalc();
var MyDbl = uc.DefineVariable("MyDbl = 123.456");
var MyStr = uc.DefineVariable("MyStr = 'Hello world!'");
var MyCplx = uc.DefineVariable("MyCplx = 3 + 4 * #i");

Console.WriteLine(uc.EvalStr("MyDbl"));
Console.WriteLine(uc.EvalStr("MyStr"));
Console.WriteLine(uc.EvalStr("MyCplx"));

uc.DataTypeOf("double").Reset(MyDbl.ValueAddr());
uc.DataTypeOf("string").Reset(MyStr.ValueAddr()); // empty string ""
uc.DataTypeOf("complex").Reset(MyCplx.ValueAddr());

Console.WriteLine(uc.EvalStr("MyDbl"));
Console.WriteLine(uc.EvalStr("MyStr"));
Console.WriteLine(uc.EvalStr("MyCplx"));


				
			
123.456
Hello world!
3+4i
0

0+0i
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto MyDbl = uc.DefineVariable("MyDbl = 123.456");
   auto MyStr = uc.DefineVariable("MyStr = 'Hello world!'");
   auto MyCplx = uc.DefineVariable("MyCplx = 3 + 4 * #i");

   cout << uc.EvalStr("MyDbl") << endl;
   cout << uc.EvalStr("MyStr") << endl;
   cout << uc.EvalStr("MyCplx") << endl;

   uc.DataTypeOf("double").Reset(MyDbl.ValueAddr());
   uc.DataTypeOf("string").Reset(MyStr.ValueAddr()); // empty string ""
   uc.DataTypeOf("complex").Reset(MyCplx.ValueAddr());

   cout << uc.EvalStr("MyDbl") << endl;
   cout << uc.EvalStr("MyStr") << endl;
   cout << uc.EvalStr("MyCplx") << endl;


}
				
			
123.456
Hello world!
3+4i
0

0+0i
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim MyDbl = uc.DefineVariable("MyDbl = 123.456")
      Dim MyStr = uc.DefineVariable("MyStr = 'Hello world!'")
      Dim MyCplx = uc.DefineVariable("MyCplx = 3 + 4 * #i")
      
      Console.WriteLine(uc.EvalStr("MyDbl"))
      Console.WriteLine(uc.EvalStr("MyStr"))
      Console.WriteLine(uc.EvalStr("MyCplx"))
      
      uc.DataTypeOf("double").Reset(MyDbl.ValueAddr())
      uc.DataTypeOf("string").Reset(MyStr.ValueAddr()) '// empty string ""
      uc.DataTypeOf("complex").Reset(MyCplx.ValueAddr())
      
      Console.WriteLine(uc.EvalStr("MyDbl"))
      Console.WriteLine(uc.EvalStr("MyStr"))
      Console.WriteLine(uc.EvalStr("MyCplx"))
      
      
   End Sub
End Module
				
			
123.456
Hello world!
3+4i
0

0+0i
Defines a C-style line comment token (`//...`) and categorizes it as whitespace so it is ignored by the parser.

ID: 1006

				
					using uCalcSoftware;

var uc = new uCalc();
var t = uc.NewTransformer();
// By default, a comment would cause a syntax error.
Console.Write("Before: ");
Console.WriteLine(uc.EvalStr("10 + 5 // Add 5"));

// Add a new token definition for C-style comments.
// The regex `//.*` matches from '//' to the end of the line.
// We classify it as Whitespace so the parser skips it.
uc.ExpressionTokens.Add("//.*", TokenType.Whitespace);

Console.Write("After:  ");
Console.WriteLine(uc.EvalStr("10 + 5 // Add 5"));
				
			
Before: Undefined identifier
After:  15
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto t = uc.NewTransformer();
   // By default, a comment would cause a syntax error.
   cout << "Before: ";
   cout << uc.EvalStr("10 + 5 // Add 5") << endl;

   // Add a new token definition for C-style comments.
   // The regex `//.*` matches from '//' to the end of the line.
   // We classify it as Whitespace so the parser skips it.
   uc.ExpressionTokens().Add("//.*", TokenType::Whitespace);

   cout << "After:  ";
   cout << uc.EvalStr("10 + 5 // Add 5") << endl;
}
				
			
Before: Undefined identifier
After:  15
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t = uc.NewTransformer()
      '// By default, a comment would cause a syntax error.
      Console.Write("Before: ")
      Console.WriteLine(uc.EvalStr("10 + 5 // Add 5"))
      
      '// Add a new token definition for C-style comments.
      '// The regex `//.*` matches from '//' to the end of the line.
      '// We classify it as Whitespace so the parser skips it.
      uc.ExpressionTokens.Add("//.*", TokenType.Whitespace)
      
      Console.Write("After:  ")
      Console.WriteLine(uc.EvalStr("10 + 5 // Add 5"))
   End Sub
End Module
				
			
Before: Undefined identifier
After:  15
Defines a custom `sum_to` operator to calculate the sum of a numeric range, demonstrating a single-word operator.

ID: 1224

				
					using uCalcSoftware;

var uc = new uCalc();
// Define the variables that the operator's expression will use.
uc.DefineVariable("i");
uc.DefineVariable("total");

// Define a new 'sum_to' operator at runtime, with precedence level of 50.
// It uses the built-in ForLoop function to sum numbers into the 'total' variable.
uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50);

// Use the new operator. The result is stored in the 'total' variable.
uc.Eval("1 sum_to 5");

Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}");
				
			
The sum from 1 to 5 is: 15
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Define the variables that the operator's expression will use.
   uc.DefineVariable("i");
   uc.DefineVariable("total");

   // Define a new 'sum_to' operator at runtime, with precedence level of 50.
   // It uses the built-in ForLoop function to sum numbers into the 'total' variable.
   uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50);

   // Use the new operator. The result is stored in the 'total' variable.
   uc.Eval("1 sum_to 5");

   cout << "The sum from 1 to 5 is: " << uc.Eval("total") << endl;
}
				
			
The sum from 1 to 5 is: 15
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define the variables that the operator's expression will use.
      uc.DefineVariable("i")
      uc.DefineVariable("total")
      
      '// Define a new 'sum_to' operator at runtime, with precedence level of 50.
      '// It uses the built-in ForLoop function to sum numbers into the 'total' variable.
      uc.DefineOperator("{start} sum_to {end} = total = 0; ForLoop(i, start, end, 1, total = total + i)", 50)
      
      '// Use the new operator. The result is stored in the 'total' variable.
      uc.Eval("1 sum_to 5")
      
      Console.WriteLine($"The sum from 1 to 5 is: {uc.Eval("total")}")
   End Sub
End Module
				
			
The sum from 1 to 5 is: 15
Defines a recursive Factorial function using the IIf function for conditional logic.

ID: 299

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineFunction("Factorial(n) = IIf(n > 1, n * Factorial(n - 1), 1)");
Console.WriteLine(uc.Eval("Factorial(5)"));
				
			
120
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineFunction("Factorial(n) = IIf(n > 1, n * Factorial(n - 1), 1)");
   cout << uc.Eval("Factorial(5)") << endl;
}
				
			
120
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineFunction("Factorial(n) = IIf(n > 1, n * Factorial(n - 1), 1)")
      Console.WriteLine(uc.Eval("Factorial(5)"))
   End Sub
End Module
				
			
120
Defines a simple `Add` function that is implemented by a native callback to perform addition.

ID: 1242

				
					using uCalcSoftware;

var uc = new uCalc();

static void MyAdd(uCalc.Callback cb) {
   var x = cb.Arg(1);
   var y = cb.Arg(2);
   cb.Return(x + y);
}

// Link the uCalc function 'Add' to the native 'MyAdd' callback.
uc.DefineFunction("Add(x, y)", MyAdd);

// Now the native code can be called from an expression.
Console.WriteLine(uc.Eval("Add(10, 5)"));
				
			
15
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyAdd(uCalcBase::Callback cb) {
   auto x = cb.Arg(1);
   auto y = cb.Arg(2);
   cb.Return(x + y);
}
int main() {
   uCalc uc;
   // Link the uCalc function 'Add' to the native 'MyAdd' callback.
   uc.DefineFunction("Add(x, y)", MyAdd);

   // Now the native code can be called from an expression.
   cout << uc.Eval("Add(10, 5)") << endl;
}
				
			
15
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyAdd(ByVal cb As uCalc.Callback)
      Dim x = cb.Arg(1)
      Dim y = cb.Arg(2)
      cb.Return(x + y)
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      '// Link the uCalc function 'Add' to the native 'MyAdd' callback.
      uc.DefineFunction("Add(x, y)", AddressOf MyAdd)
      
      '// Now the native code can be called from an expression.
      Console.WriteLine(uc.Eval("Add(10, 5)"))
   End Sub
End Module
				
			
15
Defines several application-level configuration constants for use in expressions.

ID: 287

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineConstant("MAX_USERS = 100");
uc.DefineConstant("API_VERSION = 'v2.1'");
uc.DefineConstant("ENABLE_LOGGING = true");

Console.WriteLine("Configuration Settings:");
Console.WriteLine($"Max Users: {uc.Eval("MAX_USERS")}");
Console.WriteLine($"API Version: {uc.EvalStr("API_VERSION")}");
Console.WriteLine($"Logging Enabled: {uc.EvalStr("ENABLE_LOGGING")}");

// Use in a conditional expression
int currentUserCount = 99;
if (uc.EvalStr((currentUserCount).ToString() + " < MAX_USERS") == "true") {
   Console.WriteLine("System capacity is OK.");
}
				
			
Configuration Settings:
Max Users: 100
API Version: v2.1
Logging Enabled: true
System capacity is OK.
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineConstant("MAX_USERS = 100");
   uc.DefineConstant("API_VERSION = 'v2.1'");
   uc.DefineConstant("ENABLE_LOGGING = true");

   cout << "Configuration Settings:" << endl;
   cout << "Max Users: " << uc.Eval("MAX_USERS") << endl;
   cout << "API Version: " << uc.EvalStr("API_VERSION") << endl;
   cout << "Logging Enabled: " << uc.EvalStr("ENABLE_LOGGING") << endl;

   // Use in a conditional expression
   int currentUserCount = 99;
   if (uc.EvalStr(to_string(currentUserCount) + " < MAX_USERS") == "true") {
      cout << "System capacity is OK." << endl;
   }
}
				
			
Configuration Settings:
Max Users: 100
API Version: v2.1
Logging Enabled: true
System capacity is OK.
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineConstant("MAX_USERS = 100")
      uc.DefineConstant("API_VERSION = 'v2.1'")
      uc.DefineConstant("ENABLE_LOGGING = true")
      
      Console.WriteLine("Configuration Settings:")
      Console.WriteLine($"Max Users: {uc.Eval("MAX_USERS")}")
      Console.WriteLine($"API Version: {uc.EvalStr("API_VERSION")}")
      Console.WriteLine($"Logging Enabled: {uc.EvalStr("ENABLE_LOGGING")}")
      
      '// Use in a conditional expression
      Dim currentUserCount As Integer = 99
      If uc.EvalStr((currentUserCount).ToString() + " < MAX_USERS") = "true" Then
         Console.WriteLine("System capacity is OK.")
      End If
   End Sub
End Module
				
			
Configuration Settings:
Max Users: 100
API Version: v2.1
Logging Enabled: true
System capacity is OK.
Defines variables with explicit types, inferred types, and default types.

ID: 306

				
					using uCalcSoftware;

var uc = new uCalc();
// Explicit type definition
uc.DefineVariable("explicitInt As Int = 123");

// Type inferred from the initial string value
uc.DefineVariable("inferredStr = 'hello'");

// Type defaults to Double as no type or value is given
var defaultVar = uc.DefineVariable("defaultVar");

Console.WriteLine($"explicitInt type: {uc.ItemOf("explicitInt").DataType.Name}");
Console.WriteLine($"inferredStr type: {uc.ItemOf("inferredStr").DataType.Name}");
Console.WriteLine($"defaultVar type: {defaultVar.DataType.Name}");
				
			
explicitInt type: int
inferredStr type: string
defaultVar type: double
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Explicit type definition
   uc.DefineVariable("explicitInt As Int = 123");

   // Type inferred from the initial string value
   uc.DefineVariable("inferredStr = 'hello'");

   // Type defaults to Double as no type or value is given
   auto defaultVar = uc.DefineVariable("defaultVar");

   cout << "explicitInt type: " << uc.ItemOf("explicitInt").DataType().Name() << endl;
   cout << "inferredStr type: " << uc.ItemOf("inferredStr").DataType().Name() << endl;
   cout << "defaultVar type: " << defaultVar.DataType().Name() << endl;
}
				
			
explicitInt type: int
inferredStr type: string
defaultVar type: double
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Explicit type definition
      uc.DefineVariable("explicitInt As Int = 123")
      
      '// Type inferred from the initial string value
      uc.DefineVariable("inferredStr = 'hello'")
      
      '// Type defaults to Double as no type or value is given
      Dim defaultVar = uc.DefineVariable("defaultVar")
      
      Console.WriteLine($"explicitInt type: {uc.ItemOf("explicitInt").DataType.Name}")
      Console.WriteLine($"inferredStr type: {uc.ItemOf("inferredStr").DataType.Name}")
      Console.WriteLine($"defaultVar type: {defaultVar.DataType.Name}")
   End Sub
End Module
				
			
explicitInt type: int
inferredStr type: string
defaultVar type: double
DefineVariable examples

ID: 17

				
					using uCalcSoftware;

var uc = new uCalc();
var MyVar = uc.DefineVariable("MyVar");
var MyInt = uc.DefineVariable("MyInt As Int");
var MyStr = uc.DefineVariable("MyStr As String");
uc.DefineVariable("OtherStr = 'string type inferred'");
uc.DefineVariable("MyInt16 = Int16(100/3)"); // type inferred
uc.DefineVariable("MyBool = True"); // type inferred
uc.DefineVariable("MyComplex = 3 + 4*#i"); // type inferred

MyVar.Value(123);
MyInt.ValueInt32(456);
MyStr.ValueStr("This is a test");

Console.WriteLine("MyVar = " + uc.EvalStr("MyVar"));
Console.WriteLine("MyInt = " + uc.EvalStr("MyInt"));
Console.WriteLine("MyStr = " + uc.EvalStr("MyStr"));
Console.WriteLine("OtherStr = " + uc.EvalStr("OtherStr"));
Console.WriteLine("MyInt16 = " + uc.EvalStr("MyInt16"));
Console.WriteLine("MyBool = " + uc.EvalStr("MyBool"));
Console.WriteLine("MyComplex = " + uc.EvalStr("MyComplex"));
Console.WriteLine("---");
Console.WriteLine(MyVar.Value());
Console.WriteLine(MyInt.ValueInt32());
Console.WriteLine(MyStr.ValueStr());
Console.WriteLine("---");
Console.WriteLine(uc.ItemOf("MyVar").DataType.Name);
Console.WriteLine(uc.ItemOf("MyInt").DataType.Name);
Console.WriteLine(uc.ItemOf("MyStr").DataType.Name);
Console.WriteLine(uc.ItemOf("OtherStr").DataType.Name);
Console.WriteLine(uc.ItemOf("MyInt16").DataType.Name);
Console.WriteLine(uc.ItemOf("MyBool").DataType.Name);
Console.WriteLine("---");

var Expression = "x^2 * 10";
var VarX = uc.DefineVariable("x");
var ParsedExpr = uc.Parse(Expression);

Console.Write("Expression = ");
Console.WriteLine(Expression);
for (int x = 1; x <= 10; x++) {
   VarX.Value(x); // In C++ you can skip this by passing &x to DefineVariable
   Console.WriteLine("x = " + VarX.ValueStr() + "  Result = " + ParsedExpr.EvaluateStr());
}

ParsedExpr.Release();
VarX.Release();
				
			
MyVar = 123
MyInt = 456
MyStr = This is a test
OtherStr = string type inferred
MyInt16 = 33
MyBool = true
MyComplex = 3+4i
---
123
456
This is a test
---
double
int
string
string
int16
bool
---
Expression = x^2 * 10
x = 1  Result = 10
x = 2  Result = 40
x = 3  Result = 90
x = 4  Result = 160
x = 5  Result = 250
x = 6  Result = 360
x = 7  Result = 490
x = 8  Result = 640
x = 9  Result = 810
x = 10  Result = 1000
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto MyVar = uc.DefineVariable("MyVar");
   auto MyInt = uc.DefineVariable("MyInt As Int");
   auto MyStr = uc.DefineVariable("MyStr As String");
   uc.DefineVariable("OtherStr = 'string type inferred'");
   uc.DefineVariable("MyInt16 = Int16(100/3)"); // type inferred
   uc.DefineVariable("MyBool = True"); // type inferred
   uc.DefineVariable("MyComplex = 3 + 4*#i"); // type inferred

   MyVar.Value(123);
   MyInt.ValueInt32(456);
   MyStr.ValueStr("This is a test");

   cout << "MyVar = " + uc.EvalStr("MyVar") << endl;
   cout << "MyInt = " + uc.EvalStr("MyInt") << endl;
   cout << "MyStr = " + uc.EvalStr("MyStr") << endl;
   cout << "OtherStr = " + uc.EvalStr("OtherStr") << endl;
   cout << "MyInt16 = " + uc.EvalStr("MyInt16") << endl;
   cout << "MyBool = " + uc.EvalStr("MyBool") << endl;
   cout << "MyComplex = " + uc.EvalStr("MyComplex") << endl;
   cout << "---" << endl;
   cout << MyVar.Value() << endl;
   cout << MyInt.ValueInt32() << endl;
   cout << MyStr.ValueStr() << endl;
   cout << "---" << endl;
   cout << uc.ItemOf("MyVar").DataType().Name() << endl;
   cout << uc.ItemOf("MyInt").DataType().Name() << endl;
   cout << uc.ItemOf("MyStr").DataType().Name() << endl;
   cout << uc.ItemOf("OtherStr").DataType().Name() << endl;
   cout << uc.ItemOf("MyInt16").DataType().Name() << endl;
   cout << uc.ItemOf("MyBool").DataType().Name() << endl;
   cout << "---" << endl;

   auto Expression = "x^2 * 10";
   auto VarX = uc.DefineVariable("x");
   auto ParsedExpr = uc.Parse(Expression);

   cout << "Expression = ";
   cout << Expression << endl;
   for (int x = 1; x <= 10; x++) {
      VarX.Value(x); // In C++ you can skip this by passing &x to DefineVariable
      cout << "x = " + VarX.ValueStr() + "  Result = " + ParsedExpr.EvaluateStr() << endl;
   }

   ParsedExpr.Release();
   VarX.Release();
}
				
			
MyVar = 123
MyInt = 456
MyStr = This is a test
OtherStr = string type inferred
MyInt16 = 33
MyBool = true
MyComplex = 3+4i
---
123
456
This is a test
---
double
int
string
string
int16
bool
---
Expression = x^2 * 10
x = 1  Result = 10
x = 2  Result = 40
x = 3  Result = 90
x = 4  Result = 160
x = 5  Result = 250
x = 6  Result = 360
x = 7  Result = 490
x = 8  Result = 640
x = 9  Result = 810
x = 10  Result = 1000
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim MyVar = uc.DefineVariable("MyVar")
      Dim MyInt = uc.DefineVariable("MyInt As Int")
      Dim MyStr = uc.DefineVariable("MyStr As String")
      uc.DefineVariable("OtherStr = 'string type inferred'")
      uc.DefineVariable("MyInt16 = Int16(100/3)") '// type inferred
      uc.DefineVariable("MyBool = True") '// type inferred
      uc.DefineVariable("MyComplex = 3 + 4*#i") '// type inferred
      
      MyVar.Value(123)
      MyInt.ValueInt32(456)
      MyStr.ValueStr("This is a test")
      
      Console.WriteLine("MyVar = " + uc.EvalStr("MyVar"))
      Console.WriteLine("MyInt = " + uc.EvalStr("MyInt"))
      Console.WriteLine("MyStr = " + uc.EvalStr("MyStr"))
      Console.WriteLine("OtherStr = " + uc.EvalStr("OtherStr"))
      Console.WriteLine("MyInt16 = " + uc.EvalStr("MyInt16"))
      Console.WriteLine("MyBool = " + uc.EvalStr("MyBool"))
      Console.WriteLine("MyComplex = " + uc.EvalStr("MyComplex"))
      Console.WriteLine("---")
      Console.WriteLine(MyVar.Value())
      Console.WriteLine(MyInt.ValueInt32())
      Console.WriteLine(MyStr.ValueStr())
      Console.WriteLine("---")
      Console.WriteLine(uc.ItemOf("MyVar").DataType.Name)
      Console.WriteLine(uc.ItemOf("MyInt").DataType.Name)
      Console.WriteLine(uc.ItemOf("MyStr").DataType.Name)
      Console.WriteLine(uc.ItemOf("OtherStr").DataType.Name)
      Console.WriteLine(uc.ItemOf("MyInt16").DataType.Name)
      Console.WriteLine(uc.ItemOf("MyBool").DataType.Name)
      Console.WriteLine("---")
      
      Dim Expression = "x^2 * 10"
      Dim VarX = uc.DefineVariable("x")
      Dim ParsedExpr = uc.Parse(Expression)
      
      Console.Write("Expression = ")
      Console.WriteLine(Expression)
      For x  As Integer = 1 To 10
         VarX.Value(x) '// In C++ you can skip this by passing &x to DefineVariable
         Console.WriteLine("x = " + VarX.ValueStr() + "  Result = " + ParsedExpr.EvaluateStr())
      Next
      
      ParsedExpr.Release()
      VarX.Release()
   End Sub
End Module
				
			
MyVar = 123
MyInt = 456
MyStr = This is a test
OtherStr = string type inferred
MyInt16 = 33
MyBool = true
MyComplex = 3+4i
---
123
456
This is a test
---
double
int
string
string
int16
bool
---
Expression = x^2 * 10
x = 1  Result = 10
x = 2  Result = 40
x = 3  Result = 90
x = 4  Result = 160
x = 5  Result = 250
x = 6  Result = 360
x = 7  Result = 490
x = 8  Result = 640
x = 9  Result = 810
x = 10  Result = 1000
DefineVariable; using pointers

ID: 18

				
					using uCalcSoftware;

var uc = new uCalc();
var Int8Var = uc.DefineVariable("x As Int8 = -1");
var Int16Var = uc.DefineVariable("y As Int16 = -1");
var StrVar = uc.DefineVariable("MyStr = 'Hello there'");
Console.WriteLine(uc.EvalStr("x"));
Console.WriteLine(uc.EvalStr("y"));
Console.WriteLine(uc.EvalStr("MyStr"));

var xPtr = uc.DefineVariable("xPtr As Pointer"); // General pointer
var yPtr = uc.DefineVariable("yPtr As Int16u Ptr"); // pointer specific to unsigned Int16
var yPtrB = uc.DefineVariable("yPtrB As Int16 Ptr = AddressOf(y)"); // Using AddressOf
var StrPtr = uc.DefineVariable("StrPtr As String Ptr");
xPtr.ValuePtr(Int8Var.ValueAddr()); // Sets the pointer address
yPtr.ValuePtr(Int16Var.ValueAddr()); // Note: address of signed Int16 going to an unsigned Ptr
StrPtr.ValuePtr(StrVar.ValueAddr());

// Note: for the ints we are now returning unsigned values; so -1 turns into positive numbers
Console.WriteLine(uc.EvalStr("ValueAt(Int8u, xPtr)")); // Type required because it's defined as generar pointer
Console.WriteLine(uc.EvalStr("ValueAt(yPtr)")); // Type name not needed because it's defined as Int16u Ptr
Console.WriteLine(uc.EvalStr("ValueAt(yPtrB)"));
Console.WriteLine(uc.EvalStr("ValueAt(StrPtr)"));

// Iterate through uc.ItemOf(ItemIs.DataType, n).Name()
// to see data type names you can use with ValueAt

var OtherInt = uc.DefineVariable("OtherInt As Int16 = 1234");
uc.DataTypeOf(BuiltInType.Integer_16).SetScalar(Int16Var.ValueAddr(), OtherInt.ValueAddr());

Console.WriteLine(uc.EvalStr("OtherInt"));
Console.WriteLine(uc.EvalStr("ValueAt(yPtrB)"));





				
			
-1
-1
Hello there
255
65535
-1
Hello there
1234
1234
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto Int8Var = uc.DefineVariable("x As Int8 = -1");
   auto Int16Var = uc.DefineVariable("y As Int16 = -1");
   auto StrVar = uc.DefineVariable("MyStr = 'Hello there'");
   cout << uc.EvalStr("x") << endl;
   cout << uc.EvalStr("y") << endl;
   cout << uc.EvalStr("MyStr") << endl;

   auto xPtr = uc.DefineVariable("xPtr As Pointer"); // General pointer
   auto yPtr = uc.DefineVariable("yPtr As Int16u Ptr"); // pointer specific to unsigned Int16
   auto yPtrB = uc.DefineVariable("yPtrB As Int16 Ptr = AddressOf(y)"); // Using AddressOf
   auto StrPtr = uc.DefineVariable("StrPtr As String Ptr");
   xPtr.ValuePtr(Int8Var.ValueAddr()); // Sets the pointer address
   yPtr.ValuePtr(Int16Var.ValueAddr()); // Note: address of signed Int16 going to an unsigned Ptr
   StrPtr.ValuePtr(StrVar.ValueAddr());

   // Note: for the ints we are now returning unsigned values; so -1 turns into positive numbers
   cout << uc.EvalStr("ValueAt(Int8u, xPtr)") << endl; // Type required because it's defined as generar pointer
   cout << uc.EvalStr("ValueAt(yPtr)") << endl; // Type name not needed because it's defined as Int16u Ptr
   cout << uc.EvalStr("ValueAt(yPtrB)") << endl;
   cout << uc.EvalStr("ValueAt(StrPtr)") << endl;

   // Iterate through uc.ItemOf(ItemIs.DataType, n).Name()
   // to see data type names you can use with ValueAt

   auto OtherInt = uc.DefineVariable("OtherInt As Int16 = 1234");
   uc.DataTypeOf(BuiltInType::Integer_16).SetScalar(Int16Var.ValueAddr(), OtherInt.ValueAddr());

   cout << uc.EvalStr("OtherInt") << endl;
   cout << uc.EvalStr("ValueAt(yPtrB)") << endl;





}
				
			
-1
-1
Hello there
255
65535
-1
Hello there
1234
1234
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim Int8Var = uc.DefineVariable("x As Int8 = -1")
      Dim Int16Var = uc.DefineVariable("y As Int16 = -1")
      Dim StrVar = uc.DefineVariable("MyStr = 'Hello there'")
      Console.WriteLine(uc.EvalStr("x"))
      Console.WriteLine(uc.EvalStr("y"))
      Console.WriteLine(uc.EvalStr("MyStr"))
      
      Dim xPtr = uc.DefineVariable("xPtr As Pointer") '// General pointer
      Dim yPtr = uc.DefineVariable("yPtr As Int16u Ptr") '// pointer specific to unsigned Int16
      Dim yPtrB = uc.DefineVariable("yPtrB As Int16 Ptr = AddressOf(y)") '// Using AddressOf
      Dim StrPtr = uc.DefineVariable("StrPtr As String Ptr")
      xPtr.ValuePtr(Int8Var.ValueAddr()) '// Sets the pointer address
      yPtr.ValuePtr(Int16Var.ValueAddr()) '// Note: address of signed Int16 going to an unsigned Ptr
      StrPtr.ValuePtr(StrVar.ValueAddr())
      
      '// Note: for the ints we are now returning unsigned values; so -1 turns into positive numbers
      Console.WriteLine(uc.EvalStr("ValueAt(Int8u, xPtr)")) '// Type required because it's defined as generar pointer
      Console.WriteLine(uc.EvalStr("ValueAt(yPtr)")) '// Type name not needed because it's defined as Int16u Ptr
      Console.WriteLine(uc.EvalStr("ValueAt(yPtrB)"))
      Console.WriteLine(uc.EvalStr("ValueAt(StrPtr)"))
      
      '// Iterate through uc.ItemOf(ItemIs.DataType, n).Name()
      '// to see data type names you can use with ValueAt
      
      Dim OtherInt = uc.DefineVariable("OtherInt As Int16 = 1234")
      uc.DataTypeOf(BuiltInType.Integer_16).SetScalar(Int16Var.ValueAddr(), OtherInt.ValueAddr())
      
      Console.WriteLine(uc.EvalStr("OtherInt"))
      Console.WriteLine(uc.EvalStr("ValueAt(yPtrB)"))
      
      
      
      
      
   End Sub
End Module
				
			
-1
-1
Hello there
255
65535
-1
Hello there
1234
1234
Defining a callback function with a variable number of arguments

ID: 12

				
					using uCalcSoftware;

var uc = new uCalc();

static void MyAverage(uCalc.Callback cb) {
   double Total = 0;
   for (int x = 1; x <= cb.ArgCount(); x++) {
      Total = Total + cb.Arg(x);
   }
   cb.Return(Total / cb.ArgCount());
}

uc.DefineFunction("Average(x ...)", MyAverage);
Console.WriteLine(uc.Eval("Average(10, 3, 7, 4)"));
				
			
6
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyAverage(uCalcBase::Callback cb) {
   double Total = 0;
   for (int x = 1; x <= cb.ArgCount(); x++) {
      Total = Total + cb.Arg(x);
   }
   cb.Return(Total / cb.ArgCount());
}
int main() {
   uCalc uc;
   uc.DefineFunction("Average(x ...)", MyAverage);
   cout << uc.Eval("Average(10, 3, 7, 4)") << endl;
}
				
			
6
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyAverage(ByVal cb As uCalc.Callback)
      Dim Total As Double = 0
      For x  As Integer = 1 To cb.ArgCount()
         Total = Total + cb.Arg(x)
      Next
      cb.Return(Total / cb.ArgCount())
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineFunction("Average(x ...)", AddressOf MyAverage)
      Console.WriteLine(uc.Eval("Average(10, 3, 7, 4)"))
   End Sub
End Module
				
			
6
Defining a constant

ID: 8

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineVariable("MyVar = 10");
uc.DefineConstant("MyPi = 3.14");

Console.WriteLine(uc.Eval("MyVar"));
Console.WriteLine(uc.Eval("MyPi"));

uc.EvalStr("MyVar = 20"); // Attempt to change MyVar
Console.WriteLine(uc.Error.Message);

uc.EvalStr("MyPi = 25"); // Attempt to change MyPi
Console.WriteLine(uc.Error.Message);

Console.WriteLine(uc.EvalStr("MyVar"));
Console.WriteLine(uc.EvalStr("MyPi"));
				
			
10
3.14
No error
Value cannot be assigned here
20
3.14
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineVariable("MyVar = 10");
   uc.DefineConstant("MyPi = 3.14");

   cout << uc.Eval("MyVar") << endl;
   cout << uc.Eval("MyPi") << endl;

   uc.EvalStr("MyVar = 20"); // Attempt to change MyVar
   cout << uc.Error().Message() << endl;

   uc.EvalStr("MyPi = 25"); // Attempt to change MyPi
   cout << uc.Error().Message() << endl;

   cout << uc.EvalStr("MyVar") << endl;
   cout << uc.EvalStr("MyPi") << endl;
}
				
			
10
3.14
No error
Value cannot be assigned here
20
3.14
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("MyVar = 10")
      uc.DefineConstant("MyPi = 3.14")
      
      Console.WriteLine(uc.Eval("MyVar"))
      Console.WriteLine(uc.Eval("MyPi"))
      
      uc.EvalStr("MyVar = 20") '// Attempt to change MyVar
      Console.WriteLine(uc.Error.Message)
      
      uc.EvalStr("MyPi = 25") '// Attempt to change MyPi
      Console.WriteLine(uc.Error.Message)
      
      Console.WriteLine(uc.EvalStr("MyVar"))
      Console.WriteLine(uc.EvalStr("MyPi"))
   End Sub
End Module
				
			
10
3.14
No error
Value cannot be assigned here
20
3.14
Defining a new custom operator with a precedence level set relative to an existing operator.

ID: 656

				
					using uCalcSoftware;

var uc = new uCalc();
// Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
var mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence;

// Set the new operator's precedence to be higher than multiplication.
uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10);

// The new operator should be evaluated before multiplication and addition.
// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3"));
				
			
26
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
   auto mul_precedence = uc.ItemOf("*", uCalc::Properties(ItemIs::Infix)).Precedence();

   // Set the new operator's precedence to be higher than multiplication.
   uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10);

   // The new operator should be evaluated before multiplication and addition.
   // The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
   cout << uc.Eval("2 + 3 * 2 ** 3") << endl;
}
				
			
26
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Goal: Define a new power operator '**' with higher precedence than multiplication '*'.
      Dim mul_precedence = uc.ItemOf("*", uCalc.Properties(ItemIs.Infix)).Precedence
      
      '// Set the new operator's precedence to be higher than multiplication.
      uc.DefineOperator("{base} ** {exp} = Pow(base, exp)", mul_precedence + 10)
      
      '// The new operator should be evaluated before multiplication and addition.
      '// The expression is equivalent to: 2 + (3 * (2 ** 3)) -> 2 + (3 * 8) -> 2 + 24 -> 26
      Console.WriteLine(uc.Eval("2 + 3 * 2 ** 3"))
   End Sub
End Module
				
			
26
Defining a simple variable and function.

ID: 293

See: Define
				
					using uCalcSoftware;

var uc = new uCalc();
// Define a variable and a function using the core Define method
uc.Define("Variable: my_var = 100");
uc.Define("Function: square(x) = x * x");

Console.WriteLine(uc.Eval("my_var * square(5)"));
				
			
2500
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Define a variable and a function using the core Define method
   uc.Define("Variable: my_var = 100");
   uc.Define("Function: square(x) = x * x");

   cout << uc.Eval("my_var * square(5)") << endl;
}
				
			
2500
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define a variable and a function using the core Define method
      uc.Define("Variable: my_var = 100")
      uc.Define("Function: square(x) = x * x")
      
      Console.WriteLine(uc.Eval("my_var * square(5)"))
   End Sub
End Module
				
			
2500
Defining a token bracket pair

ID: 165

				
					using uCalcSoftware;

var uc = new uCalc();

// Here we define < and > as a bracket pair.
// Such pairs can be part of a pattern match
// and a match can be found within a bracket pair
// but a match will not cross boundaries with one
// part of the match out and another part inside

var t = uc.NewTransformer();
var txt = "a < b c > c, < a > b c, < a b c >";

t.Tokens.Add("<", TokenType.Generic, ">");
t.FromTo("a {etc} c", "((a {etc} c))");

Console.WriteLine(txt);
Console.WriteLine(t.Transform(txt));
				
			
a < b c > c, < a > b c, < a b c >
((a < b c > c)), < a > b c, < ((a b c)) >
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;

   // Here we define < and > as a bracket pair.
   // Such pairs can be part of a pattern match
   // and a match can be found within a bracket pair
   // but a match will not cross boundaries with one
   // part of the match out and another part inside

   auto t = uc.NewTransformer();
   auto txt = "a < b c > c, < a > b c, < a b c >";

   t.Tokens().Add("<", TokenType::Generic, ">");
   t.FromTo("a {etc} c", "((a {etc} c))");

   cout << txt << endl;
   cout << t.Transform(txt) << endl;
}
				
			
a < b c > c, < a > b c, < a b c >
((a < b c > c)), < a > b c, < ((a b c)) >
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      
      '// Here we define < and > as a bracket pair.
      '// Such pairs can be part of a pattern match
      '// and a match can be found within a bracket pair
      '// but a match will not cross boundaries with one
      '// part of the match out and another part inside
      
      Dim t = uc.NewTransformer()
      Dim txt = "a < b c > c, < a > b c, < a b c >"
      
      t.Tokens.Add("<", TokenType.Generic, ">")
      t.FromTo("a {etc} c", "((a {etc} c))")
      
      Console.WriteLine(txt)
      Console.WriteLine(t.Transform(txt))
   End Sub
End Module
				
			
a < b c > c, < a > b c, < a b c >
((a < b c > c)), < a > b c, < ((a b c)) >
Defining another function using the same callback address of existing one

ID: 5

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineFunction("MyRound(x)", uc.ItemOf("Floor").FunctionAddress);
Console.WriteLine(uc.Eval("MyRound(2.5)"));

uc.ItemOf("MyRound").SetFunctionAddress(uc.ItemOf("Ceil").FunctionAddress);
Console.WriteLine(uc.Eval("MyRound(2.5)"));
				
			
2
3
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineFunction("MyRound(x)", uc.ItemOf("Floor").FunctionAddress());
   cout << uc.Eval("MyRound(2.5)") << endl;

   uc.ItemOf("MyRound").SetFunctionAddress(uc.ItemOf("Ceil").FunctionAddress());
   cout << uc.Eval("MyRound(2.5)") << endl;
}
				
			
2
3
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineFunction("MyRound(x)", uc.ItemOf("Floor").FunctionAddress)
      Console.WriteLine(uc.Eval("MyRound(2.5)"))
      
      uc.ItemOf("MyRound").SetFunctionAddress(uc.ItemOf("Ceil").FunctionAddress)
      Console.WriteLine(uc.Eval("MyRound(2.5)"))
   End Sub
End Module
				
			
2
3
Defining one static and one dynamic route, then matching a URL against each.

ID: 1435

				
					using uCalcSoftware;

var uc = new uCalc();
using (var router = new uCalc.Transformer()) {
   // Define routes
   router.FromTo("/home", "Handler: HomePage");
   router.FromTo("/users/{id}", "Handler: UserProfile, id: {id}");

   // Test routes
   Console.WriteLine($"Matching '/home': {router.Transform("/home")}");
   Console.WriteLine($"Matching '/users/42': {router.Transform("/users/42")}");
}
				
			
Matching '/home': Handler: HomePage
Matching '/users/42': Handler: UserProfile, id: 42
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   {
      uCalc::Transformer router;
      router.Owned(); // Causes router to be released when it goes out of scope
      // Define routes
      router.FromTo("/home", "Handler: HomePage");
      router.FromTo("/users/{id}", "Handler: UserProfile, id: {id}");

      // Test routes
      cout << "Matching '/home': " << router.Transform("/home") << endl;
      cout << "Matching '/users/42': " << router.Transform("/users/42") << endl;
   }
}
				
			
Matching '/home': Handler: HomePage
Matching '/users/42': Handler: UserProfile, id: 42
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Using router As New uCalc.Transformer()
         '// Define routes
         router.FromTo("/home", "Handler: HomePage")
         router.FromTo("/users/{id}", "Handler: UserProfile, id: {id}")
         
         '// Test routes
         Console.WriteLine($"Matching '/home': {router.Transform("/home")}")
         Console.WriteLine($"Matching '/users/42': {router.Transform("/users/42")}")
      End Using
   End Sub
End Module
				
			
Matching '/home': Handler: HomePage
Matching '/users/42': Handler: UserProfile, id: 42
Defining quoted text

ID: 166

				
					using uCalcSoftware;

var uc = new uCalc();

// In example we'll define quoted text using < and > as
// surrounding quotes.  Singe and double quotes ' and "
// are already defined by default.  This is just an example.

// Quoted text must be defined as TokenType.Literal
// The last argument, 1, means that the literal part of the match will
// be the part of the regex found int the first parenthesis (here's
// there's only one set of parenthesis).

var t = uc.NewTransformer();
var SpecialQuotes = t.Tokens.Add("<([^>]*)>", TokenType.Literal, "", 1);
SpecialQuotes.SetDataType(BuiltInType.String);
SpecialQuotes.IsProperty(ItemIs.QuotedText, true);

t.Pattern("{token:1}");
Console.WriteLine(t.Filter("abc <some quoted text> xyz 123.456 + 25e2").Matches.Text);
Console.WriteLine("");

// Based on the definition, the part within < and > is the literal part
// passed to the string + operator used by EvalStr

uc.ExpressionTokens.Add(SpecialQuotes);
Console.WriteLine(uc.EvalStr("<some quoted text> + < plus more>"));
				
			
abc
<some quoted text>
xyz
123.456
+
25e2

some quoted text plus more
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;

   // In example we'll define quoted text using < and > as
   // surrounding quotes.  Singe and double quotes ' and "
   // are already defined by default.  This is just an example.

   // Quoted text must be defined as TokenType.Literal
   // The last argument, 1, means that the literal part of the match will
   // be the part of the regex found int the first parenthesis (here's
   // there's only one set of parenthesis).

   auto t = uc.NewTransformer();
   auto SpecialQuotes = t.Tokens().Add("<([^>]*)>", TokenType::Literal, "", 1);
   SpecialQuotes.SetDataType(BuiltInType::String);
   SpecialQuotes.IsProperty(ItemIs::QuotedText, true);

   t.Pattern("{token:1}");
   cout << t.Filter("abc <some quoted text> xyz 123.456 + 25e2").Matches().Text() << endl;
   cout << "" << endl;

   // Based on the definition, the part within < and > is the literal part
   // passed to the string + operator used by EvalStr

   uc.ExpressionTokens().Add(SpecialQuotes);
   cout << uc.EvalStr("<some quoted text> + < plus more>") << endl;
}
				
			
abc
<some quoted text>
xyz
123.456
+
25e2

some quoted text plus more
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      
      '// In example we'll define quoted text using < and > as
      '// surrounding quotes.  Singe and double quotes ' and " 
      '// are already defined by default.  This is just an example.
      
      '// Quoted text must be defined as TokenType.Literal
      '// The last argument, 1, means that the literal part of the match will
      '// be the part of the regex found int the first parenthesis (here's
      '// there's only one set of parenthesis).
      
      Dim t = uc.NewTransformer()
      Dim SpecialQuotes = t.Tokens.Add("<([^>]*)>", TokenType.Literal, "", 1)
      SpecialQuotes.SetDataType(BuiltInType.String)
      SpecialQuotes.IsProperty(ItemIs.QuotedText, true)
      
      t.Pattern("{token:1}")
      Console.WriteLine(t.Filter("abc <some quoted text> xyz 123.456 + 25e2").Matches.Text)
      Console.WriteLine("")
      
      '// Based on the definition, the part within < and > is the literal part
      '// passed to the string + operator used by EvalStr
      
      uc.ExpressionTokens.Add(SpecialQuotes)
      Console.WriteLine(uc.EvalStr("<some quoted text> + < plus more>"))
   End Sub
End Module
				
			
abc
<some quoted text>
xyz
123.456
+
25e2

some quoted text plus more
Defining uCalc Strings and Expressions in the default uCalc object space

ID: 79

				
					using uCalcSoftware;

var uc = new uCalc();
var ucB = new uCalc();

uc.DefineVariable("x = 111");
ucB.DefineVariable("x = 222");

Console.WriteLine("--- using 'uc' as default ---");
uc.IsDefault = true;

uCalc.String MyString = "The variable value is: x";
Console.WriteLine(MyString.Replace("x", "{@Eval: x}"));

uCalc.Expression MyExpression = "x * 1000";
Console.WriteLine(MyExpression.Evaluate());

var MyTransformer = new uCalc.Transformer();
MyTransformer.Text = "Value is: x";
MyTransformer.FromTo("x", "{@Eval: x}");
Console.WriteLine(MyTransformer.Transform());


Console.WriteLine("--- using 'ucB' as default ---");
ucB.IsDefault = true;

uCalc.String MyStringB = "The variable value is: x";
Console.WriteLine(MyStringB.Replace("x", "{@Eval: x}"));

uCalc.Expression MyExpressionB = "x * 1000";
Console.WriteLine(MyExpressionB.Evaluate());

var MyTransformerB = new uCalc.Transformer();
MyTransformerB.Str("Value is: x");
MyTransformerB.FromTo("x", "{@Eval: x}");
Console.WriteLine(MyTransformerB.Transform());

				
			
--- using 'uc' as default ---
The variable value is: 111
111000
Value is: 111
--- using 'ucB' as default ---
The variable value is: 222
222000
Value is: 222
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uCalc ucB;

   uc.DefineVariable("x = 111");
   ucB.DefineVariable("x = 222");

   cout << "--- using 'uc' as default ---" << endl;
   uc.IsDefault(true);

   uCalc::String MyString = "The variable value is: x";
   cout << MyString.Replace("x", "{@Eval: x}") << endl;

   uCalc::Expression MyExpression = "x * 1000";
   cout << MyExpression.Evaluate() << endl;

   uCalc::Transformer MyTransformer;
   MyTransformer.Text("Value is: x");
   MyTransformer.FromTo("x", "{@Eval: x}");
   cout << MyTransformer.Transform() << endl;


   cout << "--- using 'ucB' as default ---" << endl;
   ucB.IsDefault(true);

   uCalc::String MyStringB = "The variable value is: x";
   cout << MyStringB.Replace("x", "{@Eval: x}") << endl;

   uCalc::Expression MyExpressionB = "x * 1000";
   cout << MyExpressionB.Evaluate() << endl;

   uCalc::Transformer MyTransformerB;
   MyTransformerB.Str("Value is: x");
   MyTransformerB.FromTo("x", "{@Eval: x}");
   cout << MyTransformerB.Transform() << endl;

}
				
			
--- using 'uc' as default ---
The variable value is: 111
111000
Value is: 111
--- using 'ucB' as default ---
The variable value is: 222
222000
Value is: 222
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim ucB As New uCalc()
      
      uc.DefineVariable("x = 111")
      ucB.DefineVariable("x = 222")
      
      Console.WriteLine("--- using 'uc' as default ---")
      uc.IsDefault = true
      
      Dim MyString As uCalc.String = "The variable value is: x"
      Console.WriteLine(MyString.Replace("x", "{@Eval: x}"))
      
      Dim MyExpression As uCalc.Expression = "x * 1000"
      Console.WriteLine(MyExpression.Evaluate())
      
      Dim MyTransformer As New uCalc.Transformer()
      MyTransformer.Text = "Value is: x"
      MyTransformer.FromTo("x", "{@Eval: x}")
      Console.WriteLine(MyTransformer.Transform())
      
      
      Console.WriteLine("--- using 'ucB' as default ---")
      ucB.IsDefault = true
      
      Dim MyStringB As uCalc.String = "The variable value is: x"
      Console.WriteLine(MyStringB.Replace("x", "{@Eval: x}"))
      
      Dim MyExpressionB As uCalc.Expression = "x * 1000"
      Console.WriteLine(MyExpressionB.Evaluate())
      
      Dim MyTransformerB As New uCalc.Transformer()
      MyTransformerB.Str("Value is: x")
      MyTransformerB.FromTo("x", "{@Eval: x}")
      Console.WriteLine(MyTransformerB.Transform())
      
   End Sub
End Module
				
			
--- using 'uc' as default ---
The variable value is: 111
111000
Value is: 111
--- using 'ucB' as default ---
The variable value is: 222
222000
Value is: 222
Demonstrates `ByExpr` to create a custom `Assert` function where the error message is only evaluated if the assertion fails, improving performance.

ID: 1249

				
					using uCalcSoftware;

var uc = new uCalc();

static void Assert(uCalc.Callback cb) {
   var condition = cb.ArgBool(1);

   // If the condition is false, then we evaluate the message expression
   if (condition == false) {
      var errorMessage = cb.ArgExpr(2);
      Console.WriteLine($"Assertion failed: {errorMessage.EvaluateStr()}");
   }
}

uc.DefineVariable("x = 50");

// The message is passed as an unevaluated expression
uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", Assert);

// This will do nothing because the condition is true
uc.Eval("Assert(10 < 20, 'This will not be evaluated')");

// This will trigger the assertion and evaluate the message expression
uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')");
				
			
Assertion failed: x (50) is not greater than 100
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call Assert(uCalcBase::Callback cb) {
   auto condition = cb.ArgBool(1);

   // If the condition is false, then we evaluate the message expression
   if (condition == false) {
      auto errorMessage = cb.ArgExpr(2);
      cout << "Assertion failed: " << errorMessage.EvaluateStr() << endl;
   }
}
int main() {
   uCalc uc;
   uc.DefineVariable("x = 50");

   // The message is passed as an unevaluated expression
   uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", Assert);

   // This will do nothing because the condition is true
   uc.Eval("Assert(10 < 20, 'This will not be evaluated')");

   // This will trigger the assertion and evaluate the message expression
   uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')");
}
				
			
Assertion failed: x (50) is not greater than 100
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub Assert(ByVal cb As uCalc.Callback)
      Dim condition = cb.ArgBool(1)
      
      '// If the condition is false, then we evaluate the message expression
      If condition = false Then
         Dim errorMessage = cb.ArgExpr(2)
         Console.WriteLine($"Assertion failed: {errorMessage.EvaluateStr()}")
      End If
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("x = 50")
      
      '// The message is passed as an unevaluated expression
      uc.DefineFunction("Assert(condition As Bool, ByExpr message As String)", AddressOf Assert)
      
      '// This will do nothing because the condition is true
      uc.Eval("Assert(10 < 20, 'This will not be evaluated')")
      
      '// This will trigger the assertion and evaluate the message expression
      uc.Eval("Assert(x > 100, 'x (' + Str(x) + ') is not greater than 100')")
   End Sub
End Module
				
			
Assertion failed: x (50) is not greater than 100
Demonstrates a practical callback that retrieves a 'host application setting', simulating I/O or access to native configuration.

ID: 1243

				
					using uCalcSoftware;

var uc = new uCalc();

static void GetSetting(uCalc.Callback cb) {
   // Get the name of the setting to retrieve.
   var settingName = cb.ArgStr(1);

   // In a real app, this would read from a config file, database, or registry.
   // We simulate it by evaluating another variable in the parent uCalc context.
   var value = cb.uCalc.EvalStr(settingName);

   cb.ReturnStr(value);
}

// Simulate a host application's configuration store using uCalc variables.
uc.DefineVariable("AppName = 'uCalc Demo'");
uc.DefineVariable("Version = '1.2.3'");

// Define the function that provides a bridge to the 'host'.
uc.DefineFunction("GetHostSetting(name As String) As String", GetSetting);

// Use the custom function to build a string from the host settings.
Console.WriteLine(uc.EvalStr("GetHostSetting('AppName') + ' v' + GetHostSetting('Version')"));
				
			
uCalc Demo v1.2.3
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call GetSetting(uCalcBase::Callback cb) {
   // Get the name of the setting to retrieve.
   auto settingName = cb.ArgStr(1);

   // In a real app, this would read from a config file, database, or registry.
   // We simulate it by evaluating another variable in the parent uCalc context.
   auto value = cb.uCalc().EvalStr(settingName);

   cb.ReturnStr(value);
}
int main() {
   uCalc uc;
   // Simulate a host application's configuration store using uCalc variables.
   uc.DefineVariable("AppName = 'uCalc Demo'");
   uc.DefineVariable("Version = '1.2.3'");

   // Define the function that provides a bridge to the 'host'.
   uc.DefineFunction("GetHostSetting(name As String) As String", GetSetting);

   // Use the custom function to build a string from the host settings.
   cout << uc.EvalStr("GetHostSetting('AppName') + ' v' + GetHostSetting('Version')") << endl;
}
				
			
uCalc Demo v1.2.3
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub GetSetting(ByVal cb As uCalc.Callback)
      '// Get the name of the setting to retrieve.
      Dim settingName = cb.ArgStr(1)
      
      '// In a real app, this would read from a config file, database, or registry.
      '// We simulate it by evaluating another variable in the parent uCalc context.
      Dim value = cb.uCalc.EvalStr(settingName)
      
      cb.ReturnStr(value)
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      '// Simulate a host application's configuration store using uCalc variables.
      uc.DefineVariable("AppName = 'uCalc Demo'")
      uc.DefineVariable("Version = '1.2.3'")
      
      '// Define the function that provides a bridge to the 'host'.
      uc.DefineFunction("GetHostSetting(name As String) As String", AddressOf GetSetting)
      
      '// Use the custom function to build a string from the host settings.
      Console.WriteLine(uc.EvalStr("GetHostSetting('AppName') + ' v' + GetHostSetting('Version')"))
   End Sub
End Module
				
			
uCalc Demo v1.2.3
Demonstrates automatic resource management in C++ using RAII and the Owned() method.

ID: 812

See: C++
				
					using uCalcSoftware;

var uc = new uCalc();


// This example is meant only for C++
Console.Write("Evaluating in scope: 20");

				
			
Evaluating in scope: 20
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;

   // Create a uCalc object on the stack.
   uCalc myCalc;
   // Flag it as 'owned' to enable automatic cleanup.
   myCalc.Owned();

   myCalc.DefineVariable("x=10");
   cout << "Evaluating in scope: " << myCalc.Eval("x*2");

   // When 'myCalc' goes out of scope at the end of the block,
   // its destructor is called, which automatically calls Release().


}
				
			
Evaluating in scope: 20
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      
      
      '// This example is meant only for C++
      Console.Write("Evaluating in scope: 20")
      
   End Sub
End Module
				
			
Evaluating in scope: 20
Demonstrates basic, one-line evaluations for numeric, string, and boolean expressions.

ID: 337

See: EvalStr
				
					using uCalcSoftware;

var uc = new uCalc();
// Basic arithmetic
Console.WriteLine(uc.EvalStr("15 * (4 + 3)"));
// String manipulation
Console.WriteLine(uc.EvalStr("UCase('hello') + ', world!'"));
// Boolean logic
Console.WriteLine(uc.EvalStr("10 > 5 AndAlso 'a' < 'b'"));
				
			
105
HELLO, world!
true
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Basic arithmetic
   cout << uc.EvalStr("15 * (4 + 3)") << endl;
   // String manipulation
   cout << uc.EvalStr("UCase('hello') + ', world!'") << endl;
   // Boolean logic
   cout << uc.EvalStr("10 > 5 AndAlso 'a' < 'b'") << endl;
}
				
			
105
HELLO, world!
true
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Basic arithmetic
      Console.WriteLine(uc.EvalStr("15 * (4 + 3)"))
      '// String manipulation
      Console.WriteLine(uc.EvalStr("UCase('hello') + ', world!'"))
      '// Boolean logic
      Console.WriteLine(uc.EvalStr("10 > 5 AndAlso 'a' < 'b'"))
   End Sub
End Module
				
			
105
HELLO, world!
true
Demonstrates context isolation by retrieving the parent uCalc instance from a Matches object and evaluating an expression that only exists in that parent's context.

ID: 860

				
					using uCalcSoftware;

var uc = new uCalc();
var uc1 = new uCalc();
uc1.DefineVariable("val = 100");

var uc2 = new uCalc();
uc2.DefineVariable("val = 200");

// Create the transformer in uc1's context
var t = uc1.NewTransformer();
t.Text = "data";
t.Pattern("data");
t.Find();

var m = t.Matches;
var parent_uc = m.uCalc;

Console.WriteLine($"Parent has value: {parent_uc.Eval("val")}");
Console.WriteLine($"Is parent uc1? {parent_uc.MemoryIndex == uc1.MemoryIndex}");
Console.WriteLine($"Is parent uc2? {parent_uc.MemoryIndex == uc2.MemoryIndex}");
				
			
Parent has value: 100
Is parent uc1? True
Is parent uc2? False
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

#define tf(IsTrue) ((IsTrue) ? "True" : "False")

int main() {
   uCalc uc;
   uCalc uc1;
   uc1.DefineVariable("val = 100");

   uCalc uc2;
   uc2.DefineVariable("val = 200");

   // Create the transformer in uc1's context
   auto t = uc1.NewTransformer();
   t.Text("data");
   t.Pattern("data");
   t.Find();

   auto m = t.Matches();
   auto parent_uc = m.uCalc();

   cout << "Parent has value: " << parent_uc.Eval("val") << endl;
   cout << "Is parent uc1? " << tf(parent_uc.MemoryIndex() == uc1.MemoryIndex()) << endl;
   cout << "Is parent uc2? " << tf(parent_uc.MemoryIndex() == uc2.MemoryIndex()) << endl;
}
				
			
Parent has value: 100
Is parent uc1? True
Is parent uc2? False
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim uc1 As New uCalc()
      uc1.DefineVariable("val = 100")
      
      Dim uc2 As New uCalc()
      uc2.DefineVariable("val = 200")
      
      '// Create the transformer in uc1's context
      Dim t = uc1.NewTransformer()
      t.Text = "data"
      t.Pattern("data")
      t.Find()
      
      Dim m = t.Matches
      Dim parent_uc = m.uCalc
      
      Console.WriteLine($"Parent has value: {parent_uc.Eval("val")}")
      Console.WriteLine($"Is parent uc1? {parent_uc.MemoryIndex = uc1.MemoryIndex}")
      Console.WriteLine($"Is parent uc2? {parent_uc.MemoryIndex = uc2.MemoryIndex}")
   End Sub
End Module
				
			
Parent has value: 100
Is parent uc1? True
Is parent uc2? False
Demonstrates defining a function that is implemented by a native callback in the host application.

ID: 297

				
					using uCalcSoftware;

var uc = new uCalc();

static void MyAreaCallback(uCalc.Callback cb) {
   var length = cb.Arg(1);
   var width = cb.Arg(2);
   cb.Return(length * width);
}


// The signature is defined, but the logic is provided by 'MyAreaCallback'.
uc.DefineFunction("Area(x, y)", MyAreaCallback);
Console.WriteLine(uc.Eval("Area(3, 4)"));
				
			
12
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyAreaCallback(uCalcBase::Callback cb) {
   auto length = cb.Arg(1);
   auto width = cb.Arg(2);
   cb.Return(length * width);
}

int main() {
   uCalc uc;
   // The signature is defined, but the logic is provided by 'MyAreaCallback'.
   uc.DefineFunction("Area(x, y)", MyAreaCallback);
   cout << uc.Eval("Area(3, 4)") << endl;
}
				
			
12
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyAreaCallback(ByVal cb As uCalc.Callback)
      Dim length = cb.Arg(1)
      Dim width = cb.Arg(2)
      cb.Return(length * width)
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// The signature is defined, but the logic is provided by 'MyAreaCallback'.
      uc.DefineFunction("Area(x, y)", AddressOf MyAreaCallback)
      Console.WriteLine(uc.Eval("Area(3, 4)"))
   End Sub
End Module
				
			
12