feat: add Literal in Records and LPattern for Syntaxtreegenerator
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 45s

This commit is contained in:
Ruben 2024-10-09 17:01:28 +02:00
parent 7811ecce63
commit fc22299af5
13 changed files with 653 additions and 496 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -2147,6 +2147,18 @@ public class Java17ParserBaseListener implements Java17ParserListener {
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx) { } @Override public void exitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterLPattern(Java17Parser.LPatternContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLPattern(Java17Parser.LPatternContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -1257,6 +1257,13 @@ public class Java17ParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> impl
* {@link #visitChildren} on {@code ctx}.</p> * {@link #visitChildren} on {@code ctx}.</p>
*/ */
@Override public T visitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx) { return visitChildren(ctx); } @Override public T visitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLPattern(Java17Parser.LPatternContext ctx) { return visitChildren(ctx); }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -1960,6 +1960,18 @@ public interface Java17ParserListener extends ParseTreeListener {
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx); void exitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx);
/**
* Enter a parse tree produced by the {@code lPattern}
* labeled alternative in {@link Java17Parser#primaryPattern}.
* @param ctx the parse tree
*/
void enterLPattern(Java17Parser.LPatternContext ctx);
/**
* Exit a parse tree produced by the {@code lPattern}
* labeled alternative in {@link Java17Parser#primaryPattern}.
* @param ctx the parse tree
*/
void exitLPattern(Java17Parser.LPatternContext ctx);
/** /**
* Enter a parse tree produced by {@link Java17Parser#recordPattern}. * Enter a parse tree produced by {@link Java17Parser#recordPattern}.
* @param ctx the parse tree * @param ctx the parse tree

View File

@ -1164,6 +1164,13 @@ public interface Java17ParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result * @return the visitor result
*/ */
T visitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx); T visitEnclosedPattern(Java17Parser.EnclosedPatternContext ctx);
/**
* Visit a parse tree produced by the {@code lPattern}
* labeled alternative in {@link Java17Parser#primaryPattern}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitLPattern(Java17Parser.LPatternContext ctx);
/** /**
* Visit a parse tree produced by {@link Java17Parser#recordPattern}. * Visit a parse tree produced by {@link Java17Parser#recordPattern}.
* @param ctx the parse tree * @param ctx the parse tree

View File

@ -0,0 +1,17 @@
import java.lang.Integer;
import java.lang.Double;
import java.lang.Number;
import java.lang.String;
public record R(Number n) {}
public class SwitchOverload {
public f(){}
public m(r) {
return switch(r) {
case R("test") -> f();
};
}
}

View File

@ -654,6 +654,7 @@ primaryPattern
: typePattern #tPattern : typePattern #tPattern
| recordPattern #rPattern | recordPattern #rPattern
| '(' pattern ')' #enclosedPattern | '(' pattern ')' #enclosedPattern
| literal (',' literal)* #lPattern
; ;
recordPattern recordPattern
@ -677,7 +678,6 @@ recordComponentPatternElement
recordComponentPatternList recordComponentPatternList
: pattern (',' pattern)* : pattern (',' pattern)*
| literal (',' literal)*
; ;
// Java8 // Java8

View File

@ -15,6 +15,7 @@ import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.target.tree.expression.TargetUnaryOp; import de.dhbwstuttgart.target.tree.expression.TargetUnaryOp;
import de.dhbwstuttgart.target.generate.StatementToTargetExpression;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
@ -441,6 +442,7 @@ public class StatementGenerator {
} }
private Pattern convert(PatternContext pattern) { private Pattern convert(PatternContext pattern) {
return switch (pattern) { return switch (pattern) {
case PPatternContext pPattern -> { case PPatternContext pPattern -> {
yield convert(pPattern.primaryPattern()); yield convert(pPattern.primaryPattern());
@ -455,6 +457,7 @@ public class StatementGenerator {
} }
private FormalParameter convert(PrimaryPatternContext pPattern) { private FormalParameter convert(PrimaryPatternContext pPattern) {
switch (pPattern) { switch (pPattern) {
case TPatternContext tPattern: case TPatternContext tPattern:
TypePatternContext typePattern = tPattern.typePattern(); TypePatternContext typePattern = tPattern.typePattern();
@ -465,8 +468,10 @@ public class StatementGenerator {
case RPatternContext rPattern: case RPatternContext rPattern:
RecordPatternContext recordPattern = rPattern.recordPattern(); RecordPatternContext recordPattern = rPattern.recordPattern();
return convert(recordPattern); return convert(recordPattern);
default: case Java17Parser.LPatternContext patternContext: return new LiteralPattern(TypePlaceholder.fresh(patternContext.start), patternContext.literal().get(0).getText(), patternContext.start);
throw new NotImplementedException();
default:
throw new NotImplementedException();
} }
} }
@ -479,7 +484,8 @@ public class StatementGenerator {
//Hier evtl. Typ anpassen -> wenn kein Typ bekannt ist push neuen Typ auf Hashtable //Hier evtl. Typ anpassen -> wenn kein Typ bekannt ist push neuen Typ auf Hashtable
var type = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics); var type = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics);
if (text != null) localVars.put(text, type); if (text != null) localVars.put(text, type);
return new RecordPattern(subPattern, text, type, recordPatternCtx.getStart()); var ret = new RecordPattern(subPattern, text, type, recordPatternCtx.getStart());
return ret;
} }
private Statement convert(Java17Parser.WhileloopContext stmt) { private Statement convert(Java17Parser.WhileloopContext stmt) {

View File

@ -0,0 +1,29 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.Pattern;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
public class LiteralPattern extends FormalParameter
{
public final Object value;
public LiteralPattern(RefTypeOrTPHOrWildcardOrGeneric type, Object value, Token offset) {
super((String) value, type, offset);
this.value = value;
}
@Override
public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) {
return null;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
public interface StatementVisitor { public interface StatementVisitor {
void visit(ArgumentList argumentList); void visit(ArgumentList argumentList);

View File

@ -117,6 +117,8 @@ public class TYPEStmt implements StatementVisitor {
public void visit(FieldVar fieldVar) { public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this); fieldVar.receiver.accept(this);
Set<Constraint> oderConstraints = new HashSet<>(); Set<Constraint> oderConstraints = new HashSet<>();
for (FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)) { for (FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)) {
Constraint constraint = new Constraint(); Constraint constraint = new Constraint();
GenericsResolver resolver = getResolverInstance(); GenericsResolver resolver = getResolverInstance();
@ -128,7 +130,6 @@ public class TYPEStmt implements StatementVisitor {
throw new TypeinferenceException("Kein Feld " + fieldVar.fieldVarName + " gefunden", fieldVar.getOffset()); throw new TypeinferenceException("Kein Feld " + fieldVar.fieldVarName + " gefunden", fieldVar.getOffset());
constraintsSet.addOderConstraint(oderConstraints); constraintsSet.addOderConstraint(oderConstraints);
} }
@Override @Override
public void visit(ForStmt forStmt) { public void visit(ForStmt forStmt) {
forStmt.initializer.forEach(s -> s.accept(this)); forStmt.initializer.forEach(s -> s.accept(this));

View File

@ -728,7 +728,12 @@ public class TestComplete {
assertEquals(res, expected); assertEquals(res, expected);
} }
@Test
public void testSwitchRecordLiteral() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingLiteralStyle.jav");
var clazz = classFiles.get("SwitchOverload");
}
@Test @Test
public void testSwitchCaseHeritageDetection() throws Exception { public void testSwitchCaseHeritageDetection() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchCaseHeritageDetection.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchCaseHeritageDetection.jav");