# genquest.awk, 12 Apr 04
#

function new_page()
{
print ".bp"
if (new_subject == 1)
   {
   print ".tl '#'\\s-1%\\s+1'\\s-2accu: apr 04\\s+2'"
   new_subject=0
   }
else
   print ".tl ' '\\s-1%\\s+1'\\s-2accu: apr 04\\s+2'"
}

function print_line(line_str)
{
print line_str
print ".br"
}

function rand_items(items)
{
for (v in items)
   {
   items[v]=rand() " " items[v]
   }
asort(items)
for (v in items)
   {
   sp_pos=index(items[v], " ")
   items[v]=substr(items[v], sp_pos)
   }
}

function gen_c1()
{
c[1]="5"
c[2]="6"
c[3]="8"
c[4]="9"
rand_items(c)
}

function gen_c2()
{
c[1]="58"
c[2]="62"
c[3]="83"
c[4]="94"
rand_items(c)
}

function gen_v1_let()
{
var_info[1]="w"
var_info[2]="x"
var_info[3]="y"
var_info[4]="z"
rand_items(var_info)
}

function gen_v1_word()
{
var_info[1]="van"
var_info[2]="guy"
var_info[3]="tip"
var_info[4]="mud"
rand_items(var_info)
}

function gen_v1()
{
if (v1_flag == 0)
   {
   gen_v1_let()
   v1_flag=int(2*rand())
   }
else
   {
   gen_v1_word()
   v1_flag=int(2*rand())
   }
for (v in v1)
   var_info[v]=v1[v]
}

function gen_v3_let()
{
var_info[1]="vcq"
var_info[2]="qmt"
var_info[3]="bfj"
var_info[4]="rpl"
rand_items(var_info)
}

function gen_v3_word()
{
var_info[1]="conception"
var_info[2]="suspend"
var_info[3]="prevented"
var_info[4]="liberation"
rand_items(var_info)
}

function gen_v3()
{
if (v3_flag == 0)
   {
   gen_v3_let()
   v3_flag=int(2*rand())
   }
else
   {
   gen_v3_word()
   v3_flag=int(2*rand())
   }
}

function gen_info()
{
split($2, var_name, " ")
if (var_name[1] == "v1")
   gen_v1()
else
   gen_v3()
if (var_name[2] == "c1")
   gen_c1()
else
   gen_c2()

print ".sp 12l"
print_line(var_info[1] " = " c[1] ";")
print_line(var_info[2] " = " c[2] ";")
print_line(var_info[3] " = " c[3] ";")
new_page()
}

function gen_if()
{
if_ind=int(6*rand())
if_cond=$3
sub(" ", "", if_cond)
gsub(/A/, A[if_ind], if_cond)
gsub(/E/, E[if_ind], if_cond)
gsub(/U/, U[if_ind], if_cond)

print ".sp 12l"
print_line("if (" if_cond ")				XXXXXXXXXXXXXXXXXX")

if (ift == 0)
   if_test=A[if_ind] " < " U[if_ind]
else if (ift == 1)
   if_test=A[if_ind] " > " U[if_ind]
else if (ift == 3)
   if_test=U[if_ind] " < " A[if_ind]
else
   if_test=U[if_ind] " > " A[if_ind]
ift=(ift+1) % 4

print_line("   if (" if_test ")				XXXXXXXXXXXXXXXXXX")
print_line("				XXXXXXXXXXXXXXXXXX")
#print ".sp 1l"
print_line("      .............")
print_line("   else")
print ".sp 1l"
print_line("      .............")
}

function gen_mem_test()
{
print ".sp 6l"
print "\\h'1.2i'     remember  \\h'0.5i' would refer back\\h'0.5i' not seen"
print ".sp 1l"
for (v in var_info)
   {
   print var_info[v] " = \t____\t__\t__"
   print ".sp 1l"
   }
new_page()
}

BEGIN {
	TEMP_FILE=systime() ".tmp"
	FS="|"
	A[0]="a" ; E[0]="e" ; U[0]="u";
	A[1]="a" ; E[1]="u" ; U[1]="e";
	A[2]="e" ; E[2]="a" ; U[2]="u";
	A[3]="e" ; E[3]="u" ; U[3]="a";
	A[4]="u" ; E[4]="e" ; U[4]="a";
	A[5]="u" ; E[5]="a" ; U[5]="e";
	v1_flag=0
	v3_flag=0
	new_subject=0
	seen_subject=0
	print ".pl +11.5i"
	print ".fz +2"
	print ".fam H"
	print ".ta 1.5i 2.9i 4.3i 5.0i"
	print ".ll 7.5i"
	print ".tl '#'\\s-1%\\s+1'\\s-2accu: apr 04\\s+2'"
	}

$0 == "SUBJECT" {
	new_subject=1
	next
	}

	{
	gen_info()
	gen_if()
	gen_mem_test()
	new_subject=0
	next
	}

END {
	}
