From dfe24f4a4b6f59f977240ed92ed5e01a6e2b57e8 Mon Sep 17 00:00:00 2001 From: ahmad Date: Fri, 5 Jul 2024 11:42:40 +0200 Subject: [PATCH] =?UTF-8?q?Added=20stuff=20for=20pr=C3=A4si?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- presentation/slidev/slides.md | 691 ++++++++++++++++++++++++++++++++++ 1 file changed, 691 insertions(+) diff --git a/presentation/slidev/slides.md b/presentation/slidev/slides.md index 79790eb..a72adb7 100644 --- a/presentation/slidev/slides.md +++ b/presentation/slidev/slides.md @@ -215,6 +215,697 @@ While -[#008200,dashed]u-^ Statement +## TypedAST + + + + +```plantuml +@startuml + +!theme plain +top to bottom direction +skinparam linetype ortho + +class TypedAssignment { + + TypedAssignment(TypedExpression, TypedFieldVarAccess, Type): + + TypedAssignment(TypedProgram, Assignment): + - location: TypedFieldVarAccess + - type: Type + - value: TypedExpression + + toString(): String + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + - convertToTypedAssignment(TypedProgram, Assignment): void + + typeCheck(TypedProgram): Type + - getTypeFromConstructorOrField(TypedConstructor, String, TypedClass): Type + - getOwnerChain(MethodContext): void + - getTypeLeft(TypedProgram): Type + - getTypeFromMethodOrField(TypedMethod, String, TypedClass): Type + + codeGen(MethodContext): void + location: TypedFieldVarAccess + value: TypedExpression + type: Type +} +class TypedBinary { + + TypedBinary(TypedExpression, Operator, TypedExpression, Type): + + TypedBinary(): + + TypedBinary(TypedProgram, Binary): + - left: TypedExpression + - right: TypedExpression + - type: Type + - op: Operator + + equals(Object): boolean + + hashCode(): int + # canEqual(Object): boolean + + toString(): String + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + - convertToTypedBinary(TypedProgram, Binary): void + left: TypedExpression + type: Type + op: Operator + right: TypedExpression +} +class TypedBlock { + + TypedBlock(List, List, Type): + + TypedBlock(): + + TypedBlock(List, List): + + TypedBlock(TypedProgram, Block): + - type: Type + - vars: List + - stmts: List + + toString(): String + + equals(Object): boolean + + hashCode(): int + # canEqual(Object): boolean + - clearLocalVariable(TypedProgram): void + - convertToTypedBlock(TypedProgram, Block): void + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + stmts: List + vars: List + type: Type +} +class TypedBoolLiteral { + + TypedBoolLiteral(Boolean, Type): + + TypedBoolLiteral(): + + TypedBoolLiteral(BoolLiteral): + - type: Type + - value: Boolean + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodContext): void + - convertToTypedBoolLiteral(BoolLiteral): void + + typeCheck(TypedProgram): Type + value: Boolean + type: Type +} +class TypedBreak { + + TypedBreak(): + - type: Type + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + type: Type +} +class TypedCharLiteral { + + TypedCharLiteral(char, Type): + + TypedCharLiteral(CharLiteral): + - value: char + - type: Type + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + typeCheck(TypedProgram): Type + - convertToCharLiteral(CharLiteral): void + + codeGen(MethodContext): void + value: char + type: Type +} +class TypedClass { + + TypedClass(String, List, List, List, TypedMain, TypedMethod, TypedConstructor, Type, boolean): + + TypedClass(): + + TypedClass(String, List, List, List, TypedMain, TypedMethod, TypedConstructor, Type): + + TypedClass(Class): + - typedMethods: List + - typedConstructors: List + - currentMethod: TypedMethod + - isMainCurrentMethod: boolean + - className: String + - typedMain: TypedMain + - type: Type + - currentConstructor: TypedConstructor + - typedDeclarations: List + + isParameterNameInCurrentConstructor(String): boolean + + codeGenClassWriter(): ClassWriter + + toString(): String + + hashCode(): int + + equals(Object): boolean + # canEqual(Object): boolean + + isParameterWitNameInCurrentMethod(String): boolean + + exitCurrentConstructor(): void + + isMethodOfCurrentClass(String): boolean + + typeCheck(TypedProgram): Type + + getFieldType(String): Type + + getMethodType(String): Type + + getParameterTypeInCurrentConstructor(String): Type + + enterCurrentMethod(TypedMethod): void + + getParameterTypeInCurrentMethod(String): Type + + enterCurrentConstructor(TypedConstructor): void + + codeGen(): byte[] + + covertBlocksOfConstructorsAndMethods(TypedProgram, Class): void + + convertMethodsAndConstructorsAndFields(TypedProgram, Class): void + + isThereField(String): boolean + + isParameterWitNameInCurrentConstructor(String): boolean + + exitCurrentMethod(): void + + checkMethodExists(TypedMethod): void + currentConstructorPresent: boolean + typedConstructors: List + currentMethod: TypedMethod + typedMain: TypedMain + type: Type + typedDeclarations: List + currentConstructor: TypedConstructor + className: String + typedMethods: List + currentMainMethodPresent: boolean + isMainCurrentMethod: boolean + currentMethodPresent: boolean +} +class TypedConstructor { + + TypedConstructor(String, List, TypedBlock, Type, List): + + TypedConstructor(): + + TypedConstructor(String, List, TypedBlock): + + TypedConstructor(Constructor, String): + - localVariables: List + - name: String + - typedBlock: TypedBlock + - type: Type + - typedParameters: List + + toString(): String + # canEqual(Object): boolean + + hashCode(): int + + equals(Object): boolean + - checkIfParameterExists(String): void + + deleteLocalVariableInConstructor(String): void + + isLocalVariablePresent(String): boolean + - convertToTypedConstructor(Constructor, String): void + + getLocalVariableType(String): Type + - convertToTypedParameter(List): void + + isParameterPresent(String): boolean + + typeCheck(TypedProgram): Type + + convertToBlock(TypedProgram, Constructor): void + + codeGen(ClassContext): void + + isLocalVariableInConstructor(String): boolean + localVariables: List + name: String + typedParameters: List + type: Type + typedBlock: TypedBlock +} +class TypedContinue { + + TypedContinue(): + - type: Type + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + type: Type +} +class TypedDeclaration { + + TypedDeclaration(TypedProgram, Declaration): + + TypedDeclaration(String, Type): + + TypedDeclaration(): + - name: String + - type: Type + + equals(Object): boolean + + hashCode(): int + + toString(): String + + typeCheck(TypedProgram): Type + - convertToTypedDeclaration(TypedProgram, Declaration): void + + codeGen(ClassWriter): void + name: String + type: Type +} +class TypedDoWhile { + + TypedDoWhile(TypedBlock, TypedExpression, Type): + + TypedDoWhile(TypedProgram, DoWhile): + - cond: TypedExpression + - type: Type + - typedBlock: TypedBlock + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + - convertToTypedDoWhile(TypedProgram, DoWhile): void + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + cond: TypedExpression + typedBlock: TypedBlock + type: Type +} +interface TypedExpression << interface >> { + + codeGen(MethodContext): void + type: Type +} +class TypedExpressionUtil { + + TypedExpressionUtil(): + + convertExpression(TypedProgram, Expression): TypedExpression +} +class TypedFieldVarAccess { + + TypedFieldVarAccess(Boolean, TypedExpression, String, Type): + + TypedFieldVarAccess(): + + TypedFieldVarAccess(TypedProgram, FieldVarAccess): + - field: Boolean + - type: Type + - name: String + - recursiveOwnerChain: TypedExpression + # canEqual(Object): boolean + + toString(): String + + equals(Object): boolean + + hashCode(): int + + codeGen(MethodContext): void + - checkFieldOrMethodOrRecursiveType(TypedProgram): Type + + typeCheck(TypedProgram): Type + - checkMethodVariableType(TypedProgram): Type + - convertToTypedFieldVarAccess(TypedProgram, FieldVarAccess): void + - checkVariableType(TypedProgram): Type + - checkConstructorVariableType(TypedProgram): Type + - checkFieldOrMethodType(TypedProgram): Type + - checkTypeField(TypedProgram): Type + name: String + type: Type + recursiveOwnerChain: TypedExpression + field: Boolean +} +class TypedFor { + + TypedFor(): + + TypedFor(TypedAssignment, TypedExpression, TypedAssignment, TypedBlock, Type): + + TypedFor(TypedProgram, For): + - assign: TypedAssignment + - typedBlock: TypedBlock + - type: Type + - cond: TypedExpression + - inc: TypedAssignment + + hashCode(): int + # canEqual(Object): boolean + + toString(): String + + equals(Object): boolean + + codeGen(MethodContext): void + - convertToTypedFor(TypedProgram, For): void + + typeCheck(TypedProgram): Type + inc: TypedAssignment + type: Type + cond: TypedExpression + typedBlock: TypedBlock + assign: TypedAssignment +} +class TypedIfElse { + + TypedIfElse(TypedExpression, TypedBlock, TypedBlock, Type): + + TypedIfElse(): + + TypedIfElse(TypedProgram, IfElse): + - typedCon: TypedExpression + - elseTypedBlock: TypedBlock + - ifTypedBlock: TypedBlock + - type: Type + + toString(): String + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + codeGen(MethodContext): void + - convertToTypedIfElse(TypedProgram, IfElse): void + + typeCheck(TypedProgram): Type + elseTypedBlock: TypedBlock + type: Type + ifTypedBlock: TypedBlock + typedCon: TypedExpression +} +class TypedIntLiteral { + + TypedIntLiteral(Integer, Type): + + TypedIntLiteral(): + + TypedIntLiteral(Integer): + + TypedIntLiteral(IntLiteral): + - value: Integer + - type: Type + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodContext): void + + typeCheck(TypedProgram): Type + - convertToTypedIntLiteral(IntLiteral): void + value: Integer + type: Type +} +class TypedLocalVariable { + + TypedLocalVariable(TypedProgram, Declaration): + + TypedLocalVariable(String, Type): + + TypedLocalVariable(): + - type: Type + - name: String + + typeCheck(TypedProgram): Type + + equals(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodVisitor, MethodContext): void + - convertToTypedLocalVariable(Declaration): void + name: String + type: Type +} +class TypedMain { + + TypedMain(Type, TypedMethod): + + TypedMain(): + - type: Type + - typedMethod: TypedMethod + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(ClassContext): void + + typeCheck(TypedProgram): Type + + convertToTypedMethod(TypedProgram, Class): void + typedMethod: TypedMethod + type: Type +} +class TypedMethod { + + TypedMethod(String, Type, List, List, TypedBlock): + + TypedMethod(): + + TypedMethod(TypedProgram, Method): + - returnType: Type + - typedBlock: TypedBlock + - localVariables: List + - name: String + - typedParameters: List + + toString(): String + - convertToTypedMethod(TypedProgram, Method): void + + equals(Object): boolean + + codeGen(ClassContext): void + + codeGenMain(ClassContext): void + - convertToTypedParameter(List): void + - checkReturn(List): boolean + + isLocalVariablePresent(String): boolean + + isParameterPresent(String): boolean + + deleteLocalVariableInMethod(String): void + + convertToTypedBlock(TypedProgram, Method): void + + isLocalVariableInMethod(String): boolean + + typeCheck(TypedProgram): Type + + getLocalVariableType(String): Type + + checkIfParameterExists(String): void + localVariables: List + name: String + typedParameters: List + returnType: Type + typedBlock: TypedBlock +} +class TypedMethodCall { + + TypedMethodCall(): + + TypedMethodCall(TypedFieldVarAccess, List, Type): + + TypedMethodCall(TypedProgram, MethodCall): + - recipient: TypedFieldVarAccess + - args: List + - type: Type + + hashCode(): int + + equals(Object): boolean + + toString(): String + # canEqual(Object): boolean + + typeCheck(TypedProgram): Type + + codeGen(MethodContext): void + - getOwnerChain(MethodContext): void + + findMatchingMethod(TypedClass, String): Optional + - convertToTypedMethodCall(TypedProgram, MethodCall): void + recipient: TypedFieldVarAccess + args: List + type: Type +} +class TypedNew { + + TypedNew(Type, List): + + TypedNew(TypedProgram, New): + - type: Type + - args: List + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodContext): void + - convertToTypedNew(TypedProgram, New): void + - findMatchingConstructorType(TypedProgram, List): Type + + typeCheck(TypedProgram): Type + args: List + type: Type +} +interface TypedNode << interface >> { + + typeCheck(TypedProgram): Type +} +class TypedParameter { + + TypedParameter(String, Type): + + TypedParameter(Parameter): + - paraName: String + - type: Type + + toString(): String + + typeCheck(TypedProgram): Type + + equals(Object): boolean + - convertToTypedParameter(Parameter): void + paraName: String + type: Type +} +class TypedPrint { + + TypedPrint(TypedProgram, Print): + + TypedPrint(TypedExpression, Type): + + codeGen(MethodContext): void + + typeCheck(TypedProgram): Type +} +class TypedProgram { + + TypedProgram(List, TypedClass): + + TypedProgram(Program): + - typedClasses: List + - currentClass: TypedClass + + isTypedClassPresent(String): boolean + + toString(): String + + getTypeOfFieldNameInClass(String, String): Type + + getTypedClass(String): TypedClass + + startConversion(Program): void + - exitCurrentClass(): void + + equals(Object): boolean + + isClassWithNamePresent(String): boolean + + getTypeOfFieldOrMethodNameInClass(String, String): Type + - enterCurrentClass(TypedClass): void + currentClass: TypedClass + typedClasses: List +} +class TypedReturn { + + TypedReturn(): + + TypedReturn(TypedExpression, Type): + + TypedReturn(TypedProgram, Return): + - ret: TypedExpression + - type: Type + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodContext): void + + typeCheck(TypedProgram): Type + - convertToTypedReturn(TypedProgram, Return): void + ret: TypedExpression + type: Type +} +interface TypedStatement << interface >> { + + codeGen(MethodContext): void +} +class TypedUnary { + + TypedUnary(UnaryOperator, TypedExpression, Type): + + TypedUnary(TypedProgram, Unary): + - type: Type + - op: UnaryOperator + - right: TypedExpression + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + typeCheck(TypedProgram): Type + - convertToTypedUnary(TypedProgram, Unary): void + + codeGen(MethodContext): void + right: TypedExpression + op: UnaryOperator + type: Type +} +class TypedWhile { + + TypedWhile(TypedExpression, TypedBlock, Type): + + TypedWhile(TypedProgram, While): + - type: Type + - cond: TypedExpression + - typedBlock: TypedBlock + + equals(Object): boolean + # canEqual(Object): boolean + + hashCode(): int + + toString(): String + + codeGen(MethodContext): void + + typeCheck(TypedProgram): Type + - convertToTypedWhile(TypedProgram, While): void + cond: TypedExpression + typedBlock: TypedBlock + type: Type +} + +TypedAssignment -[#008200,dashed]u-^ TypedStatement +TypedBinary -[#008200,dashed]u-^ TypedExpression +TypedBlock -[#008200,dashed]u-^ TypedNode +TypedBoolLiteral -[#008200,dashed]u-^ TypedExpression +TypedBreak -[#008200,dashed]u-^ TypedStatement +TypedCharLiteral -[#008200,dashed]u-^ TypedExpression +TypedClass -[#008200,dashed]u-^ TypedNode +TypedConstructor -[#008200,dashed]u-^ TypedNode +TypedContinue -[#008200,dashed]u-^ TypedStatement +TypedDeclaration -[#008200,dashed]u-^ TypedNode +TypedDoWhile -[#008200,dashed]u-^ TypedStatement +TypedExpression -[#008200,plain]u-^ TypedNode +TypedFieldVarAccess -[#008200,dashed]u-^ TypedExpression +TypedFor -[#008200,dashed]u-^ TypedStatement +TypedIfElse -[#008200,dashed]u-^ TypedStatement +TypedIntLiteral -[#008200,dashed]u-^ TypedExpression +TypedLocalVariable -[#008200,dashed]u-^ TypedNode +TypedMain -[#008200,dashed]u-^ TypedNode +TypedMethod -[#008200,dashed]u-^ TypedNode +TypedMethodCall -[#008200,dashed]u-^ TypedExpression +TypedMethodCall -[#008200,dashed]u-^ TypedStatement +TypedNew -[#008200,dashed]u-^ TypedExpression +TypedNew -[#008200,dashed]u-^ TypedStatement +TypedParameter -[#008200,dashed]u-^ TypedNode +TypedPrint -[#008200,dashed]u-^ TypedStatement +TypedReturn -[#008200,dashed]u-^ TypedStatement +TypedStatement -[#008200,plain]u-^ TypedNode +TypedUnary -[#008200,dashed]u-^ TypedExpression +TypedWhile -[#008200,dashed]u-^ TypedStatement +@enduml + +``` + + +--- + +# Grobe TypedAST Struktur + + +```plantuml +@startuml + +!theme plain +left to right direction +skinparam linetype ortho + +interface TypedExpression << interface >> { ++ codeGen(MethodContext): void + type: Type + } + interface TypedNode << interface >> { ++ typeCheck(TypedProgram): Type + } + interface TypedStatement << interface >> { ++ codeGen(MethodContext): void + } + +class TypedClass{ ++ typeCheck(TypedProgram): Type + } + +class TypedWhile{ ++ typeCheck(TypedProgram): Type + } + +class TypedBinary{ ++ typeCheck(TypedProgram): Type + } + +TypedExpression -[#008200,plain]r-^ TypedNode +TypedStatement -[#008200,plain]l-^ TypedNode +TypedWhile -[#008200,dashed]-^ TypedStatement +TypedClass -[#008200,dashed]-^ TypedNode +TypedBinary -[#008200,dashed]-^ TypedExpression + +@enduml +``` + + + +--- + +# Phasen der Typisierung + +- Initisialisierungsphase + - Programm -> Klassen (Name, Feld-Variablen, Methoden, Konstruktoren) +- Konvertierungsphase + - Überführung von untypisierten AST zu typisierten AST +- TypeCheck-Phase + - Semantische Überprüfung + - Ausführung von Blöcken +--- + +## Konvertierung von untypisierten AST zu typisierten AST + + + +```java +public class TypedDeclaration implements TypedNode { + private String name; + private Type type; + + ... + + private void convertToTypedDeclaration(Declaration declaration) { + name = declaration.name(); + type = declaration.type(); + } + +``` + + + + +--- + +# TypeCheck von While-Statement + + + +```java +public class TypedWhile implements TypedStatement { + private TypedExpression cond; + private TypedBlock typedBlock; + private Type type; + + ... + + @Override + public Type typeCheck(TypedProgram typedProgram) { + if (cond.typeCheck(typedProgram) != Type.BOOL) { + throw new InvalidWhileConditionException(); + } + type = typedBlock.typeCheck(typedProgram); + return type; + } +} +``` + + + +--- + +# Speichern von lokalen Variablen + +- Methoden und Konstruktoren haben jeweils eine Liste von lokalen Variablen + + + +```java +public int calculateResult(boolean isMultiple, int x, int y) { + int resultat = 0;// add resultat to list + + if(isMultiple) { + int a = x * y;// add a to list + + resultat = a; + }// remove a from list + else { + int b = x + y;// add b to list + + resultat = b; + } // remove b from list + + return resultat; +} // clear list +``` + + + --- # Code Generierung