#ifndef ___FUNCTS #define ___FUNCTS #include "env.hpp" // All list classes are implemented as arrays. No range checks are made // In this application, lists needn't be copied, so there are no // copy constructors, and the "=" operator is not overloaded. class function; // a list of symbols -- used e.g. for source language calls class list_of_symbols { protected: int size; char *symbol[LIST_SIZE]; public: list_of_symbols(void) {size=0;}; // constructor void insert(char *sym); int search(char *sym); // return the position of "sym" char *get_symbol(int i) {return symbol[i];}; int get_size(void) {return size;}; }; // A class for lists of lists of symbols class list_of_lists { protected: int size; list_of_symbols *symbols[LIST_SIZE]; public: list_of_lists(void) {size=0;}; // constructor void insert(list_of_symbols *sym); list_of_symbols *get_symbols(int i) {return symbols[i];}; int get_size(void) {return size;}; }; class list_of_functions { protected: int size; function *funct[LIST_SIZE]; public: list_of_functions(void) {size=0;}; void insert(function *f); int search(char *); // return the position of a function name function *get_funct(int i) {return funct[i];}; int get_size(void) {return size;}; }; // A class for lists of offset pairs. There are two constructors -- // one that constructs empty lists and one that builds an encoding // of a function call. The result may be interpreted as the // translation of the function call into a target language. class list_of_offsets { protected: int size; offset *offsets[LIST_SIZE]; public: list_of_offsets(void) {size=0;}; list_of_offsets(list_of_symbols *instr, function *f); void insert(offset *o); offset *get_offsets(int i) {return offsets[i];}; int get_size(void) {return size;}; }; // The member objects for a list of this class are objects of the // class defined above. They may be interpreted as function bodies // (i.e., lists of function calls) in the target language. class list_of_encodings { protected: int size; list_of_offsets *code[LIST_SIZE]; public: int get_size(void) {return size;}; list_of_offsets *get_code(int i) {return code[i];}; list_of_encodings(void) {size=0;}; list_of_encodings(function *f); }; // For representation of functions. The parameters, subroutine // names (i.e., names of other functions defined in the current // function's block) and instructions (i.e., the list of calls // making up the function body) are represented as lists of // symbols in the source language. The environment pointer // "ep" is represented at compile time simply as a pointer // to the calling function. // Member objects needed at run time are the translated body, // which is stored as the value of the "code" member object, // and information about the function's subroutines. This // latter information is stored simply as a list of objects // of class "function". // Member functions of this class include a constructor, and functions // to translate (i.e., construct offset pairs for) functions and // variables. The constructor expects a function name, a (pointer // to a) caller, a list of formal parameters, and a list of subroutines. // Any of the latter three may be empty. // Other member functions simply return the name, the environment // pointer, the formal parameters, the subroutines, and the arity // (number of parameters) of a function. class function { friend function *funct(BOOLEAN *, function *); friend list_of_functions *functions(BOOLEAN *success, function *caller); friend activation::activation(function *); friend list_of_encodings::list_of_encodings(function *); friend void environment::run(list_of_offsets *, function *); friend void environment::run(function *); protected: char *name; // the name of the function function *ep; // the environment pointer list_of_symbols *parameter; list_of_symbols *subr_names; list_of_functions *subroutines; list_of_lists *instrs; list_of_encodings *code; public: function(char *n, function *e, list_of_symbols *p, list_of_functions *s); offset *search_v(char *); offset *search_f(char *, int); char *get_name(void); function *get_ep(void) {return ep;}; }; #endif