The complete financial DSL processing a multi-line script of deposits, withdrawals, and interest calculation.
ID: 1331
using uCalcSoftware;
var uc = new uCalc();
// 1. Initialize the account balance
uc.DefineVariable("balance = 0.0");
// 2. Define the DSL rules on the expression transformer
var t = uc.ExpressionTransformer;
t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}");
t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}");
t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)");
// 3. Define the multi-line transaction script
var script = """
DEPOSIT 1000.00
WITHDRAW 50.75
INTEREST 0.5
WITHDRAW 200.00
""";
Console.WriteLine($"Initial Balance: {uc.Eval("balance")}");
Console.WriteLine("Processing script...");
// 4. Evaluate the entire script.
// Note: EvalStr returns the result of the LAST line.
// The intermediate lines modify the 'balance' variable.
uc.EvalStr(script);
// 5. Get the final balance
Console.WriteLine($"Final Balance: {uc.EvalStr("balance")}");
Initial Balance: 0
Processing script...
Final Balance: 753.99625 using uCalcSoftware; var uc = new uCalc(); // 1. Initialize the account balance uc.DefineVariable("balance = 0.0"); // 2. Define the DSL rules on the expression transformer var t = uc.ExpressionTransformer; t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}"); t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}"); t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)"); // 3. Define the multi-line transaction script var script = """ DEPOSIT 1000.00 WITHDRAW 50.75 INTEREST 0.5 WITHDRAW 200.00 """; Console.WriteLine($"Initial Balance: {uc.Eval("balance")}"); Console.WriteLine("Processing script..."); // 4. Evaluate the entire script. // Note: EvalStr returns the result of the LAST line. // The intermediate lines modify the 'balance' variable. uc.EvalStr(script); // 5. Get the final balance Console.WriteLine($"Final Balance: {uc.EvalStr("balance")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// 1. Initialize the account balance
uc.DefineVariable("balance = 0.0");
// 2. Define the DSL rules on the expression transformer
auto t = uc.ExpressionTransformer();
t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}");
t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}");
t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)");
// 3. Define the multi-line transaction script
auto script = R"(
DEPOSIT 1000.00
WITHDRAW 50.75
INTEREST 0.5
WITHDRAW 200.00
)";
cout << "Initial Balance: " << uc.Eval("balance") << endl;
cout << "Processing script..." << endl;
// 4. Evaluate the entire script.
// Note: EvalStr returns the result of the LAST line.
// The intermediate lines modify the 'balance' variable.
uc.EvalStr(script);
// 5. Get the final balance
cout << "Final Balance: " << uc.EvalStr("balance") << endl;
}
Initial Balance: 0
Processing script...
Final Balance: 753.99625 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // 1. Initialize the account balance uc.DefineVariable("balance = 0.0"); // 2. Define the DSL rules on the expression transformer auto t = uc.ExpressionTransformer(); t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}"); t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}"); t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)"); // 3. Define the multi-line transaction script auto script = R"( DEPOSIT 1000.00 WITHDRAW 50.75 INTEREST 0.5 WITHDRAW 200.00 )"; cout << "Initial Balance: " << uc.Eval("balance") << endl; cout << "Processing script..." << endl; // 4. Evaluate the entire script. // Note: EvalStr returns the result of the LAST line. // The intermediate lines modify the 'balance' variable. uc.EvalStr(script); // 5. Get the final balance cout << "Final Balance: " << uc.EvalStr("balance") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// 1. Initialize the account balance
uc.DefineVariable("balance = 0.0")
'// 2. Define the DSL rules on the expression transformer
Dim t = uc.ExpressionTransformer
t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}")
t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}")
t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)")
'// 3. Define the multi-line transaction script
Dim script = "
DEPOSIT 1000.00
WITHDRAW 50.75
INTEREST 0.5
WITHDRAW 200.00
"
Console.WriteLine($"Initial Balance: {uc.Eval("balance")}")
Console.WriteLine("Processing script...")
'// 4. Evaluate the entire script.
'// Note: EvalStr returns the result of the LAST line.
'// The intermediate lines modify the 'balance' variable.
uc.EvalStr(script)
'// 5. Get the final balance
Console.WriteLine($"Final Balance: {uc.EvalStr("balance")}")
End Sub
End Module
Initial Balance: 0
Processing script...
Final Balance: 753.99625 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// 1. Initialize the account balance uc.DefineVariable("balance = 0.0") '// 2. Define the DSL rules on the expression transformer Dim t = uc.ExpressionTransformer t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}") t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}") t.FromTo("INTEREST {@Number:rate}", "balance = balance * (1 + {rate} / 100.0)") '// 3. Define the multi-line transaction script Dim script = " DEPOSIT 1000.00 WITHDRAW 50.75 INTEREST 0.5 WITHDRAW 200.00 " Console.WriteLine($"Initial Balance: {uc.Eval("balance")}") Console.WriteLine("Processing script...") '// 4. Evaluate the entire script. '// Note: EvalStr returns the result of the LAST line. '// The intermediate lines modify the 'balance' variable. uc.EvalStr(script) '// 5. Get the final balance Console.WriteLine($"Final Balance: {uc.EvalStr("balance")}") End Sub End Module