A practical example that transpiles a multi-line legacy script, including comments, variables, and a conditional statement with a nested action.
ID: 1377
using uCalcSoftware;
var uc = new uCalc();
var t = uc.NewTransformer();
// Rules must be defined in an order that allows for proper transformation.
// 1. Comment rule
t.FromTo("REM {comment}", "// {comment}");
// 2. Variable assignment rule
t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};");
// 3. Print statement rule
t.FromTo("PRINT {output}", "console.log({output});");
// 4. IF...THEN rule with RewindOnChange to handle nested statements
var ifRule = t.FromTo("IF {condition} THEN {action}", """
if ({condition}) {
{action}
}
""");
ifRule.RewindOnChange = true;
// The legacy script to transpile
var legacyScript = """
REM Initialize variables
LET X = 10
LET Y = X * 2
IF Y > 15 THEN PRINT "Value is large"
""";
// Run the transformation
Console.WriteLine(t.Transform(legacyScript));
// Initialize variables
var X = 10;
var Y = X * 2;
if (Y > 15) {
console.log("Value is large");
} using uCalcSoftware; var uc = new uCalc(); var t = uc.NewTransformer(); // Rules must be defined in an order that allows for proper transformation. // 1. Comment rule t.FromTo("REM {comment}", "// {comment}"); // 2. Variable assignment rule t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};"); // 3. Print statement rule t.FromTo("PRINT {output}", "console.log({output});"); // 4. IF...THEN rule with RewindOnChange to handle nested statements var ifRule = t.FromTo("IF {condition} THEN {action}", """ if ({condition}) { {action} } """); ifRule.RewindOnChange = true; // The legacy script to transpile var legacyScript = """ REM Initialize variables LET X = 10 LET Y = X * 2 IF Y > 15 THEN PRINT "Value is large" """; // Run the transformation Console.WriteLine(t.Transform(legacyScript));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.NewTransformer();
// Rules must be defined in an order that allows for proper transformation.
// 1. Comment rule
t.FromTo("REM {comment}", "// {comment}");
// 2. Variable assignment rule
t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};");
// 3. Print statement rule
t.FromTo("PRINT {output}", "console.log({output});");
// 4. IF...THEN rule with RewindOnChange to handle nested statements
auto ifRule = t.FromTo("IF {condition} THEN {action}", R"(if ({condition}) {
{action}
})");
ifRule.RewindOnChange(true);
// The legacy script to transpile
auto legacyScript = R"(REM Initialize variables
LET X = 10
LET Y = X * 2
IF Y > 15 THEN PRINT "Value is large")";
// Run the transformation
cout << t.Transform(legacyScript) << endl;
}
// Initialize variables
var X = 10;
var Y = X * 2;
if (Y > 15) {
console.log("Value is large");
} #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); // Rules must be defined in an order that allows for proper transformation. // 1. Comment rule t.FromTo("REM {comment}", "// {comment}"); // 2. Variable assignment rule t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};"); // 3. Print statement rule t.FromTo("PRINT {output}", "console.log({output});"); // 4. IF...THEN rule with RewindOnChange to handle nested statements auto ifRule = t.FromTo("IF {condition} THEN {action}", R"(if ({condition}) { {action} })"); ifRule.RewindOnChange(true); // The legacy script to transpile auto legacyScript = R"(REM Initialize variables LET X = 10 LET Y = X * 2 IF Y > 15 THEN PRINT "Value is large")"; // Run the transformation cout << t.Transform(legacyScript) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.NewTransformer()
'// Rules must be defined in an order that allows for proper transformation.
'// 1. Comment rule
t.FromTo("REM {comment}", "// {comment}")
'// 2. Variable assignment rule
t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};")
'// 3. Print statement rule
t.FromTo("PRINT {output}", "console.log({output});")
'// 4. IF...THEN rule with RewindOnChange to handle nested statements
Dim ifRule = t.FromTo("IF {condition} THEN {action}", "if ({condition}) {
{action}
}")
ifRule.RewindOnChange = true
'// The legacy script to transpile
Dim legacyScript = "REM Initialize variables
LET X = 10
LET Y = X * 2
IF Y > 15 THEN PRINT ""Value is large"""
'// Run the transformation
Console.WriteLine(t.Transform(legacyScript))
End Sub
End Module
// Initialize variables
var X = 10;
var Y = X * 2;
if (Y > 15) {
console.log("Value is large");
} Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.NewTransformer() '// Rules must be defined in an order that allows for proper transformation. '// 1. Comment rule t.FromTo("REM {comment}", "// {comment}") '// 2. Variable assignment rule t.FromTo("LET {@Alpha:var} = {val}", "var {var} = {val};") '// 3. Print statement rule t.FromTo("PRINT {output}", "console.log({output});") '// 4. IF...THEN rule with RewindOnChange to handle nested statements Dim ifRule = t.FromTo("IF {condition} THEN {action}", "if ({condition}) { {action} }") ifRule.RewindOnChange = true '// The legacy script to transpile Dim legacyScript = "REM Initialize variables LET X = 10 LET Y = X * 2 IF Y > 15 THEN PRINT ""Value is large""" '// Run the transformation Console.WriteLine(t.Transform(legacyScript)) End Sub End Module