###############################################################################
#
#	File:     reorder.icn
#
#	Subject:  russell language support procedures
#
#	Author:   Dennis J Darland
#
#	Date:     October - Novmber, 2015
#
###############################################################################
#
# Copyright 1986-1989 Hans-Juergen Boehm, Alan J. Demers, Kumar Srikantan,
# Vernon Lee, and Lucy Hederman.
# Copyright Digital Equipment Corporation & INRIA 1988, 1989.
# Copyright (c) 1990-1993 by Xerox Corporation.  All rights reserved.
#
# This material is provided as is, with no warranty expressed or implied.
# Any use is at your own risk.
# 
# Permission is hereby granted to copy this compiler for any non-commercial
# purpose, provided the above notices are retained on all copies.  Further
# restrictions apply to the DEC/INRIA BigNum package.  These are stated in
# the file runtime/BigNum/doc/bnbody.tex.
#
# This work uses the BigNum package developed jointly by INRIA and
# Digital PRL.  It has been modified by Hans-J. Boehm in August 1990
# and February 1993 at Xerox PARC.  When you receive this software,
# please send mail to librarian@prl.dec.com to inform them that you
# have a copy of the package.
###############################################################################
#
#   NOTE From Dennis J. Darland (11/2/2015)
#
#   I obtained the "c" language source code for "russell" copyrighted above
#   in the 1990's. I have made efforts to get it to work over the years, but
#   always failed. It has many hardware and OS dependancies. Now (October 2015
#   forward, I am tring to port to the language "Unicon". Practically every
#   line of code will need to be changed, as someing as simple as the
#   assignment operator is different. Also I will not use the BigNum package
#   mentioned above. Also I am doing this not for profit, and am sharing
#   freely. I am not at all confident that I will get it working well, but
#   believe in open source, so am sharing my work as I make progress.
#   Unicon is also much more portable.
#
###############################################################################

# Reorder type signatures, conditionals, and loops.  Reordering of   
# type signatures is essential since subsequent passes assume a      
# normal form.  Reordering of conditionals and loops generalizes     
# signature matching and makes sure the programmer doesn't rely      
# on sequential evaluation of guarded commands.  It also makes life  
# easier for the code generator, since else guards will be permuted  
# to the end.                                                        


$include "defs.icn"
$include "global_vars.icn"
# Fix up the subtree headed by p 
procedure reorder(p)
    local  q, v, temp, e, cnt   # q pointer to next field of p to be recursively     
    # examined.                                        
    # register int v     # bit vector specifying primary link fields of *p  
    # shifted so that the most significant bit         
    # corresponds to q.                                

    if (/p)  then return TRUE
    # recursively examine subtrees
    if numeric(p) then return p
    if (is_list(p)) then {
	temp := p.lh.first
	while \temp do {
	    e := temp
	    reorder(e)
	    temp := temp.cn_tl_field
	}  
    } 
    else 
	{
	cnt := 1
	every fld := fieldnames(p) do 
	    {
		if should_examine(fld) then
		    {
			reorder(p[cnt])
		    }
		cnt +:= 1
	    }
    }
    # Now massage the subtree in question 
    case( p . kind ) of {
	GUARDEDLIST:
	    gden_order(p)
	LOOPDENOTATION:
	    # reorder list of guarded denotations 
	gden_order(p)
	TYPESIGNATURE:
	    # reorder type signature 
	tsig_order(p)
    }
return TRUE
end

