home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-1 / rtt.sit / rttnode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  5.5 KB  |  263 lines  |  [TEXT/MPS ]

  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.     * Indicate in the symbol table that the arguments are converted to C values.
  177.     */
  178.    dst_alloc(c_int, p1);
  179.    dst_alloc(c_int, p2);
  180.    dst_alloc(dbl, p1);
  181.    dst_alloc(dbl, p2);
  182.  
  183.    free_tree(c_int);
  184.    free_tree(intgr);
  185.    free_tree(dbl);
  186.  
  187.    n = node3(TrnryNd, NULL, ci_act, i_act, d_act);
  188.    return node3(TrnryNd, tok, p1, p2, n);
  189.    }
  190.  
  191. struct node *dest_node(tok)
  192. struct token *tok;
  193.    {
  194.    struct node *n;
  195.    int typcd;
  196.  
  197.    n = sym_node(tok);
  198.    typcd = n->u[0].sym->u.typ_indx; 
  199.    if (typcd != int_typ && typcd != str_typ && typcd != cset_typ &&
  200.       typcd != real_typ)
  201.       errt2(tok, "cannot convert to ", tok->image);
  202.    return n;
  203.    }
  204.  
  205.  
  206. /*
  207.  * free_tree - free storage for a syntax tree.
  208.  */
  209. novalue free_tree(n)
  210. struct node *n;
  211.    {
  212.    struct sym_entry *sym, *sym1;
  213.  
  214.    if (n == NULL)
  215.       return;
  216.  
  217.    /*
  218.     * Free any subtrees and other referenced storage.
  219.     */
  220.    switch (n->nd_id) {
  221.       case SymNd:
  222.          free_sym(n->u[0].sym); /* Indicate one less reference to symbol */
  223.          break;
  224.  
  225.       case CompNd:
  226.          /*
  227.           * Compound node. Free ordinary declarations, tended declarations,
  228.           *  and executable code.
  229.           */
  230.          free_tree(n->u[0].child);
  231.          sym = n->u[1].sym;
  232.          while (sym != NULL) {
  233.             sym1 = sym;
  234.             sym = sym->u.tnd_var.next;
  235.             free_sym(sym1);
  236.             }
  237.          free_tree(n->u[2].child);
  238.          break;
  239.  
  240.       case QuadNd:
  241.          free_tree(n->u[3].child);
  242.          /* fall thru to next case */
  243.       case TrnryNd:
  244.          free_tree(n->u[2].child);
  245.          /* fall thru to next case */
  246.       case AbstrNd: case BinryNd: case CommaNd: case ConCatNd: case LstNd:
  247.       case StrDclNd:
  248.          free_tree(n->u[1].child);
  249.          /* fall thru to next case */
  250.       case IcnTypNd: case PstfxNd: case PreSpcNd: case PrefxNd:
  251.          free_tree(n->u[0].child);
  252.          /* fall thru to next case */
  253.       case ExactCnv: case PrimryNd:
  254.          break;
  255.  
  256.       default:
  257.          fprintf(stdout, "rtt internal error: unknown node type\n");
  258.          exit(ErrorExit);
  259.          }
  260.    free_t(n->tok);             /* free token */
  261.    free((char *)n);
  262.    }
  263.