// Declarations for CS 51 Introduction to C // // File: intro51.h // Author: course ( Bob Walton ) // Version: 1 // This file contains declarations sufficient to permit // the programming of the CS 51 LISP assignments in C++. // // A LISP memory object is declared, and functions to // create objects and print objects are declared. NO // read functions are declared: all tests must be // executable programs that build data with program // calls. // // Most of the code is in basic C; only a few C++ // features are used. The main such features are // the // comment, cout << ... for output, and the new // operator for allocation. // #ifndef _INTRO51_H_ #define _INTRO51_H_ // Standard inclusions: // #include // Standard library. #include // I/O streams (cin, cout, // <<, >>, etc.). #include // I/O stream manipulators // (setw, setprecision, etc.). #include // String functions // (strcmp, etc.). #include // Character type functions // (isalpha, toupper, etc.). #include // Assert macro. // Object declarations: enum type // // A type code for an object, i.e., for a node in // the LISP memory model graph. { NO_TYPE = 0, SYMBOL = 1, // Symbols and integers are INTEGER = 2, // both atoms. CONS = 3 // I.e., a cons cell. }; struct object // // An object is a node in the graph of the LISP // memory model. // // Objects may be atoms or cons cells. The atoms // are strung together on a list called the symbol // table, that is use to prevent the duplication of // atoms in memory. Because there is no duplication // of atoms, testing `object *' pointers to see if // they are == is equivalent to LISP EQL. { unsigned type; // // One of the types above. Note that for each // type, only a few of the below components // are used, and the others are garbage. char * name; // // If type == SYMBOL, this is a pointer to the // character string that names the symbol. int value; // // If type == INTEGER, this is the value of the // integer atom. object * car; object * cdr; // // If type == CONS, these are the car and // cdr of the cons cell. object * next; // // If type == SYMBOL or INTEGER, the next atom // in the symbol table, or NULL if none. }; object * make_symbol ( char * name ); object * make_integer ( int value ); object * make_cons ( object * car, object * cdr ); // // Functions to make objects of the given types // with the given components. These functions // ensure that no atom is duplicated in memory, by // creating an atom in memory only if it is not // already in the symbol table. Note that any name // must be a string constant or be allocated to // memory permanently: it cannot be in a temporary // buffer. extern object * first; // // The first atom in the symbol table, or NULL if // none. extern object * NIL; extern object * T; // // Handy pointers to particular symbols. ostream & operator << ( ostream & s, object * ob ); // // Print an object to the given output stream, and // return the output stream as a value. The name of // this function is `operator <<', which permits // it to be called via the expression `s << ob'. void initialize_memory ( void ); // // This function is called at the beginning of // a program to initialize the LISP memory. It // creates the symbols NIL and T, for example. #endif // _INTRO51_H_