10.8 Conditional Evaluation

To support conditional evaluation in Camille, we add the following rules to the grammar and corresponding pattern-action rules to the PLY parser generator:

A list of two grammar rules to support conditional evaluation in Camille.
Description
A set of three lines of code with P L Y parser generator.
Description

We must also add the if and else keywords to the generator of the scanner on lines 10–16 of the listing at the beginning of Section 10.6.1.

The following code segment of the evaluate_expr function accesses the ntIfElse variant of the abstract-syntax tree of type TreeNode and evaluates the conditional expression it represents:

A set of 11 code lines with the evaluate underscore e x p r function.
Description

Notice that we implement conditional evaluation in Camille using the support for conditional evaluation in Python (i.e., if . . . else; lines 7–10). In addition, we avoid adding a boolean type (for now) by associating 0 with false and anything else with true (as in the C programming language). Example expressions in this version of Camille with their evaluated results follow:

A set of four code lines in Camille with evaluated results of example expressions.
Description

5. Camille version 1.1(named CLS).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.18