$include "defs.icn"
$include "global_vars.icn"

procedure open_my_files()
    if not(errfile:= open("russ_err.tmp","w")) then stop("cannot open russ_err.tmp")
    if not(reduce := open("reduce.tmp","w")) then stop("cannot open reduce.tmp")
    if not(progress := open("progress.tmp","w")) then stop("cannot open progress.tmp")
    if not(debug_file := open("debug.tmp","w")) then stop("cannot open debug.tmp")
    if not(stats := open("stats.tmp","w")) then stop("cannot open stats.tmp")
#    if not(nodes := open("nodes.tmp","w")) then stop("cannot open nodes.tmp")
    if not(ccode_file := open("ccode.tmp","w")) then stop("cannot open ccode.tmp")
    if not(nodes_created_file := open("nodes.tmp","w")) then stop("cannot open nodes.tmp")
    if not(lists_created_file := open("lists.tmp","w")) then stop("cannot open lists.tmp")
    if not(cons_file := open("cons.tmp","w")) then stop("cannot open cons.tmp")
    return
end
procedure consinfo(msg)
    write(cons_file,msg)
    write(msg)
    write(&errout,msg)
end
procedure listinfo(msg)
    write(lists_created_file,msg)
    write(msg)
    write(&errout,msg)
end
procedure nodeinfo(msg)
    write(nodes_created_file,msg)
    write(msg)
    write(&errout,msg)
end


procedure yyinfo(msg)
    static counter
    initial counter := 0
    counter +:= 1
    if counter > 100000 then
	rpt_err("PROBIBLE INFINITE LOOP max 100000 reached")
    write("INFO: " || msg )
    write(errfile, "INFO: "  || msg)
    write( debug_file  , "INFO: "||  msg )
    return
end

procedure yywarn(msg)
    write( "WARNING: " ||  msg)
    write(progress, "WARNING: " || msg)
    write(errfile , "WARNING: " ||  msg)
    write(reduce , "WARNING: " || msg)
    return
end
procedure yyperror(msg)
    write( "PERROR: " ||  msg)
    write(progress, "PERROR: " || msg)
    write(errfile , "PERROR: " ||  msg)
    write(reduce , "PERROR: " || msg)
    rpt_stats()
    exit(1)
end
procedure yyerror(msg)
    write( "ERROR: " ||  msg)
    write(progress, "ERROR: " || msg)
    write(errfile , "ERROR: " ||  msg)
    write(reduce , "ERROR: " || msg)
    return
end
procedure yylexdebug(msg)
    write( "LEXDEBUG: yystate = " || yystate || " yychar = " || yychar  )
    write(progress, "LEXDEBUG: yystate = " || yystate || " yychar = " || yychar  )
    write(errfile , "LEXDEBUG: yystate = " || yystate || " yychar = " || yychar  )
    write(reduce , "LEXDEBUG: yystate = " || yystate || " yychar = " || yychar  )
    return
end
procedure rpt_err(msg)
    write( "ERROR: " ||  msg)
    write(progress, "ERROR: " || msg)
    write(errfile , "ERROR: " ||  msg)
    write(reduce , "ERROR: " || msg)
    rpt_stats()
    exit(1)
end

procedure rpt_stats()
    statsline("Item","Count")
    statsline("Line Count",line_cnt)
    statsline("Character Count",char_cnt)
    statsline("Token Count",token_cnt)
    statsline("Reserved Id Count",reserved_id_cnt)
    statsline("Reserved Op Count",reserved_op_cnt)
    statsline("Defined Id Count",defined_id_cnt)
    statsline("Defined Op Count",defined_op_cnt)
    statsline("Unique Symbol Count",stt_unique_cnt)
    statsline("Referenced Symbol Count",stt_reference_cnt)
    statsline("Numeral Count",numeral_cnt)
    statsline("Seperator Count",seperator_cnt)
    statsline("Nodes Total Count",nodes_total_cnt)
    statsline("Nodes Made Count",nodes_made_cnt)
    statsline("Comments Count",comments_cnt)
    statsline("Single Quoted Count",sing_quoted_cnt)
    statsline("Double Quoted Count",dbl_quoted_cnt)

    statsline("Lists Total Count",lists_total_cnt)
    statsline("Lists Made Count",lists_made_cnt)
    statsline("RIC Op Codes Count",RIC_op_code_cnt)
    statsline("RIC Table Entry Count",RIC_table_entry_cnt)

    statsline("Parser Actions",parser_actions_cnt)

    statsline("YYN Compute Count",yyn_compute_cnt)
    statsline("Newstate Count",newstate_cnt)
    statsline("Setstate Count",setstate_cnt)
    statsline("Backup Count",backup_cnt)
    statsline("Default Count",default_cnt)
    statsline("Reduce Count",reduce_cnt)
    statsline("Errlab Count",errlab_cnt)
    statsline("Errlab1 Count",errlab1_cnt)
#    dump_value_stack()
#    dump_state_stack()
    return
end


procedure statsline(item,count)
    write(stats, item || repl(" ",40 - *item) || right(count,6))
    return
end


procedure yyclearin()
    yychar:=-1
    return
end

procedure is_present(x)
    if x = PRESENT then return
    else fail
end




##############################################################
#STUBS
##############################################################

# lock is dummy function
procedure lock(it)
    return  it
end
procedure unlock(it)
    return  it
end

procedure rpt_action(yyn)
    write(reduce,"action yyn = " || yyn)
end
procedure vfree()
# do nothing we are counting on unicons garbage collection
    return
end

procedure free_array(ar)
# do nothing we are counting on unicons garbage collection

end

# Followinf to avoid changes to grammar.icn after I changed these to methods
procedure length(l)
    local x
    x := l.length()
    return x
end

procedure first(l)
    l.first()
end
procedure last(l)
    l.last()
end

procedure in(element,list)
    local i
    i := 1
    while (element ~= list[i] & list[i] ~= LIST_END) do {
	i +:= 1
    }
    return (element = list[i])

end








