unit uini;

interface
  procedure iniprol;
  procedure dispnew;
  procedure final;

implementation
uses umain,usp;


procedure initdiv;
var	i:integer;
	nom:ttstring;
begin
	(* init des indicateurs *)

indfait := true;
auto := true;
echo := false;
read_bavard := true;
indput := false;
indword := false;
prompt := '>';
	(* init des fichiers input *)
user := 0;
reinput := 0;
compile := false;
noquest := false;
trace := false;
nspy := 0;
nloop :=0;
	(* init des fichiers reset et rewrite *)
save_txt := 0;
ttyout := 0;
ttyin := 0;

for i:=-1 to max_fich do
	begin
	pfichier[i] := '';
	ifichier[i] := 0;
	efichier[i] := 0;
	cfichier[i] := 0;
	end;
efichier[2]:=2;
cfichier[2]:=p_print;
nom := 'prn';
modcar:=false;
(*mrewrite(fichier[2],nom);*)

rnd_seed:=13;
ioresu := 0;


		(* init des commandes rappelables *)
last_com := 1;
for i := 1 to max_com do
	tabl_com[i] := '';
end;


procedure initsym;
		(* creation des predicats predefinis *)
var	f	:text;
	i,q	:integer;
	chain	:tstring;
	chai	:ttstring;
	car	:char;
	no_cat	:integer;
begin

nmodule := -1;	(* predefinis *)

getmem(table_symbole^[p_nombre].symbol , 1 +2);
table_symbole^[p_nombre].symbol^ := ' ';

nsym:=0;
chai := 'prolog.txt';
mcreset(f,chai);
if ioresu <> 0
then	begin
	writeln(output,'où c''est que t''as mis prolog.txt ?');
	writeln(output,'il devrait être dans le .exe avec le reste');
	end
else	begin
	while not eof(f) do
		begin
		read(f,chain);
		if (chain[1]='*') and (chain[2]=' ')
		then	begin
			car:=char(chain[3]);
			case car of
				'6'	:no_cat:=6;
				'5'	:no_cat:=5;
				'4'	:no_cat:=4;
				'3'	:no_cat:=3;
				'2'	:no_cat:=2;
				'1'	:no_cat:=1;
				'0'	:no_cat:=0;
				end;
			end
		else	begin
			q:=0;
			for i:=1 to length(chain) do
				begin
				if chain[i]=chr(9)
				then	q:=i-1;
				end;

			if q<>0
			then	chain[0]:=ansichar(chr(q));
			chai := chain;
			convertir_mot(chain,no_cat,q);
      (* writeln('ini ',chain,' ',no_cat,' ',q);       *)
			end;
		readln(f);
		end;
	end;
npredef := nsym;
nmodule := -2;	(* main *)

end;

procedure rinitsym;
var	i,j : integer;
begin
fait_libre := 0;
last_libre := 0;
nhsh_coded := 0;
for i := 1 to max_hsh_coded do
	table_coded[i] := nil;
for i := 0 to max_par do
	begin
	for j := 0 to max_hsh do
		begin
		premier_sym[i,j] := 0;
		dernier_sym[i,j] := 0;
		end;
	end;
end;


procedure initgen;
var	i,j	:integer;
begin
nb_erreur := 0;
nfait := 0;
nnfait := 0;
fait_libre := 0;
last_libre := 0;
nhsh_coded := 0;
for i := 1 to max_hsh_coded do
	table_coded[i] := nil;
for i := 0 to max_par do
	begin
	for j := 0 to max_hsh do
		begin
		premier_sym[i,j] := 0;
		dernier_sym[i,j] := 0;
		end;
	end;
for i := 1 to max_glob do
	noeud^[i].fils := i+1;
noeud^[max_glob].fils := 0;
libglob := 1;
glob := false;
nbglob := 0;
nglob := 0;

nnoeud := max_glob;
nbnoeud := max_glob;
writeln(nnoeud,' noeuds');
narbre := 0;
nbarbre := 0;
nenv := 0;
nbenv := 0;
nniveau := 0;
arret := 'n';
access_index := 0;
last_index := 0;

end;

procedure final;
var	i : integer;
begin
for i := 1 to max_fich do
	if cfichier[i] <> 0
	then	close(fichier[i]);

if user = -1
then	user := save_user;
for i := 1 to user do
	close(user_file[i]);
end;

procedure ininew;
var	i,j,k	: integer;
begin
tt_fait := nil;

max_paq := 5;
if max_paq=5
then	new(table_fait)
else	begin
	new(tt_fait);
	table_fait := addr(tt_fait^);
	end;
j := 0;
for i := 1 to max_paq do
	begin
	new(t_fait[i]);
	for k := 1 to nfait_paq do
		begin
		j := j + 1;
		table_fait^[j] := addr(t_fait[i]^[k]);
		end;
	end;
max_fait := max_paq * nfait_paq;
		(* la queue de règle égale uniquement à / *)
new(table_fait^[-1]);
with table_fait^[-1]^ do
	begin
	for i:= 1 to m_cond do
		fait[i] := 0;
	fait[1] := p_coupe0;
	suivant := 0;
	coded_next := 0;
	regle := 0;
	ral_cond := 0;
	end;


new(table_env);
new(table_arbre);
new(noeud);
new(table_symbole);
end;



procedure anacom;
begin
		(* analyse de la ligne de commande *)
phrase := '';
indphrase := 0;

cmd:=paramstr(1);
if cmd <> ''
then	begin
	phrase := cmd;
	com := true;
	end
else	com := false;

if com
then	begin
	if phrase[1]='/'
	then	begin
		voption := char(phrase[2]);
		phrase[1] := ' ';
		phrase[2] := ' ';
		end
	else	voption := ' ';
	if phrase[1] = '$'
	then	phrase[1] := ' '
	else	begin
		phrase := 'load(''' + phrase + ''').';
		end;
	end;

end;

procedure iniprol;
var	st:tstring;
begin
getdir(0,st);
writeln(output,'   ',st);
ininew;
initgen;
initsym;
initdiv;
anacom;

end;

procedure dispnew;
var	i	: integer;
begin
dispose(table_fait^[-1]);
if tt_fait <> nil
then	dispose(tt_fait)
else	dispose(table_fait);
dispose(table_env);
dispose(table_arbre);
dispose(noeud);

for i := 1 to nsym do
	begin
	if table_symbole^[i].symbol <> nil
	then	freemem(table_symbole^[i].symbol ,
			length(table_symbole^[i].symbol^) +2);
	end;

dispose(table_symbole);
for i := 1 to max_paq do
	dispose(t_fait[i]);

for i := 1 to max_hsh_coded do
	begin
	if table_coded[i] <> nil
	then	dispose(table_coded[i]);
	table_coded[i] := nil;
	end;
end;

end.
