Shows how the constructor's context (default vs. specific instance) affects variable resolution during parsing.

ID: 564

				
					using uCalcSoftware;

var uc = new uCalc();
// Set up two different uCalc contexts with the same variable name 'x'.
uCalc.DefaultInstance.DefineVariable("x = 1.2");
uc.DefineVariable("x = 10");

Console.WriteLine("--- Testing Expression Contexts ---");

// 1. Expression created in the *default* context uses x = 1.2
using (var exprDefault = new uCalc.Expression("x + 5")) {
   Console.WriteLine($"Default context (x=1.2): {exprDefault.Evaluate()}");

   // 2. Expression created in a *specific* context ('uc') uses x = 10
   using (var exprSpecific = new uCalc.Expression(uc, "x + 5")) {
      Console.WriteLine($"Specific context (x=10):  {exprSpecific.Evaluate()}");

      // 3. Create empty, then parse later (uses default context for the Parse call)
      using (var exprEmpty = new uCalc.Expression()) {
         exprEmpty.Parse("x * 10");
         Console.WriteLine($"Empty then parsed (x=1.2):{exprEmpty.Evaluate()}");
      }
   }
}
				
			
--- Testing Expression Contexts ---
Default context (x=1.2): 6.2
Specific context (x=10):  15
Empty then parsed (x=1.2):12
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Set up two different uCalc contexts with the same variable name 'x'.
   uCalc::DefaultInstance().DefineVariable("x = 1.2");
   uc.DefineVariable("x = 10");

   cout << "--- Testing Expression Contexts ---" << endl;

   // 1. Expression created in the *default* context uses x = 1.2
   {
      uCalc::Expression exprDefault("x + 5");
      exprDefault.Owned(); // Causes exprDefault to be released when it goes out of scope
      cout << "Default context (x=1.2): " << exprDefault.Evaluate() << endl;

      // 2. Expression created in a *specific* context ('uc') uses x = 10
      {
         uCalc::Expression exprSpecific(uc, "x + 5");
         exprSpecific.Owned(); // Causes exprSpecific to be released when it goes out of scope
         cout << "Specific context (x=10):  " << exprSpecific.Evaluate() << endl;

         // 3. Create empty, then parse later (uses default context for the Parse call)
         {
            uCalc::Expression exprEmpty;
            exprEmpty.Owned(); // Causes exprEmpty to be released when it goes out of scope
            exprEmpty.Parse("x * 10");
            cout << "Empty then parsed (x=1.2):" << exprEmpty.Evaluate() << endl;
         }
      }
   }
}
				
			
--- Testing Expression Contexts ---
Default context (x=1.2): 6.2
Specific context (x=10):  15
Empty then parsed (x=1.2):12
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Set up two different uCalc contexts with the same variable name 'x'.
      uCalc.DefaultInstance.DefineVariable("x = 1.2")
      uc.DefineVariable("x = 10")
      
      Console.WriteLine("--- Testing Expression Contexts ---")
      
      '// 1. Expression created in the *default* context uses x = 1.2
      Using exprDefault As New uCalc.Expression("x + 5")
         Console.WriteLine($"Default context (x=1.2): {exprDefault.Evaluate()}")
         
         '// 2. Expression created in a *specific* context ('uc') uses x = 10
         Using exprSpecific As New uCalc.Expression(uc, "x + 5")
            Console.WriteLine($"Specific context (x=10):  {exprSpecific.Evaluate()}")
            
            '// 3. Create empty, then parse later (uses default context for the Parse call)
            Using exprEmpty As New uCalc.Expression()
               exprEmpty.Parse("x * 10")
               Console.WriteLine($"Empty then parsed (x=1.2):{exprEmpty.Evaluate()}")
            End Using
         End Using
      End Using
   End Sub
End Module
				
			
--- Testing Expression Contexts ---
Default context (x=1.2): 6.2
Specific context (x=10):  15
Empty then parsed (x=1.2):12