home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / rtt / rttnode.c < prev    next >
C/C++ Source or Header  |  1996-03-22  |  6KB  |  265 lines

  1. #include "rtt.h"
  2.  
  3. /*
  4.  * node0 - create a syntax tree leaf node.
  5.  */
  6. struct node *node0(id, tok)
  7. int id;
  8. struct token *tok;
  9.    {
  10.    struct node *n;
  11.  
  12.    n = NewNode(0);
  13.    n->nd_id = id;
  14.    n->tok = tok;
  15.    return n;
  16.    }
  17.  
  18. /*
  19.  * node1 - create a syntax tree node with one child.
  20.  */
  21. struct node *node1(id, tok, n1)
  22. int id;
  23. struct token *tok;
  24. struct node *n1;
  25.    {
  26.    struct node *n;
  27.  
  28.    n = NewNode(1);
  29.    n->nd_id = id;
  30.    n->tok = tok;
  31.    n->u[0].child = n1;
  32.    return n;
  33.    }
  34.  
  35. /*
  36.  * node2 - create a syntax tree node with two children.
  37.  */
  38. struct node *node2(id, tok, n1, n2)
  39. int id;
  40. struct token *tok;
  41. struct node *n1;
  42. struct node *n2;
  43.    {
  44.    struct node *n;
  45.  
  46.    n = NewNode(2);
  47.    n->nd_id = id;
  48.    n->tok = tok;
  49.    n->u[0].child = n1;
  50.    n->u[1].child = n2;
  51.    return n;
  52.    }
  53.  
  54. /*
  55.  * node3 - create a syntax tree node with three children.
  56.  */
  57. struct node *node3(id, tok, n1, n2, n3)
  58. int id;
  59. struct token *tok;
  60. struct node *n1;
  61. struct node *n2;
  62. struct node *n3;
  63.    {
  64.    struct node *n;
  65.  
  66.    n = NewNode(3);
  67.    n->nd_id = id;
  68.    n->tok = tok;
  69.    n->u[0].child = n1;
  70.    n->u[1].child = n2;
  71.    n->u[2].child = n3;
  72.    return n;
  73.    }
  74.  
  75. /*
  76.  * node4 - create a syntax tree node with four children.
  77.  */
  78. struct node *node4(id, tok, n1, n2, n3, n4)
  79. int id;
  80. struct token *tok;
  81. struct node *n1;
  82. struct node *n2;
  83. struct node *n3;
  84. struct node *n4;
  85.    {
  86.    struct node *n;
  87.  
  88.    n = NewNode(4);
  89.    n->nd_id = id;
  90.    n->tok = tok;
  91.    n->u[0].child = n1;
  92.    n->u[1].child = n2;
  93.    n->u[2].child = n3;
  94.    n->u[3].child = n4;
  95.    return n;
  96.    }
  97.  
  98. /*
  99.  * sym_node - create a syntax tree node for a variable. If the identifier
  100.  *  is in the symbol table, create a node that references the entry,
  101.  *  otherwise create a simple leaf node.
  102.  */
  103. struct node *sym_node(tok)
  104. struct token *tok;
  105.    {
  106.    struct sym_entry *sym;
  107.    struct node *n;
  108.  
  109.    sym = sym_lkup(tok->image);
  110.    if (sym != NULL) { 
  111.       n = NewNode(1);
  112.       n->nd_id = SymNd;
  113.       n->tok = tok;
  114.       n->u[0].sym = sym;
  115.       ++sym->ref_cnt;
  116.       /*
  117.        * If this is the result location of an operation, note that it
  118.        *  is explicitly referenced.
  119.        */
  120.       if (sym->id_type == RsltLoc)
  121.          sym->u.referenced = 1;
  122.       return n;
  123.       }
  124.    else
  125.       return node0(PrimryNd, tok);
  126.    }
  127.  
  128. /*
  129.  * comp_nd - create a node for a compound statement.
  130.  */
  131. struct node *comp_nd(tok, dcls, stmts)
  132. struct token *tok;
  133. struct node *dcls;
  134. struct node *stmts;
  135.    {
  136.    struct node *n;
  137.  
  138.    n = NewNode(3);
  139.    n->nd_id = CompNd;
  140.    n->tok = tok;
  141.    n->u[0].child = dcls;
  142.    n->u[1].sym = dcl_stk->tended; /* tended declarations are not in dcls */
  143.    n->u[2].child = stmts;
  144.    return n;
  145.    }
  146.  
  147. /*
  148.  * arith_nd - create a node for an arith_case statement.
  149.  */
  150. struct node *arith_nd(tok, p1, p2, c_int, ci_act, intgr, i_act, dbl, d_act)
  151. struct token *tok;
  152. struct node *p1;
  153. struct node *p2;
  154. struct node *c_int;
  155. struct node *ci_act;
  156. struct node *intgr;
  157. struct node *i_act;
  158. struct node *dbl;
  159. struct node *d_act;
  160.    {
  161.    struct node *n;
  162.  
  163.    /*
  164.     * Insure the cases are what we expect.
  165.     */
  166.    if (c_int->tok->tok_id != C_Integer)
  167.       errt3(c_int->tok, "expected \"C_integer\", found \"", c_int->tok->image,
  168.          "\"");
  169.    if (intgr->tok->image != icontypes[int_typ].id)
  170.       errt3(intgr->tok, "expected \"integer\", found \"", intgr->tok->image,
  171.          "\"");
  172.    if (dbl->tok->tok_id != C_Double)
  173.       errt3(dbl->tok, "expected \"C_double\", found \"", dbl->tok->image,
  174.          "\"");
  175.  
  176.    /*
  177.     * Indicate in the symbol table that the arguments are converted to C
  178.     *  values.
  179.     */
  180.    dst_alloc(c_int, p1);
  181.    dst_alloc(c_int, p2);
  182.    dst_alloc(dbl, p1);
  183.    dst_alloc(dbl, p2);
  184.  
  185.    free_tree(c_int);
  186.    free_tree(intgr);
  187.    free_tree(dbl);
  188.  
  189.    n = node3(TrnryNd, NULL, ci_act, i_act, d_act);
  190.    return node3(TrnryNd, tok, p1, p2, n);
  191.    }
  192.  
  193. struct node *dest_node(tok)
  194. struct token *tok;
  195.    {
  196.    struct node *n;
  197.    int typcd;
  198.  
  199.    n = sym_node(tok);
  200.    typcd = n->u[0].sym->u.typ_indx; 
  201.    if (typcd != int_typ && typcd != str_typ && typcd != cset_typ &&
  202.       typcd != real_typ)
  203.       errt2(tok, "cannot convert to ", tok->image);
  204.    return n;
  205.    }
  206.  
  207.  
  208. /*
  209.  * free_tree - free storage for a syntax tree.
  210.  */
  211. novalue free_tree(n)
  212. struct node *n;
  213.    {
  214.    struct sym_entry *sym, *sym1;
  215.  
  216.    if (n == NULL)
  217.       return;
  218.  
  219.    /*
  220.     * Free any subtrees and other referenced storage.
  221.     */
  222.    switch (n->nd_id) {
  223.       case SymNd:
  224.          free_sym(n->u[0].sym); /* Indicate one less reference to symbol */
  225.          break;
  226.  
  227.       case CompNd:
  228.          /*
  229.           * Compound node. Free ordinary declarations, tended declarations,
  230.           *  and executable code.
  231.           */
  232.          free_tree(n->u[0].child);
  233.          sym = n->u[1].sym;
  234.          while (sym != NULL) {
  235.             sym1 = sym;
  236.             sym = sym->u.tnd_var.next;
  237.             free_sym(sym1);
  238.             }
  239.          free_tree(n->u[2].child);
  240.          break;
  241.  
  242.       case QuadNd:
  243.          free_tree(n->u[3].child);
  244.          /* fall thru to next case */
  245.       case TrnryNd:
  246.          free_tree(n->u[2].child);
  247.          /* fall thru to next case */
  248.       case AbstrNd: case BinryNd: case CommaNd: case ConCatNd: case LstNd:
  249.       case StrDclNd:
  250.          free_tree(n->u[1].child);
  251.          /* fall thru to next case */
  252.       case IcnTypNd: case PstfxNd: case PreSpcNd: case PrefxNd:
  253.          free_tree(n->u[0].child);
  254.          /* fall thru to next case */
  255.       case ExactCnv: case PrimryNd:
  256.          break;
  257.  
  258.       default:
  259.          fprintf(stdout, "rtt internal error: unknown node type\n");
  260.          exit(ErrorExit);
  261.          }
  262.    free_t(n->tok);             /* free token */
  263.    free((char *)n);
  264.    }
  265.