home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ucb_logoppc / source / paren.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-25  |  8.9 KB  |  304 lines

  1. /*
  2.  *      paren.c            logo parenthesizing module        dko
  3.  *
  4.  *    Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *  
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *  
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include "logo.h"
  23. #include "globals.h"
  24.  
  25. NODE *the_generation;
  26.  
  27. /* Set the line pointer for a tree.
  28.  */ 
  29. void make_line(NODE *tree, NODE *line) {
  30.     setobject(tree, line);
  31.     settype(tree, LINE);
  32. }
  33.  
  34. void untreeify(NODE *node) {
  35.     settreepair__tree(node, NIL);
  36.     settype(node, CONS);
  37. }
  38.  
  39. void untreeify_line(NODE *line) {
  40.     if (line != NIL && is_list(line)) {
  41.     untreeify_line(car(line));
  42.     untreeify_line(cdr(line));
  43.     untreeify(line);
  44.     }
  45. }
  46.  
  47. void untreeify_proc(NODE *procname) {
  48.  
  49.     NODE *body = bodylist__procnode(procnode__caseobj(procname));
  50.     NODE *body_ptr;
  51.  
  52.     for (body_ptr = body; body_ptr != NIL; body_ptr = cdr(body_ptr)) {
  53.     untreeify_line(car(body_ptr));
  54.     }
  55.     untreeify(body);
  56. }
  57.  
  58. /* Treeify a body by appending the trees of the lines.
  59.  */ 
  60. void make_tree_from_body(NODE *body) {
  61.  
  62.     NODE *body_ptr, *end_ptr = NIL, *tree = NIL;
  63.  
  64.     if (body == NIL ||
  65.     (is_tree(body) && generation__tree(body) == the_generation))
  66.         return;
  67.     for (body_ptr = body; body_ptr != NIL; body_ptr = cdr(body_ptr)) {
  68.     tree = car(body_ptr);
  69.     if (tree == NIL) continue;  /* skip blank line */
  70.     this_line = tree;
  71.     make_tree(tree);
  72.     if (is_tree(tree)) {
  73.         tree = tree__tree(tree);
  74.         make_line(tree, car(body_ptr));
  75.         if (end_ptr == NIL)
  76.         settree__tree(body, tree);
  77.         else
  78.         setcdr(end_ptr, tree);
  79.         if (generation__tree(car(body_ptr)) == UNBOUND)
  80.         setgeneration__tree(body, UNBOUND);
  81.         untreeify(car(body_ptr));
  82.         while (cdr(tree) != NIL)
  83.         tree = cdr(tree);
  84.         end_ptr = tree;
  85.     } else {    /* error while treeifying */
  86.         untreeify(body);
  87.         return;
  88.     }
  89.     }
  90.     settype(body, TREE);
  91. }
  92.  
  93. BOOLEAN tree_dk_how;
  94.  
  95. /* Treeify a list of tokens (runparsed or not).
  96.  */ 
  97. void make_tree(NODE *list) {
  98.  
  99.     NODE *tree = NIL;
  100.     NODE *paren_line(NODE *);
  101.  
  102.     if (list == NIL ||
  103.     (is_tree(list) && generation__tree(list) == the_generation))
  104.         return;
  105.     if (!runparsed(list)) make_runparse(list);
  106.     tree_dk_how = FALSE;
  107.     tree = paren_line(parsed__runparse(list));
  108.     if (tree != NIL && tree != UNBOUND) {
  109.     settype(list, TREE);
  110.     settree__tree(list, tree);
  111.     if (tree_dk_how || stopping_flag==THROWING)
  112.         setgeneration__tree(list, UNBOUND);
  113.     }
  114. }
  115.  
  116.  
  117. /* Fully parenthesize a complete line, i.e. transform it from a flat list
  118.  * to a tree.
  119.  */ 
  120. NODE *paren_line(NODE *line) {
  121.  
  122.     NODE *retval = NIL;
  123.     NODE *paren_expr(NODE **expr, BOOLEAN inparen);
  124.     NODE *paren_infix(NODE *left, NODE **rest, int old_pri, BOOLEAN inparen);
  125.  
  126.     if (line == NIL) return line;
  127.     retval = paren_expr(&line, FALSE);
  128.     if (NOT_THROWING && retval != UNBOUND) {
  129.     retval = paren_infix(retval, &line, -1, FALSE);
  130.     retval = cons(retval, paren_line(line));
  131.     }
  132.     return retval;
  133. }
  134.  
  135. /* Parenthesize an expression.  Set expr to the node after the first full
  136.  * expression.
  137.  */ 
  138. NODE *paren_expr(NODE **expr, BOOLEAN inparen) {
  139.  
  140.     NODE *first = NIL, *tree = NIL, *proc, *retval;
  141.     NODE **ifnode = (NODE **)NIL;
  142.     NODE *gather_args(NODE *, NODE **, BOOLEAN, NODE **);
  143.     NODE *paren_infix(NODE *, NODE **, int, BOOLEAN);
  144.  
  145.     if (*expr == NIL) {
  146.     if (inparen) err_logo(PAREN_MISMATCH, NIL);
  147.     return *expr;
  148.     }
  149.     first = car(*expr);
  150.     pop(*expr);
  151.     if (nodetype(first) == CASEOBJ && !numberp(first)) {
  152.     if (first == Left_Paren) {
  153.         tree = paren_expr(expr, TRUE);
  154.         tree = paren_infix(tree, expr, -1, TRUE);
  155.         if (*expr == NIL)
  156.         err_logo(PAREN_MISMATCH, NIL);
  157.         else if (car(*expr) != Right_Paren)
  158.         {
  159.         int parens;
  160.  
  161.         if (NOT_THROWING) err_logo(TOO_MUCH, NIL);    /* throw the rest away */
  162.         for (parens = 0; *expr; pop(*expr))
  163.             if (car(*expr) == Left_Paren)
  164.             parens++;
  165.             else if (car(*expr) == Right_Paren)
  166.             if (parens-- == 0) break;
  167.         }
  168.         else
  169.         pop(*expr);
  170.         retval = tree;
  171.     } else if (first == Right_Paren) {
  172.         err_logo(UNEXPECTED_PAREN, NIL);
  173.         if (inparen) push(first, *expr);
  174.         retval = NIL;
  175.     } else if (first == Minus_Sign) {
  176.         push(Minus_Tight, *expr);
  177.         retval = paren_infix(make_intnode((FIXNUM) 0), expr, -1, inparen);
  178.     } else {    /* it must be a procedure */
  179.         if (procnode__caseobj(first) == UNDEFINED && NOT_THROWING &&
  180.         first != Null_Word)
  181.             silent_load(first, NULL);    /* try ./<first>.lg */
  182.         if (procnode__caseobj(first) == UNDEFINED && NOT_THROWING &&
  183.         first != Null_Word)
  184.             silent_load(first, logolib); /* try <logolib>/<first> */
  185.         proc = procnode__caseobj(first);
  186.         if (proc == UNDEFINED && NOT_THROWING) {
  187.         retval = cons(first, NIL);
  188.         } else if (nodetype(proc) == INFIX && NOT_THROWING) {
  189.         err_logo(NOT_ENOUGH, first);
  190.         retval = cons(first, NIL);
  191.         } else {
  192.         /* Kludge follows to turn IF to IFELSE sometimes. */
  193.         if (first == If) {
  194.             ifnode = &first;
  195.         }
  196.         retval = gather_args(proc, expr, inparen, ifnode);
  197.         if (retval != UNBOUND) {
  198.             retval = cons(first, retval);
  199.         }
  200.         }
  201.     }
  202.     } else if (is_list(first)) {   /* quoted list */
  203.     retval = make_quote(first);
  204.     } else {
  205.     return first;
  206.     }
  207.     return retval;
  208. }
  209.  
  210. /* Gather the correct number of arguments to proc into a list.  Set args to
  211.  * immediately after the last arg.
  212.  */ 
  213. NODE *gather_args(NODE *proc, NODE **args, BOOLEAN inparen, NODE **ifnode) {
  214.  
  215.     int min, max;
  216.     NODE *gather_some_args(int, int, NODE **, BOOLEAN, NODE **);
  217.     
  218.     if (nodetype(proc) == CONS) {
  219.     min = (inparen ? getint(minargs__procnode(proc))
  220.                : getint(dfltargs__procnode(proc)));
  221.     max = (inparen ? getint(maxargs__procnode(proc))
  222.                : getint(dfltargs__procnode(proc)));
  223.     } else { /* primitive */
  224.     min = (inparen ? getprimmin(proc) : getprimdflt(proc));
  225.     if (min < 0) {        /* special form */
  226.         return (*getprimfun(proc))(*args);
  227.     }
  228.     /* Kludge follows to allow EDIT and CO without input without paren */ 
  229.     if (getprimmin(proc) == OK_NO_ARG) min = 0;
  230.     max = (inparen ? getprimmax(proc) : getprimdflt(proc));
  231.     }
  232.     return gather_some_args(min, max, args, inparen, ifnode);
  233. }
  234.  
  235. /* Make a list of the next n expressions, where n is between min and max.
  236.  * Set args to immediately after the last expression.
  237.  */ 
  238. NODE *gather_some_args(int min, int max, NODE **args, BOOLEAN inparen,
  239.                NODE **ifnode) {
  240.     NODE *paren_infix(NODE *left, NODE **rest, int old_pri, BOOLEAN inparen);
  241.  
  242.     if (*args == NIL || car(*args) == Right_Paren ||
  243.         (nodetype(car(*args)) == CASEOBJ &&
  244.          nodetype(procnode__caseobj(car(*args))) == INFIX)) {
  245.     if (min > 0) return cons(Not_Enough_Node, NIL);
  246.     } else if (max == 0) {
  247.     if (ifnode != (NODE **)NIL && is_list(car(*args))) {
  248.         /* if -> ifelse kludge */
  249.         NODE *retval;
  250.         err_logo(IF_WARNING, NIL);
  251.         *ifnode = Ifelse;
  252.         retval = paren_expr(args, FALSE);
  253.         retval = paren_infix(retval, args, -1, inparen);
  254.         return cons(retval, gather_some_args(min, max, args,
  255.                          inparen, (NODE **)NIL));
  256.     }
  257.     } else {
  258.     if (max < 0) max = 0;   /* negative max means unlimited */
  259.     if (car(*args) != Right_Paren &&
  260.         (nodetype(car(*args)) != CASEOBJ ||
  261.          nodetype(procnode__caseobj(car(*args))) != INFIX)) {
  262.         NODE *retval = paren_expr(args, FALSE);
  263.         retval = paren_infix(retval, args, -1, inparen);
  264.         return cons(retval, gather_some_args(min - 1, max - 1, args,
  265.                          inparen, ifnode));
  266.     }
  267.     }
  268.     return NIL;
  269. }
  270.  
  271. /* Calculate the priority of a procedure.
  272.  */ 
  273. int priority(NODE *proc_obj) {
  274.  
  275.     NODE *proc;
  276.  
  277.     if (proc_obj == Minus_Tight) return PREFIX_PRIORITY+4;
  278.     if (nodetype(proc_obj) != CASEOBJ ||
  279.     (proc = procnode__caseobj(proc_obj)) == UNDEFINED ||
  280.     nodetype(proc) != INFIX)
  281.         return 0;
  282.     return getprimpri(proc);
  283. }
  284.  
  285. /* Parenthesize an infix expression.  left_arg is the expression on the left
  286.  * (already parenthesized), and rest is a pointer to the list starting with the
  287.  * infix procedure, if it's there.  Set rest to after the right end of the
  288.  * infix expression.
  289.  */ 
  290. NODE *paren_infix(NODE *left_arg, NODE **rest, int old_pri, BOOLEAN inparen) {
  291.  
  292.     NODE *infix_proc, *retval;
  293.     int pri;
  294.  
  295.     if (*rest == NIL || !(pri = priority(infix_proc = car(*rest)))
  296.              || pri <= old_pri) 
  297.     return left_arg;
  298.     pop(*rest);
  299.     retval = paren_expr(rest, inparen);
  300.     retval = paren_infix(retval, rest, pri, inparen);
  301.     retval = cons_list(0,infix_proc, left_arg, retval, END_OF_LIST);
  302.     return paren_infix(retval, rest, old_pri, inparen);
  303. }
  304.