How to define a constant, variable, operator, function, output format, and token with Define()

ID: 6

See: Define
				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine("------ Basic examples -------");
uc.Define("Function: f(x, y) = x + y");
uc.Define("Operator: {x} %% {y} = x * y");
uc.Define("Variable: MyVar = 123");

Console.WriteLine(uc.Eval("f(5, 10)"));
Console.WriteLine(uc.Eval("5 %% 10"));
Console.WriteLine(uc.Eval("MyVar * 100"));

// End users can also call Define()
Console.WriteLine("------ End user definition -------");
uc.Eval("Define('Function: ff(x) = x * 1000')");
Console.WriteLine(uc.Eval("ff(987)"));

Console.WriteLine("------ Boolean format -------");
Console.WriteLine(uc.EvalStr("1 < 2"));
Console.WriteLine(uc.EvalStr("1 > 2"));
Console.WriteLine(uc.EvalStr("Int(1 < 2)"));
Console.WriteLine(uc.EvalStr("(True Or False) And (True And False)"));
uc.Define("Boolean: 55, TotallyTrue, CompletelyFalse");
Console.WriteLine(uc.EvalStr("1 < 2"));
Console.WriteLine(uc.EvalStr("1 > 2"));
Console.WriteLine(uc.EvalStr("Int(1 < 2)"));
Console.WriteLine(uc.EvalStr("(TotallyTrue Or CompletelyFalse) And (TotallyTrue And CompletelyFalse)"));

// Format - configures the formatting for the output given by EvalStr
Console.WriteLine("------ Format -------");
uc.Define("Format: Result = 'Answer: <' + Result + '>'");
uc.Define("Format: String, val = 'String Value --> ' + val");
Console.WriteLine(uc.EvalStr("1+2"));
Console.WriteLine(uc.EvalStr("'Hello ' + 'world!'"));
var Additional = uc.Define("Format: ret = 'Additional format: ' + ret");
Console.WriteLine(uc.EvalStr("1+2"));
uc.Define("Format: InsertAt: 0, result = 'The ' + result");
Console.WriteLine(uc.EvalStr("1+2"));
Additional.Release();
Console.WriteLine(uc.EvalStr("1+2"));
uc.FormatRemove();

// Bootstrap - builds new def on top of existing one
Console.WriteLine("------ Bootstrap -------");
Console.WriteLine(uc.EvalStr("Hex(123)")); // uses "built-in" version of Hex()
var MyHex = uc.Define("Bootstrap ~~ Function: Hex(number As Int) As String = '0x' + UCase(Hex(number))");
Console.WriteLine(uc.EvalStr("Hex(123)"));
MyHex.Release();
Console.WriteLine(uc.EvalStr("Hex(123)"));

// Overwrite - useful for spreadsheet-like functionality
Console.WriteLine("------ Overwrite -------");
uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 5");
uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 10");
uc.Define("Overwrite ~~ Func: SpreadsheetCell_C3() = SpreadsheetCell_A1() + SpreadsheetCell_B2()");
Console.WriteLine(uc.Eval("SpreadsheetCell_A1()"));
Console.WriteLine(uc.Eval("SpreadsheetCell_B2()"));
Console.WriteLine(uc.Eval("SpreadsheetCell_C3()"));
// SpreadsheetCell_C3() will be affected by the definition changes of SpreadsheetCell_A1() and  SpreadsheetCell_B3()
uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 100");
uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 25");
Console.WriteLine("-------");
// Note: Empty parenthesis are optional for functions with no parameters
Console.WriteLine(uc.Eval("SpreadsheetCell_A1"));
Console.WriteLine(uc.Eval("SpreadsheetCell_B2"));
Console.WriteLine(uc.Eval("SpreadsheetCell_C3"));

// Lock
Console.WriteLine("------ Lock -------");
uc.Define("Variable: xy = 555");
uc.Define("Lock ~~ Variable: pi = 3.14"); // like using DefineConstant()
Console.WriteLine(uc.EvalStr("xy"));
Console.WriteLine(uc.EvalStr("pi"));
Console.WriteLine(uc.EvalStr("xy = 222")); // This variable is being changed
Console.WriteLine(uc.EvalStr("pi = 3.14159")); // This one is locked and cannot be changed
Console.WriteLine(uc.EvalStr("xy")); // Returns the new value of xy
Console.WriteLine(uc.EvalStr("pi")); // pi did not change; returns original value
Console.WriteLine("-------");
uc.Define("Function: f1(x) = x + 100");
uc.Define("Lock ~~ Function: f2(x) = x + 200"); // End-user can't change f2
Console.WriteLine(uc.Eval("f1(5)"));
Console.WriteLine(uc.Eval("f2(5)"));
uc.Eval("Define('Function: f1(x) = x + 300')");
uc.Eval("Define('Function: f2(x) = x + 400')"); // This re-definition is ignored
Console.WriteLine(uc.Eval("f1(5)"));
Console.WriteLine(uc.Eval("f2(5)"));

// Tokens
Console.WriteLine("------ Tokens -------");
Console.WriteLine(uc.EvalStr("5 + 4 // This comment causes an error"));
Console.WriteLine(uc.EvalStr("5 + /* comment not recognized yet */ 10"));
uc.Define("TokenType: Whitespace ~~ Token: //.*"); // // C-style to end-of-line comment
uc.Define("TokenType: Whitespace ~~ Token: /[*].*?[*]/"); // /* C-style enclosed comment */
Console.WriteLine(uc.EvalStr("5 + 4 // This comment will be ignored"));
Console.WriteLine(uc.EvalStr("5 + /* comment ignored */ 10"));

// Precedence
Console.WriteLine("------ Precedence -------");
uc.Define("Precedence: 1    ~~ Operator: {a As Int32} OpA {b As Int32} = a + b");
uc.Define("Precedence: 1000 ~~ Operator: {a As Int32} OpB {b As Int32} = a + b");
Console.WriteLine(uc.Eval("5 OpA 4 * 10"));
Console.WriteLine(uc.Eval("5 OpB 4 * 10"));

// Associativity
Console.WriteLine("------ Associativity -------");
uc.Define("Associativity: LeftToRight ~~ Operator: {x} OpX {y} = x / y");
uc.Define("Associativity: RightToLeft ~~ Operator: {x} OpY {y} = x / y");
Console.WriteLine(uc.Eval("3 OpX 4 OpX 5"));
Console.WriteLine(uc.Eval("3 OpY 4 OpY 5"));
				
			
------ Basic examples -------
15
50
12300
------ End user definition -------
987000
------ Boolean format -------
true
false
1
false
TotallyTrue
CompletelyFalse
55
CompletelyFalse
------ Format -------
Answer: <3>
Answer: <String Value --> Hello world!>
Answer: <Additional format: 3>
The Answer: <Additional format: 3>
The Answer: <3>
------ Bootstrap -------
7b
0x7B
7b
------ Overwrite -------
5
50
55
-------
25
2500
2525
------ Lock -------
555
3.14
222
Value cannot be assigned here
222
3.14
-------
105
205
305
205
------ Tokens -------
Undefined identifier
Undefined identifier
9
15
------ Precedence -------
45
90
------ Associativity -------
0.15
3.75
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << "------ Basic examples -------" << endl;
   uc.Define("Function: f(x, y) = x + y");
   uc.Define("Operator: {x} %% {y} = x * y");
   uc.Define("Variable: MyVar = 123");

   cout << uc.Eval("f(5, 10)") << endl;
   cout << uc.Eval("5 %% 10") << endl;
   cout << uc.Eval("MyVar * 100") << endl;

   // End users can also call Define()
   cout << "------ End user definition -------" << endl;
   uc.Eval("Define('Function: ff(x) = x * 1000')");
   cout << uc.Eval("ff(987)") << endl;

   cout << "------ Boolean format -------" << endl;
   cout << uc.EvalStr("1 < 2") << endl;
   cout << uc.EvalStr("1 > 2") << endl;
   cout << uc.EvalStr("Int(1 < 2)") << endl;
   cout << uc.EvalStr("(True Or False) And (True And False)") << endl;
   uc.Define("Boolean: 55, TotallyTrue, CompletelyFalse");
   cout << uc.EvalStr("1 < 2") << endl;
   cout << uc.EvalStr("1 > 2") << endl;
   cout << uc.EvalStr("Int(1 < 2)") << endl;
   cout << uc.EvalStr("(TotallyTrue Or CompletelyFalse) And (TotallyTrue And CompletelyFalse)") << endl;

   // Format - configures the formatting for the output given by EvalStr
   cout << "------ Format -------" << endl;
   uc.Define("Format: Result = 'Answer: <' + Result + '>'");
   uc.Define("Format: String, val = 'String Value --> ' + val");
   cout << uc.EvalStr("1+2") << endl;
   cout << uc.EvalStr("'Hello ' + 'world!'") << endl;
   auto Additional = uc.Define("Format: ret = 'Additional format: ' + ret");
   cout << uc.EvalStr("1+2") << endl;
   uc.Define("Format: InsertAt: 0, result = 'The ' + result");
   cout << uc.EvalStr("1+2") << endl;
   Additional.Release();
   cout << uc.EvalStr("1+2") << endl;
   uc.FormatRemove();

   // Bootstrap - builds new def on top of existing one
   cout << "------ Bootstrap -------" << endl;
   cout << uc.EvalStr("Hex(123)") << endl; // uses "built-in" version of Hex()
   auto MyHex = uc.Define("Bootstrap ~~ Function: Hex(number As Int) As String = '0x' + UCase(Hex(number))");
   cout << uc.EvalStr("Hex(123)") << endl;
   MyHex.Release();
   cout << uc.EvalStr("Hex(123)") << endl;

   // Overwrite - useful for spreadsheet-like functionality
   cout << "------ Overwrite -------" << endl;
   uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 5");
   uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 10");
   uc.Define("Overwrite ~~ Func: SpreadsheetCell_C3() = SpreadsheetCell_A1() + SpreadsheetCell_B2()");
   cout << uc.Eval("SpreadsheetCell_A1()") << endl;
   cout << uc.Eval("SpreadsheetCell_B2()") << endl;
   cout << uc.Eval("SpreadsheetCell_C3()") << endl;
   // SpreadsheetCell_C3() will be affected by the definition changes of SpreadsheetCell_A1() and  SpreadsheetCell_B3()
   uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 100");
   uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 25");
   cout << "-------" << endl;
   // Note: Empty parenthesis are optional for functions with no parameters
   cout << uc.Eval("SpreadsheetCell_A1") << endl;
   cout << uc.Eval("SpreadsheetCell_B2") << endl;
   cout << uc.Eval("SpreadsheetCell_C3") << endl;

   // Lock
   cout << "------ Lock -------" << endl;
   uc.Define("Variable: xy = 555");
   uc.Define("Lock ~~ Variable: pi = 3.14"); // like using DefineConstant()
   cout << uc.EvalStr("xy") << endl;
   cout << uc.EvalStr("pi") << endl;
   cout << uc.EvalStr("xy = 222") << endl; // This variable is being changed
   cout << uc.EvalStr("pi = 3.14159") << endl; // This one is locked and cannot be changed
   cout << uc.EvalStr("xy") << endl; // Returns the new value of xy
   cout << uc.EvalStr("pi") << endl; // pi did not change; returns original value
   cout << "-------" << endl;
   uc.Define("Function: f1(x) = x + 100");
   uc.Define("Lock ~~ Function: f2(x) = x + 200"); // End-user can't change f2
   cout << uc.Eval("f1(5)") << endl;
   cout << uc.Eval("f2(5)") << endl;
   uc.Eval("Define('Function: f1(x) = x + 300')");
   uc.Eval("Define('Function: f2(x) = x + 400')"); // This re-definition is ignored
   cout << uc.Eval("f1(5)") << endl;
   cout << uc.Eval("f2(5)") << endl;

   // Tokens
   cout << "------ Tokens -------" << endl;
   cout << uc.EvalStr("5 + 4 // This comment causes an error") << endl;
   cout << uc.EvalStr("5 + /* comment not recognized yet */ 10") << endl;
   uc.Define("TokenType: Whitespace ~~ Token: //.*"); // // C-style to end-of-line comment
   uc.Define("TokenType: Whitespace ~~ Token: /[*].*?[*]/"); // /* C-style enclosed comment */
   cout << uc.EvalStr("5 + 4 // This comment will be ignored") << endl;
   cout << uc.EvalStr("5 + /* comment ignored */ 10") << endl;

   // Precedence
   cout << "------ Precedence -------" << endl;
   uc.Define("Precedence: 1    ~~ Operator: {a As Int32} OpA {b As Int32} = a + b");
   uc.Define("Precedence: 1000 ~~ Operator: {a As Int32} OpB {b As Int32} = a + b");
   cout << uc.Eval("5 OpA 4 * 10") << endl;
   cout << uc.Eval("5 OpB 4 * 10") << endl;

   // Associativity
   cout << "------ Associativity -------" << endl;
   uc.Define("Associativity: LeftToRight ~~ Operator: {x} OpX {y} = x / y");
   uc.Define("Associativity: RightToLeft ~~ Operator: {x} OpY {y} = x / y");
   cout << uc.Eval("3 OpX 4 OpX 5") << endl;
   cout << uc.Eval("3 OpY 4 OpY 5") << endl;
}
				
			
------ Basic examples -------
15
50
12300
------ End user definition -------
987000
------ Boolean format -------
true
false
1
false
TotallyTrue
CompletelyFalse
55
CompletelyFalse
------ Format -------
Answer: <3>
Answer: <String Value --> Hello world!>
Answer: <Additional format: 3>
The Answer: <Additional format: 3>
The Answer: <3>
------ Bootstrap -------
7b
0x7B
7b
------ Overwrite -------
5
50
55
-------
25
2500
2525
------ Lock -------
555
3.14
222
Value cannot be assigned here
222
3.14
-------
105
205
305
205
------ Tokens -------
Undefined identifier
Undefined identifier
9
15
------ Precedence -------
45
90
------ Associativity -------
0.15
3.75
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine("------ Basic examples -------")
      uc.Define("Function: f(x, y) = x + y")
      uc.Define("Operator: {x} %% {y} = x * y")
      uc.Define("Variable: MyVar = 123")
      
      Console.WriteLine(uc.Eval("f(5, 10)"))
      Console.WriteLine(uc.Eval("5 %% 10"))
      Console.WriteLine(uc.Eval("MyVar * 100"))
      
      '// End users can also call Define()
      Console.WriteLine("------ End user definition -------")
      uc.Eval("Define('Function: ff(x) = x * 1000')")
      Console.WriteLine(uc.Eval("ff(987)"))
      
      Console.WriteLine("------ Boolean format -------")
      Console.WriteLine(uc.EvalStr("1 < 2"))
      Console.WriteLine(uc.EvalStr("1 > 2"))
      Console.WriteLine(uc.EvalStr("Int(1 < 2)"))
      Console.WriteLine(uc.EvalStr("(True Or False) And (True And False)"))
      uc.Define("Boolean: 55, TotallyTrue, CompletelyFalse")
      Console.WriteLine(uc.EvalStr("1 < 2"))
      Console.WriteLine(uc.EvalStr("1 > 2"))
      Console.WriteLine(uc.EvalStr("Int(1 < 2)"))
      Console.WriteLine(uc.EvalStr("(TotallyTrue Or CompletelyFalse) And (TotallyTrue And CompletelyFalse)"))
      
      '// Format - configures the formatting for the output given by EvalStr
      Console.WriteLine("------ Format -------")
      uc.Define("Format: Result = 'Answer: <' + Result + '>'")
      uc.Define("Format: String, val = 'String Value --> ' + val")
      Console.WriteLine(uc.EvalStr("1+2"))
      Console.WriteLine(uc.EvalStr("'Hello ' + 'world!'"))
      Dim Additional = uc.Define("Format: ret = 'Additional format: ' + ret")
      Console.WriteLine(uc.EvalStr("1+2"))
      uc.Define("Format: InsertAt: 0, result = 'The ' + result")
      Console.WriteLine(uc.EvalStr("1+2"))
      Additional.Release()
      Console.WriteLine(uc.EvalStr("1+2"))
      uc.FormatRemove()
      
      '// Bootstrap - builds new def on top of existing one
      Console.WriteLine("------ Bootstrap -------")
      Console.WriteLine(uc.EvalStr("Hex(123)")) '// uses "built-in" version of Hex()
      Dim MyHex = uc.Define("Bootstrap ~~ Function: Hex(number As Int) As String = '0x' + UCase(Hex(number))")
      Console.WriteLine(uc.EvalStr("Hex(123)"))
      MyHex.Release()
      Console.WriteLine(uc.EvalStr("Hex(123)"))
      
      '// Overwrite - useful for spreadsheet-like functionality
      Console.WriteLine("------ Overwrite -------")
      uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 5")
      uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 10")
      uc.Define("Overwrite ~~ Func: SpreadsheetCell_C3() = SpreadsheetCell_A1() + SpreadsheetCell_B2()")
      Console.WriteLine(uc.Eval("SpreadsheetCell_A1()"))
      Console.WriteLine(uc.Eval("SpreadsheetCell_B2()"))
      Console.WriteLine(uc.Eval("SpreadsheetCell_C3()"))
      '// SpreadsheetCell_C3() will be affected by the definition changes of SpreadsheetCell_A1() and  SpreadsheetCell_B3()
      uc.Define("Overwrite ~~ Func: SpreadsheetCell_B2() = SpreadsheetCell_A1() * 100")
      uc.Define("Overwrite ~~ Func: SpreadsheetCell_A1() = 25")
      Console.WriteLine("-------")
      '// Note: Empty parenthesis are optional for functions with no parameters
      Console.WriteLine(uc.Eval("SpreadsheetCell_A1"))
      Console.WriteLine(uc.Eval("SpreadsheetCell_B2"))
      Console.WriteLine(uc.Eval("SpreadsheetCell_C3"))
      
      '// Lock
      Console.WriteLine("------ Lock -------")
      uc.Define("Variable: xy = 555")
      uc.Define("Lock ~~ Variable: pi = 3.14") '// like using DefineConstant()
      Console.WriteLine(uc.EvalStr("xy"))
      Console.WriteLine(uc.EvalStr("pi"))
      Console.WriteLine(uc.EvalStr("xy = 222")) '// This variable is being changed
      Console.WriteLine(uc.EvalStr("pi = 3.14159")) '// This one is locked and cannot be changed
      Console.WriteLine(uc.EvalStr("xy")) '// Returns the new value of xy
      Console.WriteLine(uc.EvalStr("pi")) '// pi did not change; returns original value
      Console.WriteLine("-------")
      uc.Define("Function: f1(x) = x + 100")
      uc.Define("Lock ~~ Function: f2(x) = x + 200") '// End-user can't change f2
      Console.WriteLine(uc.Eval("f1(5)"))
      Console.WriteLine(uc.Eval("f2(5)"))
      uc.Eval("Define('Function: f1(x) = x + 300')")
      uc.Eval("Define('Function: f2(x) = x + 400')") '// This re-definition is ignored
      Console.WriteLine(uc.Eval("f1(5)"))
      Console.WriteLine(uc.Eval("f2(5)"))
      
      '// Tokens
      Console.WriteLine("------ Tokens -------")
      Console.WriteLine(uc.EvalStr("5 + 4 // This comment causes an error"))
      Console.WriteLine(uc.EvalStr("5 + /* comment not recognized yet */ 10"))
      uc.Define("TokenType: Whitespace ~~ Token: //.*") '// // C-style to end-of-line comment 
      uc.Define("TokenType: Whitespace ~~ Token: /[*].*?[*]/") '// /* C-style enclosed comment */
      Console.WriteLine(uc.EvalStr("5 + 4 // This comment will be ignored"))
      Console.WriteLine(uc.EvalStr("5 + /* comment ignored */ 10"))
      
      '// Precedence
      Console.WriteLine("------ Precedence -------")
      uc.Define("Precedence: 1    ~~ Operator: {a As Int32} OpA {b As Int32} = a + b")
      uc.Define("Precedence: 1000 ~~ Operator: {a As Int32} OpB {b As Int32} = a + b")
      Console.WriteLine(uc.Eval("5 OpA 4 * 10"))
      Console.WriteLine(uc.Eval("5 OpB 4 * 10"))
      
      '// Associativity
      Console.WriteLine("------ Associativity -------")
      uc.Define("Associativity: LeftToRight ~~ Operator: {x} OpX {y} = x / y")
      uc.Define("Associativity: RightToLeft ~~ Operator: {x} OpY {y} = x / y")
      Console.WriteLine(uc.Eval("3 OpX 4 OpX 5"))
      Console.WriteLine(uc.Eval("3 OpY 4 OpY 5"))
   End Sub
End Module
				
			
------ Basic examples -------
15
50
12300
------ End user definition -------
987000
------ Boolean format -------
true
false
1
false
TotallyTrue
CompletelyFalse
55
CompletelyFalse
------ Format -------
Answer: <3>
Answer: <String Value --> Hello world!>
Answer: <Additional format: 3>
The Answer: <Additional format: 3>
The Answer: <3>
------ Bootstrap -------
7b
0x7B
7b
------ Overwrite -------
5
50
55
-------
25
2500
2525
------ Lock -------
555
3.14
222
Value cannot be assigned here
222
3.14
-------
105
205
305
205
------ Tokens -------
Undefined identifier
Undefined identifier
9
15
------ Precedence -------
45
90
------ Associativity -------
0.15
3.75