uCalc SDK Interactive Examples
A simple find-and-replace transformation to replace all occurrences of a word.
ID: 1144
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
// Define a simple replacement rule
t.FromTo("Hello", "Greetings");
// Execute the transformation on an input string
Console.WriteLine(t.Transform("Hello World, and Hello again."));
}
Greetings World, and Greetings again. using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { // Define a simple replacement rule t.FromTo("Hello", "Greetings"); // Execute the transformation on an input string Console.WriteLine(t.Transform("Hello World, and Hello again.")); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
// Define a simple replacement rule
t.FromTo("Hello", "Greetings");
// Execute the transformation on an input string
cout << t.Transform("Hello World, and Hello again.") << endl;
}
}
Greetings World, and Greetings again. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // Define a simple replacement rule t.FromTo("Hello", "Greetings"); // Execute the transformation on an input string cout << t.Transform("Hello World, and Hello again.") << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
'// Define a simple replacement rule
t.FromTo("Hello", "Greetings")
'// Execute the transformation on an input string
Console.WriteLine(t.Transform("Hello World, and Hello again."))
End Using
End Sub
End Module
Greetings World, and Greetings again. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() '// Define a simple replacement rule t.FromTo("Hello", "Greetings") '// Execute the transformation on an input string Console.WriteLine(t.Transform("Hello World, and Hello again.")) End Using End Sub End Module
A simple find-and-replace transformation.
ID: 1071
See: FromTo
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
t.FromTo("Hello {name}", "Greetings, {name}!");
Console.WriteLine(t.Transform("Hello World"));
Greetings, World! using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); t.FromTo("Hello {name}", "Greetings, {name}!"); Console.WriteLine(t.Transform("Hello World"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
t.FromTo("Hello {name}", "Greetings, {name}!");
cout << t.Transform("Hello World") << endl;
}
Greetings, World! #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; t.FromTo("Hello {name}", "Greetings, {name}!"); cout << t.Transform("Hello World") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
t.FromTo("Hello {name}", "Greetings, {name}!")
Console.WriteLine(t.Transform("Hello World"))
End Sub
End Module
Greetings, World! Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() t.FromTo("Hello {name}", "Greetings, {name}!") Console.WriteLine(t.Transform("Hello World")) End Sub End Module
A simple inline function to convert inches to centimeters.
ID: 1186
using uCalcSoftware;
var uc = new uCalc();
uc.DefineFunction("InToCm(inches) = inches * 2.54");
Console.WriteLine(uc.Eval("InToCm(10)"));
25.4 using uCalcSoftware; var uc = new uCalc(); uc.DefineFunction("InToCm(inches) = inches * 2.54"); Console.WriteLine(uc.Eval("InToCm(10)"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uc.DefineFunction("InToCm(inches) = inches * 2.54");
cout << uc.Eval("InToCm(10)") << endl;
}
25.4 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uc.DefineFunction("InToCm(inches) = inches * 2.54"); cout << uc.Eval("InToCm(10)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("InToCm(inches) = inches * 2.54")
Console.WriteLine(uc.Eval("InToCm(10)"))
End Sub
End Module
25.4 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("InToCm(inches) = inches * 2.54") Console.WriteLine(uc.Eval("InToCm(10)")) End Sub End Module
A simple Lexer/Tokenizer that categorizes content into numbers, operators, or keywords.
ID: 228
See: Multi-Pattern Search
using uCalcSoftware;
var uc = new uCalc();
// Note how the definition order matters if patterns overlap (though here they are distinct).
var t = uc.NewTransformer();
// Define patterns for a simple math language
t.FromTo("{d: {@Number}}", "[NUM:{d}]");
t.FromTo("{op: + | - | * | / }", "[OP:{op}]");
t.FromTo("print", "[CMD:PRINT]"); // Specific keyword
var code = "print 10 + 20";
Console.WriteLine(t.Transform(code));
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20] using uCalcSoftware; var uc = new uCalc(); // Note how the definition order matters if patterns overlap (though here they are distinct). var t = uc.NewTransformer(); // Define patterns for a simple math language t.FromTo("{d: {@Number}}", "[NUM:{d}]"); t.FromTo("{op: + | - | * | / }", "[OP:{op}]"); t.FromTo("print", "[CMD:PRINT]"); // Specific keyword var code = "print 10 + 20"; Console.WriteLine(t.Transform(code));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Note how the definition order matters if patterns overlap (though here they are distinct).
auto t = uc.NewTransformer();
// Define patterns for a simple math language
t.FromTo("{d: {@Number}}", "[NUM:{d}]");
t.FromTo("{op: + | - | * | / }", "[OP:{op}]");
t.FromTo("print", "[CMD:PRINT]"); // Specific keyword
auto code = "print 10 + 20";
cout << t.Transform(code) << endl;
}
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20] #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Note how the definition order matters if patterns overlap (though here they are distinct). auto t = uc.NewTransformer(); // Define patterns for a simple math language t.FromTo("{d: {@Number}}", "[NUM:{d}]"); t.FromTo("{op: + | - | * | / }", "[OP:{op}]"); t.FromTo("print", "[CMD:PRINT]"); // Specific keyword auto code = "print 10 + 20"; cout << t.Transform(code) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Note how the definition order matters if patterns overlap (though here they are distinct).
Dim t = uc.NewTransformer()
'// Define patterns for a simple math language
t.FromTo("{d: {@Number}}", "[NUM:{d}]")
t.FromTo("{op: + | - | * | / }", "[OP:{op}]")
t.FromTo("print", "[CMD:PRINT]") '// Specific keyword
Dim code = "print 10 + 20"
Console.WriteLine(t.Transform(code))
End Sub
End Module
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20] Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Note how the definition order matters if patterns overlap (though here they are distinct). Dim t = uc.NewTransformer() '// Define patterns for a simple math language t.FromTo("{d: {@Number}}", "[NUM:{d}]") t.FromTo("{op: + | - | * | / }", "[OP:{op}]") t.FromTo("print", "[CMD:PRINT]") '// Specific keyword Dim code = "print 10 + 20" Console.WriteLine(t.Transform(code)) End Sub End Module
A simple LISP transpiler that handles only binary operators.
ID: 1416
using uCalcSoftware;
var uc = new uCalc();
var t = uc.ExpressionTransformer;
t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})");
Console.WriteLine("LISP: (+ 10 20)");
Console.WriteLine($"uCalc: {t.Transform("(+ 10 20)")}");
Console.WriteLine($"Result: {uc.Eval("(+ 10 20)")}");
LISP: (+ 10 20)
uCalc: (10 + 20)
Result: 30 using uCalcSoftware; var uc = new uCalc(); var t = uc.ExpressionTransformer; t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})"); Console.WriteLine("LISP: (+ 10 20)"); Console.WriteLine($"uCalc: {t.Transform("(+ 10 20)")}"); Console.WriteLine($"Result: {uc.Eval("(+ 10 20)")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.ExpressionTransformer();
t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})");
cout << "LISP: (+ 10 20)" << endl;
cout << "uCalc: " << t.Transform("(+ 10 20)") << endl;
cout << "Result: " << uc.Eval("(+ 10 20)") << endl;
}
LISP: (+ 10 20)
uCalc: (10 + 20)
Result: 30 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.ExpressionTransformer(); t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})"); cout << "LISP: (+ 10 20)" << endl; cout << "uCalc: " << t.Transform("(+ 10 20)") << endl; cout << "Result: " << uc.Eval("(+ 10 20)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.ExpressionTransformer
t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})")
Console.WriteLine("LISP: (+ 10 20)")
Console.WriteLine($"uCalc: {t.Transform("(+ 10 20)")}")
Console.WriteLine($"Result: {uc.Eval("(+ 10 20)")}")
End Sub
End Module
LISP: (+ 10 20)
uCalc: (10 + 20)
Result: 30 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.ExpressionTransformer t.FromTo("({op:1} {a:1} {b:1})", "({a} {op} {b})") Console.WriteLine("LISP: (+ 10 20)") Console.WriteLine($"uCalc: {t.Transform("(+ 10 20)")}") Console.WriteLine($"Result: {uc.Eval("(+ 10 20)")}") End Sub End Module
A simple lookup to find the index of the second occurrence of the word 'is'.
ID: 843
See: IndexOf
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
t.Text = "This is a test. It is simple.";
t.Pattern("{@Alpha}");
t.Find();
// The second 'is' starts at character position 19
var index = t.Matches.IndexOf(19);
Console.WriteLine($"The match at character 19 is at index: {index}");
Console.WriteLine($"Match content: '{t.Matches[index].Text}'");
The match at character 19 is at index: 5
Match content: 'is' using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); t.Text = "This is a test. It is simple."; t.Pattern("{@Alpha}"); t.Find(); // The second 'is' starts at character position 19 var index = t.Matches.IndexOf(19); Console.WriteLine($"The match at character 19 is at index: {index}"); Console.WriteLine($"Match content: '{t.Matches[index].Text}'");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
t.Text("This is a test. It is simple.");
t.Pattern("{@Alpha}");
t.Find();
// The second 'is' starts at character position 19
auto index = t.Matches().IndexOf(19);
cout << "The match at character 19 is at index: " << index << endl;
cout << "Match content: '" << t.Matches()[index].Text() << "'" << endl;
}
The match at character 19 is at index: 5
Match content: 'is' #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.Text("This is a test. It is simple."); t.Pattern("{@Alpha}"); t.Find(); // The second 'is' starts at character position 19 auto index = t.Matches().IndexOf(19); cout << "The match at character 19 is at index: " << index << endl; cout << "Match content: '" << t.Matches()[index].Text() << "'" << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
t.Text = "This is a test. It is simple."
t.Pattern("{@Alpha}")
t.Find()
'// The second 'is' starts at character position 19
Dim index = t.Matches.IndexOf(19)
Console.WriteLine($"The match at character 19 is at index: {index}")
Console.WriteLine($"Match content: '{t.Matches(index).Text}'")
End Sub
End Module
The match at character 19 is at index: 5
Match content: 'is' Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() t.Text = "This is a test. It is simple." t.Pattern("{@Alpha}") t.Find() '// The second 'is' starts at character position 19 Dim index = t.Matches.IndexOf(19) Console.WriteLine($"The match at character 19 is at index: {index}") Console.WriteLine($"Match content: '{t.Matches(index).Text}'") End Sub End Module
A simple lookup to retrieve a defined variable by its name and display its value.
ID: 374
using uCalcSoftware;
var uc = new uCalc();
uc.DefineVariable("MyVar = 123");
// Retrieve the item by name
var item = uc.ItemOf("MyVar");
// Check if the item was found and print its value
if (item.NotEmpty()) {
Console.WriteLine($"Value of MyVar is: {item.Value()}");
}
Value of MyVar is: 123 using uCalcSoftware; var uc = new uCalc(); uc.DefineVariable("MyVar = 123"); // Retrieve the item by name var item = uc.ItemOf("MyVar"); // Check if the item was found and print its value if (item.NotEmpty()) { Console.WriteLine($"Value of MyVar is: {item.Value()}"); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uc.DefineVariable("MyVar = 123");
// Retrieve the item by name
auto item = uc.ItemOf("MyVar");
// Check if the item was found and print its value
if (item.NotEmpty()) {
cout << "Value of MyVar is: " << item.Value() << endl;
}
}
Value of MyVar is: 123 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uc.DefineVariable("MyVar = 123"); // Retrieve the item by name auto item = uc.ItemOf("MyVar"); // Check if the item was found and print its value if (item.NotEmpty()) { cout << "Value of MyVar is: " << item.Value() << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("MyVar = 123")
'// Retrieve the item by name
Dim item = uc.ItemOf("MyVar")
'// Check if the item was found and print its value
If item.NotEmpty() Then
Console.WriteLine($"Value of MyVar is: {item.Value()}")
End If
End Sub
End Module
Value of MyVar is: 123 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("MyVar = 123") '// Retrieve the item by name Dim item = uc.ItemOf("MyVar") '// Check if the item was found and print its value If item.NotEmpty() Then Console.WriteLine($"Value of MyVar is: {item.Value()}") End If End Sub End Module
A simple optional word.
ID: 790
See: Optional parts
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
t.FromTo("This is a [very] important test", "MATCHED");
// Matches with the optional word
Console.WriteLine(t.Transform("This is a very important test"));
// Also matches without the optional word
Console.WriteLine(t.Transform("This is a important test"));
MATCHED
MATCHED using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); t.FromTo("This is a [very] important test", "MATCHED"); // Matches with the optional word Console.WriteLine(t.Transform("This is a very important test")); // Also matches without the optional word Console.WriteLine(t.Transform("This is a important test"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
t.FromTo("This is a [very] important test", "MATCHED");
// Matches with the optional word
cout << t.Transform("This is a very important test") << endl;
// Also matches without the optional word
cout << t.Transform("This is a important test") << endl;
}
MATCHED
MATCHED #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; t.FromTo("This is a [very] important test", "MATCHED"); // Matches with the optional word cout << t.Transform("This is a very important test") << endl; // Also matches without the optional word cout << t.Transform("This is a important test") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
t.FromTo("This is a [very] important test", "MATCHED")
'// Matches with the optional word
Console.WriteLine(t.Transform("This is a very important test"))
'// Also matches without the optional word
Console.WriteLine(t.Transform("This is a important test"))
End Sub
End Module
MATCHED
MATCHED Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() t.FromTo("This is a [very] important test", "MATCHED") '// Matches with the optional word Console.WriteLine(t.Transform("This is a very important test")) '// Also matches without the optional word Console.WriteLine(t.Transform("This is a important test")) End Sub End Module
A simple replacement demonstrating how to swap specific word patterns.
ID: 392
See: NewTransformer
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
t.FromTo("Hello {name}", "Greetings, {name}!");
Console.WriteLine(t.Transform("Hello World"));
Greetings, World! using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); t.FromTo("Hello {name}", "Greetings, {name}!"); Console.WriteLine(t.Transform("Hello World"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
t.FromTo("Hello {name}", "Greetings, {name}!");
cout << t.Transform("Hello World") << endl;
}
Greetings, World! #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.FromTo("Hello {name}", "Greetings, {name}!"); cout << t.Transform("Hello World") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
t.FromTo("Hello {name}", "Greetings, {name}!")
Console.WriteLine(t.Transform("Hello World"))
End Sub
End Module
Greetings, World! Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() t.FromTo("Hello {name}", "Greetings, {name}!") Console.WriteLine(t.Transform("Hello World")) End Sub End Module
A simple two-pass transformation where the output of the first pass becomes the input for the second.
ID: 1105
See: Pass
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
t.Text = "A";
// Pass 0 will change 'A' to 'B'
var pass0 = t.Pass(0);
pass0.FromTo("A", "B");
// Pass 1 will receive 'B' and change it to 'C'
var pass1 = t.Pass(1);
pass1.FromTo("B", "C");
t.Transform();
Console.WriteLine(t); // The final output is 'C'
C using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); t.Text = "A"; // Pass 0 will change 'A' to 'B' var pass0 = t.Pass(0); pass0.FromTo("A", "B"); // Pass 1 will receive 'B' and change it to 'C' var pass1 = t.Pass(1); pass1.FromTo("B", "C"); t.Transform(); Console.WriteLine(t); // The final output is 'C'
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
t.Text("A");
// Pass 0 will change 'A' to 'B'
auto pass0 = t.Pass(0);
pass0.FromTo("A", "B");
// Pass 1 will receive 'B' and change it to 'C'
auto pass1 = t.Pass(1);
pass1.FromTo("B", "C");
t.Transform();
cout << t << endl; // The final output is 'C'
}
C #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; t.Text("A"); // Pass 0 will change 'A' to 'B' auto pass0 = t.Pass(0); pass0.FromTo("A", "B"); // Pass 1 will receive 'B' and change it to 'C' auto pass1 = t.Pass(1); pass1.FromTo("B", "C"); t.Transform(); cout << t << endl; // The final output is 'C' }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
t.Text = "A"
'// Pass 0 will change 'A' to 'B'
Dim pass0 = t.Pass(0)
pass0.FromTo("A", "B")
'// Pass 1 will receive 'B' and change it to 'C'
Dim pass1 = t.Pass(1)
pass1.FromTo("B", "C")
t.Transform()
Console.WriteLine(t) '// The final output is 'C'
End Sub
End Module
C Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() t.Text = "A" '// Pass 0 will change 'A' to 'B' Dim pass0 = t.Pass(0) pass0.FromTo("A", "B") '// Pass 1 will receive 'B' and change it to 'C' Dim pass1 = t.Pass(1) pass1.FromTo("B", "C") t.Transform() Console.WriteLine(t) '// The final output is 'C' End Sub End Module
A simple word replacement to demonstrate the basic find-and-replace capability.
ID: 1263
See: uCalc Transformer
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
t.FromTo("red", "blue");
Console.WriteLine(t.Transform("The red car and the red house."));
};
The blue car and the blue house. using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { t.FromTo("red", "blue"); Console.WriteLine(t.Transform("The red car and the red house.")); };
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
t.FromTo("red", "blue");
cout << t.Transform("The red car and the red house.") << endl;
};
}
The blue car and the blue house. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope t.FromTo("red", "blue"); cout << t.Transform("The red car and the red house.") << endl; }; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
t.FromTo("red", "blue")
Console.WriteLine(t.Transform("The red car and the red house."))
End Using
End Sub
End Module
The blue car and the blue house. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() t.FromTo("red", "blue") Console.WriteLine(t.Transform("The red car and the red house.")) End Using End Sub End Module
A single-pass transformer that converts headers, list items, bold, and italic Markdown syntax to HTML.
ID: 1404
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
t.DefaultRuleSet.RewindOnChange = true;
// 2. Define Rules (General rules first, specific rules last for LIFO precedence)
// -- Inline rules --
// Italic is defined before Bold, giving Bold higher precedence.
t.FromTo("*{text}*", "{text}");
t.FromTo("**{text}**", "{text}");
// -- Block-level rules --
t.FromTo("#{@Whitespace}{line}", "{line}
");
t.FromTo("*{@Whitespace}{line}", " {line} ");
t.FromTo("{@nl}{@nl}", "{@nl}{@nl}"); // {@nl} = NewLine
t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}{@nl}* ");
// 3. Define the input Markdown text
var markdown = """
# Main Header
* First list item
* Second list item with **bold** text.
* Third list item with *italic* text.
Another paragraph with **bold** and *italic*.
""";
// 4. Run the transformation and print the result
Console.WriteLine(t.Transform(markdown));
}
<h1>Main Header</h1>
<ul>
<li>First list item</li>
<li>Second list item with <b>bold</b> text.</li>
<li>Third list item with <i>italic</i> text.</li>
</ul>
Another paragraph with <b>bold</b> and <i>italic</i>. using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { t.DefaultRuleSet.RewindOnChange = true; // 2. Define Rules (General rules first, specific rules last for LIFO precedence) // -- Inline rules -- // Italic is defined before Bold, giving Bold higher precedence. t.FromTo("*{text}*", "<i>{text}</i>"); t.FromTo("**{text}**", "<b>{text}</b>"); // -- Block-level rules -- t.FromTo("#{@Whitespace}{line}", "<h1>{line}</h1>"); t.FromTo("*{@Whitespace}{line}", "<li>{line}</li>"); t.FromTo("</li>{@nl}{@nl}", "</li>{@nl}</ul>{@nl}"); // {@nl} = NewLine t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}<ul>{@nl}* "); // 3. Define the input Markdown text var markdown = """ # Main Header * First list item * Second list item with **bold** text. * Third list item with *italic* text. Another paragraph with **bold** and *italic*. """; // 4. Run the transformation and print the result Console.WriteLine(t.Transform(markdown)); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
t.DefaultRuleSet().RewindOnChange(true);
// 2. Define Rules (General rules first, specific rules last for LIFO precedence)
// -- Inline rules --
// Italic is defined before Bold, giving Bold higher precedence.
t.FromTo("*{text}*", "{text}");
t.FromTo("**{text}**", "{text}");
// -- Block-level rules --
t.FromTo("#{@Whitespace}{line}", "{line}
");
t.FromTo("*{@Whitespace}{line}", "{line} ");
t.FromTo("{@nl}{@nl}", "{@nl}{@nl}"); // {@nl} = NewLine
t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}{@nl}* ");
// 3. Define the input Markdown text
auto markdown = R"(
# Main Header
* First list item
* Second list item with **bold** text.
* Third list item with *italic* text.
Another paragraph with **bold** and *italic*.
)";
// 4. Run the transformation and print the result
cout << t.Transform(markdown) << endl;
}
}
<h1>Main Header</h1>
<ul>
<li>First list item</li>
<li>Second list item with <b>bold</b> text.</li>
<li>Third list item with <i>italic</i> text.</li>
</ul>
Another paragraph with <b>bold</b> and <i>italic</i>. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope t.DefaultRuleSet().RewindOnChange(true); // 2. Define Rules (General rules first, specific rules last for LIFO precedence) // -- Inline rules -- // Italic is defined before Bold, giving Bold higher precedence. t.FromTo("*{text}*", "<i>{text}</i>"); t.FromTo("**{text}**", "<b>{text}</b>"); // -- Block-level rules -- t.FromTo("#{@Whitespace}{line}", "<h1>{line}</h1>"); t.FromTo("*{@Whitespace}{line}", "<li>{line}</li>"); t.FromTo("</li>{@nl}{@nl}", "</li>{@nl}</ul>{@nl}"); // {@nl} = NewLine t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}<ul>{@nl}* "); // 3. Define the input Markdown text auto markdown = R"( # Main Header * First list item * Second list item with **bold** text. * Third list item with *italic* text. Another paragraph with **bold** and *italic*. )"; // 4. Run the transformation and print the result cout << t.Transform(markdown) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
t.DefaultRuleSet.RewindOnChange = true
'// 2. Define Rules (General rules first, specific rules last for LIFO precedence)
'// -- Inline rules --
'// Italic is defined before Bold, giving Bold higher precedence.
t.FromTo("*{text}*", "{text}")
t.FromTo("**{text}**", "{text}")
'// -- Block-level rules --
t.FromTo("#{@Whitespace}{line}", "{line}
")
t.FromTo("*{@Whitespace}{line}", " {line} ")
t.FromTo("{@nl}{@nl}", "{@nl}{@nl}") '// {@nl} = NewLine
t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}{@nl}* ")
'// 3. Define the input Markdown text
Dim markdown = "
# Main Header
* First list item
* Second list item with **bold** text.
* Third list item with *italic* text.
Another paragraph with **bold** and *italic*.
"
'// 4. Run the transformation and print the result
Console.WriteLine(t.Transform(markdown))
End Using
End Sub
End Module
<h1>Main Header</h1>
<ul>
<li>First list item</li>
<li>Second list item with <b>bold</b> text.</li>
<li>Third list item with <i>italic</i> text.</li>
</ul>
Another paragraph with <b>bold</b> and <i>italic</i>. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() t.DefaultRuleSet.RewindOnChange = true '// 2. Define Rules (General rules first, specific rules last for LIFO precedence) '// -- Inline rules -- '// Italic is defined before Bold, giving Bold higher precedence. t.FromTo("*{text}*", "<i>{text}</i>") t.FromTo("**{text}**", "<b>{text}</b>") '// -- Block-level rules -- t.FromTo("#{@Whitespace}{line}", "<h1>{line}</h1>") t.FromTo("*{@Whitespace}{line}", "<li>{line}</li>") t.FromTo("</li>{@nl}{@nl}", "</li>{@nl}</ul>{@nl}") '// {@nl} = NewLine t.FromTo("{@nl}{@nl}*{@Whitespace}", "{@nl}<ul>{@nl}* ") '// 3. Define the input Markdown text Dim markdown = " # Main Header * First list item * Second list item with **bold** text. * Third list item with *italic* text. Another paragraph with **bold** and *italic*. " '// 4. Run the transformation and print the result Console.WriteLine(t.Transform(markdown)) End Using End Sub End Module
A succinct example demonstrating a simple chain of two replacement actions on the root string.
ID: 1260
using uCalcSoftware;
var uc = new uCalc();
using (var s = new uCalc.String("A and C")) {
// Each Replace() call returns the modified string object, allowing the next call.
s.Replace("A", "B").Replace("C", "D");
Console.Write("Result: ");
Console.WriteLine(s);
};
Result: B and D using uCalcSoftware; var uc = new uCalc(); using (var s = new uCalc.String("A and C")) { // Each Replace() call returns the modified string object, allowing the next call. s.Replace("A", "B").Replace("C", "D"); Console.Write("Result: "); Console.WriteLine(s); };
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::String s("A and C");
s.Owned(); // Causes s to be released when it goes out of scope
// Each Replace() call returns the modified string object, allowing the next call.
s.Replace("A", "B").Replace("C", "D");
cout << "Result: ";
cout << s << endl;
};
}
Result: B and D #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::String s("A and C"); s.Owned(); // Causes s to be released when it goes out of scope // Each Replace() call returns the modified string object, allowing the next call. s.Replace("A", "B").Replace("C", "D"); cout << "Result: "; cout << s << endl; }; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using s As New uCalc.String("A and C")
'// Each Replace() call returns the modified string object, allowing the next call.
s.Replace("A", "B").Replace("C", "D")
Console.Write("Result: ")
Console.WriteLine(s)
End Using
End Sub
End Module
Result: B and D Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using s As New uCalc.String("A and C") '// Each Replace() call returns the modified string object, allowing the next call. s.Replace("A", "B").Replace("C", "D") Console.Write("Result: ") Console.WriteLine(s) End Using End Sub End Module
Adding an error handler callback
ID: 1
See: AddHandler, Code = [ErrorCode], Error = [ErrorInfo], Error handling and control flow, Expression = [string], Introduction, Location = [int], Message = [string], Symbol = [string], TrapOnDivideByZero = [bool]
using uCalcSoftware;
var uc = new uCalc();
static void MyErrorHandler(Handle_uCalc h) {
var uc = new uCalc(h);
Console.WriteLine("An error has occurred!");
Console.WriteLine($"Error #: {(int)uc.Error.Code}");
Console.WriteLine($"Error Message: {uc.Error.Message}");
Console.WriteLine($"Error Symbol: {uc.Error.Symbol}");
Console.WriteLine($"Error Location: {uc.Error.Location}");
Console.WriteLine($"Error Expression: {uc.Error.Expression}");
}
uc.Error.AddHandler(MyErrorHandler);
Console.WriteLine(uc.EvalStr("123+"));
Console.WriteLine("");
uc.Error.TrapOnDivideByZero = true;
Console.WriteLine(uc.EvalStr("5/0"));
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 using uCalcSoftware; var uc = new uCalc(); static void MyErrorHandler(Handle_uCalc h) { var uc = new uCalc(h); Console.WriteLine("An error has occurred!"); Console.WriteLine($"Error #: {(int)uc.Error.Code}"); Console.WriteLine($"Error Message: {uc.Error.Message}"); Console.WriteLine($"Error Symbol: {uc.Error.Symbol}"); Console.WriteLine($"Error Location: {uc.Error.Location}"); Console.WriteLine($"Error Expression: {uc.Error.Expression}"); } uc.Error.AddHandler(MyErrorHandler); Console.WriteLine(uc.EvalStr("123+")); Console.WriteLine(""); uc.Error.TrapOnDivideByZero = true; Console.WriteLine(uc.EvalStr("5/0"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyErrorHandler(Handle_uCalc h) {
auto uc = uCalc(h);
cout << "An error has occurred!" << endl;
cout << "Error #: " << (int)uc.Error().Code() << endl;
cout << "Error Message: " << uc.Error().Message() << endl;
cout << "Error Symbol: " << uc.Error().Symbol() << endl;
cout << "Error Location: " << uc.Error().Location() << endl;
cout << "Error Expression: " << uc.Error().Expression() << endl;
}
int main() {
uCalc uc;
uc.Error().AddHandler(MyErrorHandler);
cout << uc.EvalStr("123+") << endl;
cout << "" << endl;
uc.Error().TrapOnDivideByZero(true);
cout << uc.EvalStr("5/0") << endl;
}
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyErrorHandler(Handle_uCalc h) { auto uc = uCalc(h); cout << "An error has occurred!" << endl; cout << "Error #: " << (int)uc.Error().Code() << endl; cout << "Error Message: " << uc.Error().Message() << endl; cout << "Error Symbol: " << uc.Error().Symbol() << endl; cout << "Error Location: " << uc.Error().Location() << endl; cout << "Error Expression: " << uc.Error().Expression() << endl; } int main() { uCalc uc; uc.Error().AddHandler(MyErrorHandler); cout << uc.EvalStr("123+") << endl; cout << "" << endl; uc.Error().TrapOnDivideByZero(true); cout << uc.EvalStr("5/0") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyErrorHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
Console.WriteLine("An error has occurred!")
Console.WriteLine($"Error #: {CInt(uc.Error.Code)}")
Console.WriteLine($"Error Message: {uc.Error.Message}")
Console.WriteLine($"Error Symbol: {uc.Error.Symbol}")
Console.WriteLine($"Error Location: {uc.Error.Location}")
Console.WriteLine($"Error Expression: {uc.Error.Expression}")
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.Error.AddHandler(AddressOf MyErrorHandler)
Console.WriteLine(uc.EvalStr("123+"))
Console.WriteLine("")
uc.Error.TrapOnDivideByZero = true
Console.WriteLine(uc.EvalStr("5/0"))
End Sub
End Module
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 Imports System Imports uCalcSoftware Public Module Program Public Sub MyErrorHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) Console.WriteLine("An error has occurred!") Console.WriteLine($"Error #: {CInt(uc.Error.Code)}") Console.WriteLine($"Error Message: {uc.Error.Message}") Console.WriteLine($"Error Symbol: {uc.Error.Symbol}") Console.WriteLine($"Error Location: {uc.Error.Location}") Console.WriteLine($"Error Expression: {uc.Error.Expression}") End Sub Public Sub Main() Dim uc As New uCalc() uc.Error.AddHandler(AddressOf MyErrorHandler) Console.WriteLine(uc.EvalStr("123+")) Console.WriteLine("") uc.Error.TrapOnDivideByZero = true Console.WriteLine(uc.EvalStr("5/0")) End Sub End Module
Adds a C-style single-line comment token and categorizes it as whitespace to be ignored by other rules.
ID: 1135
See: Tokens = [Tokens]
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
t.FromTo("this", "THAT");
string text = "transform this but not // this in a comment";
Console.WriteLine("--- Before --- ");
// Initially, the comment is treated as regular text.
Console.WriteLine(t.Transform(text));
// Add a token for C-style comments and classify it as whitespace.
t.Tokens.Add("//.*", TokenType.Whitespace);
Console.WriteLine("");
Console.WriteLine("--- After --- ");
// Re-run the transform. The comment is now ignored.
Console.WriteLine(t.Transform(text));
--- Before ---
transform THAT but not // THAT in a comment
--- After ---
transform THAT but not // this in a comment using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); t.FromTo("this", "THAT"); string text = "transform this but not // this in a comment"; Console.WriteLine("--- Before --- "); // Initially, the comment is treated as regular text. Console.WriteLine(t.Transform(text)); // Add a token for C-style comments and classify it as whitespace. t.Tokens.Add("//.*", TokenType.Whitespace); Console.WriteLine(""); Console.WriteLine("--- After --- "); // Re-run the transform. The comment is now ignored. Console.WriteLine(t.Transform(text));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
t.FromTo("this", "THAT");
string text = "transform this but not // this in a comment";
cout << "--- Before --- " << endl;
// Initially, the comment is treated as regular text.
cout << t.Transform(text) << endl;
// Add a token for C-style comments and classify it as whitespace.
t.Tokens().Add("//.*", TokenType::Whitespace);
cout << "" << endl;
cout << "--- After --- " << endl;
// Re-run the transform. The comment is now ignored.
cout << t.Transform(text) << endl;
}
--- Before ---
transform THAT but not // THAT in a comment
--- After ---
transform THAT but not // this in a comment #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; t.FromTo("this", "THAT"); string text = "transform this but not // this in a comment"; cout << "--- Before --- " << endl; // Initially, the comment is treated as regular text. cout << t.Transform(text) << endl; // Add a token for C-style comments and classify it as whitespace. t.Tokens().Add("//.*", TokenType::Whitespace); cout << "" << endl; cout << "--- After --- " << endl; // Re-run the transform. The comment is now ignored. cout << t.Transform(text) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
t.FromTo("this", "THAT")
Dim text As String = "transform this but not // this in a comment"
Console.WriteLine("--- Before --- ")
'// Initially, the comment is treated as regular text.
Console.WriteLine(t.Transform(text))
'// Add a token for C-style comments and classify it as whitespace.
t.Tokens.Add("//.*", TokenType.Whitespace)
Console.WriteLine("")
Console.WriteLine("--- After --- ")
'// Re-run the transform. The comment is now ignored.
Console.WriteLine(t.Transform(text))
End Sub
End Module
--- Before ---
transform THAT but not // THAT in a comment
--- After ---
transform THAT but not // this in a comment Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() t.FromTo("this", "THAT") Dim text As String = "transform this but not // this in a comment" Console.WriteLine("--- Before --- ") '// Initially, the comment is treated as regular text. Console.WriteLine(t.Transform(text)) '// Add a token for C-style comments and classify it as whitespace. t.Tokens.Add("//.*", TokenType.Whitespace) Console.WriteLine("") Console.WriteLine("--- After --- ") '// Re-run the transform. The comment is now ignored. Console.WriteLine(t.Transform(text)) End Sub End Module
Alias using symbol object
ID: 56
See: CreateAlias
using uCalcSoftware;
var uc = new uCalc();
var MyVar = uc.DefineVariable("MyVar = 123");
var MyAlias = uc.CreateAlias("MyAlias", MyVar);
Console.WriteLine(uc.Eval("MyAlias")); // Contains same value as MyVar
uc.Eval("MyAlias = 456"); // Same as changing MyVar
Console.WriteLine(uc.EvalStr("MyVar")); // MyVar reflects change made in MyAlias
Console.WriteLine("");
// This section below shows how you can have Alias distinguish
// between different variables with the same name
uc.DefineFunction("MyFunc() = MyVar + 1");
// MyVar defined below is a new variable sharing the same name
// MyFunc() will still use the value of the original MyVar
var MyVarAlt = uc.DefineVariable("MyVar = 100");
// The function below uses the new MyVar variable
uc.DefineFunction("MyFunc2() = MyVar + 1");
Console.WriteLine(uc.Eval("MyFunc()"));
Console.WriteLine(uc.Eval("MyFunc2()"));
Console.WriteLine("");
uc.CreateAlias("MyAliasAlt", MyVarAlt);
uc.Eval("MyAlias = 200"); // Changes MyVar used in MyFunc()
uc.Eval("MyAliasAlt = 300"); // Changes MyVar used in MyFunc2()
Console.WriteLine(uc.Eval("MyFunc()"));
Console.WriteLine(uc.Eval("MyFunc2()"));
123
456
457
101
201
301 using uCalcSoftware; var uc = new uCalc(); var MyVar = uc.DefineVariable("MyVar = 123"); var MyAlias = uc.CreateAlias("MyAlias", MyVar); Console.WriteLine(uc.Eval("MyAlias")); // Contains same value as MyVar uc.Eval("MyAlias = 456"); // Same as changing MyVar Console.WriteLine(uc.EvalStr("MyVar")); // MyVar reflects change made in MyAlias Console.WriteLine(""); // This section below shows how you can have Alias distinguish // between different variables with the same name uc.DefineFunction("MyFunc() = MyVar + 1"); // MyVar defined below is a new variable sharing the same name // MyFunc() will still use the value of the original MyVar var MyVarAlt = uc.DefineVariable("MyVar = 100"); // The function below uses the new MyVar variable uc.DefineFunction("MyFunc2() = MyVar + 1"); Console.WriteLine(uc.Eval("MyFunc()")); Console.WriteLine(uc.Eval("MyFunc2()")); Console.WriteLine(""); uc.CreateAlias("MyAliasAlt", MyVarAlt); uc.Eval("MyAlias = 200"); // Changes MyVar used in MyFunc() uc.Eval("MyAliasAlt = 300"); // Changes MyVar used in MyFunc2() Console.WriteLine(uc.Eval("MyFunc()")); Console.WriteLine(uc.Eval("MyFunc2()"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto MyVar = uc.DefineVariable("MyVar = 123");
auto MyAlias = uc.CreateAlias("MyAlias", MyVar);
cout << uc.Eval("MyAlias") << endl; // Contains same value as MyVar
uc.Eval("MyAlias = 456"); // Same as changing MyVar
cout << uc.EvalStr("MyVar") << endl; // MyVar reflects change made in MyAlias
cout << "" << endl;
// This section below shows how you can have Alias distinguish
// between different variables with the same name
uc.DefineFunction("MyFunc() = MyVar + 1");
// MyVar defined below is a new variable sharing the same name
// MyFunc() will still use the value of the original MyVar
auto MyVarAlt = uc.DefineVariable("MyVar = 100");
// The function below uses the new MyVar variable
uc.DefineFunction("MyFunc2() = MyVar + 1");
cout << uc.Eval("MyFunc()") << endl;
cout << uc.Eval("MyFunc2()") << endl;
cout << "" << endl;
uc.CreateAlias("MyAliasAlt", MyVarAlt);
uc.Eval("MyAlias = 200"); // Changes MyVar used in MyFunc()
uc.Eval("MyAliasAlt = 300"); // Changes MyVar used in MyFunc2()
cout << uc.Eval("MyFunc()") << endl;
cout << uc.Eval("MyFunc2()") << endl;
}
123
456
457
101
201
301 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto MyVar = uc.DefineVariable("MyVar = 123"); auto MyAlias = uc.CreateAlias("MyAlias", MyVar); cout << uc.Eval("MyAlias") << endl; // Contains same value as MyVar uc.Eval("MyAlias = 456"); // Same as changing MyVar cout << uc.EvalStr("MyVar") << endl; // MyVar reflects change made in MyAlias cout << "" << endl; // This section below shows how you can have Alias distinguish // between different variables with the same name uc.DefineFunction("MyFunc() = MyVar + 1"); // MyVar defined below is a new variable sharing the same name // MyFunc() will still use the value of the original MyVar auto MyVarAlt = uc.DefineVariable("MyVar = 100"); // The function below uses the new MyVar variable uc.DefineFunction("MyFunc2() = MyVar + 1"); cout << uc.Eval("MyFunc()") << endl; cout << uc.Eval("MyFunc2()") << endl; cout << "" << endl; uc.CreateAlias("MyAliasAlt", MyVarAlt); uc.Eval("MyAlias = 200"); // Changes MyVar used in MyFunc() uc.Eval("MyAliasAlt = 300"); // Changes MyVar used in MyFunc2() cout << uc.Eval("MyFunc()") << endl; cout << uc.Eval("MyFunc2()") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim MyVar = uc.DefineVariable("MyVar = 123")
Dim MyAlias = uc.CreateAlias("MyAlias", MyVar)
Console.WriteLine(uc.Eval("MyAlias")) '// Contains same value as MyVar
uc.Eval("MyAlias = 456") '// Same as changing MyVar
Console.WriteLine(uc.EvalStr("MyVar")) '// MyVar reflects change made in MyAlias
Console.WriteLine("")
'// This section below shows how you can have Alias distinguish
'// between different variables with the same name
uc.DefineFunction("MyFunc() = MyVar + 1")
'// MyVar defined below is a new variable sharing the same name
'// MyFunc() will still use the value of the original MyVar
Dim MyVarAlt = uc.DefineVariable("MyVar = 100")
'// The function below uses the new MyVar variable
uc.DefineFunction("MyFunc2() = MyVar + 1")
Console.WriteLine(uc.Eval("MyFunc()"))
Console.WriteLine(uc.Eval("MyFunc2()"))
Console.WriteLine("")
uc.CreateAlias("MyAliasAlt", MyVarAlt)
uc.Eval("MyAlias = 200") '// Changes MyVar used in MyFunc()
uc.Eval("MyAliasAlt = 300") '// Changes MyVar used in MyFunc2()
Console.WriteLine(uc.Eval("MyFunc()"))
Console.WriteLine(uc.Eval("MyFunc2()"))
End Sub
End Module
123
456
457
101
201
301 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim MyVar = uc.DefineVariable("MyVar = 123") Dim MyAlias = uc.CreateAlias("MyAlias", MyVar) Console.WriteLine(uc.Eval("MyAlias")) '// Contains same value as MyVar uc.Eval("MyAlias = 456") '// Same as changing MyVar Console.WriteLine(uc.EvalStr("MyVar")) '// MyVar reflects change made in MyAlias Console.WriteLine("") '// This section below shows how you can have Alias distinguish '// between different variables with the same name uc.DefineFunction("MyFunc() = MyVar + 1") '// MyVar defined below is a new variable sharing the same name '// MyFunc() will still use the value of the original MyVar Dim MyVarAlt = uc.DefineVariable("MyVar = 100") '// The function below uses the new MyVar variable uc.DefineFunction("MyFunc2() = MyVar + 1") Console.WriteLine(uc.Eval("MyFunc()")) Console.WriteLine(uc.Eval("MyFunc2()")) Console.WriteLine("") uc.CreateAlias("MyAliasAlt", MyVarAlt) uc.Eval("MyAlias = 200") '// Changes MyVar used in MyFunc() uc.Eval("MyAliasAlt = 300") '// Changes MyVar used in MyFunc2() Console.WriteLine(uc.Eval("MyFunc()")) Console.WriteLine(uc.Eval("MyFunc2()")) End Sub End Module
Alternative parts
ID: 196
See: Alternation
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
t.FromTo("This is a {adjective: simple | small | nice } test",
"The adjective in '{@Self}' is: {adjective}.");
Console.WriteLine(t.Transform("This is a test"));
Console.WriteLine(t.Transform("This is a simple test"));
Console.WriteLine(t.Transform("This is a small test"));
Console.WriteLine(t.Transform("This is a nice test"));
Console.WriteLine(t.Transform("This is a random test"));
This is a test
The adjective in 'This is a simple test' is: simple.
The adjective in 'This is a small test' is: small.
The adjective in 'This is a nice test' is: nice.
This is a random test using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); t.FromTo("This is a {adjective: simple | small | nice } test", "The adjective in '{@Self}' is: {adjective}."); Console.WriteLine(t.Transform("This is a test")); Console.WriteLine(t.Transform("This is a simple test")); Console.WriteLine(t.Transform("This is a small test")); Console.WriteLine(t.Transform("This is a nice test")); Console.WriteLine(t.Transform("This is a random test"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
t.FromTo("This is a {adjective: simple | small | nice } test",
"The adjective in '{@Self}' is: {adjective}.");
cout << t.Transform("This is a test") << endl;
cout << t.Transform("This is a simple test") << endl;
cout << t.Transform("This is a small test") << endl;
cout << t.Transform("This is a nice test") << endl;
cout << t.Transform("This is a random test") << endl;
}
This is a test
The adjective in 'This is a simple test' is: simple.
The adjective in 'This is a small test' is: small.
The adjective in 'This is a nice test' is: nice.
This is a random test #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.FromTo("This is a {adjective: simple | small | nice } test", "The adjective in '{@Self}' is: {adjective}."); cout << t.Transform("This is a test") << endl; cout << t.Transform("This is a simple test") << endl; cout << t.Transform("This is a small test") << endl; cout << t.Transform("This is a nice test") << endl; cout << t.Transform("This is a random test") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
t.FromTo("This is a {adjective: simple | small | nice } test",
"The adjective in '{@Self}' is: {adjective}.")
Console.WriteLine(t.Transform("This is a test"))
Console.WriteLine(t.Transform("This is a simple test"))
Console.WriteLine(t.Transform("This is a small test"))
Console.WriteLine(t.Transform("This is a nice test"))
Console.WriteLine(t.Transform("This is a random test"))
End Sub
End Module
This is a test
The adjective in 'This is a simple test' is: simple.
The adjective in 'This is a small test' is: small.
The adjective in 'This is a nice test' is: nice.
This is a random test Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() t.FromTo("This is a {adjective: simple | small | nice } test", "The adjective in '{@Self}' is: {adjective}.") Console.WriteLine(t.Transform("This is a test")) Console.WriteLine(t.Transform("This is a simple test")) Console.WriteLine(t.Transform("This is a small test")) Console.WriteLine(t.Transform("This is a nice test")) Console.WriteLine(t.Transform("This is a random test")) End Sub End Module
An internal test to confirm that ErrorExpression returns an empty string for an error triggered manually by a user function during evaluation.
ID: 318
using uCalcSoftware;
var uc = new uCalc();
static void MyFunc(uCalc.Callback cb) {
// This error occurs during evaluation, not parsing.
cb.Error.Raise("Manual evaluation failure!");
}
static void MyHandler(Handle_uCalc h) {
var uc = new uCalc(h);
Console.WriteLine($"Handler triggered for error: {uc.Error.Message}");
Console.WriteLine($"ErrorExpression() returned: '{uc.Error.Expression}'");
Console.WriteLine($"Is expression empty? {uc.Error.Expression == ""}");
}
uc.DefineFunction("MyFunc()", MyFunc);
uc.Error.AddHandler(MyHandler);
// The expression 'MyFunc()' itself is valid syntactically.
Console.WriteLine(uc.EvalStr("MyFunc()"));
Handler triggered for error: Manual evaluation failure!
ErrorExpression() returned: ''
Is expression empty? True
Manual evaluation failure! using uCalcSoftware; var uc = new uCalc(); static void MyFunc(uCalc.Callback cb) { // This error occurs during evaluation, not parsing. cb.Error.Raise("Manual evaluation failure!"); } static void MyHandler(Handle_uCalc h) { var uc = new uCalc(h); Console.WriteLine($"Handler triggered for error: {uc.Error.Message}"); Console.WriteLine($"ErrorExpression() returned: '{uc.Error.Expression}'"); Console.WriteLine($"Is expression empty? {uc.Error.Expression == ""}"); } uc.DefineFunction("MyFunc()", MyFunc); uc.Error.AddHandler(MyHandler); // The expression 'MyFunc()' itself is valid syntactically. Console.WriteLine(uc.EvalStr("MyFunc()"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
void ucalc_call MyFunc(uCalcBase::Callback cb) {
// This error occurs during evaluation, not parsing.
cb.Error().Raise("Manual evaluation failure!");
}
void ucalc_call MyHandler(Handle_uCalc h) {
auto uc = uCalc(h);
cout << "Handler triggered for error: " << uc.Error().Message() << endl;
cout << "ErrorExpression() returned: '" << uc.Error().Expression() << "'" << endl;
cout << "Is expression empty? " << tf(uc.Error().Expression() == "") << endl;
}
int main() {
uCalc uc;
uc.DefineFunction("MyFunc()", MyFunc);
uc.Error().AddHandler(MyHandler);
// The expression 'MyFunc()' itself is valid syntactically.
cout << uc.EvalStr("MyFunc()") << endl;
}
Handler triggered for error: Manual evaluation failure!
ErrorExpression() returned: ''
Is expression empty? True
Manual evaluation failure! #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") void ucalc_call MyFunc(uCalcBase::Callback cb) { // This error occurs during evaluation, not parsing. cb.Error().Raise("Manual evaluation failure!"); } void ucalc_call MyHandler(Handle_uCalc h) { auto uc = uCalc(h); cout << "Handler triggered for error: " << uc.Error().Message() << endl; cout << "ErrorExpression() returned: '" << uc.Error().Expression() << "'" << endl; cout << "Is expression empty? " << tf(uc.Error().Expression() == "") << endl; } int main() { uCalc uc; uc.DefineFunction("MyFunc()", MyFunc); uc.Error().AddHandler(MyHandler); // The expression 'MyFunc()' itself is valid syntactically. cout << uc.EvalStr("MyFunc()") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyFunc(ByVal cb As uCalc.Callback)
'// This error occurs during evaluation, not parsing.
cb.Error.Raise("Manual evaluation failure!")
End Sub
Public Sub MyHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
Console.WriteLine($"Handler triggered for error: {uc.Error.Message}")
Console.WriteLine($"ErrorExpression() returned: '{uc.Error.Expression}'")
Console.WriteLine($"Is expression empty? {uc.Error.Expression = ""}")
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("MyFunc()", AddressOf MyFunc)
uc.Error.AddHandler(AddressOf MyHandler)
'// The expression 'MyFunc()' itself is valid syntactically.
Console.WriteLine(uc.EvalStr("MyFunc()"))
End Sub
End Module
Handler triggered for error: Manual evaluation failure!
ErrorExpression() returned: ''
Is expression empty? True
Manual evaluation failure! Imports System Imports uCalcSoftware Public Module Program Public Sub MyFunc(ByVal cb As uCalc.Callback) '// This error occurs during evaluation, not parsing. cb.Error.Raise("Manual evaluation failure!") End Sub Public Sub MyHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) Console.WriteLine($"Handler triggered for error: {uc.Error.Message}") Console.WriteLine($"ErrorExpression() returned: '{uc.Error.Expression}'") Console.WriteLine($"Is expression empty? {uc.Error.Expression = ""}") End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("MyFunc()", AddressOf MyFunc) uc.Error.AddHandler(AddressOf MyHandler) '// The expression 'MyFunc()' itself is valid syntactically. Console.WriteLine(uc.EvalStr("MyFunc()")) End Sub End Module
An internal test verifying that an expression remains tied to its original context, even after the context has been cloned and modified.
ID: 606
See: uCalc = [uCalc]
using uCalcSoftware;
var uc = new uCalc();
// Internal Test: Context integrity after cloning
var baseUc = new uCalc();
baseUc.DefineVariable("x = 100");
// Clone the base instance and modify the variable in the clone
var clonedUc = baseUc.Clone();
clonedUc.Eval("x = 200");
// Parse an expression in the original base context
var baseExpr = baseUc.Parse("x");
// 1. Verify the expression evaluates using its original context's value
Console.WriteLine($"Base expression evaluates to: {baseExpr.Evaluate()}");
// 2. Get the parent and verify it is not the cloned instance
Console.WriteLine($"Parent is not the clone: {baseExpr.uCalc.MemoryIndex != clonedUc.MemoryIndex}");
Base expression evaluates to: 100
Parent is not the clone: True using uCalcSoftware; var uc = new uCalc(); // Internal Test: Context integrity after cloning var baseUc = new uCalc(); baseUc.DefineVariable("x = 100"); // Clone the base instance and modify the variable in the clone var clonedUc = baseUc.Clone(); clonedUc.Eval("x = 200"); // Parse an expression in the original base context var baseExpr = baseUc.Parse("x"); // 1. Verify the expression evaluates using its original context's value Console.WriteLine($"Base expression evaluates to: {baseExpr.Evaluate()}"); // 2. Get the parent and verify it is not the cloned instance Console.WriteLine($"Parent is not the clone: {baseExpr.uCalc.MemoryIndex != clonedUc.MemoryIndex}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
int main() {
uCalc uc;
// Internal Test: Context integrity after cloning
uCalc baseUc;
baseUc.DefineVariable("x = 100");
// Clone the base instance and modify the variable in the clone
auto clonedUc = baseUc.Clone();
clonedUc.Eval("x = 200");
// Parse an expression in the original base context
auto baseExpr = baseUc.Parse("x");
// 1. Verify the expression evaluates using its original context's value
cout << "Base expression evaluates to: " << baseExpr.Evaluate() << endl;
// 2. Get the parent and verify it is not the cloned instance
cout << "Parent is not the clone: " << tf(baseExpr.uCalc().MemoryIndex() != clonedUc.MemoryIndex()) << endl;
}
Base expression evaluates to: 100
Parent is not the clone: True #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") int main() { uCalc uc; // Internal Test: Context integrity after cloning uCalc baseUc; baseUc.DefineVariable("x = 100"); // Clone the base instance and modify the variable in the clone auto clonedUc = baseUc.Clone(); clonedUc.Eval("x = 200"); // Parse an expression in the original base context auto baseExpr = baseUc.Parse("x"); // 1. Verify the expression evaluates using its original context's value cout << "Base expression evaluates to: " << baseExpr.Evaluate() << endl; // 2. Get the parent and verify it is not the cloned instance cout << "Parent is not the clone: " << tf(baseExpr.uCalc().MemoryIndex() != clonedUc.MemoryIndex()) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Internal Test: Context integrity after cloning
Dim baseUc As New uCalc()
baseUc.DefineVariable("x = 100")
'// Clone the base instance and modify the variable in the clone
Dim clonedUc = baseUc.Clone()
clonedUc.Eval("x = 200")
'// Parse an expression in the original base context
Dim baseExpr = baseUc.Parse("x")
'// 1. Verify the expression evaluates using its original context's value
Console.WriteLine($"Base expression evaluates to: {baseExpr.Evaluate()}")
'// 2. Get the parent and verify it is not the cloned instance
Console.WriteLine($"Parent is not the clone: {baseExpr.uCalc.MemoryIndex <> clonedUc.MemoryIndex}")
End Sub
End Module
Base expression evaluates to: 100
Parent is not the clone: True Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Internal Test: Context integrity after cloning Dim baseUc As New uCalc() baseUc.DefineVariable("x = 100") '// Clone the base instance and modify the variable in the clone Dim clonedUc = baseUc.Clone() clonedUc.Eval("x = 200") '// Parse an expression in the original base context Dim baseExpr = baseUc.Parse("x") '// 1. Verify the expression evaluates using its original context's value Console.WriteLine($"Base expression evaluates to: {baseExpr.Evaluate()}") '// 2. Get the parent and verify it is not the cloned instance Console.WriteLine($"Parent is not the clone: {baseExpr.uCalc.MemoryIndex <> clonedUc.MemoryIndex}") End Sub End Module
Applies `RewindOnChange` to a default rule set to enable complex, multi-rule transformations for a custom `Average` function.
ID: 981
using uCalcSoftware;
var uc = new uCalc();
var t = uc.ExpressionTransformer;
// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet.SetRewindOnChange(true);
// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}");
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))");
t.FromTo("ArgCount({x})", "1");
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))");
// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})");
var expression = "Average(1, 2, 3, 4)";
Console.WriteLine($"Input: {expression}");
Console.WriteLine($"Transform: {t.Transform(expression)}");
Console.WriteLine($"Eval: {uc.Eval(expression)}");
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 using uCalcSoftware; var uc = new uCalc(); var t = uc.ExpressionTransformer; // Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet.SetRewindOnChange(true); // Define the recursive rules. t.FromTo("AddUp({x})", "{x}"); t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))"); t.FromTo("ArgCount({x})", "1"); t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))"); // The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})"); var expression = "Average(1, 2, 3, 4)"; Console.WriteLine($"Input: {expression}"); Console.WriteLine($"Transform: {t.Transform(expression)}"); Console.WriteLine($"Eval: {uc.Eval(expression)}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.ExpressionTransformer();
// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet().SetRewindOnChange(true);
// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}");
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))");
t.FromTo("ArgCount({x})", "1");
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))");
// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})");
auto expression = "Average(1, 2, 3, 4)";
cout << "Input: " << expression << endl;
cout << "Transform: " << t.Transform(expression) << endl;
cout << "Eval: " << uc.Eval(expression) << endl;
}
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.ExpressionTransformer(); // Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet().SetRewindOnChange(true); // Define the recursive rules. t.FromTo("AddUp({x})", "{x}"); t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))"); t.FromTo("ArgCount({x})", "1"); t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))"); // The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})"); auto expression = "Average(1, 2, 3, 4)"; cout << "Input: " << expression << endl; cout << "Transform: " << t.Transform(expression) << endl; cout << "Eval: " << uc.Eval(expression) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.ExpressionTransformer
'// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet.SetRewindOnChange(true)
'// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}")
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))")
t.FromTo("ArgCount({x})", "1")
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))")
'// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})")
Dim expression = "Average(1, 2, 3, 4)"
Console.WriteLine($"Input: {expression}")
Console.WriteLine($"Transform: {t.Transform(expression)}")
Console.WriteLine($"Eval: {uc.Eval(expression)}")
End Sub
End Module
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.ExpressionTransformer '// Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet.SetRewindOnChange(true) '// Define the recursive rules. t.FromTo("AddUp({x})", "{x}") t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))") t.FromTo("ArgCount({x})", "1") t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))") '// The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})") Dim expression = "Average(1, 2, 3, 4)" Console.WriteLine($"Input: {expression}") Console.WriteLine($"Transform: {t.Transform(expression)}") Console.WriteLine($"Eval: {uc.Eval(expression)}") End Sub End Module
ArgDbl (same as Arg)
ID: 84
See: ArgDbl
using uCalcSoftware;
var uc = new uCalc();
static void MyArea(uCalc.Callback cb) {
var Length = cb.ArgDbl(1); // Same as cb.Arg(1);
var Width = cb.ArgDbl(2); // Same as cb.Arg(2);
cb.Return(Length * Width);
}
uc.DefineFunction("Area(x, y)", MyArea);
Console.WriteLine(uc.Eval("Area(3, 4)"));
12 using uCalcSoftware; var uc = new uCalc(); static void MyArea(uCalc.Callback cb) { var Length = cb.ArgDbl(1); // Same as cb.Arg(1); var Width = cb.ArgDbl(2); // Same as cb.Arg(2); cb.Return(Length * Width); } uc.DefineFunction("Area(x, y)", MyArea); Console.WriteLine(uc.Eval("Area(3, 4)"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyArea(uCalcBase::Callback cb) {
auto Length = cb.ArgDbl(1); // Same as cb.Arg(1);
auto Width = cb.ArgDbl(2); // Same as cb.Arg(2);
cb.Return(Length * Width);
}
int main() {
uCalc uc;
uc.DefineFunction("Area(x, y)", MyArea);
cout << uc.Eval("Area(3, 4)") << endl;
}
12 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyArea(uCalcBase::Callback cb) { auto Length = cb.ArgDbl(1); // Same as cb.Arg(1); auto Width = cb.ArgDbl(2); // Same as cb.Arg(2); cb.Return(Length * Width); } int main() { uCalc uc; uc.DefineFunction("Area(x, y)", MyArea); cout << uc.Eval("Area(3, 4)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyArea(ByVal cb As uCalc.Callback)
Dim Length = cb.ArgDbl(1) '// Same as cb.Arg(1);
Dim Width = cb.ArgDbl(2) '// Same as cb.Arg(2);
cb.Return(Length * Width)
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("Area(x, y)", AddressOf MyArea)
Console.WriteLine(uc.Eval("Area(3, 4)"))
End Sub
End Module
12 Imports System Imports uCalcSoftware Public Module Program Public Sub MyArea(ByVal cb As uCalc.Callback) Dim Length = cb.ArgDbl(1) '// Same as cb.Arg(1); Dim Width = cb.ArgDbl(2) '// Same as cb.Arg(2); cb.Return(Length * Width) End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("Area(x, y)", AddressOf MyArea) Console.WriteLine(uc.Eval("Area(3, 4)")) End Sub End Module
Arrays with DefineVariable
ID: 67
See: DefineVariable
using uCalcSoftware;
var uc = new uCalc();
var MyArray = uc.DefineVariable("MyArray[3]");
uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}");
uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333");
Console.WriteLine(uc.EvalStr("MyArray[0]"));
Console.WriteLine(uc.EvalStr("MyArray[1]"));
Console.WriteLine(uc.EvalStr("MyArray[2]"));
Console.WriteLine(uc.EvalStr("MyArrayStr[0]"));
Console.WriteLine(uc.EvalStr("MyArrayStr[1]"));
Console.WriteLine(uc.EvalStr("MyArrayStr[2]"));
Console.WriteLine(MyArray.Count);
Console.WriteLine(uc.ItemOf("MyArrayStr").Count);
111
222
333
aa
bb
cc
3
3 using uCalcSoftware; var uc = new uCalc(); var MyArray = uc.DefineVariable("MyArray[3]"); uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}"); uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333"); Console.WriteLine(uc.EvalStr("MyArray[0]")); Console.WriteLine(uc.EvalStr("MyArray[1]")); Console.WriteLine(uc.EvalStr("MyArray[2]")); Console.WriteLine(uc.EvalStr("MyArrayStr[0]")); Console.WriteLine(uc.EvalStr("MyArrayStr[1]")); Console.WriteLine(uc.EvalStr("MyArrayStr[2]")); Console.WriteLine(MyArray.Count); Console.WriteLine(uc.ItemOf("MyArrayStr").Count);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto MyArray = uc.DefineVariable("MyArray[3]");
uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}");
uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333");
cout << uc.EvalStr("MyArray[0]") << endl;
cout << uc.EvalStr("MyArray[1]") << endl;
cout << uc.EvalStr("MyArray[2]") << endl;
cout << uc.EvalStr("MyArrayStr[0]") << endl;
cout << uc.EvalStr("MyArrayStr[1]") << endl;
cout << uc.EvalStr("MyArrayStr[2]") << endl;
cout << MyArray.Count() << endl;
cout << uc.ItemOf("MyArrayStr").Count() << endl;
}
111
222
333
aa
bb
cc
3
3 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto MyArray = uc.DefineVariable("MyArray[3]"); uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}"); uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333"); cout << uc.EvalStr("MyArray[0]") << endl; cout << uc.EvalStr("MyArray[1]") << endl; cout << uc.EvalStr("MyArray[2]") << endl; cout << uc.EvalStr("MyArrayStr[0]") << endl; cout << uc.EvalStr("MyArrayStr[1]") << endl; cout << uc.EvalStr("MyArrayStr[2]") << endl; cout << MyArray.Count() << endl; cout << uc.ItemOf("MyArrayStr").Count() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim MyArray = uc.DefineVariable("MyArray[3]")
uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}")
uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333")
Console.WriteLine(uc.EvalStr("MyArray[0]"))
Console.WriteLine(uc.EvalStr("MyArray[1]"))
Console.WriteLine(uc.EvalStr("MyArray[2]"))
Console.WriteLine(uc.EvalStr("MyArrayStr[0]"))
Console.WriteLine(uc.EvalStr("MyArrayStr[1]"))
Console.WriteLine(uc.EvalStr("MyArrayStr[2]"))
Console.WriteLine(MyArray.Count)
Console.WriteLine(uc.ItemOf("MyArrayStr").Count)
End Sub
End Module
111
222
333
aa
bb
cc
3
3 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim MyArray = uc.DefineVariable("MyArray[3]") uc.DefineVariable("MyArrayStr[] = {'aa', 'bb', 'cc'}") uc.Eval("MyArray[0] = 111; MyArray[1] = 222; MyArray[2] = 333") Console.WriteLine(uc.EvalStr("MyArray[0]")) Console.WriteLine(uc.EvalStr("MyArray[1]")) Console.WriteLine(uc.EvalStr("MyArray[2]")) Console.WriteLine(uc.EvalStr("MyArrayStr[0]")) Console.WriteLine(uc.EvalStr("MyArrayStr[1]")) Console.WriteLine(uc.EvalStr("MyArrayStr[2]")) Console.WriteLine(MyArray.Count) Console.WriteLine(uc.ItemOf("MyArrayStr").Count) End Sub End Module
Assigns descriptive text to multiple rules and uses the `Rule` property to identify which rule generated each match.
ID: 827
See: Introduction, Rule = [Rule]
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
t.Text = "Title
Bold statementTitle B
Other textMy paragraph
";
var AnyOtherTag = t.Pattern("<{tag}>{text}{tag}>").SetDescription("other kind of tag");
var BoldTag = t.Pattern("{text}").SetDescription("bold tag");
var H3Tag = t.Pattern("{text}
").SetDescription("h3 tag");
t.Find();
foreach(var match in t.Matches) {
Console.WriteLine(match.Text + " Description: " + match.Rule.Description);
}
<h3>Title</h3> Description: h3 tag
<b>Bold statement</b> Description: bold tag
<h3>Title B</h3> Description: h3 tag
<b>Other text</b> Description: bold tag
<p>My paragraph</p> Description: other kind of tag using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); t.Text = "<h3>Title</h3><b>Bold statement</b><h3>Title B</h3><b>Other text</b><p>My paragraph</p>"; var AnyOtherTag = t.Pattern("<{tag}>{text}</{tag}>").SetDescription("other kind of tag"); var BoldTag = t.Pattern("<b>{text}</b>").SetDescription("bold tag"); var H3Tag = t.Pattern("<h3>{text}</h3>").SetDescription("h3 tag"); t.Find(); foreach(var match in t.Matches) { Console.WriteLine(match.Text + " Description: " + match.Rule.Description); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
t.Text("Title
Bold statementTitle B
Other textMy paragraph
");
auto AnyOtherTag = t.Pattern("<{tag}>{text}{tag}>").SetDescription("other kind of tag");
auto BoldTag = t.Pattern("{text}").SetDescription("bold tag");
auto H3Tag = t.Pattern("{text}
").SetDescription("h3 tag");
t.Find();
for(auto match : t.Matches()) {
cout << match.Text() + " Description: " + match.Rule().Description() << endl;
}
}
<h3>Title</h3> Description: h3 tag
<b>Bold statement</b> Description: bold tag
<h3>Title B</h3> Description: h3 tag
<b>Other text</b> Description: bold tag
<p>My paragraph</p> Description: other kind of tag #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.Text("<h3>Title</h3><b>Bold statement</b><h3>Title B</h3><b>Other text</b><p>My paragraph</p>"); auto AnyOtherTag = t.Pattern("<{tag}>{text}</{tag}>").SetDescription("other kind of tag"); auto BoldTag = t.Pattern("<b>{text}</b>").SetDescription("bold tag"); auto H3Tag = t.Pattern("<h3>{text}</h3>").SetDescription("h3 tag"); t.Find(); for(auto match : t.Matches()) { cout << match.Text() + " Description: " + match.Rule().Description() << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
t.Text = "Title
Bold statementTitle B
Other textMy paragraph
"
Dim AnyOtherTag = t.Pattern("<{tag}>{text}{tag}>").SetDescription("other kind of tag")
Dim BoldTag = t.Pattern("{text}").SetDescription("bold tag")
Dim H3Tag = t.Pattern("{text}
").SetDescription("h3 tag")
t.Find()
For Each match In t.Matches
Console.WriteLine(match.Text + " Description: " + match.Rule.Description)
Next
End Sub
End Module
<h3>Title</h3> Description: h3 tag
<b>Bold statement</b> Description: bold tag
<h3>Title B</h3> Description: h3 tag
<b>Other text</b> Description: bold tag
<p>My paragraph</p> Description: other kind of tag Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() t.Text = "<h3>Title</h3><b>Bold statement</b><h3>Title B</h3><b>Other text</b><p>My paragraph</p>" Dim AnyOtherTag = t.Pattern("<{tag}>{text}</{tag}>").SetDescription("other kind of tag") Dim BoldTag = t.Pattern("<b>{text}</b>").SetDescription("bold tag") Dim H3Tag = t.Pattern("<h3>{text}</h3>").SetDescription("h3 tag") t.Find() For Each match In t.Matches Console.WriteLine(match.Text + " Description: " + match.Rule.Description) Next End Sub End Module
Attaches and retrieves a simple metadata description from a variable.
ID: 622
using uCalcSoftware;
var uc = new uCalc();
var myVar = uc.DefineVariable("x = 10");
myVar.Description = "Represents the user's current score.";
Console.WriteLine($"Variable Name: {myVar.Name}");
Console.WriteLine($"Description: {myVar.Description}");
Variable Name: x
Description: Represents the user's current score. using uCalcSoftware; var uc = new uCalc(); var myVar = uc.DefineVariable("x = 10"); myVar.Description = "Represents the user's current score."; Console.WriteLine($"Variable Name: {myVar.Name}"); Console.WriteLine($"Description: {myVar.Description}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto myVar = uc.DefineVariable("x = 10");
myVar.Description("Represents the user's current score.");
cout << "Variable Name: " << myVar.Name() << endl;
cout << "Description: " << myVar.Description() << endl;
}
Variable Name: x
Description: Represents the user's current score. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto myVar = uc.DefineVariable("x = 10"); myVar.Description("Represents the user's current score."); cout << "Variable Name: " << myVar.Name() << endl; cout << "Description: " << myVar.Description() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim myVar = uc.DefineVariable("x = 10")
myVar.Description = "Represents the user's current score."
Console.WriteLine($"Variable Name: {myVar.Name}")
Console.WriteLine($"Description: {myVar.Description}")
End Sub
End Module
Variable Name: x
Description: Represents the user's current score. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim myVar = uc.DefineVariable("x = 10") myVar.Description = "Represents the user's current score." Console.WriteLine($"Variable Name: {myVar.Name}") Console.WriteLine($"Description: {myVar.Description}") End Sub End Module
Automatic recalculation when a source cell is changed.
ID: 1379
using uCalcSoftware;
var uc = new uCalc();
// Define cells A1 and B1, where B1 depends on A1
uc.Define("Overwrite ~~ Function: A1() = 10");
uc.Define("Overwrite ~~ Function: B1() = A1() * 5");
Console.WriteLine($"Initial B1 value: {uc.Eval("B1()")}"); // Expected: 50
// Now, change the value of A1. B1 will update automatically.
uc.Define("Overwrite ~~ Function: A1() = 20");
Console.WriteLine($"Updated B1 value: {uc.Eval("B1()")}"); // Expected: 100
Initial B1 value: 50
Updated B1 value: 100 using uCalcSoftware; var uc = new uCalc(); // Define cells A1 and B1, where B1 depends on A1 uc.Define("Overwrite ~~ Function: A1() = 10"); uc.Define("Overwrite ~~ Function: B1() = A1() * 5"); Console.WriteLine($"Initial B1 value: {uc.Eval("B1()")}"); // Expected: 50 // Now, change the value of A1. B1 will update automatically. uc.Define("Overwrite ~~ Function: A1() = 20"); Console.WriteLine($"Updated B1 value: {uc.Eval("B1()")}"); // Expected: 100
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Define cells A1 and B1, where B1 depends on A1
uc.Define("Overwrite ~~ Function: A1() = 10");
uc.Define("Overwrite ~~ Function: B1() = A1() * 5");
cout << "Initial B1 value: " << uc.Eval("B1()") << endl; // Expected: 50
// Now, change the value of A1. B1 will update automatically.
uc.Define("Overwrite ~~ Function: A1() = 20");
cout << "Updated B1 value: " << uc.Eval("B1()") << endl; // Expected: 100
}
Initial B1 value: 50
Updated B1 value: 100 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Define cells A1 and B1, where B1 depends on A1 uc.Define("Overwrite ~~ Function: A1() = 10"); uc.Define("Overwrite ~~ Function: B1() = A1() * 5"); cout << "Initial B1 value: " << uc.Eval("B1()") << endl; // Expected: 50 // Now, change the value of A1. B1 will update automatically. uc.Define("Overwrite ~~ Function: A1() = 20"); cout << "Updated B1 value: " << uc.Eval("B1()") << endl; // Expected: 100 }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Define cells A1 and B1, where B1 depends on A1
uc.Define("Overwrite ~~ Function: A1() = 10")
uc.Define("Overwrite ~~ Function: B1() = A1() * 5")
Console.WriteLine($"Initial B1 value: {uc.Eval("B1()")}") '// Expected: 50
'// Now, change the value of A1. B1 will update automatically.
uc.Define("Overwrite ~~ Function: A1() = 20")
Console.WriteLine($"Updated B1 value: {uc.Eval("B1()")}") '// Expected: 100
End Sub
End Module
Initial B1 value: 50
Updated B1 value: 100 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Define cells A1 and B1, where B1 depends on A1 uc.Define("Overwrite ~~ Function: A1() = 10") uc.Define("Overwrite ~~ Function: B1() = A1() * 5") Console.WriteLine($"Initial B1 value: {uc.Eval("B1()")}") '// Expected: 50 '// Now, change the value of A1. B1 will update automatically. uc.Define("Overwrite ~~ Function: A1() = 20") Console.WriteLine($"Updated B1 value: {uc.Eval("B1()")}") '// Expected: 100 End Sub End Module
Basic data cleaning by trimming whitespace and changing the case of a single key-value pair.
ID: 1370
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
// Rule to find 'status', capture its value, trim whitespace, and convert to uppercase.
t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}");
var input = "status= active ";
Console.WriteLine(t.Transform(input));
Status: ACTIVE using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); // Rule to find 'status', capture its value, trim whitespace, and convert to uppercase. t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}"); var input = "status= active "; Console.WriteLine(t.Transform(input));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
// Rule to find 'status', capture its value, trim whitespace, and convert to uppercase.
t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}");
auto input = "status= active ";
cout << t.Transform(input) << endl;
}
Status: ACTIVE #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); // Rule to find 'status', capture its value, trim whitespace, and convert to uppercase. t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}"); auto input = "status= active "; cout << t.Transform(input) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
'// Rule to find 'status', capture its value, trim whitespace, and convert to uppercase.
t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}")
Dim input = "status= active "
Console.WriteLine(t.Transform(input))
End Sub
End Module
Status: ACTIVE Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() '// Rule to find 'status', capture its value, trim whitespace, and convert to uppercase. t.FromTo("status = {val}", "Status: {@Eval: UCase(val)}") Dim input = "status= active " Console.WriteLine(t.Transform(input)) End Sub End Module
Basic distinction between a root rule and a child rule.
ID: 937
See: IsChildRule = [bool]
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
var rootRule = t.Pattern("root");
var localTransformer = rootRule.LocalTransformer;
var childRule = localTransformer.Pattern("child");
Console.WriteLine($"Is 'rootRule' a child? {rootRule.IsChildRule}");
Console.WriteLine($"Is 'childRule' a child? {childRule.IsChildRule}");
Is 'rootRule' a child? False
Is 'childRule' a child? True using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); var rootRule = t.Pattern("root"); var localTransformer = rootRule.LocalTransformer; var childRule = localTransformer.Pattern("child"); Console.WriteLine($"Is 'rootRule' a child? {rootRule.IsChildRule}"); Console.WriteLine($"Is 'childRule' a child? {childRule.IsChildRule}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
int main() {
uCalc uc;
uCalc::Transformer t;
auto rootRule = t.Pattern("root");
auto localTransformer = rootRule.LocalTransformer();
auto childRule = localTransformer.Pattern("child");
cout << "Is 'rootRule' a child? " << tf(rootRule.IsChildRule()) << endl;
cout << "Is 'childRule' a child? " << tf(childRule.IsChildRule()) << endl;
}
Is 'rootRule' a child? False
Is 'childRule' a child? True #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") int main() { uCalc uc; uCalc::Transformer t; auto rootRule = t.Pattern("root"); auto localTransformer = rootRule.LocalTransformer(); auto childRule = localTransformer.Pattern("child"); cout << "Is 'rootRule' a child? " << tf(rootRule.IsChildRule()) << endl; cout << "Is 'childRule' a child? " << tf(childRule.IsChildRule()) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
Dim rootRule = t.Pattern("root")
Dim localTransformer = rootRule.LocalTransformer
Dim childRule = localTransformer.Pattern("child")
Console.WriteLine($"Is 'rootRule' a child? {rootRule.IsChildRule}")
Console.WriteLine($"Is 'childRule' a child? {childRule.IsChildRule}")
End Sub
End Module
Is 'rootRule' a child? False
Is 'childRule' a child? True Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() Dim rootRule = t.Pattern("root") Dim localTransformer = rootRule.LocalTransformer Dim childRule = localTransformer.Pattern("child") Console.WriteLine($"Is 'rootRule' a child? {rootRule.IsChildRule}") Console.WriteLine($"Is 'childRule' a child? {childRule.IsChildRule}") End Sub End Module
Basic forward and turn commands (LOGO).
ID: 1409
using uCalcSoftware;
var uc = new uCalc();
static void MoveTurtle(uCalc.Callback cb) {
var dist = cb.Arg(1);
var uc_inst = cb.uCalc;
var x = uc_inst.ItemOf("x").Value();
var y = uc_inst.ItemOf("y").Value();
var angle = uc_inst.ItemOf("angle").Value();
var pen_down = uc_inst.ItemOf("pen_down").ValueBool();
int new_x = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")"));
int new_y = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")"));
if (pen_down) {
Console.WriteLine($"Drawing line to ({new_x},{new_y})");
} else {
Console.WriteLine($"Moving to ({new_x},{new_y})");
}
uc_inst.ItemOf("x").Value(new_x);
uc_inst.ItemOf("y").Value(new_y);
}
// --- Setup ---
uc.DefineVariable("x = 0.0");
uc.DefineVariable("y = 0.0");
uc.DefineVariable("angle = 90.0");
uc.DefineVariable("pen_down = false");
uc.DefineConstant("PI = 3.1415926535");
uc.DefineFunction("CosD(a) = Cos(a * PI / 180)");
uc.DefineFunction("SinD(a) = Sin(a * PI / 180)");
uc.DefineFunction("Move(dist)", MoveTurtle);
var t = uc.ExpressionTransformer;
t.FromTo("FD {@Number:dist}", "Move({dist})");
t.FromTo("RT {@Number:deg}", "angle = angle - {deg}");
t.FromTo("PD", "pen_down = true");
// --- Script ---
var script = """
PD
FD 100
RT 90
FD 50
""";
uc.EvalStr(script);
Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})");
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100) using uCalcSoftware; var uc = new uCalc(); static void MoveTurtle(uCalc.Callback cb) { var dist = cb.Arg(1); var uc_inst = cb.uCalc; var x = uc_inst.ItemOf("x").Value(); var y = uc_inst.ItemOf("y").Value(); var angle = uc_inst.ItemOf("angle").Value(); var pen_down = uc_inst.ItemOf("pen_down").ValueBool(); int new_x = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")")); int new_y = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")")); if (pen_down) { Console.WriteLine($"Drawing line to ({new_x},{new_y})"); } else { Console.WriteLine($"Moving to ({new_x},{new_y})"); } uc_inst.ItemOf("x").Value(new_x); uc_inst.ItemOf("y").Value(new_y); } // --- Setup --- uc.DefineVariable("x = 0.0"); uc.DefineVariable("y = 0.0"); uc.DefineVariable("angle = 90.0"); uc.DefineVariable("pen_down = false"); uc.DefineConstant("PI = 3.1415926535"); uc.DefineFunction("CosD(a) = Cos(a * PI / 180)"); uc.DefineFunction("SinD(a) = Sin(a * PI / 180)"); uc.DefineFunction("Move(dist)", MoveTurtle); var t = uc.ExpressionTransformer; t.FromTo("FD {@Number:dist}", "Move({dist})"); t.FromTo("RT {@Number:deg}", "angle = angle - {deg}"); t.FromTo("PD", "pen_down = true"); // --- Script --- var script = """ PD FD 100 RT 90 FD 50 """; uc.EvalStr(script); Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MoveTurtle(uCalcBase::Callback cb) {
auto dist = cb.Arg(1);
auto uc_inst = cb.uCalc();
auto x = uc_inst.ItemOf("x").Value();
auto y = uc_inst.ItemOf("y").Value();
auto angle = uc_inst.ItemOf("angle").Value();
auto pen_down = uc_inst.ItemOf("pen_down").ValueBool();
int new_x = x + dist * uc_inst.Eval("CosD(" + to_string(angle) + ")");
int new_y = y + dist * uc_inst.Eval("SinD(" + to_string(angle) + ")");
if (pen_down) {
cout << "Drawing line to (" << new_x << "," << new_y << ")" << endl;
} else {
cout << "Moving to (" << new_x << "," << new_y << ")" << endl;
}
uc_inst.ItemOf("x").Value(new_x);
uc_inst.ItemOf("y").Value(new_y);
}
int main() {
uCalc uc;
// --- Setup ---
uc.DefineVariable("x = 0.0");
uc.DefineVariable("y = 0.0");
uc.DefineVariable("angle = 90.0");
uc.DefineVariable("pen_down = false");
uc.DefineConstant("PI = 3.1415926535");
uc.DefineFunction("CosD(a) = Cos(a * PI / 180)");
uc.DefineFunction("SinD(a) = Sin(a * PI / 180)");
uc.DefineFunction("Move(dist)", MoveTurtle);
auto t = uc.ExpressionTransformer();
t.FromTo("FD {@Number:dist}", "Move({dist})");
t.FromTo("RT {@Number:deg}", "angle = angle - {deg}");
t.FromTo("PD", "pen_down = true");
// --- Script ---
auto script = R"(
PD
FD 100
RT 90
FD 50
)";
uc.EvalStr(script);
cout << "Final Position: (" << uc.Eval("x") << ", " << uc.Eval("y") << ")" << endl;
}
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100) #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MoveTurtle(uCalcBase::Callback cb) { auto dist = cb.Arg(1); auto uc_inst = cb.uCalc(); auto x = uc_inst.ItemOf("x").Value(); auto y = uc_inst.ItemOf("y").Value(); auto angle = uc_inst.ItemOf("angle").Value(); auto pen_down = uc_inst.ItemOf("pen_down").ValueBool(); int new_x = x + dist * uc_inst.Eval("CosD(" + to_string(angle) + ")"); int new_y = y + dist * uc_inst.Eval("SinD(" + to_string(angle) + ")"); if (pen_down) { cout << "Drawing line to (" << new_x << "," << new_y << ")" << endl; } else { cout << "Moving to (" << new_x << "," << new_y << ")" << endl; } uc_inst.ItemOf("x").Value(new_x); uc_inst.ItemOf("y").Value(new_y); } int main() { uCalc uc; // --- Setup --- uc.DefineVariable("x = 0.0"); uc.DefineVariable("y = 0.0"); uc.DefineVariable("angle = 90.0"); uc.DefineVariable("pen_down = false"); uc.DefineConstant("PI = 3.1415926535"); uc.DefineFunction("CosD(a) = Cos(a * PI / 180)"); uc.DefineFunction("SinD(a) = Sin(a * PI / 180)"); uc.DefineFunction("Move(dist)", MoveTurtle); auto t = uc.ExpressionTransformer(); t.FromTo("FD {@Number:dist}", "Move({dist})"); t.FromTo("RT {@Number:deg}", "angle = angle - {deg}"); t.FromTo("PD", "pen_down = true"); // --- Script --- auto script = R"( PD FD 100 RT 90 FD 50 )"; uc.EvalStr(script); cout << "Final Position: (" << uc.Eval("x") << ", " << uc.Eval("y") << ")" << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MoveTurtle(ByVal cb As uCalc.Callback)
Dim dist = cb.Arg(1)
Dim uc_inst = cb.uCalc
Dim x = uc_inst.ItemOf("x").Value()
Dim y = uc_inst.ItemOf("y").Value()
Dim angle = uc_inst.ItemOf("angle").Value()
Dim pen_down = uc_inst.ItemOf("pen_down").ValueBool()
Dim new_x As Integer = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")"))
Dim new_y As Integer = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")"))
If pen_down Then
Console.WriteLine($"Drawing line to ({new_x},{new_y})")
Else
Console.WriteLine($"Moving to ({new_x},{new_y})")
End If
uc_inst.ItemOf("x").Value(new_x)
uc_inst.ItemOf("y").Value(new_y)
End Sub
Public Sub Main()
Dim uc As New uCalc()
'// --- Setup ---
uc.DefineVariable("x = 0.0")
uc.DefineVariable("y = 0.0")
uc.DefineVariable("angle = 90.0")
uc.DefineVariable("pen_down = false")
uc.DefineConstant("PI = 3.1415926535")
uc.DefineFunction("CosD(a) = Cos(a * PI / 180)")
uc.DefineFunction("SinD(a) = Sin(a * PI / 180)")
uc.DefineFunction("Move(dist)", AddressOf MoveTurtle)
Dim t = uc.ExpressionTransformer
t.FromTo("FD {@Number:dist}", "Move({dist})")
t.FromTo("RT {@Number:deg}", "angle = angle - {deg}")
t.FromTo("PD", "pen_down = true")
'// --- Script ---
Dim script = "
PD
FD 100
RT 90
FD 50
"
uc.EvalStr(script)
Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})")
End Sub
End Module
Drawing line to (0,100)
Drawing line to (50,100)
Final Position: (50, 100) Imports System Imports uCalcSoftware Public Module Program Public Sub MoveTurtle(ByVal cb As uCalc.Callback) Dim dist = cb.Arg(1) Dim uc_inst = cb.uCalc Dim x = uc_inst.ItemOf("x").Value() Dim y = uc_inst.ItemOf("y").Value() Dim angle = uc_inst.ItemOf("angle").Value() Dim pen_down = uc_inst.ItemOf("pen_down").ValueBool() Dim new_x As Integer = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")")) Dim new_y As Integer = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")")) If pen_down Then Console.WriteLine($"Drawing line to ({new_x},{new_y})") Else Console.WriteLine($"Moving to ({new_x},{new_y})") End If uc_inst.ItemOf("x").Value(new_x) uc_inst.ItemOf("y").Value(new_y) End Sub Public Sub Main() Dim uc As New uCalc() '// --- Setup --- uc.DefineVariable("x = 0.0") uc.DefineVariable("y = 0.0") uc.DefineVariable("angle = 90.0") uc.DefineVariable("pen_down = false") uc.DefineConstant("PI = 3.1415926535") uc.DefineFunction("CosD(a) = Cos(a * PI / 180)") uc.DefineFunction("SinD(a) = Sin(a * PI / 180)") uc.DefineFunction("Move(dist)", AddressOf MoveTurtle) Dim t = uc.ExpressionTransformer t.FromTo("FD {@Number:dist}", "Move({dist})") t.FromTo("RT {@Number:deg}", "angle = angle - {deg}") t.FromTo("PD", "pen_down = true") '// --- Script --- Dim script = " PD FD 100 RT 90 FD 50 " uc.EvalStr(script) Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})") End Sub End Module
Basic Key-Value extraction using a colon as an anchor.
ID: 230
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
// "ID" and ":" are Anchors. "{id}" is the Variable.
t.FromTo("ID: {id}", "Found ID: {id}");
Console.WriteLine(t.Transform("ID: 12345").Text);
Found ID: 12345 using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); // "ID" and ":" are Anchors. "{id}" is the Variable. t.FromTo("ID: {id}", "Found ID: {id}"); Console.WriteLine(t.Transform("ID: 12345").Text);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
// "ID" and ":" are Anchors. "{id}" is the Variable.
t.FromTo("ID: {id}", "Found ID: {id}");
cout << t.Transform("ID: 12345").Text() << endl;
}
Found ID: 12345 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); // "ID" and ":" are Anchors. "{id}" is the Variable. t.FromTo("ID: {id}", "Found ID: {id}"); cout << t.Transform("ID: 12345").Text() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
'// "ID" and ":" are Anchors. "{id}" is the Variable.
t.FromTo("ID: {id}", "Found ID: {id}")
Console.WriteLine(t.Transform("ID: 12345").Text)
End Sub
End Module
Found ID: 12345 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() '// "ID" and ":" are Anchors. "{id}" is the Variable. t.FromTo("ID: {id}", "Found ID: {id}") Console.WriteLine(t.Transform("ID: 12345").Text) End Sub End Module
Basic round-trip from an item back to its parent instance.
ID: 680
See: uCalc = [uCalc]
using uCalcSoftware;
var uc = new uCalc();
var myInstance = new uCalc();
// Define a variable and get its Item object
var v = myInstance.DefineVariable("v = 10");
// Use the item to get back to its parent uCalc instance
var parentInstance = v.uCalc;
// Perform another evaluation in the same context
Console.WriteLine(parentInstance.EvalStr("v + 5"));
15 using uCalcSoftware; var uc = new uCalc(); var myInstance = new uCalc(); // Define a variable and get its Item object var v = myInstance.DefineVariable("v = 10"); // Use the item to get back to its parent uCalc instance var parentInstance = v.uCalc; // Perform another evaluation in the same context Console.WriteLine(parentInstance.EvalStr("v + 5"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc myInstance;
// Define a variable and get its Item object
auto v = myInstance.DefineVariable("v = 10");
// Use the item to get back to its parent uCalc instance
auto parentInstance = v.uCalc();
// Perform another evaluation in the same context
cout << parentInstance.EvalStr("v + 5") << endl;
}
15 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc myInstance; // Define a variable and get its Item object auto v = myInstance.DefineVariable("v = 10"); // Use the item to get back to its parent uCalc instance auto parentInstance = v.uCalc(); // Perform another evaluation in the same context cout << parentInstance.EvalStr("v + 5") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim myInstance As New uCalc()
'// Define a variable and get its Item object
Dim v = myInstance.DefineVariable("v = 10")
'// Use the item to get back to its parent uCalc instance
Dim parentInstance = v.uCalc
'// Perform another evaluation in the same context
Console.WriteLine(parentInstance.EvalStr("v + 5"))
End Sub
End Module
15 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim myInstance As New uCalc() '// Define a variable and get its Item object Dim v = myInstance.DefineVariable("v = 10") '// Use the item to get back to its parent uCalc instance Dim parentInstance = v.uCalc '// Perform another evaluation in the same context Console.WriteLine(parentInstance.EvalStr("v + 5")) End Sub End Module