// Function definition. // // File: function.h // Author: course // Version: 3 #ifndef FUNCTION_H #define FUNCTION_H #include "object.h" // A pointer to a C++ function that implements a // primitive or special function by taking an argment // list and returning a value. // typedef object * (* c_function) (object * arglist); class function : public object { public: // Create a primitive or special function with a // given C++ function. // friend function * make_function (c_function cfun, bool special); // Apply a primitive or special function to an // arglist. Just does a simple C call, and does // not touch the environment (unlike apply()). // friend object * function_apply (function * fun, object * arglist); // Test if special. Unchecked version assumes // object is a function. // friend bool specialp (function * fun); friend bool unchecked_specialp (object * ob); private: c_function cfun; bool special; // Virtuals to be implemented by subclasses. virtual ostream& print (ostream& s); // Print object. virtual void scavenge(); // Scavenge object during garbage collection. }; // Return true iff object is a function. // bool functionp (object * ob); // Convert a pointer to an object to a pointer to a // function (which is the same as the object) if the // object is a function, or return NULL if the object // is not a function. // function * may_be_function (object * ob); // Convert a pointer to an object to a pointer to a // function (which is the same as the object) if the // object is a function, or call error if the object is // not a function. // function * must_be_function (object * ob); // Convert a pointer to an object to a pointer to a // function (which is the same as the object) if the // object is a function, Produces undefined results if // object is not a function, but is the most efficient // conversion because it does no checking. // function * unchecked_must_be_function (object * ob); // Some symbols associated with functions. // extern object * LAMBDA; extern object * LAMBDA_CLOSURE; extern object * MACRO; extern object * REST; // "&REST" extern object * OPTIONAL; // "&OPTIONAL" // Object type for function. // extern object_type * FUNCTION_TYPE; // Inline implementations of above. See above for // documentation. // inline object * function_apply (function * fun, object * arglist) { return (*fun->cfun)(arglist); } inline bool specialp (function * fun) { return fun->special; } inline bool unchecked_specialp (object * ob) { return ((function *) ob)->special; } inline bool functionp (object * ob) { return type_of (ob) == FUNCTION_TYPE; } inline function * may_be_function (object * ob) { return functionp(ob) ? (function *) ob : (function *) NULL; } inline function * must_be_function (object * ob) { if ( ob == NULL || ! functionp (ob) ) type_error (ob, FUNCTION_TYPE); return (function *) ob; } inline function * unchecked_must_be_function (object * ob) { return (function *) ob; } #endif // FUNCTION_H