Records in AST aufgenommen

This commit is contained in:
luca9913 2023-04-01 15:40:12 +02:00
parent 9a780127c0
commit dc8578632c
14 changed files with 203 additions and 185 deletions

View File

@ -17,8 +17,7 @@ import java.util.ArrayList;
import java.util.List;
public class JavaTXParser {
public static Java17Parser.SourceFileContext parse(File source)
throws IOException, java.lang.ClassNotFoundException {
public static Java17Parser.SourceFileContext parse(File source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = new FileInputStream(source);
// DEPRECATED: ANTLRInputStream input = new ANTLRInputStream(stream);
CharStream input = CharStreams.fromStream(stream);
@ -27,19 +26,11 @@ public class JavaTXParser {
Java17Parser parser = new Java17Parser(tokens);
return parser.sourceFile();
/*
* SyntaxTreeGenerator generator = new
* SyntaxTreeGenerator(environment.getRegistry(source));
* return generator.convert(tree);
* SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source)); return generator.convert(tree);
*/
}
/*
* Für das Typsystem ist es notwendig, dass sich der Source in einer Datei
* befindet:
* public SourceFile parse(String fileContent) throws IOException,
* java.lang.ClassNotFoundException {
* return this.parse(new
* ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8)));
* }
* Für das Typsystem ist es notwendig, dass sich der Source in einer Datei befindet: public SourceFile parse(String fileContent) throws IOException, java.lang.ClassNotFoundException { return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))); }
*/
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -14,6 +15,8 @@ import java.util.stream.Collectors;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.Token;
import com.google.common.graph.ElementOrder.Type;
import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
@ -49,6 +52,8 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodblockContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.MethoddeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.RecordComponentContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.RecordDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ReftypeContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeArgumentsContext;
@ -59,17 +64,27 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.Record;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
public class SyntaxTreeGenerator {
private JavaClassRegistry reg;
@ -114,9 +129,7 @@ public class SyntaxTreeGenerator {
public String convertQualifiedName(Java17Parser.QualifiedNameContext ctx) {
/*
* String ret = ""; for (Java17Parser.IdentifierContext ident :
* ctx.identifier()) { ret += ident.getText(); if
* (ctx.identifier().iterator().hasNext()) { ret += '.'; } }
* String ret = ""; for (Java17Parser.IdentifierContext ident : ctx.identifier()) { ret += ident.getText(); if (ctx.identifier().iterator().hasNext()) { ret += '.'; } }
*/
return ctx.getText();
}
@ -152,7 +165,8 @@ public class SyntaxTreeGenerator {
newClass = convertClass(clsoif.classDeclaration(), modifiers);
} else if (!Objects.isNull(clsoif.interfaceDeclaration())) {
newClass = convertInterface(clsoif.interfaceDeclaration(), modifiers);
// TODO: else if(!Objects.isNull(clsoif.recordDeclaration())){
} else if (!Objects.isNull(clsoif.recordDeclaration())) {
newClass = convertRecord(clsoif.recordDeclaration(), modifiers);
} else {
throw new NotImplementedException();
}
@ -196,62 +210,117 @@ public class SyntaxTreeGenerator {
List<RefType> implementedInterfaces = new ArrayList<>();
List<RefType> permittedSubtypes = new ArrayList<>();
for (ClassBodyDeclarationContext clsbodydecl : ctx.classBody().classBodyDeclaration()) {
MemberdeclContext member;
// Statement-Blöcke und "leere Zeilen" (;) werden noch nicht berücksichtigt
if (clsbodydecl instanceof MemberdeclContext) {
member = (MemberdeclContext) clsbodydecl;
Integer membermodifiers = 0;
for (ModifierContext mod : member.modifier()) {
membermodifiers += allmodifiers.get(mod.getText());
}
switch (member.memberDeclaration()) {
case MemberclassorinterfaceContext memberclsoif: {
break;
}
case MemberfieldContext memberfield: {
fielddecl.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics));
break;
}
case MembermethodContext membermethod: {
methods.add(convert(membermodifiers, membermethod.method(), name, superClass, generics));
break;
}
case MemberconstructorContext memberconstructor: {
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics));
break;
}
default:
break;
}
} else {
continue;
convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics);
}
if (constructors.isEmpty()) {
constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset));
}
if (ctx.IMPLEMENTS() != null) {
implementedInterfaces.addAll(convert(ctx.typeList(0), generics));
}
// Ist Bit für 'sealed'-Modifier gesetzt
if ((modifiers & 4096) != 0) {
switch (ctx.typeList().size()) {
case 1: {
permittedSubtypes.addAll(convert(ctx.typeList(0), generics));
break;
}
if (constructors.isEmpty()) {
constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset));
case 2: {
permittedSubtypes.addAll(convert(ctx.typeList(1), generics));
break;
}
if (ctx.IMPLEMENTS() != null) {
implementedInterfaces.addAll(convert(ctx.typeList(0), generics));
default: {
break;
}
// Ist Bit für 'sealed'-Modifier gesetzt
if ((modifiers & 4096) != 0) {
switch (ctx.typeList().size()) {
case 1: {
permittedSubtypes.addAll(convert(ctx.typeList(0), generics));
break;
}
case 2: {
permittedSubtypes.addAll(convert(ctx.typeList(1), generics));
break;
}
default: {
break;
}
}
}
}
return new ClassOrInterface(modifiers, name, fielddecl, Optional.of(this.generatePseudoConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset)), methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
}
private de.dhbwstuttgart.syntaxtree.Record convertRecord(RecordDeclarationContext recordDeclaration, int modifiers) {
String identifier = recordDeclaration.identifier().getText();
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + identifier;
JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
Token offset = recordDeclaration.getStart();
GenericsRegistry generics = createGenerics(recordDeclaration.genericDeclarationList(), name, "", reg, new GenericsRegistry(globalGenerics));
if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
throw new TypeinferenceException("Name " + className + " bereits vorhanden in " + reg.getName(className).toString(), recordDeclaration.getStart());
}
GenericDeclarationList genericClassParameters;
if (recordDeclaration.genericDeclarationList() == null) {
genericClassParameters = new GenericDeclarationList(new ArrayList<>(), recordDeclaration.identifier().getStop());
} else {
genericClassParameters = TypeGenerator.convert(recordDeclaration.genericDeclarationList(), name, "", reg, generics);
}
RefType superClass = new RefType(ASTFactory.createObjectClass().getClassName(), offset);
List<Field> fielddecl = new ArrayList<>();
List<Method> methods = new ArrayList<>();
List<Constructor> constructors = new ArrayList<>();
Boolean isInterface = false;
List<RefType> implementedInterfaces = new ArrayList<>();
List<FormalParameter> constructorParameters = new ArrayList<>();
List<Statement> constructorStatements = new ArrayList<>();
for (RecordComponentContext component : recordDeclaration.recordHeader().recordComponentList().recordComponent()) {
int fieldmodifiers = allmodifiers.get("private") + allmodifiers.get("final");
String fieldname = component.identifier().getText();
Token fieldoffset = component.getStart();
RefTypeOrTPHOrWildcardOrGeneric fieldtype = null;
if (Objects.isNull(component.typeType())) {
fieldtype = TypePlaceholder.fresh(offset);
} else {
fieldtype = TypeGenerator.convert(component.typeType(), reg, generics);
}
fielddecl.add(new Field(fieldname, fieldtype, fieldmodifiers, fieldoffset));
constructorParameters.add(new FormalParameter(fieldname, fieldtype, fieldoffset));
FieldVar fieldvar = new FieldVar(new This(offset), fieldname, fieldtype, fieldoffset);
constructorStatements.add(new Assign(new AssignToField(fieldvar), new LocalVar(fieldname, fieldtype, fieldoffset), offset));
Statement returnStatement = new Return(fieldvar, offset);
methods.add(new Method(allmodifiers.get("public"), fieldname, fieldtype, new ParameterList(new ArrayList<>(), offset), new Block(Arrays.asList(returnStatement), offset), new GenericDeclarationList(new ArrayList<>(), offset), offset));
}
RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), genericClassParameters, offset);
Constructor implicitConstructor = new Constructor(allmodifiers.get("public"), identifier, classType, new ParameterList(constructorParameters, offset), new Block(constructorStatements, offset), genericClassParameters, offset);
Optional<Constructor> initializations = Optional.of(implicitConstructor);
constructors.add(implicitConstructor);
for (ClassBodyDeclarationContext bodyDeclaration : recordDeclaration.recordBody().classBodyDeclaration()) {
convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics);
}
if (!Objects.isNull(recordDeclaration.IMPLEMENTS())) {
implementedInterfaces.addAll(convert(recordDeclaration.typeList(), generics));
}
return new Record(modifiers, name, fielddecl, initializations, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
}
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics) {
MemberdeclContext member;
// Statement-Blöcke und "leere Zeilen" (;) werden noch nicht berücksichtigt
if (classBody instanceof MemberdeclContext) {
member = (MemberdeclContext) classBody;
Integer membermodifiers = 0;
for (ModifierContext mod : member.modifier()) {
membermodifiers += allmodifiers.get(mod.getText());
}
switch (member.memberDeclaration()) {
case MemberclassorinterfaceContext memberclsoif: {
break;
}
case MemberfieldContext memberfield: {
fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics));
break;
}
case MembermethodContext membermethod: {
methods.add(convert(membermodifiers, membermethod.method(), name, superClass, generics));
break;
}
case MemberconstructorContext memberconstructor: {
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics));
break;
}
default:
break;
}
}
}
private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx, int modifiers) {
String className = this.pkgName.length() > 0 ? this.pkgName + "." : "" + ctx.identifier().getText();
JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
@ -368,8 +437,7 @@ public class SyntaxTreeGenerator {
}
/*
* fieldInitializations werden in einem Psedokonstruktor in der abstrakten
* Syntax gespeichert
* fieldInitializations werden in einem Psedokonstruktor in der abstrakten Syntax gespeichert
*/
private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset) {
RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset);

View File

@ -32,20 +32,11 @@ public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
Java17Parser.TypeArgumentsContext arguments = null;
/*
* PL 2019-03-19 auskommentiert ANFANG if(unannClassOrInterfaceTypeContext.
* unannClassType_lfno_unannClassOrInterfaceType() != null){ arguments =
* unannClassOrInterfaceTypeContext.
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); }else{//
* if(unannClassOrInterfaceTypeContext.
* unannInterfaceType_lfno_unannClassOrInterfaceType() != null){ arguments =
* unannClassOrInterfaceTypeContext.
* unannInterfaceType_lfno_unannClassOrInterfaceType().
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); } PL
* 2019-03-19 auskommentiert ENDE
* PL 2019-03-19 auskommentiert ANFANG if(unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType() != null){ arguments = unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); }else{// if(unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType() != null){ arguments = unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType().
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); } PL 2019-03-19 auskommentiert ENDE
*/
/**
* Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten
* Beispiel: Typ<String>.InnererTyp<Integer>
* Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten Beispiel: Typ<String>.InnererTyp<Integer>
*/
if (classOrInterfaceTypeContext.typeArguments().size() > 1)
throw new NotImplementedException();
@ -57,31 +48,9 @@ public class TypeGenerator {
name += classOrInterfaceTypeContext.typeIdentifier().getText();
if (classOrInterfaceTypeContext.getStop().getText().equals(">")) {
/*
* Fuer Debug-Zwecke unannClassOrInterfaceTypeContext.
* unannInterfaceType_lfno_unannClassOrInterfaceType();
* unannClassOrInterfaceTypeContext.
* unannClassType_lfno_unannClassOrInterfaceType().getText();
* //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lfno_unannClassOrInterfaceType().
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
* //UnannClassType_lfno_unannClassOrInterfaceTypeContext
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
* 0);
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
* 0).getText();
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
* 1);
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
* 1).getText(); unannClassOrInterfaceTypeContext.
* unannClassType_lfno_unannClassOrInterfaceType().getText();
* //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lf_unannClassOrInterfaceType();
* //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lf_unannClassOrInterfaceType(0).getText();
* //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lf_unannClassOrInterfaceType(1).getText();
* //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lfno_unannClassOrInterfaceType().getText();
* Fuer Debug-Zwecke unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType(); unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType(). unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); //UnannClassType_lfno_unannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 0);
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 0).getText(); unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 1); unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 1).getText(); unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lf_unannClassOrInterfaceType(); //unannClassOrInterfaceTypeContext.
* unannInterfaceType_lf_unannClassOrInterfaceType(0).getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lf_unannClassOrInterfaceType(1).getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType().getText();
*/
List<TypeArgumentsContext> typeargs = classOrInterfaceTypeContext.typeArguments();
arguments = typeargs.size() != 0 ? classOrInterfaceTypeContext.typeArguments(0) : null;
@ -91,14 +60,12 @@ public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.TypeTypeContext typeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
if (typeContext.primitiveType() != null) {
if (typeContext.primitiveType().getText().equals("boolean")) {
switch (typeContext.primitiveType().getText()) {
case "boolean":
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart());
} else {
/*
* Nicht benötigt, wenn danach eine Exception erstellt wird
* Java17Parser.NumericTypeContext numericType =
* typeContext.unannPrimitiveType().numericType();
*/
case "int":
return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart());
default:
throw new NotImplementedException();
}
} else if (!typeContext.LBRACK().isEmpty()) { // ArrayType über eckige Klammer prüfen
@ -106,9 +73,7 @@ public class TypeGenerator {
throw new NotImplementedException();
}
/*
* else if (typeContext.classOrInterfaceType() != null) { JavaClassName name =
* reg .getName(typeContext.classOrInterfaceType().typeIdentifier().getText());
* return new RefType(name, typeContext.getStart()); }
* else if (typeContext.classOrInterfaceType() != null) { JavaClassName name = reg .getName(typeContext.classOrInterfaceType().typeIdentifier().getText()); return new RefType(name, typeContext.getStart()); }
*/
return TypeGenerator.convert(typeContext.classOrInterfaceType(), reg, genericsRegistry);
}

View File

@ -12,20 +12,16 @@ import java.util.List;
public class Constructor extends Method {
//TODO: Constructor braucht ein super-Statement
public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor,
GenericDeclarationList gtvDeclarations, Token offset /*, List<Statement> fieldInitializations geloescht PL 2018-11-24 */) {
super(modifier, name, returnType, parameterList, /*codeInsideConstructor,*/ prepareBlock(codeInsideConstructor ) /*,fieldInitializations )geloescht PL 2018-11-24 )*/, gtvDeclarations, offset);
// TODO: Constructor braucht ein super-Statement
public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset /* , List<Statement> fieldInitializations geloescht PL 2018-11-24 */) {
super(modifier, name, returnType, parameterList, /* codeInsideConstructor, */ prepareBlock(codeInsideConstructor) /* ,fieldInitializations )geloescht PL 2018-11-24 ) */, gtvDeclarations, offset);
}
/**
* @param fieldInitializations - Das sind die Statements,
* welche die Felder der zugehörigen Klasse dieses
* Konstruktor initialisieren
* @param fieldInitializations - Das sind die Statements, welche die Felder der zugehörigen Klasse dieses Konstruktor initialisieren
*/
protected static Block prepareBlock(Block constructorBlock /*, List<Statement> fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */){
protected static Block prepareBlock(Block constructorBlock /* , List<Statement> fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */) {
List<Statement> statements = constructorBlock.getStatements();
statements.add(0, new SuperCall(null, null, constructorBlock.getOffset()));
/* statements.addAll(fieldInitializations); geloescht PL 2018-11-24 */

View File

@ -5,22 +5,22 @@ import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
public class Field extends SyntaxTreeNode implements TypeScope{
public class Field extends SyntaxTreeNode implements TypeScope {
public final int modifier;
private String name;
private RefTypeOrTPHOrWildcardOrGeneric type;
public Field(String name, RefTypeOrTPHOrWildcardOrGeneric type, int modifier, Token offset){
public Field(String name, RefTypeOrTPHOrWildcardOrGeneric type, int modifier, Token offset) {
super(offset);
this.name = name;
this.type = type;
this.modifier = modifier;
}
public String getName(){
return this.name;
}
public String getName() {
return this.name;
}
public RefTypeOrTPHOrWildcardOrGeneric getType() {
return type;
@ -41,4 +41,3 @@ public class Field extends SyntaxTreeNode implements TypeScope{
return type;
}
}

View File

@ -3,15 +3,14 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public class FormalParameter extends SyntaxTreeNode
{
public class FormalParameter extends SyntaxTreeNode {
private RefTypeOrTPHOrWildcardOrGeneric type;
private String name;
public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset){
public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(offset);
this.name = name;
this.type = type;
this.type = type;
}
public RefTypeOrTPHOrWildcardOrGeneric getType() {

View File

@ -1,39 +1,33 @@
package de.dhbwstuttgart.syntaxtree;
import org.antlr.v4.runtime.Token;
import java.util.Iterator;
import java.util.List;
public class ParameterList extends SyntaxTreeNode implements Iterable<FormalParameter>
{
public class ParameterList extends SyntaxTreeNode implements Iterable<FormalParameter> {
private List<FormalParameter> formalparameter;
public ParameterList(List<FormalParameter> params, Token offset){
public ParameterList(List<FormalParameter> params, Token offset) {
super(offset);
this.formalparameter = params;
}
public FormalParameter getParameterAt(int i)
{
if (i >= formalparameter.size() ) return null;
public FormalParameter getParameterAt(int i) {
if (i >= formalparameter.size())
return null;
return formalparameter.get(i);
}
public List<FormalParameter> getFormalparalist()
{
public List<FormalParameter> getFormalparalist() {
return formalparameter;
}
@Override
public Iterator<FormalParameter> iterator() {
return formalparameter.iterator();
}
@Override
public Iterator<FormalParameter> iterator() {
return formalparameter.iterator();
}
@Override
public void accept(ASTVisitor visitor) {

View File

@ -0,0 +1,16 @@
package de.dhbwstuttgart.syntaxtree;
import java.util.List;
import java.util.Optional;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefType;
public class Record extends ClassOrInterface {
public Record(int modifiers, JavaClassName name, List<Field> fielddecl, Optional<Constructor> fieldInitializations, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters, RefType superClass, Boolean isInterface, List<RefType> implementedInterfaces, Token offset) {
super(modifiers, name, fielddecl, fieldInitializations, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
}
}

View File

@ -11,16 +11,15 @@ import org.antlr.v4.runtime.Token;
Aufbau:
rightSide = leftSide
*/
public class Assign extends Statement
{
public class Assign extends Statement {
public final Expression rightSide;
public final AssignLeftSide lefSide;
public Assign(AssignLeftSide leftHandSide, Expression value, Token offset) {
super(leftHandSide.getType(), offset);
super(leftHandSide.getType(), offset);
this.rightSide = value;
this.lefSide = leftHandSide;
}
}
@Override
public void accept(StatementVisitor visitor) {

View File

@ -2,8 +2,9 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
public class AssignToField extends AssignLeftSide{
public class AssignToField extends AssignLeftSide {
public final FieldVar field;
public AssignToField(FieldVar fieldVar) {
super(fieldVar.getType(), fieldVar.getOffset());
field = fieldVar;

View File

@ -1,24 +1,22 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public class BinaryExpr extends Expression
{
public class BinaryExpr extends Expression {
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
public enum Operator{
public enum Operator {
ADD, // +
SUB, // -
MUL, // *
MOD, // Modulo Operator %
AND, // &&
OR, // ||
OR, // ||
DIV, // /
LESSTHAN, // <
BIGGERTHAN, // >
@ -32,9 +30,8 @@ public class BinaryExpr extends Expression
public final Expression lexpr;
public final Expression rexpr;
public BinaryExpr(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset)
{
super(type,offset);
public BinaryExpr(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset) {
super(type, offset);
this.operation = operation;
this.lexpr = lexpr;

View File

@ -4,14 +4,11 @@ import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class InstanceOf extends BinaryExpr
{
public class InstanceOf extends BinaryExpr {
public Expression expr;
private RefTypeOrTPHOrWildcardOrGeneric reftype;
public InstanceOf(int offset,int variableLength)
{
public InstanceOf(int offset, int variableLength) {
super(null, null, null, null, null);
throw new NotImplementedException();
// #JB# 20.04.2005

View File

@ -4,19 +4,17 @@ import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public class LocalVar extends Statement{
public class LocalVar extends Statement {
public final String name;
public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset)
{
super(type,offset);
public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset);
this.name = n;
}
public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access)
{
super(type,e1.getOffset());
public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access) {
super(type, e1.getOffset());
this.name = access;
}

View File

@ -1,18 +1,16 @@
import java.lang.String;
record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
interface Shape {}
record ColoredPoint(Point pt, Color color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {}
record Rectangle(Point upperLeft, Point lowerRight) implements Shape {}
class PatternMatching {
void printColorOfUpperLeftPoint(Shape shape)
void printCoordinatesOfUpperLeftPoint(Shape shape)
{
switch (shape) {
case Rectangle(ColoredPoint(Point pt, Color color), ColoredPoint lowerRight) -> System.out.println("x: " + pt.x() + " / color: " + color + " / lowerRight: " + lowerRight);
case Rectangle(Point(int x, int y), Point lowerRight) -> System.out.println("x: " + x + " / y: " + y + " / lowerRight: " + lowerRight);
default -> System.out.println("not a rectangle");
}
}
}
}