1. Check undefined variables in the function

2. Automatically add parameters if a variable has been inserted
This commit is contained in:
serso 2016-03-15 21:16:04 +01:00
parent 9f94f9fd90
commit b1bd694a8a
2 changed files with 24 additions and 1 deletions

View File

@ -50,6 +50,11 @@ public class PreparedExpression implements CharSequence {
return !undefinedVariables.isEmpty(); return !undefinedVariables.isEmpty();
} }
@Nonnull
public List<IConstant> getUndefinedVariables() {
return undefinedVariables;
}
@Override @Override
public int length() { public int length() {
return value.length(); return value.length();

View File

@ -38,6 +38,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import jscl.math.function.IConstant;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
@ -275,7 +276,17 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements
return false; return false;
} }
try { try {
calculator.prepare(body); final PreparedExpression pe = calculator.prepare(body);
if (pe.hasUndefinedVariables()) {
// check that all undefined variables are actually function parameters
final List<String> parameters = collectParameters();
for (IConstant undefinedVariable : pe.getUndefinedVariables()) {
if (!parameters.contains(undefinedVariable.getName())) {
setError(bodyLabel, getString(R.string.c_error));
return false;
}
}
}
clearError(bodyLabel); clearError(bodyLabel);
return true; return true;
} catch (ParseException e) { } catch (ParseException e) {
@ -472,6 +483,13 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements
bodyView.setSelection(newSelection); bodyView.setSelection(newSelection);
} }
} }
// if a parameter has been inserted - update the parameter's list
if (TextUtils.equals(text, "x") || TextUtils.equals(text, "y")) {
final String possibleParam = text.toString();
if (!collectParameters().contains(possibleParam)) {
paramsView.addParam(possibleParam);
}
}
} }
@Override @Override