Commit 34cabfcf authored by Florian Hölzl's avatar Florian Hölzl
Browse files

added enumeration functions generation

refs 118
parent a6afb89b
......@@ -17,11 +17,31 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.af3.expression.generator.imperative;
import static org.eclipse.emf.ecore.util.EcoreUtil.copy;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createEnumeration;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createEnumerationMember;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createUserDefinedFunctionDefinition;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.definedConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.definedType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.equal;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.ifthenelse;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.returns;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.sequence;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.var;
import static org.fortiss.af3.expression.utils.GeneratorModelElementFactory.createProgram;
import java.util.ArrayList;
import java.util.List;
import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.expression.model.definitions.Enumeration;
import org.fortiss.af3.expression.model.definitions.EnumerationMember;
import org.fortiss.af3.expression.model.definitions.FunctionDefinition;
import org.fortiss.af3.expression.model.definitions.TypeDefinition;
import org.fortiss.af3.expression.model.generator.Program;
import org.fortiss.af3.expression.model.terms.imperative.IStatementTerm;
import org.fortiss.af3.expression.model.terms.imperative.IfThenElse;
import org.fortiss.af3.expression.utils.ExpressionModelElementFactory;
import org.fortiss.tooling.kernel.extension.ITransformationProvider;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
......@@ -64,17 +84,54 @@ public class DataDictionaryTransformation implements ITransformationProvider {
/** Creates the program for the data dictionary. */
private Object createProgramFor(DataDictionary dict) {
Program p = createProgram("data_dictionary");
addBooleanType(p);
for(TypeDefinition tdef : dict.getTypeDefinitionsList()) {
addTypeDefinition(tdef, p);
}
return p;
}
/** Creates the int type definition. */
private TypeDefinition createIntTypeDefinition() {
return null;
/** Adds the given type definition and constructor functions. */
private void addTypeDefinition(TypeDefinition tdef, Program p) {
if(tdef instanceof Enumeration) {
addEnumerationTypeDefinition((Enumeration)tdef, p);
}
}
/** Adds the given enumeration type definition with constructor and toString functions. */
private void addEnumerationTypeDefinition(Enumeration enumeration, Program p) {
p.getTypesList().add(copy(enumeration));
List<IStatementTerm> toStringBody = new ArrayList<IStatementTerm>();
for(EnumerationMember mem : enumeration.getMembersList()) {
// constructor function
FunctionDefinition fdef =
createUserDefinedFunctionDefinition(mem.getName(), enumeration.createIType());
fdef.setDefinition(sequence(returns(definedConst(mem.getName()))));
p.getFunctionsList().add(fdef);
// toString body part
IfThenElse ifte =
ifthenelse(equal(var("value"), definedConst(mem.getName())),
sequence(returns(definedConst('"' + mem.getName() + '"'))), null);
toStringBody.add(ifte);
}
toStringBody.add(sequence(returns(definedConst("\"null\""))));
// toString function
FunctionDefinition fdef =
createUserDefinedFunctionDefinition(enumeration.getName() + "ToString",
definedType("String"));
fdef.getParametersList().add(
ExpressionModelElementFactory.createFunctionParameter("value",
enumeration.createIType()));
fdef.setDefinition(sequence(toStringBody));
p.getFunctionsList().add(fdef);
}
/** Creates the boolean type definition. */
private TypeDefinition createBooleanTypeDefinition() {
return null;
/** Adds the boolean type definition. */
private void addBooleanType(Program p) {
Enumeration boolEnum = createEnumeration("boolean");
boolEnum.getMembersList().add(createEnumerationMember("false"));
boolEnum.getMembersList().add(createEnumerationMember("true"));
addEnumerationTypeDefinition(boolEnum, p);
}
}
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