being assertive

This commit is contained in:
Denis Molony 2017-03-18 20:21:11 +11:00
parent 76a220acd3
commit eff78e0108
31 changed files with 125 additions and 82 deletions

View File

@ -8,6 +8,8 @@ public class Abs extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ABS(") : text;
source = new Expression (parent, cell, functionText); source = new Expression (parent, cell, functionText);
values.add (source); values.add (source);
} }

View File

@ -8,6 +8,8 @@ public class Acos extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ACOS(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -11,6 +11,8 @@ class And extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@AND(") : text;
String remainder = functionText; String remainder = functionText;
while (true) while (true)
{ {

View File

@ -8,6 +8,8 @@ public class Asin extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ASIN(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -8,6 +8,8 @@ public class Atan extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ATAN(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -11,6 +11,8 @@ public class Average extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@AVERAGE(") : text;
list = new ValueList (parent, cell, functionText); list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0; isRange = functionText.indexOf ("...") > 0;
} }

View File

@ -11,6 +11,8 @@ public class Choose extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@CHOOSE(") : text;
// int pos = functionText.indexOf (','); // int pos = functionText.indexOf (',');
// sourceText = functionText.substring (0, pos); // sourceText = functionText.substring (0, pos);
sourceText = Expression.getParameter (functionText); sourceText = Expression.getParameter (functionText);

View File

@ -8,6 +8,8 @@ public class Cos extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@COS(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -11,6 +11,8 @@ class Count extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@COUNT(") : text;
list = new ValueList (parent, cell, functionText); list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0; isRange = functionText.indexOf ("...") > 0;
} }

View File

@ -5,6 +5,9 @@ class Error extends Function
public Error (Cell cell, String text) public Error (Cell cell, String text)
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ERROR") : text;
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
} }
} }

View File

@ -8,6 +8,8 @@ public class Exp extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@EXP(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -6,6 +6,8 @@ public class False extends Function
{ {
super (cell, text); super (cell, text);
assert text.equals ("@FALSE") : text;
value = 0; value = 0;
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -14,6 +14,8 @@ class If extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@IF(") : text;
conditionText = Expression.getParameter (functionText); conditionText = Expression.getParameter (functionText);
textTrue = textTrue =
Expression.getParameter (functionText.substring (conditionText.length () + 1)); Expression.getParameter (functionText.substring (conditionText.length () + 1));

View File

@ -8,6 +8,8 @@ public class Int extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@INT(") : text;
source = new Expression (parent, cell, functionText); source = new Expression (parent, cell, functionText);
values.add (source); values.add (source);
} }

View File

@ -8,6 +8,8 @@ class IsError extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ISERROR(") : text;
expression = new Expression (parent, cell, functionText).reduce (); expression = new Expression (parent, cell, functionText).reduce ();
} }

View File

@ -8,6 +8,8 @@ public class IsNa extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@ISNA(") : text;
expression = new Expression (parent, cell, functionText).reduce (); expression = new Expression (parent, cell, functionText).reduce ();
} }

View File

@ -8,6 +8,8 @@ public class Ln extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@LN(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -8,6 +8,8 @@ public class Log10 extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@LOG10(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -11,17 +11,14 @@ class Lookup extends Function
{ {
super (cell, text); super (cell, text);
// int pos = text.indexOf (','); assert text.startsWith ("@LOOKUP(") : text;
sourceText = Expression.getParameter (functionText); sourceText = Expression.getParameter (functionText);
// sourceText = text.substring (8, pos);
source = new Expression (parent, cell, sourceText); source = new Expression (parent, cell, sourceText);
values.add (source);
// rangeText = text.substring (pos + 1, text.length () - 1);
rangeText = functionText.substring (sourceText.length () + 1); rangeText = functionText.substring (sourceText.length () + 1);
range = new Range (parent, cell, rangeText); range = new Range (parent, cell, rangeText);
values.add (source);
} }
@Override @Override

View File

@ -8,6 +8,8 @@ class Max extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@MAX(") : text;
list = new ValueList (parent, cell, functionText); list = new ValueList (parent, cell, functionText);
} }

View File

@ -8,6 +8,8 @@ class Min extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@MIN(") : text;
list = new ValueList (parent, cell, functionText); list = new ValueList (parent, cell, functionText);
} }

View File

@ -5,6 +5,9 @@ public class Na extends Function
public Na (Cell cell, String text) public Na (Cell cell, String text)
{ {
super (cell, text); super (cell, text);
assert text.equals ("@NA") : text;
valueType = ValueType.NA; valueType = ValueType.NA;
} }
} }

View File

@ -14,14 +14,14 @@ public class Npv extends Function
{ {
super (cell, text); super (cell, text);
int pos = text.indexOf (','); assert text.startsWith ("@NPV(") : text;
valueText = text.substring (5, pos);
rangeText = text.substring (pos + 1, text.length () - 1);
valueText = Expression.getParameter (functionText);
rateExp = new Expression (parent, cell, valueText); rateExp = new Expression (parent, cell, valueText);
range = new Range (parent, cell, rangeText);
values.add (rateExp); values.add (rateExp);
rangeText = functionText.substring (valueText.length () + 1);
range = new Range (parent, cell, rangeText);
} }
@Override @Override

View File

@ -11,14 +11,13 @@ class Or extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@OR(") : text;
String remainder = functionText; String remainder = functionText;
while (true) while (true)
{ {
String parameter = Expression.getParameter (remainder); String parameter = Expression.getParameter (remainder);
// System.out.printf ("cond: [%s]%n", parameter);
conditions.add (new Condition (parent, cell, parameter)); conditions.add (new Condition (parent, cell, parameter));
// System.out.printf (" [%s]%n", remainder);
// System.out.printf (" [%s]%n", parameter);
if (remainder.length () == parameter.length ()) if (remainder.length () == parameter.length ())
break; break;
remainder = remainder.substring (parameter.length () + 1); remainder = remainder.substring (parameter.length () + 1);

View File

@ -7,6 +7,9 @@ class Pi extends Function
super (cell, text); super (cell, text);
value = Math.PI; value = Math.PI;
assert text.equals ("@PI") : text;
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }
} }

View File

@ -500,129 +500,118 @@ public class Sheet
return new Error (cell, "@ERROR"); return new Error (cell, "@ERROR");
} }
String functionName = "";
int functionId = -1;
for (int i = 0; i < Function.functionList.length; i++) for (int i = 0; i < Function.functionList.length; i++)
if (text.startsWith (Function.functionList[i])) if (text.startsWith (Function.functionList[i]))
{ {
functionId = i;
functionTotals[i]++; functionTotals[i]++;
functionName = Function.functionList[i];
break; break;
} }
if (text.charAt (1) == 'A') if (functionName.isEmpty ())
{ {
if (text.startsWith ("@ABS(")) System.out.printf ("Unknown function: [%s]%n", text);
return new Error (cell, "@ERROR");
}
switch (functionId)
{
case 0:
return new Abs (cell, text); return new Abs (cell, text);
if (text.startsWith ("@ACOS(")) case 1:
return new Acos (cell, text); return new Acos (cell, text);
if (text.startsWith ("@AND(")) case 2:
return new And (cell, text); return new And (cell, text);
if (text.startsWith ("@ASIN(")) case 3:
return new Asin (cell, text); return new Asin (cell, text);
if (text.startsWith ("@ATAN(")) case 4:
return new Atan (cell, text); return new Atan (cell, text);
if (text.startsWith ("@AVERAGE(")) case 5:
return new Average (cell, text); return new Average (cell, text);
}
else if (text.charAt (1) == 'C') case 6:
{
if (text.startsWith ("@COUNT("))
return new Count (cell, text); return new Count (cell, text);
if (text.startsWith ("@CHOOSE(")) case 7:
return new Choose (cell, text); return new Choose (cell, text);
if (text.startsWith ("@COS(")) case 8:
return new Cos (cell, text); return new Cos (cell, text);
}
else if (text.charAt (1) == 'E') case 9:
{
if (text.startsWith ("@ERROR"))
return new Error (cell, text); return new Error (cell, text);
if (text.startsWith ("@EXP(")) case 10:
return new Exp (cell, text); return new Exp (cell, text);
}
else if (text.charAt (1) == 'F') case 11:
{
if (text.startsWith ("@FALSE"))
return new False (cell, text); return new False (cell, text);
}
else if (text.charAt (1) == 'I') case 12:
{
if (text.startsWith ("@IF("))
return new If (cell, text); return new If (cell, text);
if (text.startsWith ("@INT(")) case 13:
return new Int (cell, text); return new Int (cell, text);
if (text.startsWith ("@ISERROR(")) case 14:
return new IsError (cell, text); return new IsError (cell, text);
if (text.startsWith ("@ISNA(")) case 15:
return new IsNa (cell, text); return new IsNa (cell, text);
}
else if (text.charAt (1) == 'L') case 16:
{
if (text.startsWith ("@LOG10("))
return new Log10 (cell, text); return new Log10 (cell, text);
if (text.startsWith ("@LOOKUP(")) case 17:
return new Lookup (cell, text); return new Lookup (cell, text);
if (text.startsWith ("@LN(")) case 18:
return new Ln (cell, text); return new Ln (cell, text);
}
else if (text.charAt (1) == 'M') case 19:
{
if (text.startsWith ("@MIN("))
return new Min (cell, text); return new Min (cell, text);
if (text.startsWith ("@MAX(")) case 20:
return new Max (cell, text); return new Max (cell, text);
}
else if (text.charAt (1) == 'N') case 21:
{
if (text.equals ("@NA"))
return new Na (cell, text); return new Na (cell, text);
if (text.startsWith ("@NPV(")) case 22:
return new Npv (cell, text); return new Npv (cell, text);
}
else if (text.charAt (1) == 'O') case 23:
{
if (text.startsWith ("@OR("))
return new Or (cell, text); return new Or (cell, text);
}
else if (text.charAt (1) == 'P') case 24:
{
if (text.startsWith ("@PI"))
return new Pi (cell, text); return new Pi (cell, text);
}
else if (text.charAt (1) == 'S') case 25:
{
if (text.startsWith ("@SIN("))
return new Sin (cell, text); return new Sin (cell, text);
if (text.startsWith ("@SUM(")) case 26:
return new Sum (cell, text); return new Sum (cell, text);
if (text.startsWith ("@SQRT(")) case 27:
return new Sqrt (cell, text); return new Sqrt (cell, text);
}
else if (text.charAt (1) == 'T') case 28:
{
if (text.startsWith ("@TAN("))
return new Tan (cell, text); return new Tan (cell, text);
if (text.startsWith ("@TRUE")) case 29:
return new True (cell, text); return new True (cell, text);
}
System.out.printf ("Unknown function: [%s]%n", text); default:
return new Error (cell, "@ERROR"); System.out.printf ("Unknown function: [%s]%n", text);
return new Error (cell, "@ERROR");
}
} }
} }

View File

@ -8,6 +8,8 @@ public class Sin extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@SIN(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -7,7 +7,10 @@ public class Sqrt extends Function
Sqrt (Cell cell, String text) Sqrt (Cell cell, String text)
{ {
super (cell, text); super (cell, text);
source = new Expression (parent, cell, text.substring (5, text.length () - 1));
assert text.startsWith ("@SQRT(") : text;
source = new Expression (parent, cell, functionText);
values.add (source); values.add (source);
} }

View File

@ -8,6 +8,8 @@ class Sum extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@SUM(") : text;
list = new ValueList (parent, cell, functionText); list = new ValueList (parent, cell, functionText);
} }

View File

@ -8,6 +8,8 @@ public class Tan extends Function
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@TAN(") : text;
v = new Expression (parent, cell, functionText).reduce (); v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }

View File

@ -6,6 +6,8 @@ public class True extends Function
{ {
super (cell, text); super (cell, text);
assert text.equals ("@TRUE") : text;
value = 1; value = 1;
valueType = ValueType.VALUE; valueType = ValueType.VALUE;
} }