Merge branch 'refs/heads/main' into testsuites

This commit is contained in:
JonathanFleischmann 2024-05-14 09:44:22 +02:00
commit fb7a7d8230
48 changed files with 1607 additions and 1033 deletions

View File

@ -3,7 +3,7 @@ grammar Decaf;
class : PUBLIC 'class' id '{' mainmeth? (field | meth | constructor)* '}';
field : type id ';';
field : PUBLIC? type id ';';
localVar : type id ';';
assignSign : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN;
returntype : type | VOID;
@ -20,7 +20,7 @@ block : '{' (localVar | stmt)* '}';
stmt : 'if' '(' expr ')' block ('else' block)? #If
| 'for' '(' assign ';' expr ';' assign ')' block #For
| 'while' '(' expr ')' block #While
| 'do' block 'while' '(' expr ')' #DoWhile
| 'do' block 'while' '(' expr ')' ';'? #DoWhile
| 'return' expr ';' #Return
| 'return' ';' #ReturnVoid
| 'break' ';' #Break
@ -97,4 +97,4 @@ COMMENT
LINE_COMMENT
: '//' ~[\r\n]* -> skip
;
;

View File

@ -17,9 +17,12 @@ public class ASTGenerator {
declarations = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
}
List<Constructor> constructors = new ArrayList<>();
if (ctx.constructor() != null) {
if (!ctx.constructor().isEmpty()) {
constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList();
}
else {
constructors.add(new Constructor(ctx.id().IDENTIFIER().getText(), List.of(), new Block(List.of(), List.of())));
}
List<Method> meths = new ArrayList<>();
if (ctx.meth() != null) {
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();

View File

@ -13,7 +13,7 @@ import java.io.IOException;
* Decaf language Compiler
*/
public class Compiler {
/*
public static void main(String[] args) {
generateAST("""
public class ClassWithConstructorWithParameters {
@ -32,7 +32,7 @@ public class Compiler {
}
}
""");
}
} */
public static Class generateAST(String fromSource) {
CharStream input = CharStreams.fromString(fromSource);
@ -92,7 +92,7 @@ public class Compiler {
CodeGenUtils.writeClassfile(bytes, classname);
}
// public static void main(String[] args) {
// generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java", "ClassWithConstructor");
// }
public static void main(String[] args) {
generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/ClassCanBeTyped.java", "ClassCanBeTyped");
}
}

View File

@ -93,9 +93,11 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null);
}
List<Expression> args = new ArrayList<>();
for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(this);
args.add(astExpr);
if(ctx.methCall().methName().args() != null){
for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(this);
args.add(astExpr);
}
}
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
}
@ -115,17 +117,19 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
if (ctxList.isEmpty()) {
return recipient;
}
DecafParser.RecipientContext ctx = ctxList.get(0);
ctxList.remove(0);
int lastElement = ctxList.size() - 1;
DecafParser.RecipientContext ctx = ctxList.get(lastElement);
ctxList.remove(lastElement);
if (ctx.id() != null) {
return new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().IDENTIFIER().getText());
}
if (ctx.methName() != null) {
List<Expression> args = new ArrayList<>();
for (var expr : ctx.methName().args().expr()) {
Expression astExpr = expr.accept(new ExpressionGenerator());
args.add(astExpr);
if (ctx.methName().args() != null) {
for (var expr : ctx.methName().args().expr()) {
Expression astExpr = expr.accept(new ExpressionGenerator());
args.add(astExpr);
}
}
return new MethodCall(new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().IDENTIFIER().getText()), args);

View File

@ -53,7 +53,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
@Override
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
return new ReturnVoid();
return new Return(null);
}
@Override

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Pressmium/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.ParserRuleContext;

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Pressmium/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Pressmium/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Pressmium/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener;

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Generated from C:/Users/laure/Documents/Dev/Compilerbau/Projekt/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
// Generated from C:/dev/Pressmium/CompilerULTIMATE/src/main/antlr/Decaf.g4 by ANTLR 4.13.1
package de.maishai.antlr;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record ReturnVoid() implements Statement {
}

View File

@ -1,4 +1,4 @@
package de.maishai.ast.records;
public sealed interface Statement extends Node permits Assignment, Break, DoWhile, For, IfElse, MethodCall, New, Return, ReturnVoid, While {
public sealed interface Statement extends Node permits Assignment, Break, DoWhile, For, IfElse, MethodCall, New, Return, While {
}

View File

@ -0,0 +1,16 @@
package de.maishai.typedast;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.objectweb.asm.ClassWriter;
@Data
public class ClassContext {
private Type name;
private ClassWriter cw;
public ClassContext(String name, ClassWriter cw) {
this.name = Type.REFERENCE(name);
this.cw = cw;
}
}

View File

@ -9,6 +9,10 @@ import java.util.List;
public class CodeGenUtils {
public static String generateDescriptor(List<TypedParameter> arguments, Type returnType) {
if (returnType == null) {
returnType = Type.VOID;
}
StringBuilder builder = new StringBuilder();
builder.append('(');
arguments.forEach(type -> builder.append(type.getType().getDescriptor()));

View File

@ -0,0 +1,56 @@
package de.maishai.typedast.Help;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.typedclass.*;
import java.util.Map;
public class TypedExpressionHelp {
public static TypedExpression convertExpression( TypedClass clas, Expression expression) {
if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( clas, boolLiteral);
typedBoolLiteral.typeCheck( clas);
return typedBoolLiteral;
}
else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral( clas, charLiteral);
typedCharLiteral.typeCheck( clas);
return typedCharLiteral;
}
else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral( clas, intLiteral);
typedIntLiteral.typeCheck( clas);
return typedIntLiteral;
}
else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary( clas, binary);
typedBinary.typeCheck( clas);
return typedBinary;
}
else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( clas, fieldVarAccess);
typedFieldVarAccess.typeCheck( clas);
return typedFieldVarAccess;
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall( clas, methodCall);
typedMethodCall.typeCheck( clas);
return typedMethodCall;
}
else if (expression instanceof New newStmt) {
TypedNew typedNew = new TypedNew( clas, newStmt);
typedNew.typeCheck( clas);
return typedNew;
}
else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary( clas, unary);
typedUnary.typeCheck( clas);
return typedUnary;
} else {
return null;
}
}
}

View File

@ -3,38 +3,70 @@ package de.maishai.typedast;
import lombok.*;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
@Getter
@Setter
@Data
public class MethodContext {
private record LocalVariable(String name, int index, Type type) {
}
private Label startLabel;
private Label endLabel;
private MethodVisitor mv;
private ClassContext classContext;
private int localVarIndex = 0;
private Map<String, Integer> variableIndex = new HashMap<>();
private final Map<String, LocalVariable> variableIndex = new HashMap<>();
private Stack<Integer> stack = new Stack<>();
private int maxStack = 0;
public MethodContext(MethodVisitor mv) {
public MethodContext(ClassContext classContext, MethodVisitor mv) {
startLabel = new Label();
endLabel = new Label();
this.mv = mv;
this.classContext = classContext;
registerVariable("this", classContext.getName());
mv.visitCode();
mv.visitLabel(startLabel);
}
public int addVariable(String name) {
public void registerVariable(String name, Type type) {
int index = localVarIndex;
localVarIndex++;
variableIndex.put(name, index);
variableIndex.put(name, new LocalVariable(name, index, type));
}
public int getVariableIndex(String name) {
int index = variableIndex.get(name).index;
if (index == -1) {
throw new RuntimeException("Variable not declared");
}
return index;
}
public void pushStack(String varName) {
stack.push(localVarIndex);
if (stack.size() > maxStack) {
maxStack = stack.size();
}
LocalVariable localVariable = variableIndex.get(varName);
if (localVariable.type.getKind() == Type.Kind.REFERENCE) {
mv.visitVarInsn(Opcodes.ALOAD, localVariable.index);
} else {
mv.visitVarInsn(Opcodes.ILOAD, localVariable.index);
}
}
public void popStack() {
stack.pop();
}
public void wrapUp() {
mv.visitLabel(endLabel);
mv.visitMaxs(0, 0);
mv.visitMaxs(maxStack, localVarIndex);
mv.visitEnd();
}
}

View File

@ -1,4 +1,7 @@
package de.maishai.typedast;
public interface TypedExpression extends TypedNode {
public void codeGen(MethodContext ctx);
public Type getType();
}

View File

@ -2,9 +2,6 @@ package de.maishai.typedast;
import de.maishai.typedast.typedclass.TypedClass;
import java.util.Map;
public interface TypedNode {
Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes);
TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST);
Type typeCheck(TypedClass clas);
}

View File

@ -1,7 +1,6 @@
package de.maishai.typedast;
import org.objectweb.asm.MethodVisitor;
public interface TypedStatement extends TypedNode {
public void codeGen(MethodVisitor mv, MethodContext ctx);
public void codeGen(MethodContext ctx);
}

View File

@ -1,31 +1,60 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Assignment;
import de.maishai.ast.records.Expression;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedAssignment implements TypedStatement {
private String varName;
private TypedExpression value;
private TypedFieldVarAccess location;
private Type type;
public TypedAssignment(TypedClass clas, Assignment untyped) {
convertToTypedAssignment(clas, untyped);
}
public void convertToTypedAssignment(TypedClass clas, Assignment untyped) {
value = convertExpression(clas, untyped.value());
location = new TypedFieldVarAccess(clas, untyped.location());
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (!localVar.containsKey(varName)) {
throw new RuntimeException("Variable not declared");
public Type typeCheck(TypedClass clas) {
Type typeLeft = null;
if (clas.isThereField(location.getName())) {
typeLeft = clas.getFieldType(location.getName());
} else {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
if (clas.getCurrentMethod().isLocalVariableInMethod(location.getName())) {
typeLeft = clas.getCurrentMethod().getLocalVariableType(location.getName());
} else {
throw new RuntimeException("Variable " + location.getName() + " not declared in method");
}
}
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
if (clas.getCurrentConstructor().isLocalVariableInConstructor(location.getName())) {
typeLeft = clas.getCurrentConstructor().getLocalVariableType(location.getName());
} else {
throw new RuntimeException("Variable " + location.getName() + " not declared in constructor");
}
}
}
Type typeLeft = localVar.get(varName);
Type typeRight = value.typeCheck(localVar, classes);
Type typeRight = value.typeCheck(clas);
if (typeLeft.equals(typeRight) ) {
if (typeLeft.equals(typeRight)) {
type = typeLeft;
return typeLeft;
}
@ -33,16 +62,10 @@ public class TypedAssignment implements TypedStatement {
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Assignment untyped = (Assignment) unTypedAST;
TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(untyped.location().id());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
return typedAssignment;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
if (value instanceof TypedIntLiteral) {
ctx.getMv().visitIntInsn(Opcodes.BIPUSH, ((TypedIntLiteral) value).getValue());
ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVarIndex());
}
}
}

View File

@ -2,59 +2,72 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator;
import de.maishai.ast.records.Binary;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
public class TypedBinary implements TypedExpression {
private TypedExpression left;
private Operator op;
private TypedExpression right;
private Type type;
public TypedBinary(TypedClass clas, Binary unTypedBinary) {
convertToTypedBinary(clas, unTypedBinary);
}
public void convertToTypedBinary(TypedClass clas, Binary unTypedBinary) {
left = convertExpression(clas, unTypedBinary.left());
right = convertExpression(clas, unTypedBinary.right());
op = unTypedBinary.op();
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
if (left.typeCheck(localVar, classes) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) {
public Type typeCheck(TypedClass clas) {
Type leftType = left.typeCheck(clas);
Type rightType = right.typeCheck(clas);
if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
if (leftType == Type.INT && rightType == Type.INT) {
type = Type.INT;
return Type.INT;
} else {
throw new RuntimeException("Type mismatch");
throw new RuntimeException("Type mismatch in " + op);
}
}else if(op == Operator.GT || op == Operator.LT || op == Operator.GE || op == Operator.LE || op == Operator.EQ || op == Operator.NE || op == Operator.AND || op == Operator.OR) {
if (left.typeCheck(localVar, classes) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) {
} else if (op == Operator.GT || op == Operator.LT || op == Operator.GE || op == Operator.LE) {
if (leftType == Type.INT && rightType == Type.INT) {
type = Type.BOOL;
return Type.BOOL;
} else {
throw new RuntimeException("Type mismatch");
throw new RuntimeException("Type mismatch in " + op);
}
} else if (op == Operator.AND || op == Operator.OR) {
if (left.typeCheck(localVar, classes) == Type.BOOL &&
right.typeCheck(localVar, classes) == Type.BOOL) {
if (leftType == Type.BOOL && rightType == Type.BOOL) {
type = Type.BOOL;
return Type.BOOL;
} else {
throw new RuntimeException("Type mismatch");
throw new RuntimeException("Type mismatch in " + op);
}
} else {
throw new RuntimeException("Invalid operator");
}
if (leftType == rightType && leftType != Type.VOID) {
type = rightType;
return type;
}
throw new RuntimeException("Void can not be compared with Void");
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Binary binary = (Binary) unTypedAST;
public void codeGen(MethodContext ctx) {
TypedBinary typedBinary = new TypedBinary();
typedBinary.setLeft((TypedExpression) this.left.convertToTypedAST(localVar, classes, binary.left()));
typedBinary.setOp(binary.op());
typedBinary.setRight((TypedExpression) this.right.convertToTypedAST(localVar, classes, binary.right()));
return typedBinary;
}
}

View File

@ -1,159 +1,128 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class TypedBlock implements TypedNode {
private List<TypedLocalVariable> vars;
private List<TypedStatement> stmts;
private List<TypedLocalVariable> vars = new ArrayList<>();
private List<TypedStatement> stmts = new ArrayList<>();
private Type type;
public TypedBlock(TypedClass clas, Block unTypedBlock) {
convertToTypedBlock(clas, unTypedBlock);
}
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
this.vars = vars;
this.stmts = stmts;
}
TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>());
typedBlock.setVars(new ArrayList<>());
public void convertToTypedBlock(TypedClass clas, Block unTypedBlock) {
if(unTypedBlock.localVariables().isEmpty()){
typedBlock.setVars(List.of());
}
if(unTypedBlock.stmts().isEmpty()){
typedBlock.setStmts(List.of());
if (unTypedBlock == null) {
return;
}
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar);
vars.add(new TypedLocalVariable(clas, var));
}
for(var stmt : unTypedBlock.stmts()){
if(stmt instanceof Assignment assignment){
TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(assignment.location().id());
typedAssignment.setValue(null);
typedBlock.getStmts().add(typedAssignment);
for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment(clas, assignment);
typedAssignment.typeCheck(clas);
stmts.add(typedAssignment);
continue;
}
if(stmt instanceof For forStmt){
TypedFor typedFor = new TypedFor();
TypedBlock typedBlock1 = new TypedBlock();
typedFor.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock((forStmt.block())));
typedFor.setAssign(null);
typedFor.setCond(null);
typedFor.setInc(null);
typedBlock.getStmts().add(typedFor);
if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor(clas, forStmt);
typedFor.typeCheck(clas);
stmts.add(typedFor);
continue;
}
if(stmt instanceof IfElse ifElse){
TypedIfElse typedIfElse = new TypedIfElse();
TypedBlock typedBlockIf = new TypedBlock();
TypedBlock typedBlockElse = new TypedBlock();
typedIfElse.setIfTypedBlock(typedBlockIf.unTypedBlockToTypedBlock(ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(typedBlockElse.unTypedBlockToTypedBlock(ifElse.elseBlock()));
typedIfElse.setTypedCon(null);
typedBlock.getStmts().add(typedIfElse);
if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse(clas, ifElse);
typedIfElse.typeCheck(clas);
stmts.add(typedIfElse);
continue;
}
if(stmt instanceof MethodCall methodCall){
TypedMethodCall typedMethodCall = new TypedMethodCall();
typedMethodCall.setArgs(null);
typedMethodCall.setRecipient(null);
typedBlock.getStmts().add(typedMethodCall);
if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile(clas, whileStmt);
typedWhile.typeCheck(clas);
stmts.add(typedWhile);
continue;
}
if(stmt instanceof New newStmt){
TypedNew typedNew = new TypedNew();
typedNew.setArgs(null);
typedNew.setType(newStmt.type());
typedBlock.getStmts().add(typedNew);
if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile(clas, doWhile);
typedDoWhile.typeCheck(clas);
stmts.add(typedDoWhile);
continue;
}
if(stmt instanceof Return returnStmt){
TypedReturn typedReturn = new TypedReturn();
typedReturn.setRet(null);
typedBlock.getStmts().add(typedReturn);
if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn(clas, returnStmt);
typedReturn.typeCheck(clas);
stmts.add(typedReturn);
continue;
}
if(stmt instanceof While whileStmt){
TypedWhile typedWhile = new TypedWhile();
TypedBlock typedBlock1 = new TypedBlock();
typedWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(whileStmt.block()));
typedWhile.setCond(null);
typedBlock.getStmts().add(typedWhile);
if (stmt instanceof New newStmt) {
TypedNew typedNew = new TypedNew(clas, newStmt);
typedNew.typeCheck(clas);
stmts.add(typedNew);
continue;
}
if(stmt instanceof DoWhile doWhile){
TypedDoWhile typedDoWhile = new TypedDoWhile();
TypedBlock typedBlock1 = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(doWhile.block()));
typedDoWhile.setCond(null);
typedBlock.getStmts().add(typedDoWhile);
continue;
}
if(stmt instanceof Break){
TypedBreak typedBreak = new TypedBreak();
typedBlock.getStmts().add(typedBreak);
}
if (stmt instanceof Break) {
stmts.add(new TypedBreak());
continue;
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(clas, methodCall);
typedMethodCall.typeCheck(clas);
stmts.add(typedMethodCall);
}
}
//System.out.println("stmts:" + typedBlock.getStmts().toString());
return typedBlock;
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
Type typeOfLastStmt = Type.VOID;
for (TypedLocalVariable var : vars) {
var.typeCheck(localVar, classes);
}
for (TypedStatement stmt : stmts) {
stmt.typeCheck(localVar, classes);
typeOfLastStmt = stmt.typeCheck(localVar, classes);
}
return typeOfLastStmt;
this.typeCheck(clas);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Block untyped = (Block) unTypedAST;
TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>());
typedBlock.setVars(new ArrayList<>());
for (Declaration var : untyped.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar);
}
typedBlock.getStmts().add((TypedStatement) untyped.stmts());
int i = 0;
for(var stmt : untyped.stmts()){
System.out.println(i++ +" :" + stmt);
public Type typeCheck(TypedClass clas) {
Type chekType = null;
for (TypedStatement stmt : stmts) {
stmt.typeCheck(clas);
if(stmt instanceof TypedReturn returnStmt) {
chekType = returnStmt.getType();
}
}
return typedBlock;
if(chekType == null) {
chekType = Type.VOID;
}
type = chekType;
return type;
}
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
for (TypedLocalVariable var : vars) {
var.codeGen(mv, ctx);
//var.codeGen(ctx);
}
for (TypedStatement stmt : stmts) {
stmt.codeGen(mv, ctx);
stmt.codeGen(ctx);
}
}
}

View File

@ -1,29 +1,46 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.BoolLiteral;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
@Data
@NoArgsConstructor
public class TypedBoolLiteral implements TypedExpression {
private Boolean value;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
public TypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(clas, unTypedBoolLiteral);
}
public void convertToTypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value();
type = Type.BOOL;
return Type.BOOL;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
BoolLiteral untyped = (BoolLiteral) unTypedAST;
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
typedBoolLiteral.setValue(untyped.value());
return typedBoolLiteral;
public Type typeCheck(TypedClass clas) {
return type;
}
@Override
public void codeGen(MethodContext ctx) {
if(value){
ctx.getMv().visitInsn(Opcodes.ICONST_1);
}else{
ctx.getMv().visitInsn(Opcodes.ICONST_0);
}
}
}

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Break;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
@ -9,24 +10,22 @@ import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
public class TypedBreak implements TypedStatement {
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID;
private Type type = Type.VOID;
public TypedBreak convertToTypedBreak(TypedClass clas, Break unTypedBreak) {
return this;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Break untyped = (Break) unTypedAST;
if(untyped == null) {
throw new RuntimeException("Break not found");
}
return new TypedBreak();
public Type typeCheck(TypedClass clas) {
return type;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,25 +1,38 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.CharLiteral;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
public class TypedCharLiteral implements TypedExpression {
private char value;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.CHAR;
private Type type;
public TypedCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(clas, unTypedCharLiteral);
}
public void convertToCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value();
type = Type.CHAR;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
CharLiteral untyped = (CharLiteral) unTypedAST;
TypedCharLiteral typedCharLiteral = new TypedCharLiteral();
typedCharLiteral.setValue(untyped.value());
return typedCharLiteral;
public Type typeCheck(TypedClass clas) {
return type;
}
@Override
public void codeGen(MethodContext ctx) {
ctx.getMv().visitLdcInsn(value);
}
}

View File

@ -1,9 +1,9 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Method;
import de.maishai.typedast.ClassContext;
import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
@ -23,77 +23,196 @@ import java.util.Map;
@NoArgsConstructor
public class TypedClass implements TypedNode {
private String className;
private List<TypedField> typedFields;
private List<TypedMethod> typedMethods;
private List<TypedConstructor> typedConstructors;
private List<TypedDeclaration> typedDeclarations = new ArrayList<>();
private List<TypedMethod> typedMethods = new ArrayList<>();
private List<TypedConstructor> typedConstructors = new ArrayList<>();
private TypedMethod currentMethod;
private TypedConstructor currentConstructor;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (classes.containsKey(className)) {
throw new RuntimeException("Class " + className + " already exists");
}
classes.put(className, this);
for (TypedField field : typedFields) {
field.typeCheck(localVar, classes);
}
for(TypedConstructor constructor : typedConstructors) {
constructor.typeCheck(localVar, classes);
}
for (TypedMethod typedMethod : typedMethods) {
typedMethod.typeCheck(localVar, classes);
}
return Type.REFERENCE(className);
public TypedClass(Class c) {
convertToTypedClass(c);
}
public TypedNode startConversion(de.maishai.ast.records.Class c) {
Map<String, Type> local = new HashMap<>();
Map<String, TypedClass> classMap = new HashMap<>();
return convertToTypedAST(local, classMap, c);
public void enterCurrentMethod(TypedMethod method) {
currentMethod = method;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST;
typedClass.setClassName(c.classname());
typedClass.setTypedFields(new ArrayList<>());
typedClass.setTypedMethods(new ArrayList<>());
typedClass.setTypedConstructors(new ArrayList<>());
public void exitCurrentMethod() {
currentMethod = null;
}
for (Declaration field : c.fieldDeclarations()) {
TypedField typedField = new TypedField();
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
public void enterCurrentConstructor(TypedConstructor constructor) {
currentConstructor = constructor;
}
public void exitCurrentConstructor() {
currentConstructor = null;
}
public boolean isCurrentMethodPresent() {
return currentMethod != null;
}
public boolean isCurrentConstructorPresent() {
return currentConstructor != null;
}
public boolean isParameterNameInCurrentMethod(String parameterName) {
if (currentMethod == null) {
return false;
}
for (TypedParameter p : currentMethod.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
return false;
}
public boolean isParameterNameInCurrentConstructor(String parameterName) {
if (currentConstructor == null) {
return false;
}
for (TypedParameter p : currentConstructor.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
return false;
}
public void convertToTypedClass(Class c) {
className = c.classname();
type = Type.REFERENCE(className);
// Am Anfang werden die Attribute, die Konstruktoren und Methoden in die jeweilige Liste eingefügt.
// damit die Methoden verwendet werden können, bevor deren Blöcke ausgeführt werden
for (Declaration declaration : c.fieldDeclarations()) {
typedDeclarations.add(new TypedDeclaration(this, declaration));
}
for (Constructor constructor : c.constructors()) {
typedConstructors.add(new TypedConstructor(this, constructor));
}
for (Method method : c.methods()) {
TypedMethod typedMethod = new TypedMethod();
typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method));
typedMethods.add(new TypedMethod(this, method));
}
// Hier werden die Blöcke der Konstruktoren ausgeführt
int i = 0;
for (Constructor constructor : c.constructors()) {
TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
enterCurrentConstructor(typedConstructors.get(i));
typedConstructors.get(i).convertToBlock(this, constructor);
exitCurrentConstructor();
i++;
}
// Hier werden die Blöcke der Methoden ausgeführt
int j = 0;
for (Method method : c.methods()) {
enterCurrentMethod(typedMethods.get(j));
typedMethods.get(j).convertToTypedBlock(this, method);
exitCurrentMethod();
j++;
}
return typedClass;
}
@Override
public Type typeCheck(TypedClass clas) {
return type;
}
public TypedNode startConversion(Class c) {
Map<String, Type> local = new HashMap<>();
return new TypedClass(c);
}
public boolean isParameterWitNameInMethod(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
}
return false;
}
public boolean isParameterWitNameInConstructor(String parameterName) {
for (TypedConstructor c : typedConstructors) {
for (TypedParameter p : c.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
}
return false;
}
public Type getParameterTypeInCurrentMethod(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return p.getType();
}
}
}
return null;
}
public Type getParameterTypeInCurrentConstructor(String parameterName) {
for (TypedParameter p : currentConstructor.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return p.getType();
}
}
return null;
}
public boolean isThereField(String fieldName) {
for (TypedDeclaration f : typedDeclarations) {
if (f.getName().equals(fieldName)) {
return true;
}
}
return false;
}
public Type getFieldType(String fieldName) {
for (TypedDeclaration f : typedDeclarations) {
if (f.getName().equals(fieldName)) {
return f.getType();
}
}
return null;
}
public Type getMethodType(String methodName) {
for (TypedMethod m : typedMethods) {
if (m.getName().equals(methodName)) {
return m.getReturnType();
}
}
return null;
}
public byte[] codeGen() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassContext ctx = new ClassContext(className, cw);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
for (TypedField field : typedFields) {
field.codeGen(cw);
for (TypedDeclaration declaration : typedDeclarations) {
declaration.codeGen(cw);
}
for (TypedConstructor constructor : typedConstructors) {
constructor.codeGen(cw);
constructor.codeGen(ctx);
}
for (TypedMethod m : typedMethods) {
//m.codeGen(cw);
m.codeGen(ctx);
}
return cw.toByteArray();
@ -105,16 +224,28 @@ public class TypedClass implements TypedNode {
c.setClassName("SomeClass");
//Fields
TypedField f1 = new TypedField("someNumber", Type.INT);
TypedField f2 = new TypedField("someChar", Type.CHAR);
c.typedFields = List.of(f1, f2);
TypedDeclaration f1 = new TypedDeclaration("someNumber", Type.INT);
TypedDeclaration f2 = new TypedDeclaration("someChar", Type.CHAR);
c.typedDeclarations = List.of(f1, f2);
//Constructors
TypedConstructor constructor = new TypedConstructor( "SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock());
TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>()));
c.typedConstructors = List.of(constructor);
//Methods
c.typedMethods = new ArrayList<>();
TypedMethod m = new TypedMethod();
m.setName("someMethod");
m.setReturnType(Type.INT);
m.setTypedParameters(List.of(new TypedParameter("test", Type.INT)));
TypedIfElse ifs = new TypedIfElse();
TypedBinary cond = new TypedBinary();
cond.setOp(Operator.GT);
//cond.setLeft(??);
cond.setRight(new TypedIntLiteral(12));
ifs.setTypedCon(cond);
m.setTypedBlock(new TypedBlock(List.of(), List.of(ifs, new TypedReturn())));
c.typedMethods.add(m);
//codeGen
byte[] code = c.codeGen();

View File

@ -1,22 +1,16 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Block;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import de.maishai.typedast.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@AllArgsConstructor
@ -24,82 +18,75 @@ import java.util.Map;
public class TypedConstructor implements TypedNode {
private String name;
private List<TypedParameter> typedParameters;
private List<TypedParameter> typedParameters = new ArrayList<>();
private TypedBlock typedBlock;
private Type type;
private List<TypedLocalVariable> localVariables = new ArrayList<>();
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(name)) {
throw new RuntimeException("constructor already declared");
}
localVar.put(name, Type.VOID);
if (typedParameters != null) {
for (TypedParameter param : typedParameters) {
param.typeCheck(localVar, classes);
}
}
typedBlock.typeCheck(localVar, classes);
return Type.VOID;
public TypedConstructor(String name, List<TypedParameter> typedParameters, TypedBlock typedBlock) {
this.name = name;
this.typedParameters = typedParameters;
this.typedBlock = typedBlock;
}
public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.className());
typedConstructor.setTypedParameters(new ArrayList<>());
public TypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
convertToTypedConstructor(clas, unTypedConstructor);
}
if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(List.of());
} else {
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
for (Parameter param : unTypedConstructor.params()) {
TypedParameter typedParam = new TypedParameter();
typedParam.setParaName(param.name());
typedParam.setType(param.type());
typedConstructor.getTypedParameters().add(typedParam);
}
public boolean isParameterPresent(String parameterName) {
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
public boolean isLocalVariableInConstructor(String localVarName) {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
}
public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}
public void convertToTypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
name = unTypedConstructor.className();
for (Parameter param : unTypedConstructor.params()) {
typedParameters.add(new TypedParameter(clas, param));
}
TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(unTypedConstructor.block()));
return typedConstructor;
type = Type.VOID;
}
public void convertToBlock(TypedClass clas, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(clas, unTypedConstructor.block());
typeCheck(clas);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Constructor untyped = (Constructor) unTypedAST;
TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(untyped.className());
typedConstructor.setTypedParameters(new ArrayList<>());
if (untyped.params().isEmpty()) {
typedConstructor.setTypedParameters(null);
} else {
for (Parameter param : untyped.params()) {
TypedParameter typedParam = new TypedParameter();
typedParam.setType(param.type());
typedParam.setParaName(param.name());
typedConstructor.getTypedParameters().add(typedParam);
}
public Type typeCheck(TypedClass clas) {
type = typedBlock.typeCheck(clas);
if (type != Type.VOID) {
throw new RuntimeException("Constructor must not habe a return statement");
}
TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
return typedConstructor;
return type;
}
public void codeGen(ClassWriter cw) {
public void codeGen(ClassContext ctx) {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
MethodVisitor mv = cw.visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
MethodContext context = new MethodContext(mv);
MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
MethodContext mctx = new MethodContext(ctx, mv);
typedParameters.forEach(param -> mctx.registerVariable(param.getParaName(), param.getType()));
//super();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mctx.pushStack("this");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
typedBlock.codeGen(mv, context);
typedBlock.codeGen(mctx);
mv.visitInsn(Opcodes.RETURN);
context.wrapUp();
mctx.wrapUp();
}
}

View File

@ -1,28 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
public class TypedContinue implements TypedStatement {
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return new TypedContinue();
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
}
}

View File

@ -0,0 +1,62 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Type.Kind.REFERENCE;
@Data
@AllArgsConstructor
@NoArgsConstructor
public final class TypedDeclaration implements TypedNode {
private String name;
private Type type;
public TypedDeclaration(TypedClass clas, Declaration declaration) {
convertToTypedDeclaration(clas, declaration);
}
public void convertToTypedDeclaration(TypedClass clas, Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(clas);
}
@Override
public Type typeCheck(TypedClass clas) {
if (clas.isThereField(name)) {
throw new RuntimeException("Field " + name + " already declared");
}
if (type.getKind() == REFERENCE) {
if (!type.getReference().equals(clas.getClassName())) {
throw new RuntimeException("Field " + name + " has wrong type");
}
}
return type;
}
/*
public void codeGen(MethodVisitor mv, MethodContext ctx) {
System.out.println("Generating code for local variable " + name);
int index = ctx.addVariable(name);
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index);
}
*/
public void codeGen(ClassWriter cw) {
int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind
cw.visitField(access, name, type.getDescriptor(), null, null).visitEnd();
}
}

View File

@ -1,38 +1,41 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.DoWhile;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedDoWhile implements TypedStatement {
private TypedBlock typedBlock;
private TypedExpression cond;
private Type type;
public TypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
convertToTypedDoWhile(clas, unTypedDoWhile);
}
public void convertToTypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock(clas, unTypedDoWhile.block());
cond = convertExpression(clas, unTypedDoWhile.cond());
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(cond.typeCheck(localVar, classes) != Type.BOOL){
public Type typeCheck(TypedClass clas) {
if (cond.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("Condition must be boolean");
}
typedBlock.typeCheck(localVar, classes);
typedBlock.typeCheck(clas);
this.type = Type.VOID;
return Type.VOID;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
DoWhile untyped = (DoWhile) unTypedAST;
TypedDoWhile typedDoWhile = new TypedDoWhile();
TypedBlock typedBlock = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
return typedDoWhile;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,53 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import java.util.Map;
@AllArgsConstructor
@RequiredArgsConstructor
@Data
public class TypedField implements TypedNode {
private String varName;
private Type type;
public TypedField unTypedFieldToTypedField(Declaration declaration){
TypedField typedField = new TypedField();
typedField.setType(declaration.type());
typedField.setVarName(declaration.name());
return typedField;
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(varName)) {
throw new RuntimeException("Variable " + varName + " already declared");
}
localVar.put(varName, type);
return type;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField();
typedField.setType(untypedDeclaration.type());
typedField.setVarName(untypedDeclaration.name());
return typedField;
}
public void codeGen(ClassWriter cw) {
int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind
cw.visitField(access, varName, type.getDescriptor(), null, null).visitEnd();
}
}

View File

@ -1,35 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.FieldVarAccess;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import java.util.Map;
@Data
public class TypedFieldId implements TypedExpression {
private Boolean field;
private TypedExpression recipient;
private String name;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
//TODO: Implement typeCheck for FieldVarAccess
if(field){
return null;
}else{
return null;
}
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST;
TypedFieldId typedFieldId = new TypedFieldId();
typedFieldId.setField(untypedFieldVarAccess.field());
typedFieldId.setRecipient((TypedExpression) recipient.convertToTypedAST(localVar, classes, untypedFieldVarAccess.recursiveOwnerChain()));
typedFieldId.setName(untypedFieldVarAccess.id());
return typedFieldId;
}
}

View File

@ -0,0 +1,81 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
public class TypedFieldVarAccess implements TypedExpression {
private Boolean field;
private TypedExpression recursiveOwnerChain;
private String name;
private Type type;
public TypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(clas, unTypedFieldVarAccess);
}
public void convertToTypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
field = unTypedFieldVarAccess.field();
recursiveOwnerChain = convertExpression(clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id();
}
@Override
public Type typeCheck(TypedClass clas) {
if (field) {
if (clas.isThereField(name)) {
type = clas.getFieldType(name);
return clas.getFieldType(name);
}else{
throw new RuntimeException("Field " + name + " not declared ");
}
} else {
if (clas.isCurrentConstructorPresent()) {
if (clas.isParameterNameInCurrentConstructor(name)) {
type = clas.getParameterTypeInCurrentConstructor(name);
return type;
} else if (clas.getCurrentConstructor().isLocalVariablePresent(name)) {
type = clas.getCurrentConstructor().getLocalVariableType(name);
return type;
} else if(clas.isThereField(name)){
type = clas.getFieldType(name);
return type;
}
else {
throw new RuntimeException("Variable " + name + " not declared in constructor");
}
} else if (clas.isCurrentMethodPresent()) {
if (clas.isParameterWitNameInMethod(name)) {
type = clas.getParameterTypeInCurrentMethod(name);
return type;
} else if (clas.getCurrentMethod().isLocalVariablePresent(name)) {
type = clas.getCurrentMethod().getLocalVariableType(name);
return type;
} else if(clas.isThereField(name)){
type = clas.getFieldType(name);
return type;
}
else {
throw new RuntimeException("Variable " + name + " not declared in method");
}
}
throw new RuntimeException("Variable " + name + " not declared ");
}
}
@Override
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,6 +1,6 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.For;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -9,40 +9,42 @@ import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TypedFor implements TypedStatement {
private TypedExpression assign;
private TypedAssignment assign;
private TypedExpression cond;
private TypedExpression inc;
private TypedAssignment inc;
private TypedBlock typedBlock;
private Type type;
public TypedFor(TypedClass clas, For unTypedFor) {
convertToTypedFor(clas, unTypedFor);
}
public void convertToTypedFor(TypedClass clas, For unTypedFor) {
assign = new TypedAssignment(clas, unTypedFor.assign());
cond = convertExpression(clas, unTypedFor.cond());
inc = new TypedAssignment(clas, unTypedFor.inc());
typedBlock = new TypedBlock(clas, unTypedFor.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
assign.typeCheck(localVar, classes);
if(!cond.typeCheck(localVar, classes).equals(Type.BOOL)) {
public Type typeCheck(TypedClass clas) {
assign.typeCheck(clas);
if (!cond.typeCheck(clas).equals(Type.BOOL)) {
throw new RuntimeException("Condition must be a boolean");
}
inc.typeCheck(localVar, classes);
return typedBlock.typeCheck(localVar, classes);
inc.typeCheck(clas);
type = typedBlock.typeCheck(clas);
return type;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
For untyped = (For) unTypedAST;
TypedFor typedFor = new TypedFor();
typedFor.setAssign((TypedExpression) assign.convertToTypedAST(localVar, classes, untyped.assign()));
typedFor.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
typedFor.setInc((TypedExpression) inc.convertToTypedAST(localVar, classes, untyped.inc()));
TypedBlock typedBlock = new TypedBlock();
typedFor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
return typedFor;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,48 +1,72 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.IfElse;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
public class TypedIfElse implements TypedStatement {
private TypedExpression typedCon;
private TypedBlock ifTypedBlock;
private TypedBlock elseTypedBlock;
private Type type;
public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
convertToTypedIfElse(clas, unTypedIfElse);
}
public void convertToTypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock(clas, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock(clas, unTypedIfElse.elseBlock());
typedCon = convertExpression(clas, unTypedIfElse.cond());
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
// condition must be a boolean
if (typedCon.typeCheck(localVar, classes) != Type.BOOL) {
public Type typeCheck(TypedClass clas) {
/*
if (typedCon.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("If condition must be a boolean");
}
//Statement1 and Statement2 must be of type void
if (ifTypedBlock.typeCheck(localVar, classes) != Type.VOID) {
if (ifTypedBlock.typeCheck(clas) != Type.VOID) {
throw new RuntimeException("If block must be of type void");
}
return Type.VOID;
*/
//TODO: it still not catching the all cases when return is used
if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) {
type = ifTypedBlock.typeCheck(clas);
}
if (elseTypedBlock.typeCheck(clas) == Type.VOID) {
type = ifTypedBlock.typeCheck(clas);
}
if (ifTypedBlock.typeCheck(clas) == Type.VOID) {
type = elseTypedBlock.typeCheck(clas);
}
return type;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
IfElse ifElse = (IfElse) unTypedAST;
TypedIfElse typedIfElse = new TypedIfElse();
typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, classes, ifElse.cond()));
TypedBlock ifTypedBlock = new TypedBlock();
TypedBlock elseTypedBlock = new TypedBlock();
typedIfElse.setIfTypedBlock(ifTypedBlock.unTypedBlockToTypedBlock(ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(elseTypedBlock.unTypedBlockToTypedBlock(ifElse.elseBlock()));
return typedIfElse;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
Label falseLabel = new Label();
Label end = new Label();
typedCon.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFEQ, falseLabel);
ifTypedBlock.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);
ctx.getMv().visitLabel(falseLabel);
if (elseTypedBlock != null) {
elseTypedBlock.codeGen(ctx);
}
ctx.getMv().visitLabel(end);
}
}

View File

@ -1,28 +1,46 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.IntLiteral;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
public class TypedIntLiteral implements TypedExpression {
private Integer value;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.INT;
private Type type;
public TypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(clas, unTypedIntLiteral);
}
public void convertToTypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
value = unTypedIntLiteral.value();
type = Type.INT;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
IntLiteral untyped = (IntLiteral) unTypedAST;
return new TypedIntLiteral(untyped.value());
public Type typeCheck(TypedClass clas) {
return type;
}
public TypedIntLiteral(Integer value) {
this.value = value;
}
@Override
public void codeGen(MethodContext ctx) {
ctx.getMv().visitLdcInsn(value);
}
}

View File

@ -9,7 +9,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ -17,29 +16,36 @@ public final class TypedLocalVariable implements TypedNode {
private String name;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(name)) {
throw new RuntimeException("Variable " + name + " already exists");
}
localVar.put(name, type);
return type;
public TypedLocalVariable(TypedClass clas, Declaration declaration) {
convertToTypedLocalVariable(clas, declaration);
}
public void convertToTypedLocalVariable(TypedClass clas, Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(clas);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Declaration untypedLocalVariable = (Declaration) unTypedAST;
TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
typedLocalVariable.setName(untypedLocalVariable.name());
typedLocalVariable.setType(untypedLocalVariable.type());
return typedLocalVariable;
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
if (clas.getCurrentMethod().isLocalVariableInMethod(name)) {
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentMethod().getLocalVariables().add(this);
return type;
}
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
if (clas.getCurrentConstructor().isLocalVariableInConstructor(name)) {
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentConstructor().getLocalVariables().add(this);
return type;
}
throw new RuntimeException("not found method or constructor in class");
}
public void codeGen(MethodVisitor mv, MethodContext ctx) {
int index = ctx.addVariable(name);
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index);
ctx.registerVariable(name, type);
}
}

View File

@ -1,101 +1,121 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.IfElse;
import de.maishai.ast.records.Method;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.ClassWriter;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Data
@NoArgsConstructor
public class TypedMethod implements TypedNode {
private String name;
private Type returnType;
private List<TypedParameter> typedParameters;
private List<TypedParameter> typedParameters = new ArrayList<>();
private List<TypedLocalVariable> localVariables = new ArrayList<>();
private TypedBlock typedBlock;
public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) {
TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(unTypedMethod.methodName());
typedMethod.setTypedParameters(new ArrayList<>());
public TypedMethod(TypedClass clas, Method unTypedMethod) {
convertToTypedMethod(clas, unTypedMethod);
}
if(unTypedMethod.params().isEmpty()){
typedMethod.setTypedParameters(List.of());
}else {
for (Parameter parameter : unTypedMethod.params()) {
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type());
typedParameters.add(typedParameter);
public void convertToTypedMethod(TypedClass clas, Method unTypedMethod) {
name = unTypedMethod.methodName();
returnType = unTypedMethod.type();
for (var parameter : unTypedMethod.params()) {
typedParameters.add(new TypedParameter(clas, parameter));
}
clas.getTypedMethods().stream().filter(method -> method.equals(this)).findFirst().ifPresentOrElse(
method -> {
throw new RuntimeException("Method " + name + " already exists");
}, () -> {
});
}
public void convertToTypedBlock(TypedClass clas, Method unTypedMethod) {
typedBlock = new TypedBlock(clas, unTypedMethod.block());
typeCheck(clas);
}
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
public boolean isParameterPresent(String parameterName) {
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
public boolean isLocalVariableInMethod(String localVarName) {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
}
public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}
private boolean hasEvenReturnsInIfElseBlocks() {
List<TypedIfElse> typedIfElses = new ArrayList<>();
for (var stmt : typedBlock.getStmts()) {
if (stmt instanceof TypedIfElse ifElse) {
for (var stmt2 : ifElse.getIfTypedBlock().getStmts()) {
if (stmt2 instanceof TypedReturn) {
typedIfElses.add(ifElse);
}
}
for (var stmt2 : ifElse.getElseTypedBlock().getStmts()) {
if (stmt2 instanceof TypedReturn) {
typedIfElses.add(ifElse);
}
}
}
}
TypedBlock block = new TypedBlock();
typedMethod.setTypedBlock(block.unTypedBlockToTypedBlock(unTypedMethod.block()));
return typedMethod;
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(name)) {
throw new RuntimeException("Method " + name + " already exists");
if (typedIfElses.size() % 2 == 0) {
return true;
} else {
return false;
}
localVar.put(name, returnType);
for(TypedParameter typedParameter : typedParameters) {
typedParameter.typeCheck(localVar, classes);
}
return typedBlock.typeCheck(localVar, classes);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod();
typedMethod.setTypedParameters(new ArrayList<>());
typedMethod.setName(untypedMethod.methodName());
typedMethod.setReturnType(untypedMethod.type());
for(Parameter parameter : untypedMethod.params()) {
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type());
typedMethod.getTypedParameters().add(typedParameter);
public Type typeCheck(TypedClass clas) {
if (returnType != Type.VOID && !hasEvenReturnsInIfElseBlocks()) {
if (typedBlock.typeCheck(clas).getKind() != returnType.getKind()) {
if (hasEvenReturnsInIfElseBlocks()) {
throw new RuntimeException("Method " + name + " must have even returns in if else blocks");
} else {
throw new RuntimeException("Method " + name + " must return " + returnType.getKind());
}
}
}
TypedBlock typedBlock = new TypedBlock();
typedMethod.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untypedMethod.block()));
return typedMethod;
return returnType;
}
private TypedParameter getTypedParameter(Parameter parameter) {
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
if(parameter.type() == Type.CHAR){
typedParameter.setType(Type.CHAR);
@Override
public boolean equals(Object obj) {
if (obj instanceof TypedMethod) {
TypedMethod other = (TypedMethod) obj;
return name.equals(other.name) && returnType.equals(other.returnType) && typedParameters.equals(other.typedParameters);
}
if(parameter.type() == Type.BOOL){
typedParameter.setType(Type.BOOL);
}
if(parameter.type() == Type.INT){
typedParameter.setType(Type.INT);
}
return typedParameter;
return false;
}
public void codeGen(ClassWriter cw) {
public void codeGen(ClassContext ctx) {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
MethodVisitor mv = cw.visitMethod(accessModifier, name,
MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, name,
CodeGenUtils.generateDescriptor(typedParameters, returnType), null, null);
MethodContext context = new MethodContext(mv);
MethodContext context = new MethodContext(ctx, mv);
typedParameters.forEach(param -> context.registerVariable(param.getParaName(), param.getType()));
typedBlock.codeGen(mv, context);
typedBlock.codeGen(context);
context.wrapUp();
}
}

View File

@ -1,31 +1,66 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.MethodCall;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
//TODO: test this after fixing error from parser
@Data
@NoArgsConstructor
public class TypedMethodCall implements TypedExpression, TypedStatement {
private TypedExpression recipient;
private List<TypedExpression> args;
private TypedFieldVarAccess recipient;
private List<TypedExpression> args = new ArrayList<>();
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
//TODO: Implement typeCheck for MethodCall
return null;
public TypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
convertToTypedMethodCall(clas, unTypedMethodCall);
}
public void convertToTypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(clas, unTypedMethodCall.recipient());
for (Expression arg : unTypedMethodCall.args()) {
args.add(convertExpression(clas, arg));
}
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
//TODO: Implement this
return null;
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent() || clas.isCurrentConstructorPresent()) {
List<TypedMethod> methods = clas.getTypedMethods().stream()
.filter(method -> method.getName().equals(recipient.getName()))
.toList();
for (TypedMethod method : methods) {
if (method.getTypedParameters().size() == args.size()) {
boolean allMatch = true;
for (int i = 0; i < args.size(); i++) {
if (!args.get(i).typeCheck(clas).equals(method.getTypedParameters().get(i).getType())) {
allMatch = false;
break;
}
}
if (allMatch) {
type = method.getReturnType();
return type;
}
}
}
}
throw new RuntimeException("Method not found");
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,56 +1,55 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.New;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedNew implements TypedExpression, TypedStatement {
private Type type;
private List<TypedExpression> args;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
// new A(1, 2, 3)
if (!classes.containsKey(this.type.getReference())) {
throw new RuntimeException("Class " + this.type.getReference() + " not found");
}
private List<TypedExpression> args = new ArrayList<>();
TypedClass klasse = classes.get(this.type.getReference());
if (klasse.getTypedFields().size() != args.size()) {
throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference());
public TypedNew(TypedClass clas, New unTypedNew) {
convertToTypedNew(clas, unTypedNew);
}
public void convertToTypedNew(TypedClass clas, New unTypedNew) {
type = unTypedNew.type();
for (Expression arg : unTypedNew.args()) {
args.add(convertExpression(clas, arg));
}
// compare the types of the arguments
for (int i = 0; i < args.size(); i++) {
Type type = args.get(i).typeCheck(localVar, classes);
if (!type.equals(klasse.getTypedFields().get(i).getType())) {
throw new RuntimeException("False type for argument " + i + " in class " + type.getKind());
}
@Override
public Type typeCheck(TypedClass clas) {
for (var constructor : clas.getTypedConstructors()) {
if (constructor.getTypedParameters().size() == args.size()) {
boolean valid = true;
for (int i = 0; i < args.size(); i++) {
if (!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(clas))) {
valid = false;
break;
}
}
if (valid) {
return Type.REFERENCE(clas.getClassName());
}
}
}
return Type.VOID;
throw new RuntimeException("No matching constructor found");
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
New untyped = (New) unTypedAST;
TypedNew typedNew = new TypedNew();
typedNew.setType(untyped.type());
if(untyped.args().isEmpty()){
//TODO: Implement this
}
for(de.maishai.ast.records.Expression arg : untyped.args()) {
//TODO: Implement this
}
return typedNew;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -1,37 +1,52 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.Map;
@AllArgsConstructor
@RequiredArgsConstructor
@Data
public class TypedParameter implements TypedNode {
private String paraName;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
localVar.put(paraName, type);
return type;
public TypedParameter(TypedClass clas, Parameter unTypedParameter) {
convertToTypedParameter(clas, unTypedParameter);
}
public void convertToTypedParameter(TypedClass clas, Parameter unTypedParameter) {
paraName = unTypedParameter.name();
type = unTypedParameter.type();
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Parameter untyped = (Parameter) unTypedAST;
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(untyped.name());
typedParameter.setType(untyped.type());
return typedParameter;
public boolean equals(Object obj) {
if (obj instanceof TypedParameter) {
TypedParameter other = (TypedParameter) obj;
return paraName.equals(other.paraName) && type.equals(other.type);
}
return false;
}
@Override
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent()) {
if (clas.isParameterWitNameInMethod(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
} else if (clas.isCurrentConstructorPresent()) {
if (clas.isParameterWitNameInConstructor(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
}
return type;
}

View File

@ -1,31 +1,63 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Return;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
public class TypedReturn implements TypedStatement {
private TypedExpression ret;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return ret.typeCheck(localVar, classes);
public TypedReturn(TypedClass clas, Return unTypedReturn) {
convertToTypedReturn(clas, unTypedReturn);
}
public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) {
ret = convertExpression(clas, unTypedReturn.ret());
if(ret == null){
type = Type.VOID;
}else{
type = ret.getType();
}
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Return untyped = (Return) unTypedAST;
TypedReturn typedReturn = new TypedReturn();
typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, classes, untyped.ret()));
return typedReturn;
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent()) {
if (clas.getCurrentMethod().getReturnType().getKind() != this.type.getKind()) {
StringBuilder exp = new StringBuilder();
exp.append("\nMismatched return type: ");
exp.append("\nExpected: ").append(clas.getCurrentMethod().getReturnType().getKind());
exp.append("\nActual: ").append(this.type.getKind());
exp.append("\nMethod name: ").append(clas.getCurrentMethod().getName());
throw new RuntimeException(exp.toString());
}
}
return type;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
if (ret == null) {
ctx.getMv().visitInsn(Opcodes.RETURN);
} else {
ret.codeGen(ctx);
if (ret.getType().getKind() != Type.Kind.REFERENCE) {
ctx.getMv().visitInsn(Opcodes.IRETURN);
} else {
ctx.getMv().visitInsn(Opcodes.ARETURN);
}
}
}
}

View File

@ -2,28 +2,44 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Unary;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedUnary implements TypedExpression {
private UnaryOperator op;
private TypedExpression right;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
private Type type;
if(op == UnaryOperator.NOT){
if(right.typeCheck(localVar, classes) != Type.BOOL){
public TypedUnary(TypedClass clas, Unary unTypedUnary) {
convertToTypedUnary(clas, unTypedUnary);
}
public void convertToTypedUnary(TypedClass clas, Unary unTypedUnary) {
op = unTypedUnary.op();
right = convertExpression(clas, unTypedUnary.right());
}
@Override
public Type typeCheck(TypedClass clas) {
if (op == UnaryOperator.NOT) {
if (right.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("Not operator must be applied to boolean");
}
return Type.BOOL;
}
if(op == UnaryOperator.SUB){
if(right.typeCheck(localVar, classes) != Type.INT){
if (op == UnaryOperator.SUB) {
if (right.typeCheck(clas) != Type.INT) {
throw new RuntimeException("Minus operator must be applied to int");
}
return Type.INT;
@ -32,11 +48,14 @@ public class TypedUnary implements TypedExpression {
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Unary untyped = (Unary) unTypedAST;
TypedUnary typedUnary = new TypedUnary();
typedUnary.setOp(untyped.op());
typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, classes, untyped.right()));
return typedUnary;
public void codeGen(MethodContext ctx) {
right.codeGen(ctx);
if(op == UnaryOperator.NOT){
ctx.getMv().visitInsn(Opcodes.ICONST_1);
ctx.getMv().visitInsn(Opcodes.IXOR);
}
if(op == UnaryOperator.SUB){
ctx.getMv().visitInsn(Opcodes.INEG);
}
}
}

View File

@ -1,36 +1,41 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.While;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedWhile implements TypedStatement {
private TypedExpression cond;
private TypedBlock typedBlock;
private Type type;
public TypedWhile(TypedClass clas, While unTypedWhile) {
convertToTypedWhile(clas, unTypedWhile);
}
public void convertToTypedWhile(TypedClass clas, While unTypedWhile) {
cond = convertExpression(clas, unTypedWhile.cond());
typedBlock = new TypedBlock(clas, unTypedWhile.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(cond.typeCheck(localVar, classes) != Type.BOOL){
public Type typeCheck(TypedClass clas) {
if (cond.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("While condition must be a boolean");
}
return typedBlock.typeCheck(localVar, classes);
type = typedBlock.typeCheck(clas);
return type;
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
While untyped = (While) unTypedAST;
TypedWhile typedWhile = new TypedWhile();
typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
TypedBlock typedBlock = new TypedBlock();
typedWhile.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(untyped.block()));
return typedWhile;
}
@Override
public void codeGen(MethodVisitor mv, MethodContext ctx) {
public void codeGen(MethodContext ctx) {
}
}

View File

@ -0,0 +1,81 @@
public class ClassCanBeTyped {
int x;
int y;
ClassCanBeTyped b;
ClassCanBeTyped c;
public ClassCanBeTyped(int x) {
this.x = x;
}
public ClassCanBeTyped(int x, int y) {
this.x = x;
this.y = y;
}
public ClassCanBeTyped initClassCanBeTyped(int x) {
int a;
a = 10;
b = new ClassCanBeTyped(x);
b.x = 10 + a;
b.y = 20;
b.c.x = 20 + a;
b.c.b.y = b.x;
return b;
}
public ClassCanBeTyped init(int x, int y) {
return new ClassCanBeTyped(x, y);
}
public ClassCanBeTyped(int x) {
this.x = x;
int i;
b = b.getX().c.getC();
i = b.getX().c.getX();
}
public ClassCanBeTyped() {
this.x = 10;
int i;
for (i = 0; i < (x + 1); i = i + 1) {
int j;
for (j = 0; j < this.x; j += 1) {
this.x = this.x * this.x;
break;
}
}
do {
this.y = this.y + 1;
} while (this.y < 10);
int k;
k = 0;
for (k = 0; k < 10; k = k + 1) {
if (k == 5) {
return;
}
}
}
public ClassCanBeTyped(int x, int y, char z) {
this.x = x;
this.y = y;
}
public int getX(char z) {
return this.x;
}
public ClassCanBeTyped getC() {
return c;
}
public int getX() {
return x;
}
public boolean methodCall() {
return false;
}
}