Commit 8c3a4c3b authored by Florian Hölzl's avatar Florian Hölzl
Browse files

fixed expression evaluator

refs 916
parent 35a57a55
......@@ -31,9 +31,17 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.fortiss.af3.expression.language.ParseException;
import org.fortiss.af3.expression.language.TypeChecker;
import org.fortiss.af3.expression.language.TypeSystemHandler;
import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.expression.model.definitions.TypeDefinition;
import org.fortiss.af3.expression.model.terms.IExpressionTerm;
import org.fortiss.af3.expression.model.types.impl.TBoolStaticImpl;
import org.fortiss.af3.expression.model.types.impl.TDoubleStaticImpl;
import org.fortiss.af3.expression.model.types.impl.TIntStaticImpl;
import org.fortiss.af3.expression.ui.AF3ExpressionUIActivator;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.model.typesystem.IType;
import org.fortiss.af3.project.services.ITypeSystemService;
import org.fortiss.af3.project.typesystem.IEvaluationContext;
import org.fortiss.af3.project.typesystem.ITermEvaluator;
......@@ -48,7 +56,7 @@ import org.fortiss.tooling.kernel.ui.extension.base.EditorBase;
* @author eder
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 13D4DC2782F83C094E671A7C43A8996B
* @ConQAT.Rating YELLOW Hash: A0A2AC6CE7B4A8406DA40E00BB01C8DA
*/
public final class DataDictionaryEvaluator extends EditorBase<DataDictionary> {
......@@ -86,6 +94,13 @@ public final class DataDictionaryEvaluator extends EditorBase<DataDictionary> {
private static Image INFO = AF3ExpressionUIActivator.getImageDescriptor("icons/info.gif")
.createImage();
/** The flag if return is activated, i.e., parsing and type check succeed for current input. */
private boolean canExecute = false;
/** The default types. */
private static final IType[] DEFAULT_TYPES = new IType[] {TBoolStaticImpl.INSTANCE,
TIntStaticImpl.INSTANCE, TDoubleStaticImpl.INSTANCE};
/** {@inheritDoc} */
@Override
public void createPartControl(Composite parent) {
......@@ -102,12 +117,11 @@ public final class DataDictionaryEvaluator extends EditorBase<DataDictionary> {
@Override
public void keyReleased(KeyEvent e) {
if(e.keyCode == CR && evaluatorInput.getText() != "") {
evaluatorHelp.setText("");
evaluatorHelpIcon.setImage(null);
if(e.keyCode == CR && canExecute) {
setHelpAndCanExecuteFlag("", null);
evaluate();
return;
} else if(e.keyCode == ARROW_DOWN) {
if(stackPointer < evaluatorStack.size() - 1) {
stackPointer++;
evaluatorInput.setText(evaluatorStack.get(stackPointer));
......@@ -117,78 +131,93 @@ public final class DataDictionaryEvaluator extends EditorBase<DataDictionary> {
}
} else if(e.keyCode == ARROW_UP) {
if(stackPointer > 0) {
stackPointer--;
evaluatorInput.setText(evaluatorStack.get(stackPointer));
}
} else { // compile and show errors
evaluatorHelp.setText("");
evaluatorHelpIcon.setImage(null);
String input = evaluatorInput.getText().trim();
final ITypeSystemCompiler compiler =
ITypeSystemService.INSTANCE.getHandler(editedObject)
.getTypeSystemCompiler();
try {
compiler.compileTerm(input);
} catch(ParseException pe) {
evaluatorHelp.setText(pe.getMessage());
evaluatorHelpIcon.setImage(ERROR);
}
}
// compile and show errors
setHelpAndCanExecuteFlag("", null);
String input = evaluatorInput.getText().trim();
ITypeSystemCompiler compiler = TypeSystemHandler.INSTANCE.getTypeSystemCompiler();
ITerm term = null;
try {
term = compiler.compileTerm(input);
} catch(ParseException pe) {
setHelpAndCanExecuteFlag(pe.getMessage(), ERROR);
}
// expression check and type checking
if(term instanceof IExpressionTerm) {
typecheckTerm((IExpressionTerm)term);
} else {
setHelpAndCanExecuteFlag("Input is not an expression.", ERROR);
}
}
});
}
/** Sets the help content. */
private void setHelpAndCanExecuteFlag(String text, Image image) {
evaluatorHelp.setText(text);
evaluatorHelpIcon.setImage(image);
canExecute = image != ERROR;
}
/** Evaluates the user input. */
private void evaluate() {
try {
ITerm inputTerm = compileInput(evaluatorInput.getText());
ITerm typecheckedTerm = typecheckTerm(inputTerm);
Term<? extends ITerm> resultTerm = evaluateTerm(typecheckedTerm);
ITerm inputTerm = compileInput(evaluatorInput.getText());
Term<? extends ITerm> resultTerm = evaluateTerm(inputTerm);
evaluatorOutput.append(" > " + inputTerm.toString() + "\n");
evaluatorOutput.append(" = " + resultTerm.toString() + "\n\n");
evaluatorOutput.append(" > " + inputTerm.toString() + "\n");
evaluatorOutput.append(" = " + resultTerm.toString() + "\n\n");
evaluatorStack.add(inputTerm.toString());
evaluatorStack.add(inputTerm.toString());
stackPointer = evaluatorStack.size();
stackPointer = evaluatorStack.size();
evaluatorInput.setText("");
evaluatorHelpIcon.setImage(null);
} catch(Exception e) {
// FIXME CD: thrown exceptions end up in console - especially NPEs
evaluatorHelp.setText(e.getMessage());
evaluatorHelpIcon.setImage(ERROR);
}
evaluatorInput.setText("");
setHelpAndCanExecuteFlag("", null);
}
/** Evaluates the given term. */
private Term<? extends ITerm> evaluateTerm(ITerm term) throws Exception {
private Term<? extends ITerm> evaluateTerm(ITerm term) {
ITermEvaluator evaluator =
ITypeSystemService.INSTANCE.getHandler(editedObject).getTermEvaluator();
IEvaluationContext context =
ITypeSystemService.INSTANCE.createEvaluationContext(editedObject);
return evaluator.evaluate(term, context);
}
/** Performs type checking on the given term. */
private ITerm typecheckTerm(ITerm term) {
// not yet implemented
return term;
private void typecheckTerm(IExpressionTerm term) {
TypeChecker tc = new TypeChecker();
for(IType t : DEFAULT_TYPES) {
if(tc.typecheck(term, t, editedObject)) {
setDerivedTypeHelp(t);
return;
}
}
for(TypeDefinition td : editedObject.getTypeDefinitionsList()) {
IType t = td.createIType();
if(tc.typecheck(term, t, editedObject)) {
setDerivedTypeHelp(t);
return;
}
}
setHelpAndCanExecuteFlag("Type check failed for entered expression.", ERROR);
}
/** Sets the derived type info messsage. */
private void setDerivedTypeHelp(IType t) {
setHelpAndCanExecuteFlag("Derived type is: '" + t.toString() + "'", INFO);
}
/** Compiles the given input string. */
private ITerm compileInput(String input) {
input = input.trim();
ITypeSystemCompiler compiler =
ITypeSystemService.INSTANCE.getHandler(editedObject).getTypeSystemCompiler();
return compiler.compileTerm(input);
return TypeSystemHandler.INSTANCE.getTypeSystemCompiler().compileTerm(input);
}
/** {@inheritDoc} */
......
......@@ -33,7 +33,7 @@ import org.eclipse.ui.forms.widgets.FormToolkit;
* @author doebber
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 29E0AA6A1E4888127A6F4E054DE2098E
* @ConQAT.Rating GREEN Hash: 9030CD726C1420BA07951EF845FCCBF1
*/
public class DataDictionaryEvaluatorGUI extends Composite {
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
......@@ -79,7 +79,7 @@ public class DataDictionaryEvaluatorGUI extends Composite {
lblErrors = new Label(composite, SWT.NONE);
lblErrors.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
formToolkit.adapt(lblErrors, true, true);
lblErrors.setText("Enter terms below...");
lblErrors.setText("Enter expression below and hit return ...");
text_1 = new Text(frmEvaluator.getBody(), SWT.BORDER);
text_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment