
$include "defs.icn"
$include "global_vars.icn"
procedure main(args)
    local flagp,arg,arg_cnt,spcl_to_inline
    &trace := 100000
    &dump := 1
    globals_init()
    rpt_info("Hello Russell!")
#   Initialize flags */
    dloadflag := initflag := BADflag := aflag := yydebug := FALSE
#    Initialize file name pointers. */
    ifname := ofname := sigfname := optfname := actual_ifname := ""
# Process Command Line Arguments
    arg_cnt := 1
    every(arg := !args) do {
	write("arg = ", arg)
	write("arg[1:2] = ", arg[1:2])
        case arg[1:2] of { # case 1
	    "-" : {  # -
		       write("case - ")
		       flagp := arg[2:0]
		       write("flagp = ",flagp)
		       case flagp of {   #case 2
			   "B": BADflag +:= 1
			   "D": yydebug +:= 1
#		     "S": catchsigs()
			   "i": initflag := TRUE
			   "d":  {initflag := TRUE
				  dloadflag := TRUE
			      }
			   "a":  aflag := TRUE
			   "M":   Mflag := TRUE
			   "c":   cflag := TRUE
			   "p":   pflag := TRUE
			   "L":   Lflag := TRUE
			   "P":   Pflag := TRUE
			   "T":   Tflag := TRUE
			   "G":    Gflag := TRUE
			   "V":   Vflag := TRUE
			   "N":   Nflag := TRUE
			   "f":   fflag := TRUE
			   "F": { fflag := TRUE
				  Fflag := TRUE }
			   "R":  Rflag := TRUE
			   "O": {
			       if (OOflag = TRUE) then
				   OOOflag := TRUE
			       else if (Oflag = TRUE) then
				   OOflag := TRUE
			       else 
				   Oflag := TRUE
			   }
			   "X": Xflag := TRUE
			   "x": xflag := TRUE
			   "h": hflag := TRUE
			   default: {
			       write("illegal flag")
#			       rpt_err("Rc: illegal flag option: %c\n", flagp)
			   }
		       } # -
		       }
	    default: {
		write("2nd default")
		write("arg_cnt = ", arg_cnt)
		write("ifname = ", ifname)
		write("ofname = ", ofname)
		write("sigfname = ", sigfname)
		write("optfname = ", optfname)
		if ( ifname == "") then {
		    ifname := arg
		    actual_ifname := arg
		    arg_cnt +:= 1
		}
		else if (ofname == "") then {
		    ofname := arg
		    arg_cnt +:= 1
		}
		else if (sigfname == "" & cflag = TRUE) then {
		    sigfname := arg
		    arg_cnt +:= 1
		}
		else if (optfname == "" & OOflag = TRUE) then {
		    optfname := arg
		}
		else {
		    write(&errout, "Rc: Too many arguments\n")
		    exit(1)
		}
	    }
	} # case 1
	# every
	}
    if (ofname == "") then {
	write(&errout, "Rc: Too few arguments\n")
	exit(1)
    }
    
    if (cflag = TRUE & sigfname == "") then {
	write(&errout, "Rc: Missing file name for signature info\n")
	exit(1)
    }
    if (OOflag = TRUE & optfname == "") then {
	write(&errout, "Rc: Missing file name for optimization info\n")
	exit(1)
    }
    
    if (Xflag = TRUE & Gflag ~= TRUE) then {
	write(&errout, "Vax code generator cannot be used with -X\n")
	exit(1)
    }
    
    if (Mflag = TRUE & Rflag = TRUE ) then {
	write (&errout, "-M contradicts -R, ignoring both\n")
	Mflag := Rflag := FALSE
    }


#  Initialize 
    if (/insrtptr) then # no previous initialization has taken place 
    {
	initflag := TRUE # added DJD	initids()
	if (Gflag = TRUE) then {
	    init_RIC_table()
#	    insrtptr := TRUE # added DJD
	}
    }
#     open input file 
    if not(infile:= open(ifname,"r")) then stop("cannot open ifname = " || ifname)
    if (OOflag & optfname ~== "") then
	if not(optfile:= open(optfname,"w")) then stop("cannot open optfname = " || optfname)
# Set up entry_name 
    if (cflag | Xflag) then {
	
	entry_name := actual_ifname[0:-2]
	# Replace all slashes by periods. 
	map(entry_name,"/",".")
    } else {
        entry_name := "";
    }
    
    # Set up correct line number, file name 
#    if (pflag = FALSE) then {
        yyinfnm := stt_enter(ifname, *ifname)
        yyline := 0
#        addposition(yyinfnm, yyline)
#    }

    if (Gflag = FALSE) then { Rflag := FALSE }

    # Set up correct input translation for in-line code 
    inline_cnvt := Ginline_cnvt
#
#      Build syntax tree (pass 1).
#
    if (VERBOSE = 1) then
	yyinfo("Calling yyparse")
    nodes_made_cnt := 0 
    lists_made_cnt := 0 

    yyparse()
    if initflag = TRUE then {
	# Write image & exit
	rpt_err("Write image not implemented")
    }


# CHECK insrtptr
    write(treefile,"DUMP OF insrtptr NODE ")
    if \insrtptr then
	insrtptr.dump(treefile)
    write(treefile,"END DUMP OF insrtptr NODE ")


    if( yynerrs ~= 0 ) then
        rpt_err("Parse Failed")

# SKIPPED PARTS I didn't think I could do ENDPASS1

    yyinfo("*** End of Russell parse")
    write(treefile, "main stxtree = " || dump_info(stxtree))
    write(treefile,"DUMP OF TREE ROOT NODE ")
    stxtree.dump(treefile)
    write(treefile,"END DUMP OF TREE ROOT NODE ")
    visited := set()
    write(treefile,"DUMP OF TREE")
    proc_dump_tree("root","root",stxtree)
    write(treefile,"END DUMP OF TREE")

    build_Idtable()
    list_Idtable()
    if( yynerrs ~= 0 ) then
	exit(1)

    #
    #  Pass2 - build symbol table
    # 
    yyinfo( "Building symbol table")

    build_symtab( stxtree )
    if (yynerrs ~= 0) then exit(2)

    #
    #  Pass3 - number tree nodes and fix up tree
    #
    
    yyinfo("Fixing up syntax tree")

    fixup(stxtree)
    if (yynerrs ~= 0) then exit(3)
    visited := set()
    write(treefile,"DUMP OF TREE AFTER FIXUP")
    proc_dump_tree("root","root",stxtree)
    write(treefile,"END DUMP OF TREE AFTER FIXUP")


    #
    # Pass4 - signature deduction and checking
    #
    # Set up the correct routine for in_line code
    spcl_to_inline := Gspcl_to_inline
    yyinfo("Checking signatures")
    tl_checksigs(stxtree)
    if (yynerrs ~= 0) then exit(1)

    visited := set()
    write(treefile,"DUMP OF TREE AFTER CHECKING SUGNATURES")
    proc_dump_tree("root","root",stxtree)
    write(treefile,"END DUMP OF TREE AFTER CHECKING SIGNATURES")
    #
    #  Pass 5 - abstract syntax tree or code generation
    #
    yyinfo("Generating code")
    if not(ofile := open("ofname","w")) then stop("cannot open ofname = " || ofname)
    # generate vax or intermediate code 
    # Partial compilation - need to write out signature 
    # and external file info                            
    Ggeneratepcode( ofile, stxtree,first_elmnt(bld_den_seq_f(insrtptr)) )
    if (yynerrs > 0) then exit(1)
    if not(sigfile := open("sigfname","w")) then stop("cannot open sigfname = " || sigfname)

    # Copy object file list to signature file 
    # Terminate with empty line               

    # Probably still open for writing. 
    # Close and reopen                 
    if (\objfilelist) then {
	close(objfilelist)
    }

    if not(objfilelist := open(OBJFILELIST,"r")) then stop("cannot open OBJFILELIST = " || OBJFILELIST)
    if (\objfilelist) then {
	while (line := read(objfilelist)) do {
	    write( sigfile, line)
	}
	       write( sigfile,"")
	   }
    sig_out(sigfile, signature_f(insrtptr))
    close(sigfile)
    Ggeneratecode( ofile, stxtree )
    if (yynerrs > 0) then exit(1)
    close( ofile )
    rpt_stats()
    exit(0)
end


