Skip to content

Commit 92359c0

Browse files
Merge pull request #11 from VariantSync/defliterals
Defliterals
2 parents e472307 + c636685 commit 92359c0

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

src/main/java/org/variantsync/vevos/simulation/io/kernelhaven/ArtefactCSVExporter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public CSV export() {
5959
private String[] toRow(final LineBasedAnnotation annotation) {
6060
final String[] row = makeRow();
6161
row[0] = currentFile.getFile().toString();
62-
row[1] = FormulaUtils.toString(currentFile.getPresenceCondition(), NodeWriter.javaSymbols);
63-
row[2] = FormulaUtils.toString(annotation.getFeatureMapping(), NodeWriter.javaSymbols);
64-
row[3] = FormulaUtils.toString(annotation.getPresenceCondition(), NodeWriter.javaSymbols);
62+
row[1] = FormulaUtils.toFormulaString(currentFile.getPresenceCondition(), NodeWriter.javaSymbols);
63+
row[2] = FormulaUtils.toFormulaString(annotation.getFeatureMapping(), NodeWriter.javaSymbols);
64+
row[3] = FormulaUtils.toFormulaString(annotation.getPresenceCondition(), NodeWriter.javaSymbols);
6565
row[4] = "" + annotation.getLineFrom();
6666
// -1 because Kernelhaven stores annotations as [#if, #endif) intervals, so we have to point one line before the annotation end (#endif).
6767
row[5] = "" + (annotation.getLineTo() - (annotation.isMacro() ? 1 : 0));

src/main/java/org/variantsync/vevos/simulation/util/fide/FormulaUtils.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,39 @@ public static Node replaceAllInplace(final Node root, final Predicate<Node> who,
103103
}
104104
}
105105

106-
public static String toString(Node formula, final String[] symbols) {
106+
/**
107+
* Wraps a literal name in a defined statement.
108+
* x -> defined(x)
109+
* !x -> !defined(x)
110+
*/
111+
private static Node ifdef(final Literal l) {
112+
final Function<Object, Literal> toDefinedLiteral = s -> new Literal("defined(" + s + ")");
113+
114+
if (l.positive) {
115+
return toDefinedLiteral.apply(l);
116+
} else {
117+
return new Not(toDefinedLiteral.apply(new Literal(l.var, true)));
118+
}
119+
}
120+
121+
/**
122+
* Serializes the given formula to a string usable in C preprocessor conditions.
123+
* True and false will be converted to 1 and 0, respectively.
124+
* Variables will be wrapped in a defined expression.
125+
* @see FixTrueFalse#TrueAs1
126+
* @see FixTrueFalse#FalseAs0
127+
*/
128+
public static String toCPPString(Node formula) {
107129
formula = replaceAll(formula, FixTrueFalse::isTrue, n -> FixTrueFalse.TrueAs1);
108130
formula = replaceAllInplace(formula, FixTrueFalse::isFalse, n -> FixTrueFalse.FalseAs0);
131+
formula = replaceAllInplace(formula, FixTrueFalse::isVariable,
132+
// we know that n is a literal because FixTureFalse::isVariable returned true
133+
n -> ifdef((Literal) n)
134+
);
135+
return toFormulaString(formula, NodeWriter.javaSymbols);
136+
}
109137

138+
public static String toFormulaString(final Node formula, final String[] symbols) {
110139
final NodeWriter writer = new NodeWriter(formula);
111140
writer.setNotation(NodeWriter.Notation.INFIX);
112141
writer.setEnquoteWhitespace(false);

src/main/java/org/variantsync/vevos/simulation/util/fide/bugfix/FixTrueFalse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public static boolean isFalse(final Node n) {
5050
return n instanceof Literal l && isFalseLiteral(l);
5151
}
5252

53+
/**
54+
* @return True iff the given formula is a literal that neither {@link #isTrue} nor {@link #isFalse}.
55+
*/
56+
public static boolean isVariable(final Node n) {
57+
return n instanceof Literal l && !isTrueLiteral(l) && !isFalseLiteral(l);
58+
}
59+
5360
/**
5461
* @return True iff the given name represents the atomic value true w.r.t. the constant TrueNames.
5562
*/

src/main/java/org/variantsync/vevos/simulation/variability/pc/variantlines/VariantAnnotation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.variantsync.vevos.simulation.variability.pc.variantlines;
22

33
import org.prop4j.Node;
4-
import org.prop4j.NodeWriter;
54
import org.variantsync.vevos.simulation.util.fide.FormulaUtils;
5+
import org.variantsync.vevos.simulation.util.fide.bugfix.FixTrueFalse;
66
import org.variantsync.vevos.simulation.variability.pc.options.VariantGenerationOptions;
77

88
import java.util.ArrayList;
@@ -16,18 +16,24 @@ public record VariantAnnotation(
1616
public List<String> project(final VariantGenerationOptions projectionOptions, final List<String> splFileLines) {
1717
final List<String> result = new ArrayList<>();
1818

19-
if (projectionOptions.withMacros()) {
20-
result.add("#if " + FormulaUtils.toString(condition, NodeWriter.javaSymbols));
19+
final boolean genMacro = projectionOptions.withMacros() && !isTrue();
20+
21+
if (genMacro) {
22+
result.add("#if " + FormulaUtils.toCPPString(condition));
2123
}
2224

2325
for (final VariantLineChunk child : lines) {
2426
result.addAll(child.project(projectionOptions, splFileLines));
2527
}
2628

27-
if (projectionOptions.withMacros()) {
29+
if (genMacro) {
2830
result.add("#endif");
2931
}
3032

3133
return result;
3234
}
35+
36+
public boolean isTrue() {
37+
return FixTrueFalse.isTrue(condition);
38+
}
3339
}

0 commit comments

Comments
 (0)