diff --git a/src/com/bytezone/diskbrowser/visicalc/Cell.java b/src/com/bytezone/diskbrowser/visicalc/Cell.java index 1618e13..bc32f61 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Cell.java +++ b/src/com/bytezone/diskbrowser/visicalc/Cell.java @@ -112,10 +112,6 @@ class Cell implements Comparable, Value String getText (int colWidth, char defaultFormat) { - // cell may have been created when formatted but no type set - // if (type == null) - // return justify ("", colWidth); - switch (type) { case LABEL: @@ -192,14 +188,14 @@ class Cell implements Comparable, Value @Override public String getText () { - assert type == CellType.VALUE : "Cell type: " + type; + assert isValue () : "Cell type: " + type; return value.getText (); } @Override public boolean isError () { - assert type == CellType.VALUE : "Cell type: " + type; + assert isValue () : "Cell type: " + type; return value.isError (); } @@ -235,20 +231,20 @@ class Cell implements Comparable, Value @Override public String toString () { - String contents = ""; - if (type != null) - switch (type) - { - case LABEL: - contents = "Labl: " + label; - break; - case REPEATING_CHARACTER: - contents = "Rept: " + repeatingChar; - break; - case VALUE: - contents = "Exp : " + expressionText; - break; - } + String contents = ""; + + switch (type) + { + case LABEL: + contents = "Labl: " + label; + break; + case REPEATING_CHARACTER: + contents = "Rept: " + repeatingChar; + break; + case VALUE: + contents = "Exp : " + expressionText; + break; + } return String.format ("[Cell:%5s %s]", address, contents); } diff --git a/src/com/bytezone/diskbrowser/visicalc/Condition.java b/src/com/bytezone/diskbrowser/visicalc/Condition.java index 2a5e1ad..18917a1 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Condition.java +++ b/src/com/bytezone/diskbrowser/visicalc/Condition.java @@ -45,14 +45,20 @@ class Condition public boolean getResult () { + // System.out.println (conditionText); if (conditionExpression == null) { + // System.out.printf ("creating %s%n", conditionText); conditionExpression = new Expression (parent, conditionText); + // System.out.printf ("creating %s%n", valueText); valueExpression = new Expression (parent, valueText); + // System.out.printf ("calculating %s%n", conditionExpression); conditionExpression.calculate (); + // System.out.printf ("calculating %s%n", valueExpression); valueExpression.calculate (); } + // System.out.println ("after calculation"); if (conditionExpression.isError () || valueExpression.isError ()) return false; diff --git a/src/com/bytezone/diskbrowser/visicalc/Expression.java b/src/com/bytezone/diskbrowser/visicalc/Expression.java index e5af794..5371b1f 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Expression.java +++ b/src/com/bytezone/diskbrowser/visicalc/Expression.java @@ -32,12 +32,13 @@ class Expression implements Value private final List operators = new ArrayList (); private final List signs = new ArrayList (); - // protected boolean isError; private ValueType valueType; private double value; + private String text; public Expression (Sheet parent, String text) { + this.text = text; String line = checkBrackets (text); // System.out.printf ("Exp[%s]%n", line); @@ -121,7 +122,10 @@ class Expression implements Value @Override public Value calculate () { - // System.out.println (this); + // System.out.printf ("Calculating: %s%n", text); + // if (text.equals ("@NA")) + // Utility.printStackTrace (); + try { Value thisValue = values.get (0); @@ -129,6 +133,7 @@ class Expression implements Value if (thisValue.isError ()) { valueType = thisValue.getValueType (); + System.out.println ("error"); return this; } value = thisValue.isNotAvailable () ? 0 : thisValue.getValue (); @@ -144,6 +149,7 @@ class Expression implements Value if (thisValue.isError ()) { valueType = thisValue.getValueType (); + System.out.println ("error"); return this; } @@ -171,6 +177,8 @@ class Expression implements Value { valueType = ValueType.ERROR; } + + // System.out.printf ("Result: %f%n", value); return this; } @@ -292,25 +300,26 @@ class Expression implements Value @Override public String toString () { - StringBuilder text = new StringBuilder (); - - int ptr = 0; - for (Value value : values) - { - assert value != null; - text.append (signs.get (ptr)); - // value.calculate (); - // if (value.isValue ()) - // text.append (value.getValue ()); - if (ptr < operators.size ()) - { - // System.out.println (operators.get (ptr)); - text.append (operators.get (ptr++)); - } - } - // System.out.println ("finished building"); - - return text.toString (); + // StringBuilder text = new StringBuilder (); + // + // int ptr = 0; + // for (Value value : values) + // { + // assert value != null; + // text.append (signs.get (ptr)); + // // value.calculate (); + // // if (value.isValue ()) + // // text.append (value.getValue ()); + // if (ptr < operators.size ()) + // { + // // System.out.println (operators.get (ptr)); + // text.append (operators.get (ptr++)); + // } + // } + // // System.out.println ("finished building"); + // + // return text.toString (); + return "Expression: " + text; } public static void main (String[] args) diff --git a/src/com/bytezone/diskbrowser/visicalc/Function.java b/src/com/bytezone/diskbrowser/visicalc/Function.java index 05f508d..a7a6afa 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Function.java +++ b/src/com/bytezone/diskbrowser/visicalc/Function.java @@ -85,7 +85,9 @@ abstract class Function implements Value return new Error (parent, text); if (text.equals ("@NA")) + { return new Na (parent, text); + } System.out.printf ("Unknown function: [%s]%n", text); return new Error (parent, "@ERROR"); @@ -136,6 +138,7 @@ abstract class Function implements Value @Override public double getValue () { + // System.out.printf ("Getting value of : %s %s%n", functionName, functionText); assert valueType == ValueType.VALUE : "Function ValueType = " + valueType; return value; } diff --git a/src/com/bytezone/diskbrowser/visicalc/If.java b/src/com/bytezone/diskbrowser/visicalc/If.java index 8cfa5f8..7b80996 100644 --- a/src/com/bytezone/diskbrowser/visicalc/If.java +++ b/src/com/bytezone/diskbrowser/visicalc/If.java @@ -29,11 +29,12 @@ class If extends Function if (condition.getResult ()) { + // System.out.println ("true"); if (expTrue == null) - { expTrue = new Expression (parent, textTrue); - expTrue.calculate (); - } + + expTrue.calculate (); + if (expTrue.isError () || expTrue.isNotAvailable ()) valueType = expTrue.getValueType (); else @@ -41,16 +42,18 @@ class If extends Function } else { + // System.out.println ("false"); if (expFalse == null) - { expFalse = new Expression (parent, textFalse); - expFalse.calculate (); - } + + expFalse.calculate (); + if (expFalse.isError () || expFalse.isNotAvailable ()) valueType = expFalse.getValueType (); else value = expFalse.getValue (); } + return this; } diff --git a/src/com/bytezone/diskbrowser/visicalc/IsError.java b/src/com/bytezone/diskbrowser/visicalc/IsError.java index 6ecfc8b..a493547 100644 --- a/src/com/bytezone/diskbrowser/visicalc/IsError.java +++ b/src/com/bytezone/diskbrowser/visicalc/IsError.java @@ -2,21 +2,22 @@ package com.bytezone.diskbrowser.visicalc; class IsError extends Function { - Expression expression; + Cell cell; public IsError (Sheet parent, String text) { super (parent, text); - expression = new Expression (parent, functionText); } @Override public Value calculate () { - expression.calculate (); - // value = expression.getValue (); - valueType = expression.getValueType (); - value = isError () ? 1 : 0; + if (cell == null) + cell = parent.getCell (functionText); + + value = cell == null ? 1 : 0; + valueType = ValueType.VALUE; + return this; } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/IsNa.java b/src/com/bytezone/diskbrowser/visicalc/IsNa.java index 6ed200e..4cc9044 100644 --- a/src/com/bytezone/diskbrowser/visicalc/IsNa.java +++ b/src/com/bytezone/diskbrowser/visicalc/IsNa.java @@ -7,12 +7,15 @@ public class IsNa extends Function IsNa (Sheet parent, String text) { super (parent, text); - expression = new Expression (parent, functionText); } @Override public Value calculate () { + if (expression == null) + expression = new Expression (parent, functionText); + + expression.calculate (); value = expression.getValue (); valueType = expression.getValueType (); return this; diff --git a/src/com/bytezone/diskbrowser/visicalc/Lookup.java b/src/com/bytezone/diskbrowser/visicalc/Lookup.java index c120b78..0eb389b 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Lookup.java +++ b/src/com/bytezone/diskbrowser/visicalc/Lookup.java @@ -14,20 +14,25 @@ class Lookup extends Function int pos = text.indexOf (','); sourceText = text.substring (8, pos); rangeText = text.substring (pos + 1, text.length () - 1); - - source = new Expression (parent, sourceText); - range = getRange (rangeText); } @Override public Value calculate () { + if (source == null) + { + source = new Expression (parent, sourceText); + range = getRange (rangeText); + } + source.calculate (); + // System.out.println ("calculated source"); if (source.isError () || source.isNotAvailable ()) { valueType = source.getValueType (); return this; } + double sourceValue = source.getValue (); Address target = null; @@ -40,10 +45,17 @@ class Lookup extends Function } if (target != null) + { if (range.isVertical ()) value = parent.getCell (target.nextColumn ()).getValue (); else value = parent.getCell (target.nextRow ()).getValue (); + valueType = ValueType.VALUE; + } + else + { + System.out.println ("Target is null!"); + } return this; } diff --git a/src/com/bytezone/diskbrowser/visicalc/Na.java b/src/com/bytezone/diskbrowser/visicalc/Na.java index 12d40bb..e7fa2d8 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Na.java +++ b/src/com/bytezone/diskbrowser/visicalc/Na.java @@ -10,7 +10,7 @@ public class Na extends Function @Override public boolean isError () { - return true; + return false; } @Override diff --git a/src/com/bytezone/diskbrowser/visicalc/Sheet.java b/src/com/bytezone/diskbrowser/visicalc/Sheet.java index 4fa4364..ae210e4 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Sheet.java +++ b/src/com/bytezone/diskbrowser/visicalc/Sheet.java @@ -178,6 +178,7 @@ public class Sheet private void processLine (String line) { assert !line.isEmpty (); + // System.out.println (line); if (line.startsWith ("/")) {