Difference between revisions of "Parsed Expressions"
(19 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | [[Category:Expressions]] | |
+ | [[Category:Data Type Functions]] | ||
− | |||
− | + | __TOC__ | |
− | + | When you enter a valid expression into the definition of an object, Analytica parses the expression and stores a parsed representation of the expression in an attributed named '''FixedDef'''. Analytica internally utilizes these parsed expressions elsewhere also, such as in '''FixedCheck'''. | |
− | |||
− | |||
− | |||
− | |||
− | + | Users and modelers almost never need to, or should want to, utilize Analytica's internal parsed representations directly. However, it is conceivable that in some very rare cases, very advanced users might want to accessed these parsed representations for certain types of [[Meta-Inference]] Algorithms. The parse-tree uses special internal data structures, so access to the internals of a parse tree require the use of special built-in functions, namely [[#TypeOf(expr)|TypeOf]], [[#ParsedExprFunction(expr)|ParsedExprFunction]] and [[##ParsedExprParameters.28expr.29|ParsedExprParameters]]. | |
− | The general function of the [[TypeOf]] function is described at [[TypeOf]]. Here | + | == TypeOf(expr) == |
+ | |||
+ | The general function of the [[TypeOf]] function is described at [[TypeOf]]. Here we discuss its application only as relevant to parsed expressions. | ||
The representation of a parsed expression may be one of several types: | The representation of a parsed expression may be one of several types: | ||
Line 19: | Line 17: | ||
* A literal text | * A literal text | ||
* An identifier (which will be a [[handle]] to the object in the parsed expression) | * An identifier (which will be a [[handle]] to the object in the parsed expression) | ||
− | * A literal list, such as <code>[1,2,3]</code> | + | * A literal list, such as <code>[1, 2, 3]</code> |
− | * An operator, such as <code>X+Y</code>, or <code>X<Y</code>. | + | * An operator, such as <code>X + Y</code>, or <code>X < Y</code>. |
* A function call | * A function call | ||
The [[TypeOf]] function tells you the type of the parsed expression. Note that the parameter is evaluated, so you can compute the parsed expression. Normally in the case discussed here, the parameter will be something like | The [[TypeOf]] function tells you the type of the parsed expression. Note that the parameter is evaluated, so you can compute the parsed expression. Normally in the case discussed here, the parameter will be something like | ||
− | + | :<code>TypeOf(fixedDef of X)</code> | |
[[TypeOf]] returns a textual name of the type -- for a parsed expression, you may see: "Number", "Integer", "FixedPointReal", "Text", "Handle", "Expression", or "FunctionCall". | [[TypeOf]] returns a textual name of the type -- for a parsed expression, you may see: "Number", "Integer", "FixedPointReal", "Text", "Handle", "Expression", or "FunctionCall". | ||
− | Detecting the case in which the parsed expression contains a literal list, such as <code>[1,2,3]</code>, is tricky, because [[TypeOf]] will array abstract over the elements and give you a list of the types of the elements. To detect a list, | + | Detecting the case in which the parsed expression contains a literal list, such as <code>[1, 2, 3]</code>, is tricky, because [[TypeOf]] will array abstract over the elements and give you a list of the types of the elements. To detect a list at the top level of a parse tree, use: |
− | + | ||
+ | :<code>Size(IndexesOf(expr)) = 1 and Slice(IndexesOf(expr), 1) = Null</code> | ||
+ | |||
+ | Once you are recursing down the parse tree, detecting a literal list becomes less tricky since [[ParsedExprParameters]] will return a reference to an indexed list, so for sub-trees, you can simply test for [[IsReference]](expr). | ||
== ParsedExprFunction(expr) == | == ParsedExprFunction(expr) == | ||
− | |||
Once you've determined that your parsed expression is either a "FunctionCall" or "Expression" (aka operation), you may then want to determine which function is called, or which operation is performed. | Once you've determined that your parsed expression is either a "FunctionCall" or "Expression" (aka operation), you may then want to determine which function is called, or which operation is performed. | ||
− | In the case of a function call, | + | In the case of a function call, [[ParsedExprFunction]](expr) returns the [[handle]] to the function object. Note that «expr» is evaluated, so the parsed expression needs to be the result of evaluating «expr». Suppose we have the following variable defined in our model: |
− | + | :<code>Variable The_Npv := Npv(8%, Cash_flow, Time)</code> | |
− | |||
Then: | Then: | ||
− | + | :<code>ParsedExprFunction(fixedDef of The_Npv) → Npv { A handle to the Npv function object }</code> | |
− | |||
You could test whether [[Npv]] is being called using: | You could test whether [[Npv]] is being called using: | ||
+ | :<code>ParsedExprFunction'''(fixedDef of The_Npv) = Handle(Npv) → 1</code> | ||
− | + | == ParsedExprParameters(expr) == | |
− | + | When «expr» contains a parsed function call or parsed operator expression, this returns the parsed parameters to the function call or operator as a 1-D array. The result is indexed by a local index with the name .Parameter, containing function parameter names (in the case of a function) or numbers (in the case of an operator). | |
− | |||
− | = | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | If an explicit list is passed to a parameter, or for any repeated parameter, a reference to an unindex list is returned. This is to ensure that every parameter returned is atomic. | |
− | + | The parameter «expr» is evaluated, so you are retrieving information on the parsed expression that is the result of evaluating «expr». This means that in most cases, «expr» will be of the form <code>fixedDef of obj</code>. | |
− | + | === Examples: === | |
+ | :<code>Function H(u, v: number; w: ...; x, y, z: optional)</code> | ||
+ | :<code>Variable X1 := H(3, 2 + 2, 5, 6)</code> | ||
+ | :<code>Variable X2 := H(z: [1, 2, 3], v: 2, w: 3, 4, u: 8)</code> | ||
− | + | :<code>ParsedExprParameters(fixedDef of X1) →</code> | |
+ | :{| class="wikitable" | ||
+ | ! colspan=3|.Parameter ▶ | ||
+ | |- | ||
+ | ! u | ||
+ | ! v | ||
+ | ! w | ||
+ | |- | ||
+ | | 3 | ||
+ | | (2 + 2) | ||
+ | | \[5, 6] | ||
+ | |} | ||
− | + | :<code>ParsedExprParameters(fixedDef of X2) →</code> | |
− | + | :{| class="wikitable" | |
− | + | ! colspan=4|.Parameter ▼ | |
− | + | |- | |
− | + | ! u | |
− | + | ! v | |
− | + | ! w | |
− | + | ! z | |
− | + | |- | |
− | + | |8 | |
− | + | |2 | |
− | + | |\[3,4] | |
− | + | |\[1,2,3] | |
+ | |} | ||
− | + | ==History== | |
+ | Parsed expressions were introduced in Analytica 4.3. | ||
− | + | == See Also == | |
+ | * [[Expressions]] | ||
+ | * [[Expression Syntax]] | ||
+ | * [[Expression Assist]] | ||
+ | * [[The Expression popup menu]] | ||
+ | * [[Expressions that don't array-abstract]] | ||
+ | * [[TypeOf]] | ||
+ | * [[Size]] | ||
+ | * [[Using References]] | ||
+ | * [[IsReference]] |
Revision as of 00:17, 1 June 2016
When you enter a valid expression into the definition of an object, Analytica parses the expression and stores a parsed representation of the expression in an attributed named FixedDef. Analytica internally utilizes these parsed expressions elsewhere also, such as in FixedCheck.
Users and modelers almost never need to, or should want to, utilize Analytica's internal parsed representations directly. However, it is conceivable that in some very rare cases, very advanced users might want to accessed these parsed representations for certain types of Meta-Inference Algorithms. The parse-tree uses special internal data structures, so access to the internals of a parse tree require the use of special built-in functions, namely TypeOf, ParsedExprFunction and ParsedExprParameters.
TypeOf(expr)
The general function of the TypeOf function is described at TypeOf. Here we discuss its application only as relevant to parsed expressions.
The representation of a parsed expression may be one of several types:
- A literal number
- A literal text
- An identifier (which will be a handle to the object in the parsed expression)
- A literal list, such as
[1, 2, 3]
- An operator, such as
X + Y
, orX < Y
. - A function call
The TypeOf function tells you the type of the parsed expression. Note that the parameter is evaluated, so you can compute the parsed expression. Normally in the case discussed here, the parameter will be something like
TypeOf(fixedDef of X)
TypeOf returns a textual name of the type -- for a parsed expression, you may see: "Number", "Integer", "FixedPointReal", "Text", "Handle", "Expression", or "FunctionCall".
Detecting the case in which the parsed expression contains a literal list, such as [1, 2, 3]
, is tricky, because TypeOf will array abstract over the elements and give you a list of the types of the elements. To detect a list at the top level of a parse tree, use:
Size(IndexesOf(expr)) = 1 and Slice(IndexesOf(expr), 1) = Null
Once you are recursing down the parse tree, detecting a literal list becomes less tricky since ParsedExprParameters will return a reference to an indexed list, so for sub-trees, you can simply test for IsReference(expr).
ParsedExprFunction(expr)
Once you've determined that your parsed expression is either a "FunctionCall" or "Expression" (aka operation), you may then want to determine which function is called, or which operation is performed.
In the case of a function call, ParsedExprFunction(expr) returns the handle to the function object. Note that «expr» is evaluated, so the parsed expression needs to be the result of evaluating «expr». Suppose we have the following variable defined in our model:
Variable The_Npv := Npv(8%, Cash_flow, Time)
Then:
ParsedExprFunction(fixedDef of The_Npv) → Npv { A handle to the Npv function object }
You could test whether Npv is being called using:
ParsedExprFunction(fixedDef of The_Npv) = Handle(Npv) → 1
ParsedExprParameters(expr)
When «expr» contains a parsed function call or parsed operator expression, this returns the parsed parameters to the function call or operator as a 1-D array. The result is indexed by a local index with the name .Parameter, containing function parameter names (in the case of a function) or numbers (in the case of an operator).
If an explicit list is passed to a parameter, or for any repeated parameter, a reference to an unindex list is returned. This is to ensure that every parameter returned is atomic.
The parameter «expr» is evaluated, so you are retrieving information on the parsed expression that is the result of evaluating «expr». This means that in most cases, «expr» will be of the form fixedDef of obj
.
Examples:
Function H(u, v: number; w: ...; x, y, z: optional)
Variable X1 := H(3, 2 + 2, 5, 6)
Variable X2 := H(z: [1, 2, 3], v: 2, w: 3, 4, u: 8)
ParsedExprParameters(fixedDef of X1) →
.Parameter ▶ u v w 3 (2 + 2) \[5, 6]
ParsedExprParameters(fixedDef of X2) →
.Parameter ▼ u v w z 8 2 \[3,4] \[1,2,3]
History
Parsed expressions were introduced in Analytica 4.3.
Enable comment auto-refresher