$include "defs.icn"
$include "global_vars.icn"
procedure precedence(p) # return the operator precedence associated with
    # denotation p.                                  */
     local stti, lvl
     yyinfo("precedence p = " || type(p))
     case (p.kind) of {
	 BLOCKDENOTATION : {
	  if /p.bld_precedence then
	     p.bld_precedence := 0
	     return(p.bld_precedence)
	 }
	 USELIST: {
	  if /p.bld_precedence then
	     p.bld_precedence := 0
	     return(p.usl_precedence)
      }
	 WORDCAND:
	     return(2)
	 WORDCOR:
	     return(1)
	 OPRID: {
	     if /(p.id_str_table_index) then
		 p.id_str_table_index := 0
	     stti := p.id_str_table_index

	     if (stti > pend[NLEVELS])
	     then
		 {
		     if ((p.kind) = OPRID)
		     then
			 {
			     return(3) # default precedence
			 }
		     else

				 {
				     return(INFINITE)
				 }
		 }
	     lvl := 0
	     while stti > pend[1 + lvl] do
		 lvl +:= 1
	     return(lvl)
	 }



	 LETTERID : {
	     if /(p.id_str_table_index) then
		 p.id_str_table_index := 0
	     stti := p.id_str_table_index
	     if (stti > pend[NLEVELS])
	     then
		 {
		     if ((p.kind) = OPRID)
		     then
			 {
			     return(3) # default precedence
			 }
		     else

				 {
				     return(INFINITE)
				 }
		 }
	     lvl := 0
	     while stti > pend[lvl + 1] do
		 lvl +:= 1
	     return(lvl)

	 }

	 default :
	     return(INFINITE)
     }
 end
