Skip to content

Defining constants

amomra edited this page Apr 20, 2015 · 2 revisions

Defining constants

Defining parser constants

Parser constants can either be values of type double or string. Constness refers to the bytecode. Constants will be stored by their value in the bytecode, not by a reference to their address. Thus accessing them is faster. They may be optimized away if this is possible. Defining new constants or changing old ones will reset the parser to string parsing mode thus resetting the bytecode. The Names of user defined constants may contain only the following characters: 0-9, a-z, A-Z, _, and they may not start with a number. Violating this rule will raise a parser error.

// Define value constant _pi
parser.DefineConst("_pi", 3.14);

// Define a string constant named strBuf
parser.DefineStrConst("strBuf", "hello world");

Querying parser constants

Querying parser constants is similar to querying variables.

// Get the map with the constants
var constants = p.Consts;
// Get the number of constants
Console.WriteLine(constants.Count);

// Query one constant
double v = constants["varName"];

// Query the constants
foreach(var constant in constants)
    Console.WriteLine("Name: {0} | Value: {1}", constant.Key, constant.Value);

Removing constants

Removing constants can only be done all at once using ClearConst.

// Remove all constants
parser.ClearConst();
Clone this wiki locally