home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************
- file: NODE.CPP Copyright 1989 by John M. Dlugosz
- dealings with nodes
- *****************************************************/
-
- #include "usual.hpp"
- #include <stream.hpp>
- #include <assert.h>
- #include "atom.hpp"
- #include "node.hpp"
-
- extern "C" void* malloc (unsigned size);
- extern "C" void free (void*);
- extern "C" void* realloc (void*, unsigned);
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- node::node()
- {
- flavor= nf_base;
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- node::~node()
- {
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- void node::print()
- {
- cout << "\nerror: base class node printed.\n";
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
- /* type_node */
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- type_node::~type_node()
- {
- if (to_what) delete to_what;
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- type_node::type_node ()
- {
- flavor= nf_type;
- tag= -1;
- to_what= NULL;
- flags= 0;
- primary= -1; //not filled in yet
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- void type_node::print()
- {
- static char* typenames[]= {
- "void", //0
- "char", //1
- "int", //2
- "long", //3
- "float", //4
- "double", //5
- "long double", //6
- "enum ", //7
- "class ", //8
- "union ", //9
- "pointer to ", //10
- "reference to ",//11
- "array of ", //12
- "function ", //13
- };
-
- if (isConst()) cout << "const ";
- if (isVol()) cout << "volotile ";
- if (primary < 10) { // a simple type
- if (isUnsigned()) cout << "unsigned ";
- cout << typenames[primary];
- if (primary >= 7) {
- cout << (tag > -1 ? atoms[tag] : "<no name>");
- cout.put (' ');
- }
- }
- else { //something fancy
- cout << typenames[primary];
- if (primary == 12) {
- // >> print array dimention
- }
- else if (primary == 13) {
- cout.put('(');
- int max= aggr->size();
- for (int loop= 0; loop < max; loop++) {
- (*aggr)[loop]->type->print();
- if (loop < max-1) cout << ", ";
- }
- cout << ") returning ";
- }
- if (secondary()) to_what->print();
- else cout << "**unknown type**";
- }
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- void type_node::stuff_primary (int x, atom tagname)
- {
- /* given the 'x' parameter from the grammer production, stick the right
- values into the 'primary' and set the 'unsigned' flag if needed. */
-
- static int lookup[15]= { 0, 1,1,1,2,2,3,3,4,5,6,0,7,8,9};
- assert (x >= 0 && x < 15);
- primary= lookup[x];
- tag= tagname; //harmless if not needed
- if (x == 3 || x==5) flags|=16; //mark as unsigned
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
- /* def_node */
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- def_node::def_node (atom n, int store, type_node* t)
- {
- flavor= nf_def;
- name= n;
- storage_class= store;
- type=t;
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- void def_node::print()
- {
- static char* names[]= {"", "static ", "extern ", "typedef ", "auto ", "register " };
- cout << names[storage_class] << atoms[name] << " is ";
- type->print();
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
- /* node list */
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- node_list::node_list()
- {
- count= 0;
- list= malloc ((capacity=8) * sizeof (node*));
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
- node** node_list::access (int x)
- {
- if (x >= capacity) {
- capacity += capacity/2;
- list= realloc (list, capacity * sizeof (node*));
- }
- return list+x;
- }
-
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
- /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
-
-