Click or drag to resize

MCompile Function (String, Logic)

X#
Macro compile a string.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.19
Syntax
 FUNCTION MCompile(
	cString AS STRING,
	lAllowSingleQuotes AS LOGIC
) AS _Codeblock
Request Example View Source

Parameters

cString
Type: String
The string to compile.
lAllowSingleQuotes
Type: Logic
Should single quotes be allowed as string delimiters

Return Value

Type: _Codeblock
The string in a macro-compiled form.
Remarks
MCompile() allows you to use the macro compiler to compile a string and store the compiled results for later execution. Instead of invoking the macro compiler each time an expression is evaluated, you could speed up your application by compiling an expression only once and executing the compiled form as often as desired.
Tip Tip
In Visual Objects the return value of MCompile() is a specially formatted string with PCode tokens. We have changed that in X#, and the return value of MCompile() is now a codeblock of the _Codeblock type.
If the string that was compiled in Visual Objects was in the form of "{||.....}" then you hed to call MExec() to convert the string into a _Codeblock object. To evaluate the codeblock you would then have to pass the result of MExec() into the EVal() function.
If the string that was compiled in Visual Objects was not in the form of "{||.....}", like in the examples of this topic then calling MExec() on return value of MCompile() would immediately evaluate the expression and return the result.
Since in X#the return the return value of MCompile() is a codeblock of the _Codeblock type, you can you can also use the Eval() function to evaluate the codeblock.
In X# Calling MExec() on the codeblock returned from MCompile() will either directly evaluate the codeblock (when the original string was not in the codeblock format) or will simply return the same codeblock when the original string was in the codeblock format.
Examples
This examples show typical uses of MCompile() and MExec():
X#
 1LOCAL cComp AS CodeBlock        // Note that this had to be string in Visual Objects
 2cComp := MCompile("2+3")
 3? MExec(cComp)                  // 5
 4nResult := MExec(cComp)         // Assigning
 5? MExec(cComp) = 5              // Comparing
 6MEMVAR Two // Declare and initialize a Private variable
 7Two := 2
 8cComp := MCompile("Two+3")      // Macro refers to the private variable
 9? MExec(cComp)                  // 5
10// The followin works in X# only, since the result of cComp is now a CodeBlock
11? Eval(cComp)
See Also