Merge branch 'refs/heads/main' into testsuites

This commit is contained in:
JonathanFleischmann 2024-05-08 15:35:08 +02:00
commit 8c1ae8712e
18 changed files with 432 additions and 351 deletions

View File

@ -43,10 +43,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
@Override @Override
public Expression visitIdentifier(DecafParser.IdentifierContext ctx) { public Expression visitIdentifier(DecafParser.IdentifierContext ctx) {
boolean isField = ctx.fieldId().THIS() != null; boolean isField = ctx.fieldVarAccess().THIS() != null;
Expression recipient = null; Expression recipient = null;
if (ctx.fieldId().recipient() != null) { if (ctx.fieldVarAccess().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.fieldId().recipient(); List<DecafParser.RecipientContext> recipientList = ctx.fieldVarAccess().recipient();
recipient = generateRecursiveOwnerChain(recipientList, null); recipient = generateRecursiveOwnerChain(recipientList, null);
} }
return new FieldVarAccess(isField, recipient, ctx.getText()); return new FieldVarAccess(isField, recipient, ctx.getText());

View File

@ -67,12 +67,12 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
} }
private Assignment generateAssign(DecafParser.AssignContext ctx) { private Assignment generateAssign(DecafParser.AssignContext ctx) {
FieldVarAccess fieldVarAccess = generateField(ctx.fieldId()); FieldVarAccess fieldVarAccess = generateField(ctx.fieldVarAccess());
Expression expr = resolveFancyAssign(ctx.assignSign(), fieldVarAccess, new ExpressionGenerator().visit(ctx.expr())); Expression expr = resolveFancyAssign(ctx.assignSign(), fieldVarAccess, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(fieldVarAccess, expr); return new Assignment(fieldVarAccess, expr);
} }
private FieldVarAccess generateField(DecafParser.FieldIdContext fieldIdContext) { private FieldVarAccess generateField(DecafParser.FieldVarAccessContext fieldIdContext) {
return new FieldVarAccess(fieldIdContext.THIS() != null, return new FieldVarAccess(fieldIdContext.THIS() != null,
ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(), ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(),
null), fieldIdContext.id().getText()); null), fieldIdContext.id().getText());

File diff suppressed because one or more lines are too long

View File

@ -401,13 +401,13 @@ public class DecafBaseListener implements DecafListener {
* *
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void enterFieldId(DecafParser.FieldIdContext ctx) { } @Override public void enterFieldVarAccess(DecafParser.FieldVarAccessContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitFieldId(DecafParser.FieldIdContext ctx) { } @Override public void exitFieldVarAccess(DecafParser.FieldVarAccessContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -242,7 +242,7 @@ public class DecafBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* <p>The default implementation returns the result of calling * <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p> * {@link #visitChildren} on {@code ctx}.</p>
*/ */
@Override public T visitFieldId(DecafParser.FieldIdContext ctx) { return visitChildren(ctx); } @Override public T visitFieldVarAccess(DecafParser.FieldVarAccessContext ctx) { return visitChildren(ctx); }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

File diff suppressed because one or more lines are too long

View File

@ -123,7 +123,7 @@ public class DecafLexer extends Lexer {
public ATN getATN() { return _ATN; } public ATN getATN() { return _ATN; }
public static final String _serializedATN = public static final String _serializedATN =
"\u0004\u0000/\u0120\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002\u0001"+ "\u0004\u0000/\u012a\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002\u0001"+
"\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004"+ "\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004"+
"\u0007\u0004\u0002\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007"+ "\u0007\u0004\u0002\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007"+
"\u0007\u0007\u0002\b\u0007\b\u0002\t\u0007\t\u0002\n\u0007\n\u0002\u000b"+ "\u0007\u0007\u0002\b\u0007\b\u0002\t\u0007\t\u0002\n\u0007\n\u0002\u000b"+
@ -161,14 +161,15 @@ public class DecafLexer extends Lexer {
"\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001(\u0001"+ "\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001\'\u0001(\u0001"+
"(\u0001(\u0001(\u0001(\u0001)\u0001)\u0001)\u0001)\u0001)\u0001*\u0001"+ "(\u0001(\u0001(\u0001(\u0001)\u0001)\u0001)\u0001)\u0001)\u0001*\u0001"+
"*\u0001*\u0001*\u0001*\u0001*\u0001*\u0001*\u0001*\u0003*\u010d\b*\u0001"+ "*\u0001*\u0001*\u0001*\u0001*\u0001*\u0001*\u0001*\u0003*\u010d\b*\u0001"+
"+\u0001+\u0001+\u0001+\u0001,\u0004,\u0114\b,\u000b,\f,\u0115\u0001-\u0004"+ "+\u0001+\u0001+\u0001+\u0001,\u0004,\u0114\b,\u000b,\f,\u0115\u0001,\u0005"+
"-\u0119\b-\u000b-\f-\u011a\u0001.\u0001.\u0001.\u0001.\u0000\u0000/\u0001"+ ",\u0119\b,\n,\f,\u011c\t,\u0004,\u011e\b,\u000b,\f,\u011f\u0001-\u0004"+
"-\u0123\b-\u000b-\f-\u0124\u0001.\u0001.\u0001.\u0001.\u0000\u0000/\u0001"+
"\u0001\u0003\u0002\u0005\u0003\u0007\u0004\t\u0005\u000b\u0006\r\u0007"+ "\u0001\u0003\u0002\u0005\u0003\u0007\u0004\t\u0005\u000b\u0006\r\u0007"+
"\u000f\b\u0011\t\u0013\n\u0015\u000b\u0017\f\u0019\r\u001b\u000e\u001d"+ "\u000f\b\u0011\t\u0013\n\u0015\u000b\u0017\f\u0019\r\u001b\u000e\u001d"+
"\u000f\u001f\u0010!\u0011#\u0012%\u0013\'\u0014)\u0015+\u0016-\u0017/"+ "\u000f\u001f\u0010!\u0011#\u0012%\u0013\'\u0014)\u0015+\u0016-\u0017/"+
"\u00181\u00193\u001a5\u001b7\u001c9\u001d;\u001e=\u001f? A!C\"E#G$I%K"+ "\u00181\u00193\u001a5\u001b7\u001c9\u001d;\u001e=\u001f? A!C\"E#G$I%K"+
"&M\'O(Q)S*U+W,Y-[.]/\u0001\u0000\u0004\u0001\u0000\'\'\u0002\u0000AZa"+ "&M\'O(Q)S*U+W,Y-[.]/\u0001\u0000\u0004\u0001\u0000\'\'\u0002\u0000AZa"+
"z\u0001\u000009\u0003\u0000\t\n\r\r \u0122\u0000\u0001\u0001\u0000\u0000"+ "z\u0001\u000009\u0003\u0000\t\n\r\r \u012e\u0000\u0001\u0001\u0000\u0000"+
"\u0000\u0000\u0003\u0001\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000"+ "\u0000\u0000\u0003\u0001\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000"+
"\u0000\u0000\u0007\u0001\u0000\u0000\u0000\u0000\t\u0001\u0000\u0000\u0000"+ "\u0000\u0000\u0007\u0001\u0000\u0000\u0000\u0000\t\u0001\u0000\u0000\u0000"+
"\u0000\u000b\u0001\u0000\u0000\u0000\u0000\r\u0001\u0000\u0000\u0000\u0000"+ "\u0000\u000b\u0001\u0000\u0000\u0000\u0000\r\u0001\u0000\u0000\u0000\u0000"+
@ -209,8 +210,8 @@ public class DecafLexer extends Lexer {
"\u0000K\u00eb\u0001\u0000\u0000\u0000M\u00ed\u0001\u0000\u0000\u0000O"+ "\u0000K\u00eb\u0001\u0000\u0000\u0000M\u00ed\u0001\u0000\u0000\u0000O"+
"\u00f1\u0001\u0000\u0000\u0000Q\u00f9\u0001\u0000\u0000\u0000S\u00fe\u0001"+ "\u00f1\u0001\u0000\u0000\u0000Q\u00f9\u0001\u0000\u0000\u0000S\u00fe\u0001"+
"\u0000\u0000\u0000U\u010c\u0001\u0000\u0000\u0000W\u010e\u0001\u0000\u0000"+ "\u0000\u0000\u0000U\u010c\u0001\u0000\u0000\u0000W\u010e\u0001\u0000\u0000"+
"\u0000Y\u0113\u0001\u0000\u0000\u0000[\u0118\u0001\u0000\u0000\u0000]"+ "\u0000Y\u011d\u0001\u0000\u0000\u0000[\u0122\u0001\u0000\u0000\u0000]"+
"\u011c\u0001\u0000\u0000\u0000_`\u0005c\u0000\u0000`a\u0005l\u0000\u0000"+ "\u0126\u0001\u0000\u0000\u0000_`\u0005c\u0000\u0000`a\u0005l\u0000\u0000"+
"ab\u0005a\u0000\u0000bc\u0005s\u0000\u0000cd\u0005s\u0000\u0000d\u0002"+ "ab\u0005a\u0000\u0000bc\u0005s\u0000\u0000cd\u0005s\u0000\u0000d\u0002"+
"\u0001\u0000\u0000\u0000ef\u0005{\u0000\u0000f\u0004\u0001\u0000\u0000"+ "\u0001\u0000\u0000\u0000ef\u0005{\u0000\u0000f\u0004\u0001\u0000\u0000"+
"\u0000gh\u0005}\u0000\u0000h\u0006\u0001\u0000\u0000\u0000ij\u0005;\u0000"+ "\u0000gh\u0005}\u0000\u0000h\u0006\u0001\u0000\u0000\u0000ij\u0005;\u0000"+
@ -287,13 +288,19 @@ public class DecafLexer extends Lexer {
"\u0000\u0000\u0110\u0111\u0007\u0000\u0000\u0000\u0111X\u0001\u0000\u0000"+ "\u0000\u0000\u0110\u0111\u0007\u0000\u0000\u0000\u0111X\u0001\u0000\u0000"+
"\u0000\u0112\u0114\u0007\u0001\u0000\u0000\u0113\u0112\u0001\u0000\u0000"+ "\u0000\u0112\u0114\u0007\u0001\u0000\u0000\u0113\u0112\u0001\u0000\u0000"+
"\u0000\u0114\u0115\u0001\u0000\u0000\u0000\u0115\u0113\u0001\u0000\u0000"+ "\u0000\u0114\u0115\u0001\u0000\u0000\u0000\u0115\u0113\u0001\u0000\u0000"+
"\u0000\u0115\u0116\u0001\u0000\u0000\u0000\u0116Z\u0001\u0000\u0000\u0000"+ "\u0000\u0115\u0116\u0001\u0000\u0000\u0000\u0116\u011a\u0001\u0000\u0000"+
"\u0117\u0119\u0007\u0002\u0000\u0000\u0118\u0117\u0001\u0000\u0000\u0000"+ "\u0000\u0117\u0119\u0007\u0002\u0000\u0000\u0118\u0117\u0001\u0000\u0000"+
"\u0119\u011a\u0001\u0000\u0000\u0000\u011a\u0118\u0001\u0000\u0000\u0000"+ "\u0000\u0119\u011c\u0001\u0000\u0000\u0000\u011a\u0118\u0001\u0000\u0000"+
"\u011a\u011b\u0001\u0000\u0000\u0000\u011b\\\u0001\u0000\u0000\u0000\u011c"+ "\u0000\u011a\u011b\u0001\u0000\u0000\u0000\u011b\u011e\u0001\u0000\u0000"+
"\u011d\u0007\u0003\u0000\u0000\u011d\u011e\u0001\u0000\u0000\u0000\u011e"+ "\u0000\u011c\u011a\u0001\u0000\u0000\u0000\u011d\u0113\u0001\u0000\u0000"+
"\u011f\u0006.\u0000\u0000\u011f^\u0001\u0000\u0000\u0000\u0004\u0000\u010c"+ "\u0000\u011e\u011f\u0001\u0000\u0000\u0000\u011f\u011d\u0001\u0000\u0000"+
"\u0115\u011a\u0001\u0006\u0000\u0000"; "\u0000\u011f\u0120\u0001\u0000\u0000\u0000\u0120Z\u0001\u0000\u0000\u0000"+
"\u0121\u0123\u0007\u0002\u0000\u0000\u0122\u0121\u0001\u0000\u0000\u0000"+
"\u0123\u0124\u0001\u0000\u0000\u0000\u0124\u0122\u0001\u0000\u0000\u0000"+
"\u0124\u0125\u0001\u0000\u0000\u0000\u0125\\\u0001\u0000\u0000\u0000\u0126"+
"\u0127\u0007\u0003\u0000\u0000\u0127\u0128\u0001\u0000\u0000\u0000\u0128"+
"\u0129\u0006.\u0000\u0000\u0129^\u0001\u0000\u0000\u0000\u0006\u0000\u010c"+
"\u0115\u011a\u011f\u0124\u0001\u0006\u0000\u0000";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@ -364,15 +364,15 @@ public interface DecafListener extends ParseTreeListener {
*/ */
void exitUnaryOp(DecafParser.UnaryOpContext ctx); void exitUnaryOp(DecafParser.UnaryOpContext ctx);
/** /**
* Enter a parse tree produced by {@link DecafParser#fieldId}. * Enter a parse tree produced by {@link DecafParser#fieldVarAccess}.
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void enterFieldId(DecafParser.FieldIdContext ctx); void enterFieldVarAccess(DecafParser.FieldVarAccessContext ctx);
/** /**
* Exit a parse tree produced by {@link DecafParser#fieldId}. * Exit a parse tree produced by {@link DecafParser#fieldVarAccess}.
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitFieldId(DecafParser.FieldIdContext ctx); void exitFieldVarAccess(DecafParser.FieldVarAccessContext ctx);
/** /**
* Enter a parse tree produced by {@link DecafParser#assign}. * Enter a parse tree produced by {@link DecafParser#assign}.
* @param ctx the parse tree * @param ctx the parse tree

File diff suppressed because it is too large Load Diff

View File

@ -221,11 +221,11 @@ public interface DecafVisitor<T> extends ParseTreeVisitor<T> {
*/ */
T visitUnaryOp(DecafParser.UnaryOpContext ctx); T visitUnaryOp(DecafParser.UnaryOpContext ctx);
/** /**
* Visit a parse tree produced by {@link DecafParser#fieldId}. * Visit a parse tree produced by {@link DecafParser#fieldVarAccess}.
* @param ctx the parse tree * @param ctx the parse tree
* @return the visitor result * @return the visitor result
*/ */
T visitFieldId(DecafParser.FieldIdContext ctx); T visitFieldVarAccess(DecafParser.FieldVarAccessContext ctx);
/** /**
* Visit a parse tree produced by {@link DecafParser#assign}. * Visit a parse tree produced by {@link DecafParser#assign}.
* @param ctx the parse tree * @param ctx the parse tree

View File

@ -15,7 +15,7 @@ import java.util.Map;
public class TypedAssignment implements TypedStatement { public class TypedAssignment implements TypedStatement {
private String varName; private String varName;
private TypedExpression value; private TypedExpression value;
private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (!localVar.containsKey(varName)) { if (!localVar.containsKey(varName)) {
@ -26,10 +26,18 @@ public class TypedAssignment implements TypedStatement {
Type typeRight = value.typeCheck(localVar, classes); Type typeRight = value.typeCheck(localVar, classes);
if (typeLeft.equals(typeRight) ) { if (typeLeft.equals(typeRight) ) {
type = typeLeft;
return typeLeft; return typeLeft;
} }
throw new RuntimeException("type of left not equals with type of right"); throw new RuntimeException("type of left not equals with type of right");
} }
// int y = 5;
// int x = (y + 5 + z);
//TypedAssignment([
// localVar("x", "int"),
// localVar("y","int")
// ],
// "int")
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {

View File

@ -13,11 +13,13 @@ public class TypedBinary implements TypedExpression {
private TypedExpression left; private TypedExpression left;
private Operator op; private Operator op;
private TypedExpression right; private TypedExpression right;
private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) { if(op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
if (left.typeCheck(localVar, classes) == Type.INT && if (left.typeCheck(localVar, classes) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) { right.typeCheck(localVar, classes) == Type.INT) {
type = Type.INT;
return Type.INT; return Type.INT;
} else { } else {
throw new RuntimeException("Type mismatch"); throw new RuntimeException("Type mismatch");
@ -25,6 +27,7 @@ public class TypedBinary implements TypedExpression {
}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) { }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 && if (left.typeCheck(localVar, classes) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) { right.typeCheck(localVar, classes) == Type.INT) {
type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} else { } else {
throw new RuntimeException("Type mismatch"); throw new RuntimeException("Type mismatch");
@ -32,6 +35,7 @@ public class TypedBinary implements TypedExpression {
} else if (op == Operator.AND || op == Operator.OR) { } else if (op == Operator.AND || op == Operator.OR) {
if (left.typeCheck(localVar, classes) == Type.BOOL && if (left.typeCheck(localVar, classes) == Type.BOOL &&
right.typeCheck(localVar, classes) == Type.BOOL) { right.typeCheck(localVar, classes) == Type.BOOL) {
type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} else { } else {
throw new RuntimeException("Type mismatch"); throw new RuntimeException("Type mismatch");

View File

@ -17,6 +17,22 @@ public class TypedBlock implements TypedNode {
private List<TypedLocalVariable> vars; private List<TypedLocalVariable> vars;
private List<TypedStatement> stmts; private List<TypedStatement> stmts;
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
TypedBlock typedBlock = new TypedBlock();
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar);
}
typedBlock.getStmts().add((TypedStatement) unTypedBlock.stmts());
return typedBlock;
}
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
Type typeOfLastStmt = Type.VOID; Type typeOfLastStmt = Type.VOID;

View File

@ -11,9 +11,10 @@ import java.util.Map;
public class TypedBoolLiteral implements TypedExpression { public class TypedBoolLiteral implements TypedExpression {
private Boolean value; private Boolean value;
private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} }

View File

@ -1,8 +1,11 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypedStatement;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -14,7 +17,6 @@ import java.util.Map;
@Data @Data
public class TypedClass implements TypedNode { public class TypedClass implements TypedNode {
private Boolean isPublic;
private String className; private String className;
private List<TypedField> typedFields; private List<TypedField> typedFields;
private List<TypedMethod> typedMethods; private List<TypedMethod> typedMethods;
@ -49,17 +51,26 @@ public class TypedClass implements TypedNode {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
TypedClass typedClass = new TypedClass(); TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST;
for (TypedField field : typedFields) { for (Declaration field : c.fieldDeclarations()) {
typedClass.getTypedFields().add((TypedField) field.convertToTypedAST(localVar, classes, unTypedAST)); TypedField typedField = new TypedField();
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
} }
for (TypedConstructor constructor : typedConstructors) { for (Constructor constructor : c.constructors()) {
typedClass.getTypedConstructors().add((TypedConstructor) constructor.convertToTypedAST(localVar, classes, unTypedAST)); TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
} }
for (TypedMethod method : typedMethods) { for(Constructor constructor : c.constructors()){
typedClass.getTypedMethods().add((TypedMethod) method.convertToTypedAST(localVar, classes, unTypedAST)); TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
}
for (Method method : c.methods()) {
TypedMethod typedMethod = new TypedMethod();
typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method));
} }
return typedClass; return typedClass;

View File

@ -43,6 +43,28 @@ public class TypedConstructor implements TypedNode {
return Type.VOID; return Type.VOID;
} }
public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.className());
if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(null);
} else {
for (Parameter param : unTypedConstructor.params()) {
TypedParameter typedParam = new TypedParameter();
typedParam.setParaName(param.name());
typedParam.setType(param.type());
typedConstructor.getTypedParameters().add(typedParam);
}
}
Block block = unTypedConstructor.block();
TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(block));
return typedConstructor;
}
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Constructor untyped = (Constructor) unTypedAST; Constructor untyped = (Constructor) unTypedAST;

View File

@ -18,6 +18,14 @@ public class TypedField implements TypedNode {
private String varName; private String varName;
private Type type; private Type type;
public TypedField unTypedFieldToTypedField(Declaration declaration){
TypedField typedField = new TypedField();
typedField.setType(declaration.type());
typedField.setVarName(declaration.name());
return typedField;
}
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(varName)) { if (localVar.containsKey(varName)) {

View File

@ -19,6 +19,20 @@ public class TypedMethod implements TypedNode {
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) {
TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(unTypedMethod.methodName());
for(Parameter parameter : unTypedMethod.params()){
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type());
typedParameters.add(typedParameter);
}
TypedBlock block = new TypedBlock();
typedMethod.setTypedBlock(block.unTypedBlockToTypedBlock(unTypedMethod.block()));
return typedMethod;
}
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(name)) { if(localVar.containsKey(name)) {