Changing the parent uCalc object for a Transformer

ID: 160

				
					using uCalcSoftware;

var uc = new uCalc();
uc.DefineVariable("x = 1");
uc.DefineVariable("y = 2");
uc.DefineFunction("f(x) = x * 10");

var t = uc.NewTransformer();
var text = "Adding {x} and {y} gives: {x + y}. f(5) = {f(5)}";

var uNew = new uCalc();
uNew.DefineVariable("x = 111");
uNew.DefineVariable("y = 222");
uNew.DefineFunction("f(x) = x * 1000");

// Note: {@@Eval: txt} is equivalent of {@Eval: Eval(txt)}
// which is what's needed for to evaluate the expression
// resulting from the match that is not known ahead of time
t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
Console.WriteLine(t.Transform(text));

t.uCalc = uNew;
t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
Console.WriteLine(t.Transform(text));

				
			
Adding 1 and 2 gives: 3. f(5) = 50
Adding 111 and 222 gives: 333. f(5) = 5000
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uc.DefineVariable("x = 1");
   uc.DefineVariable("y = 2");
   uc.DefineFunction("f(x) = x * 10");

   auto t = uc.NewTransformer();
   auto text = "Adding {x} and {y} gives: {x + y}. f(5) = {f(5)}";

   uCalc uNew;
   uNew.DefineVariable("x = 111");
   uNew.DefineVariable("y = 222");
   uNew.DefineFunction("f(x) = x * 1000");

   // Note: {@@Eval: txt} is equivalent of {@Eval: Eval(txt)}
   // which is what's needed for to evaluate the expression
   // resulting from the match that is not known ahead of time
   t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
   cout << t.Transform(text) << endl;

   t.uCalc(uNew);
   t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
   cout << t.Transform(text) << endl;

}
				
			
Adding 1 and 2 gives: 3. f(5) = 50
Adding 111 and 222 gives: 333. f(5) = 5000
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("x = 1")
      uc.DefineVariable("y = 2")
      uc.DefineFunction("f(x) = x * 10")
      
      Dim t = uc.NewTransformer()
      Dim text = "Adding {x} and {y} gives: {x + y}. f(5) = {f(5)}"
      
      Dim uNew As New uCalc()
      uNew.DefineVariable("x = 111")
      uNew.DefineVariable("y = 222")
      uNew.DefineFunction("f(x) = x * 1000")
      
      '// Note: {@@Eval: txt} is equivalent of {@Eval: Eval(txt)}
      '// which is what's needed for to evaluate the expression
      '// resulting from the match that is not known ahead of time
      t.FromTo("'{' {expr} '}'", "{@@Eval: expr}")
      Console.WriteLine(t.Transform(text))
      
      t.uCalc = uNew
      t.FromTo("'{' {expr} '}'", "{@@Eval: expr}")
      Console.WriteLine(t.Transform(text))
      
   End Sub
End Module
				
			
Adding 1 and 2 gives: 3. f(5) = 50
Adding 111 and 222 gives: 333. f(5) = 5000