11.3.3 Definitions and Names
Charme provides an if expression special form with a syntax and evaluation rule identical to the Scheme if expression. The grammar rule for an if expression is:
The expression object representing an if expression should be a list containing three elements, with the first element matching the keyword
All special forms have this property: they are represented by lists where the first element is a keyword that identifies the special form.
is_special_form procedure takes an expression and a keyword and outputs a Boolean. The result is
True if the expression is a special form matching the keyword:
return isinstance(expr, list) and len(expr) > 0 and expr == keyword
We can use this to recognize different special forms by passing in different keywords. We recognize an if expression by the
if token at the beginning of the expression:
return is_special_form(expr, 'if')
The evaluation rule for an if expression is:1
Charme Evaluation Rule 5: If. To evaluate an if expression in the current environment, (a) evaluate the predicate expression in the current environment; then, (b) if the value of the predicate expression is a false value then the value of the if expression is the value of the alternate expression in the current environment; otherwise, the value of the if expression is the value of the consequent expression in the current environment.
This procedure implements the if evaluation rule:
if meval(expr, env) != False: return meval(expr,env)
else: return meval(expr,env)
We number the Charme evaluation rules using the numbers we used for the analogous Scheme evaluation rules, but present them in a different order. ↩