DefineMethod

Applies to:uCalc Transform
Class:uCalc
Defines a uCalc.Item object (a constunction, function, operator, variable, etc)
Syntax
Define(Expression, Address, ExistingItem)
Parameters
Expression
String
Text containing item to be defined. Expression must start with a keyword such as Function, Operator, Variable, Token, Syntax, etc., followed by a colon, before the expression
Address
ADDR
(Default = 0)
Address a function or variable is attached to
ExistingItem
ADDR
(Default = 0)
Address of existing item this definition is associated with
Returns
Type: Item
Defines a uCalc.Item object (a constunction, function, operator, variable, etc)
Remarks
Functions, operators, variables, constants, tokens, syntax constructs, etc., can be defined with this method. Although there are specialized methods that do the same things, such as DefineFunction, DefineOperator, DefineVariable, DefineConstant, and DefineSyntax, this method may in same cases offer more flexibility.

The expression must be preceded by a keyword and colon, such as "Variable:". For a full list of keywords, see Definition Keywords.

Sections are separated by ~~ (two tilda signs) .

RemarksEnd

Example 1: Defining another Abs function using the same callback address of existing one

uc.Define("Function: MyAbs(x)", uc.GetItemOf("Abs").FunctionAddress())
Console.WriteLine(uc.Eval("MyAbs(-123)")) ' Returns 123

          

uc.Define("Function: MyAbs(x)", uc.GetItemOf("Abs").FunctionAddress());
Console.WriteLine(uc.Eval("MyAbs(-123)")); // Returns 123;

          

      uc.Define('Function: MyAbs(x)', (void *)uc.GetItemOf('Abs').FunctionAddress());
      WriteLn(uc.Eval('MyAbs(-123)')); // Returns 123;

          

uc.Define("Function: MyAbs(x)", (void *)uc.GetItemOf("Abs").FunctionAddress());
cout << uc.Eval("MyAbs(-123)") << endl; // Returns 123;

          

uc.Define("Function: MyAbs(x)", (void *)uc.GetItemOf("Abs").FunctionAddress());
Console::WriteLine(uc.Eval("MyAbs(-123)")); // Returns 123;

          
Example 2: Defining various items

uc.Define("Function: f(x, y) = x + y")
uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b")
uc.Define("Variable: MyVar = 123")
uc.Define("Lock ~~ Variable: Pi = 3.14"): ' like uc.DefineConstant
uc.Define("Syntax: aa ::= bb")
      
Console.WriteLine(uc.Eval("f(10, 20) + MyVar")) ' Returns 153
Console.WriteLine(uc.Eval("Pi")) ' Returns 3.14
Console.WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")) ' Returns "Hello World!"
Console.WriteLine(uc.Expand("aa, bb, cc")) ' Returns "bb, bb, cc"

          

uc.Define("Function: f(x, y) = x + y");
uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
uc.Define("Variable: MyVar = 123");
uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
uc.Define("Syntax: aa ::= bb");

Console.WriteLine(uc.Eval("f(10, 20) + MyVar")); // Returns 153;
Console.WriteLine(uc.Eval("Pi")); // Returns 3.14;
Console.WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")); // Returns "Hello World!";
Console.WriteLine(uc.Expand("aa, bb, cc")); // Returns "bb, bb, cc";

uc.GetItemOf("Pi").Release();

          

      uc.Define('Function: f(x, y) = x + y');
      uc.Define('Operator: {a As String} CONCAT {b As String} As String = a + b');
      uc.Define('Variable: MyVar = 123');
      uc.Define('Lock ~~ Variable: Pi = 3.14'); // like uc.DefineConstant
      uc.Define('Syntax: aa ::= bb');
      
      WriteLn(uc.Eval('f(10, 20) + MyVar')); // Returns 153;
      WriteLn(uc.Eval('Pi')); // Returns 3.14;
      WriteLn(uc.EvalStr('"Hello " CONCAT "World!"')); // Returns 'Hello World!';
      WriteLn(uc.Expand('aa, bb, cc')); // Returns 'bb, bb, cc';

          

uc.Define("Function: f(x, y) = x + y");
uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
uc.Define("Variable: MyVar = 123");
uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
uc.Define("Syntax: aa ::= bb");

cout << uc.Eval("f(10, 20) + MyVar") << endl; // Returns 153;
cout << uc.Eval("Pi") << endl; // Returns 3.14;
cout << uc.EvalStr("'Hello ' CONCAT 'World!'") << endl; // Returns "Hello World!";
cout << uc.Expand("aa, bb, cc") << endl; // Returns "bb, bb, cc";

uc.GetItemOf("Pi").Release();

          

uc.Define("Function: f(x, y) = x + y");
uc.Define("Operator: {a As String} CONCAT {b As String} As String = a + b");
uc.Define("Variable: MyVar = 123");
uc.Define("Lock ~~ Variable: Pi = 3.14"); // like uc.DefineConstant
uc.Define("Syntax: aa ::= bb");

Console::WriteLine(uc.Eval("f(10, 20) + MyVar")); // Returns 153;
Console::WriteLine(uc.Eval("Pi")); // Returns 3.14;
Console::WriteLine(uc.EvalStr("'Hello ' CONCAT 'World!'")); // Returns "Hello World!";
Console::WriteLine(uc.Expand("aa, bb, cc")); // Returns "bb, bb, cc";

uc.GetItemOf("Pi").Release();

          
Example 3: Bootstraping the Cos function definition to change it to Degree mode

uc.DefineConstant("pi = Atan(1) * 4")

' Default Radian mode
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns -1
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -0.59846

' Cos redefined for Degree mode
Dim Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)")
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns 0.998497
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -1

' Reverts back to original definition
Degree.Release()
Console.WriteLine(uc.Eval("Cos(pi)")) ' Returns -1
Console.WriteLine(uc.Eval("Cos(180)")) ' Returns -0.59846

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

// Cos redefined for Degree mode
var Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns 0.998497;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -1;

// Reverts back to original definition
Degree.Release();
Console.WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console.WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

          

         uc.DefineConstant('pi = Atan(1) * 4');

         // Default Radian mode
         WriteLn(uc.Eval('Cos(pi)')); // Returns -1;
         WriteLn(uc.Eval('Cos(180)')); // Returns -0.59846;

         // Cos redefined for Degree mode
//var Degree = uc.Define('Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)');
         WriteLn(uc.Eval('Cos(pi)')); // Returns 0.998497;
         WriteLn(uc.Eval('Cos(180)')); // Returns -1;

         // Reverts back to original definition
         Degree.Release();
         WriteLn(uc.Eval('Cos(pi)')); // Returns -1;
         WriteLn(uc.Eval('Cos(180)')); // Returns -0.59846;

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
cout << uc.Eval("Cos(pi)") << endl; // Returns -1;
cout << uc.Eval("Cos(180)") << endl; // Returns -0.59846;

// Cos redefined for Degree mode
auto Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
cout << uc.Eval("Cos(pi)") << endl; // Returns 0.998497;
cout << uc.Eval("Cos(180)") << endl; // Returns -1;

// Reverts back to original definition
Degree.Release();
cout << uc.Eval("Cos(pi)") << endl; // Returns -1;
cout << uc.Eval("Cos(180)") << endl; // Returns -0.59846;

          

uc.DefineConstant("pi = Atan(1) * 4");

// Default Radian mode
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

// Cos redefined for Degree mode
auto Degree = uc.Define("Bootstrap ~~ Function: Cos(x) = Cos(x*pi/180)");
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns 0.998497;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -1;

// Reverts back to original definition
Degree.Release();
Console::WriteLine(uc.Eval("Cos(pi)")); // Returns -1;
Console::WriteLine(uc.Eval("Cos(180)")); // Returns -0.59846;

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="Define")> _

Private Function Define__(ByVal uCalcHandle As IntPtr,ByVal Expression As String , ByVal Address As IntPtr  , ByVal ExistingItem As IntPtr  ) As IntPtr
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="Define")]

protected static extern IntPtr Define_(IntPtr uCalcHandle, string Expression , IntPtr Address   , IntPtr ExistingItem   );
            
{DLLImport}function Define__(uCalcHandle: System.Pointer;Expression: AnsiString ; Address: System.Pointer  ; ExistingItem: System.Pointer): System.Pointer; cdecl; external uCalcDLL name 'Define';

            
typedef uCalcPtr (* __Define)(void *uCalcHandle, CONSTCHAR Expression  ,  void * Address  ,  void * ExistingItem  ); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "Define")]

static uCalcPtr Define_(void *  uCalcHandle, MARSHALSTR Expression  ,  void *   Address  ,  void *   ExistingItem);
            
See also
Prev | Next