This module defines a class cnst_c. The objects of this class correspond to ground terms, i.e. constants. Currently, SLP supports only symbolic constants (atoms) and integer values, but one could easily add constants of other data types. This should require only changes in this module and in the scanner/parser.
If the ground term is a symbolic constant (atom), the term object contains a pointer to an object of class atom_c (the symbol table entry). If the ground term is an integer, it contains the integer value.
It is possible that there are different cnst_c
objects
that are actually the same term,
therefore this class has a method equal_to
that compares the atom pointers or integer values.
Of course,
it is an alternative to represent all data values
in the symbol table in order to ensure that different objects
cannot be equal terms.
The interface of this module hides such details,
so the change should be easy.
However,
in Prolog systems data type values are usually not represented
in the symbol table,
so I followed this example.
Probably this is more efficient if an input program
uses many different integers
and computes integers at runtime
(the latter is currently impossible in SLP,
but one could easily imagine such an extension for a future version).
Note that there are other classes for argument terms in input formulas
(class term_c),
and for argument terms in rules
(class rarg_c).
Both use objects of class cnst_c
for ground terms.
Because there are two kinds of ground terms (more may be added later), the constructor is overloaded:
cnst_c(atom_t atom)
;atom
.
cnst_c(int n)
;n
.
bool_t is_atom(void) const
;BOOL_TRUE
if this term is a symbolic constant (atom).
Otherwise it returns BOOL_FALSE
.
bool_t is_int(void) const
;BOOL_TRUE
if this term is an integer
Otherwise it returns BOOL_FALSE
.
atom_t get_atom(void) const
;atom_c
(symbol table entry).
int get_int(void) const
;bool_t equal_to(cnst_c *cnst) const
;cnst
.
It is not sufficient to compare the pointers
for checking two ground terms for equality,
since the same atom or integer may appear
in two different cnst_c
objects.
Therefore this method looks inside the objects
and compares the two atoms or integers.
void print(void) const
;