Skip to content

Commit

Permalink
further refactoring and grouped transformation test
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Aug 14, 2022
1 parent d4e8560 commit 4c9e926
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public abstract class ASTTransformer<T extends JobParameters, V> extends ASTParser
implements ParameterizedTransformer<T, V> {
private T jobParameters;
private PrintType defaultPrintType = PrintType.COMPACT;

@Override
public T getJobParameters() {
Expand All @@ -24,10 +25,18 @@ public void setJobParameters(T parameters) {
jobParameters = parameters;
}

public PrintType getDefaultPrintType() {
return defaultPrintType;
}

public abstract V transform(PrintType printType, V str) throws RecognitionException;

@Override
public V transform(V str) throws RecognitionException {
return transform(PrintType.COMPACT, str);
return transform(getDefaultPrintType(), str);
}

public V transform(PrintType printType, V str, T parameters) throws RecognitionException {
return withJobParameters(parameters, () -> transform(printType, str));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

public class EnumASTTransformer<T extends JobParameters, E extends Enum<E>>
extends GroupedASTTransformer<T, E, EnumMap<E, String>, EnumMap<E, TranslationUnit>> {

public EnumASTTransformer(Consumer<EnumMap<E, TranslationUnit>> transformation, Class<E> enumClass) {
super(transformation, () -> new EnumMap<>(enumClass), () -> new EnumMap<>(enumClass));
}
Expand All @@ -18,12 +17,17 @@ public EnumASTTransformer(Class<E> enumClass) {
}

@Override
public void setResultMapSupplier(Supplier<EnumMap<E, String>> resultMapSupplier) {
throw new UnsupportedOperationException("The enum map suppliers may not be changed.");
public void setTuMapSupplier(Supplier<EnumMap<E, TranslationUnit>> tuMapSupplier) {
throw new UnsupportedOperationException("The enum map suppliers may not be changed individually.");
}

@Override
public void setTuMapSupplier(Supplier<EnumMap<E, TranslationUnit>> tuMapSupplier) {
throw new UnsupportedOperationException("The enum map suppliers may not be changed.");
public void setResultMapSupplier(Supplier<EnumMap<E, String>> resultMapSupplier) {
throw new UnsupportedOperationException("The enum map suppliers may not be changed individually.");
}

public void setEnumType(Class<E> enumClass) {
super.setTuMapSupplier(() -> new EnumMap<>(enumClass));
super.setResultMapSupplier(() -> new EnumMap<>(enumClass));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,4 @@ public String transform(PrintType printType, String str) throws RecognitionExcep
transformation.accept(translationUnit);
return ASTPrinter.print(printType, translationUnit);
}

public String transform(
PrintType printType, String str, T parameters) throws RecognitionException {
return withJobParameters(parameters, () -> transform(printType, str));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.github.douira.glsl_transformer.ast.node.TranslationUnit;
import io.github.douira.glsl_transformer.ast.print.PrintType;
import io.github.douira.glsl_transformer.job_parameter.JobParameters;
import io.github.douira.glsl_transformer.util.TriConsumer;
import io.github.douira.glsl_transformer.util.*;

public class TriASTTransformer<T extends JobParameters, E extends Enum<E>> extends EnumASTTransformer<T, E> {
private final E aType, bType, cType;
Expand All @@ -27,8 +27,6 @@ public TriASTTransformer(
this.enumClass = enumClass;
}

// TODO: add condensed version with consumers of 3, 4 and 5 parameters like in single AST transformer

public TriASTTransformer(Class<E> enumClass, E aType, E bType, E cType) {
super(enumClass);
this.aType = aType;
Expand All @@ -37,12 +35,82 @@ public TriASTTransformer(Class<E> enumClass, E aType, E bType, E cType) {
this.enumClass = enumClass;
}

public EnumMap<E, String> transformThree(PrintType printType, String a, String b, String c)
public TriASTTransformer(
TriConsumer<TranslationUnit, TranslationUnit, TranslationUnit> transformation,
Class<E> enumClass,
E aType,
E bType,
E cType) {
this(enumClass, aType, bType, cType);
setTransformation(transformation);
}

public TriASTTransformer(
TriRootOnlyTransformation<TranslationUnit> transformation,
Class<E> enumClass,
E aType,
E bType,
E cType) {
this(enumClass, aType, bType, cType);
setTransformation(transformation);
}

public TriASTTransformer(
TriFullTransformation<TranslationUnit, T> transformation,
Class<E> enumClass,
E aType,
E bType,
E cType) {
this(enumClass, aType, bType, cType);
setTransformation(transformation);
}

public void setTransformation(TriConsumer<TranslationUnit, TranslationUnit, TranslationUnit> transformation) {
super.setTransformation(map -> transformation.accept(map.get(aType), map.get(bType), map.get(cType)));
}

public void setTransformation(TriRootOnlyTransformation<TranslationUnit> transformation) {
super.setTransformation(map -> {
final var a = map.get(aType);
final var b = map.get(bType);
final var c = map.get(cType);
transformation.accept(a, b, c, a.getRoot(), b.getRoot(), c.getRoot());
});
}

public void setTransformation(TriFullTransformation<TranslationUnit, T> transformation) {
super.setTransformation(map -> {
final var a = map.get(aType);
final var b = map.get(bType);
final var c = map.get(cType);
transformation.accept(a, b, c, a.getRoot(), b.getRoot(), c.getRoot(), getJobParameters());
});
}

@Override
public void setEnumType(Class<E> enumClass) {
throw new UnsupportedOperationException("The tri enum map types may not be changed.");
}

public EnumMap<E, String> transform(PrintType printType, String a, String b, String c)
throws RecognitionException {
var items = new EnumMap<E, String>(enumClass);
items.put(aType, a);
items.put(bType, b);
items.put(cType, c);
return transform(printType, items);
}

public EnumMap<E, String> transform(String a, String b, String c) throws RecognitionException {
return transform(getDefaultPrintType(), a, b, c);
}

public Triple<String> transform(PrintType printType, Triple<String> str) throws RecognitionException {
var result = transform(printType, str.a, str.b, str.c);
return new Triple<>(result.get(aType), result.get(bType), result.get(cType));
}

public Triple<String> transform(Triple<String> str) throws RecognitionException {
return transform(getDefaultPrintType(), str);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.douira.glsl_transformer.ast.transform;

import io.github.douira.glsl_transformer.ast.node.basic.ASTNode;
import io.github.douira.glsl_transformer.ast.query.Root;
import io.github.douira.glsl_transformer.job_parameter.JobParameters;

@FunctionalInterface
public interface TriFullTransformation<A extends ASTNode, T extends JobParameters> {
void accept(A a, A b, A c, Root rootA, Root rootB, Root rootC, T jobParameters);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.douira.glsl_transformer.ast.transform;

import io.github.douira.glsl_transformer.ast.node.basic.ASTNode;
import io.github.douira.glsl_transformer.ast.query.Root;

@FunctionalInterface
public interface TriRootOnlyTransformation<A extends ASTNode> {
void accept(A a, A b, A c, Root rootA, Root rootB, Root rootC);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.douira.glsl_transformer.util;

public class Triple<V> {
public final V a, b, c;

public Triple(V a, V b, V c) {
this.a = a;
this.b = b;
this.c = c;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((a == null) ? 0 : a.hashCode());
result = prime * result + ((b == null) ? 0 : b.hashCode());
result = prime * result + ((c == null) ? 0 : c.hashCode());
return result;
}

@Override
@SuppressWarnings("unchecked")
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Triple<V> other = (Triple<V>) obj;
if (a == null) {
if (other.a != null)
return false;
} else if (!a.equals(other.a))
return false;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
if (c == null) {
if (other.c != null)
return false;
} else if (!c.equals(other.c))
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@

import org.junit.jupiter.api.Test;

import io.github.douira.glsl_transformer.ast.print.PrintType;
import io.github.douira.glsl_transformer.ast.node.expression.LiteralExpression;
import io.github.douira.glsl_transformer.test_util.TestWithGroupedASTTransformer;

public class GroupedASTTransformerTest extends TestWithGroupedASTTransformer {
@Test
void testInsertion() {
// TODO: add tri ast transformer test
// p.setTransformation(translationUnit -> {
// translationUnit.children.add(index, p.parseExternalDeclaration(
// translationUnit, "int a;"));
// });
// assertEquals(output, p.transform(PrintType.COMPACT, input));
void testGroupedTransformation() {
p.setTransformation((a, b, c, rootA, rootB, rootC) -> {
var index = rootB.nodeIndex.getOne(LiteralExpression.class).getInteger();
a.children.add((int) index, p.parseExternalDeclaration(a, "int z;"));
assertTrue(rootA.identifierIndex.has("z"));

b.children.add(c.children.get(0));
c.children.remove(0);
assertTrue(rootB.identifierIndex.has("bar"));
assertFalse(rootC.identifierIndex.has("bar"));

var zou = c.children.get(0);
zou.detach();
b.children.add(zou);
assertTrue(rootB.identifierIndex.has("zou"));
assertFalse(rootC.identifierIndex.has("zou"));
});
assertTransform(
"int a; int z; int b; int c; ",
"int f = 1; int foo = bar; int foo = zou; ",
"",
"int a; int b; int c; ",
"int f = 1;",
"int foo = bar; int foo = zou;");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.jupiter.api.BeforeEach;

import io.github.douira.glsl_transformer.ast.print.PrintType;
import io.github.douira.glsl_transformer.ast.transform.*;
import io.github.douira.glsl_transformer.ast.transform.TriASTTransformer;
import io.github.douira.glsl_transformer.job_parameter.JobParameters;

public class TestWithGroupedASTTransformer {
Expand All @@ -30,6 +30,19 @@ public Map<Part, String> transform(Map<Part, String> str) throws RecognitionExce
}

public void assertTransform(Map<Part, String> expected, Map<Part, String> input) {
assertEquals(expected, p.transform(PrintType.COMPACT, input));
var result = p.transform(PrintType.COMPACT, input);
assertEquals(expected.get(Part.A), result.get(Part.A));
assertEquals(expected.get(Part.B), result.get(Part.B));
assertEquals(expected.get(Part.C), result.get(Part.C));
assertEquals(expected, result);
}

public void assertTransform(String aExpected, String bExpected, String cExpected, String aInput, String bInput,
String cInput) {
var result = p.transform(PrintType.COMPACT, Map.of(Part.A, aInput, Part.B, bInput, Part.C, cInput));
assertEquals(aExpected, result.get(Part.A));
assertEquals(bExpected, result.get(Part.B));
assertEquals(cExpected, result.get(Part.C));
assertEquals(Map.of(Part.A, aExpected, Part.B, bExpected, Part.C, cExpected), result);
}
}

0 comments on commit 4c9e926

Please sign in to comment.