volatility

This commit is contained in:
Denis Molony 2017-03-15 15:40:18 +11:00
parent cad2106c71
commit 7056f5c49d
5 changed files with 53 additions and 32 deletions

View File

@ -42,6 +42,12 @@ class Address implements Comparable<Address>
set (address.substring (0, 2), address.substring (2)); set (address.substring (0, 2), address.substring (2));
} }
public boolean matches (String addressText)
{
Address address = new Address (addressText);
return this.rowMatches (address) && this.columnMatches (address);
}
private void set (String sCol, String sRow) private void set (String sCol, String sRow)
{ {
if (sCol.length () == 1) if (sCol.length () == 1)

View File

@ -30,7 +30,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
this.address = address; this.address = address;
cellType = CellType.EMPTY; cellType = CellType.EMPTY;
isVolatile = true; isVolatile = false;
} }
boolean isCellType (CellType cellType) boolean isCellType (CellType cellType)
@ -103,8 +103,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
expressionText = command; expressionText = command;
value = new Expression (parent, this, expressionText).reduce (); value = new Expression (parent, this, expressionText).reduce ();
cellType = CellType.VALUE; cellType = CellType.VALUE;
isVolatile = true;
isVolatile = value.isVolatile ();
} }
catch (IllegalArgumentException e) catch (IllegalArgumentException e)
{ {
@ -200,20 +199,14 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override @Override
public ValueType getValueType () public ValueType getValueType ()
{ {
if (cellType == CellType.EMPTY) return cellType == CellType.VALUE ? value.getValueType () : ValueType.VALUE;
return ValueType.NA;
if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER)
return ValueType.VALUE;
return value.getValueType ();
} }
@Override @Override
public String getText () public String getText ()
{ {
if (cellType == CellType.EMPTY) if (cellType == CellType.EMPTY)
return ""; return "MPT";
if (cellType == CellType.LABEL) if (cellType == CellType.LABEL)
return "LBL"; return "LBL";
@ -236,8 +229,11 @@ class Cell extends AbstractValue implements Comparable<Cell>
{ {
if (cellType == CellType.VALUE) if (cellType == CellType.VALUE)
{ {
value.calculate (); if (isVolatile)
isVolatile = value.isVolatile (); {
value.calculate ();
isVolatile = value.isVolatile ();
}
} }
} }
@ -301,7 +297,8 @@ class Cell extends AbstractValue implements Comparable<Cell>
contents = "Empty"; contents = "Empty";
} }
return String.format ("[Cell:%5s %s]", address, contents); return String.format ("[Cell:%5s %s %s]", address, contents,
isVolatile ? "volatile" : "fixed");
} }
@Override @Override

View File

@ -1,5 +1,7 @@
package com.bytezone.diskbrowser.visicalc; package com.bytezone.diskbrowser.visicalc;
import com.bytezone.diskbrowser.visicalc.Cell.CellType;
class Count extends Function class Count extends Function
{ {
private final ExpressionList list; private final ExpressionList list;
@ -26,15 +28,9 @@ class Count extends Function
{ {
v.calculate (); v.calculate ();
if (v instanceof Cell && v.isValueType (ValueType.NA)) if (v instanceof Cell && ((Cell) v).isCellType (CellType.EMPTY))
continue; continue;
if (!v.isValueType (ValueType.VALUE))
{
valueType = v.getValueType ();
return;
}
value++; value++;
} }
} }

View File

@ -146,18 +146,32 @@ class Expression extends AbstractValue implements Iterable<Value>
System.out.println ("nothing to calculate: " + text); System.out.println ("nothing to calculate: " + text);
return; return;
} }
System.out.printf (" calc %-6s %s%n", cell.getAddressText (), text);
// System.out.printf (" calc %-6s %s%n", cell.getAddressText (), text);
if (!isVolatile) if (!isVolatile)
return; return;
boolean currentVolatile = false; boolean currentVolatile = false;
// System.out.printf ("exp %s is currently %svolatile%n", text,
// isVolatile ? " " : "not "); // boolean debug = cell.getAddress ().matches ("C8");
boolean debug = false;
if (debug)
{
System.out.println (this);
System.out.printf ("(1) exp %s is currently %svolatile%n", text,
isVolatile ? " " : "not ");
}
try try
{ {
Value thisValue = values.get (0); Value thisValue = values.get (0);
thisValue.calculate (); thisValue.calculate ();
if (debug)
{
System.out.println (this);
System.out.printf ("(2) exp %s is currently %svolatile%n", thisValue.getText (),
thisValue.isVolatile () ? " " : "not ");
}
value = 0; value = 0;
if (!thisValue.isValueType (ValueType.VALUE)) if (!thisValue.isValueType (ValueType.VALUE))
@ -167,8 +181,6 @@ class Expression extends AbstractValue implements Iterable<Value>
} }
value = thisValue.getValue (); value = thisValue.getValue ();
// System.out.printf ("exp %s is currently %svolatile%n", thisValue.getText (),
// thisValue.isVolatile () ? " " : "not ");
if (!currentVolatile) if (!currentVolatile)
currentVolatile = thisValue.isVolatile (); currentVolatile = thisValue.isVolatile ();
@ -188,8 +200,12 @@ class Expression extends AbstractValue implements Iterable<Value>
} }
double nextValue = thisValue.getValue (); double nextValue = thisValue.getValue ();
// System.out.printf ("exp %s is currently %svolatile%n", thisValue.getText (), if (debug)
// thisValue.isVolatile () ? " " : "not "); {
System.out.println (this);
System.out.printf ("(3.%d) exp %s is currently %svolatile%n", i,
thisValue.getText (), thisValue.isVolatile () ? " " : "not ");
}
if (!currentVolatile) if (!currentVolatile)
currentVolatile = thisValue.isVolatile (); currentVolatile = thisValue.isVolatile ();
@ -230,7 +246,13 @@ class Expression extends AbstractValue implements Iterable<Value>
} }
isVolatile = currentVolatile; isVolatile = currentVolatile;
// System.out.println (currentVolatile); if (debug)
{
System.out.println (this);
System.out.printf ("(4) exp %s is currently %svolatile%n", text,
isVolatile ? " " : "not ");
System.out.println ();
}
} }
private String balanceBrackets (String input) private String balanceBrackets (String input)

View File

@ -166,7 +166,7 @@ public class Sheet
} }
// might have to keep recalculating until nothing changes?? // might have to keep recalculating until nothing changes??
System.out.println ("\n*********** Calculating\n"); // System.out.println ("\n*********** Calculating\n");
calculate (recalculationOrder); calculate (recalculationOrder);
// System.out.println ("\n*********** Calculating\n"); // System.out.println ("\n*********** Calculating\n");
// calculate (recalculationOrder); // calculate (recalculationOrder);
@ -179,9 +179,9 @@ public class Sheet
for (Cell cell : cells.values ()) for (Cell cell : cells.values ())
if (cell.isCellType (CellType.VALUE)) if (cell.isCellType (CellType.VALUE))
{ {
System.out.printf ("%5d start %s%n", count, cell.getAddressText ()); // System.out.printf ("%5d start %s%n", count, cell.getAddressText ());
cell.calculate (); cell.calculate ();
System.out.printf ("%5d stop %s%n", count++, cell.getAddressText ()); // System.out.printf ("%5d stop %s%n", count++, cell.getAddressText ());
} }
} }