/* ======================================================================
DESC: 		Checks for membership in PL and defines component structure CS[]
							[this is just a version of isPL() ]
CS provides a connective position's immediate component positions.  comp gives each position's component
while compOrder provides an ordering for connective evaluation.  absMcPos gives the main connective position
of each comp's relative to itself and includes any outside parentheses in the character count..
USAGE NOTES: structureSL()  removes spaces and adds parens if needed, then returns structure().

	isPLsentence(string): boolean.  This function also defines a global variable 'result' which defines whether the string is a sentence, 
	formula with first free variable at position j (first character is 1), or not a sentence.

	The functions here require simp.js, def.js  and mainLO.js to work!!!
====================================================================== */

/* ======================================================================
FUNCTIONS: 	structureSL(), structure()

INPUT:		text string
				
RETURNS:		boolean (true iff text is an SL sentence)

DESC:					
====================================================================== */
var pos; var k;
function structurePL(text)  {
CS = null;CS = new Array;
comp = null; comp = new Array; compOrder = null; compOrder = new Array; absMcPos = null; absMcPos = new Array; bound = null; bound = new Array
k=1; pos = 0
text = deSpace(text);
mainLO(text);
	if ( biC.indexOf(c)>-1 && !out(text)  )   {
			text = '[' + text + ']'; p++
	}
comp[p] = text; absMcPos[p] = p;
compOrder[0] = p; newtext = text; nowtext = text
	return structure(text);
}
function structure(text)  {
	if ( isAtomic(text) )   {
		CS[pos] = text; comp[pos] = text
		return true; 
		}
	else if ( mainLO(text) == '~' )  { 
		gamma = text.substring(1);
		var x = pos
		pos += 1;
		mainLO(gamma);
		if ( biC.indexOf(c) > -1 ) {
   		p++			}
		CS[x] = p + pos;
		var y = CS[x];
		comp[y] = gamma; absMcPos[y] = p
		compOrder[k] = y; k++
		return structure(gamma);
		}
	else if ( ((c == '%' && text.substring(0,2) == '(%')||(c == '^' && text.substring(0,2) == '(^')) &&  isIn( text.charAt(2), variable ) && text.charAt(3) == ')' )  { 
		gamma = text.substring(4);
		var v = text.charAt(2)
		if ( isIn('%' + v , gamma) || isIn('^' + v , gamma) ) return false
		else {
		bound[pos+2] = true 
		for ( var i=5; i<text.length; i++ ) {
   		if ( v == text.charAt(i) ) bound[pos+i] = true
		}
		var x = pos + 1
		pos += 4;
		mainLO(gamma);
		if ( biC.indexOf(c) > -1 ) 	p++
		CS[x] = p + pos;
		var y = CS[x];
		comp[y] = gamma; absMcPos[y] = p
		compOrder[k] = y; k++
		return structure(gamma);
		}
	}	
	else if ( biC.indexOf(c) > -1 && out(text) )  { 
	x = p +  ++pos;
	alpha = text.substring(1,p+1); 
	mainLO(alpha);
		if ( biC.indexOf(c) > -1 ) p++
	y = pos + p
	CS[x] = y
	comp[y] = alpha; absMcPos[y] = p
	compOrder[k] = y; k++
		CS[x] += '|';
	if ( !structure(alpha) )  {
		return false;} 
	else { mainLO(text);
	beta = text.substring(p+2,text.length - 1);
	pos = x + 1;
	mainLO(beta);
		if ( biC.indexOf(c) > -1 ) p++
	l = pos + p;
	CS[x] += '' + l
		comp[l] = beta; absMcPos[l] = p
		compOrder[k] = l; k++
	if ( !structure(beta) )  {
		return false;}  
	else {return true}
}}
else { return false }
}
var varFree, varFreePos
function isPLsentence(string) {
if (structurePL(string)) {
stop = false
y=newtext; j = 0
if ( isIn(mainLO(string),biC) && !out(string) ) j = -1
for ( var i=0; i<y.length; i++ ) {
   if ( isIn(y.charAt(i),variable) && !bound[i] ) {
  	j += i + 1
	result = "The entered text is a formula but with free variable \'" + y.charAt(i) + "\' at position " + j + "." 
	varFree = y.charAt(i)
	varFreePos = i
	stop = true
	break;
	}
}
if ( !stop ) {
	result = "The entered text is a sentence."
	return true
	}
else return false
}
else {
	result = "The entered text is NOT a formula."
   return false
}
}

