/* Création d'une table de vérité.
   $VER: 1.00 (Samedi 4 Avril 1998, © R.Florac) */

options results     /* indispensable pour récupérer le résultat des macros */

signal on error     /* pour l'interception des erreurs */
signal on syntax

'ASK("Nombre d''entrées")'; nbe=result
if nbe="" then exit

l=0
do i=1 to nbe
    'ASK("Nom de l''entrée 'i'")'; nomE.i=result
    'TXWIDTH("'nomE.i'")'; lc=result
    if lc>l then l=lc
end

'ASK("Nombre de sorties")'; nbs=result
if nbs="" then exit

do i=1 to nbs
    'ASK("Nom de la sortie 'i'")'; nomS.i=result
    'TXWIDTH("'nomS.i'")'; lc=result
    if lc>l then l=lc
end

'SETSCALE(0,1,1):ROTATE(0,0):SYMMETRY(0,0):DRAWMODE(1)'

nbl=2**nbe	/* nombre de lignes */

l=l+20
do i=1 to nbe
    cole.i=10+l*i
    '(TXWIDTH("'nomE.i'")-TXWIDTH("0"))/2'
    'MARK(WRITE("'nomE.i'", 'cole.i-result', 20))'
end

do i=1 to nbs
    cols.i=10+l*(i+nbe)
    '(TXWIDTH("'nomS.i'")-TXWIDTH("0"))/2'
    'MARK(WRITE("'nomS.i'", 'cols.i-result', 20))'
end

do c=1 to nbe
    ng=2**c
    ligne=1
    do i=1 to ng/2
	do j=1 to nbl/ng
	    'MARK(WRITE("0", 'cole.c', 20+'ligne'*15))'
	    val.c.ligne=0
	    ligne=ligne+1
	end
	do j=j to nbl/ng*2
	    'MARK(WRITE("1", 'cole.c', 20+'ligne'*15))'
	    val.c.ligne=1
	    ligne=ligne+1
	end
    end
end

/* Tracé des lignes verticales */
col=cole.1-l%2+5
do c=1 to nbe+nbs+1
    select
	when c=1 then 'DRAWMODE(2)'
	when c=nbe+1 then 'DRAWMODE(2)'
	when c=nbe+nbs+1 then 'DRAWMODE(2)'
	otherwise 'DRAWMODE(1)'
    end
    'MARK(DRAW('col',8,'col',23+'nbl'*15))'
    col=col+l
end

/* Tracé des lignes horizontales */
ligne=8
do i=1 to nbl+2
    select
	when i=1 then 'DRAWMODE(2)'
	when i=2 then 'DRAWMODE(2)'
	when i=nbl+2 then 'DRAWMODE(2)'
	otherwise 'DRAWMODE(1)'
    end
    'MARK(DRAW('cole.1-l%2+5','ligne','col-l','ligne'))'
    ligne=ligne+15
end

/* Remplissage éventuel de la table */
'DRAWMODE(1):REQUEST("Voulez-vous remplir la table?")'
if result=1 then do
    'DEF OR(A,B)=IF(A+B,1,0)'
    'DEF AND(A,B)=IF(A*B,1,0)'
    'DEF NOT(A)=IF(A,0,1)'
    'DEF NAND(A,B)=NOT(AND(A,B))'
    'DEF NOR(A,B)=NOT(OR(A,B))'
    'DEF XOR(A,B)=IF(A+B==1,1,0)'
    do i=1 to nbs
	'ASK("Équation de 'nomS.i'?"+CHR(10)+"Vous pouvez utiliser les"+CHR(10)+"fonctions NOT(A),"+CHR(10)+"AND(A,B), OR(A,B), XOR(A,B)"+CHR(10)+"NAND(A,B) et NOR(A,B)."+CHR(10)+"Utilisez les noms des variables"+CHR(10)+"tels que vous les avez donnés.")'
	eq=result
	if eq~= "" then do
	    do ligne=1 to nbl
		do j=1 to nbe
		    'EXEC("'nomE.j'='val.j.ligne'")'
		end
		'EXEC("'eq'")'; r=result
		'MARK(WRITE("'r'",'cols.i','20+ligne*15'))'
	    end
	end
    end
end

exit

/* Traitement des erreurs, interruption du programme */
syntax:
erreur=RC
'MESSAGE("Script TableVérité"+CHR(10)+"Erreur de syntaxe"+CHR(10)+"en ligne 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
exit

error:
'MESSAGE("Script TableVérité"+CHR(10)+"Erreur en ligne 'SIGL'")'
exit
