Handling Merge

This commit is contained in:
Ahmad 2024-05-08 15:13:38 +02:00
commit 4a5aba765d
55 changed files with 249 additions and 421 deletions

View File

@ -11,7 +11,7 @@ type : INT | BOOL | CHAR | id;
meth : PUBLIC returntype id '(' params? ')' block;
mainmeth : PUBLIC 'static' 'void' 'main' '(' 'String[] args' ')' block;
constructor: PUBLIC? id '(' params? ')' block;
constructor: PUBLIC id '(' params? ')' block;
params : param (',' param)*;
param : type id;
@ -36,7 +36,7 @@ expr : expr binaryOp expr #BinaryOperation
| unaryOp expr #UnaryOperation
| literal #Constant
| '(' expr ')' #Expression
| fieldId #Identifier
| fieldVarAccess #Identifier
| stmtexpr #StatementExpressionexpr
| NULL #Null
;
@ -44,9 +44,9 @@ expr : expr binaryOp expr #BinaryOperation
binaryOp : ADD | SUB | MUL | GT | LT | GE | LE | EQ | NE | AND | OR;
unaryOp : SUB | NOT;
fieldId : (THIS '.')? (recipient '.')* id;
fieldVarAccess : (THIS '.')? (recipient '.')* id;
assign : id assignSign expr ;
assign : fieldVarAccess assignSign expr ;
methCall : (THIS '.')? (recipient '.')* methName;
recipient : methName | id;
methName : id '(' args? ')';

View File

@ -1,10 +1,9 @@
package de.maishai;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.ReturnType;
import de.maishai.ast.Type;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import de.maishai.typedast.Type;
import java.util.ArrayList;
import java.util.List;
@ -13,13 +12,9 @@ public class ASTGenerator {
public static Class generateAST(DecafParser.ClassContext ctx) {
List<Field> fields = new ArrayList<>();
List<Declaration> declarations = new ArrayList<>();
if (ctx.field() != null) {
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
}
MainMethod mainMethod = null;
if(ctx.mainmeth() != null){
mainMethod = generateMainMethod(ctx.mainmeth());
declarations = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
}
List<Constructor> constructors = new ArrayList<>();
if (ctx.constructor() != null) {
@ -29,14 +24,12 @@ public class ASTGenerator {
if (ctx.meth() != null) {
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
}
Id classId = new Id(ctx.id().getText());
return new Class(classId, fields, meths, mainMethod, constructors);
return new Class(ctx.id().getText(), declarations, meths, constructors);
}
public static Field generateFieldVariable(DecafParser.FieldContext ctx) {
Id id = new Id(ctx.id().getText());
public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) {
Type type = ASTGenerator.getType(ctx.type());
return new Field(id, type);
return new Declaration(ctx.id().getText(), type);
}
public static Parameter generateParameter(DecafParser.ParamContext ctx) {
@ -49,24 +42,16 @@ public class ASTGenerator {
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
}
Block block = new BlockGenerator().visit(ctx.block());
Id methId = new Id(ctx.id().getText());
return new Method(getReturnType(ctx.returntype()), methId, params, block);
return new Method(getType(ctx.returntype()), ctx.id().getText(), params, block);
}
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
Boolean isPublic = ctx.PUBLIC() != null;
List<Parameter> params = new ArrayList<>();
if (ctx.params() != null) {
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
}
Block block = new BlockGenerator().visit(ctx.block());
Id constructorId = new Id(ctx.id().getText());
return new Constructor(isPublic, constructorId, params, block);
}
public static MainMethod generateMainMethod(DecafParser.MainmethContext ctx) {
Block block = new BlockGenerator().visit(ctx.block());
return new MainMethod(block);
return new Constructor( ctx.id().getText(), params, block);
}
public static Type getType(DecafParser.TypeContext ctx) {
@ -77,28 +62,17 @@ public class ASTGenerator {
if (ctx.CHAR() != null)
return Type.CHAR;
if (ctx.id() != null) {
Type type = Type.OBJECT;
type.setObjectType(new Id(ctx.id().getText()));
return type;
return Type.REFERENCE(ctx.id().getText());
}
throw new RuntimeException("No type found!");
}
public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) {
if (ctx.type().INT() != null)
return ReturnType.INT;
if (ctx.type().BOOL() != null)
return ReturnType.BOOL;
if (ctx.type().CHAR() != null)
return ReturnType.CHAR;
public static Type getType(DecafParser.ReturntypeContext ctx) {
if (ctx.type() != null){
return getType(ctx.type());
}
if (ctx.VOID() != null){
return ReturnType.VOID;
return Type.VOID;
}
if (ctx.type().id() != null){
ReturnType type = ReturnType.OBJECT;
type.setObjectType(new Id(ctx.type().id().getText()));
return type;
}
throw new RuntimeException("No return type found!");
throw new RuntimeException("No type found!");
}
}

View File

@ -2,16 +2,16 @@ package de.maishai;
import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Statement;
import de.maishai.ast.records.Block;
import de.maishai.ast.records.LocalVariable;
import java.util.List;
public class BlockGenerator extends DecafBaseVisitor<Block> {
@Override
public Block visitBlock(DecafParser.BlockContext ctx) {
List<LocalVariable> vars = ctx.localVar().stream().map(var -> new VariableGenerator().visit(var)).toList();
List<Declaration> vars = ctx.localVar().stream().map(var -> new VariableGenerator().visit(var)).toList();
List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
return new Block(vars, statements);
}

View File

@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser;
import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Expression;
import de.maishai.ast.Operator;
import de.maishai.ast.Type;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList;
import java.util.List;
@ -47,9 +47,9 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
Expression recipient = null;
if (ctx.fieldId().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.fieldId().recipient();
recipient = generateRecipient(recipientList, null);
recipient = generateRecursiveOwnerChain(recipientList, null);
}
return new FieldId(isField, recipient, new Id(ctx.getText()));
return new FieldVarAccess(isField, recipient, ctx.getText());
}
public static Expression generateConstant(DecafParser.LiteralContext ctx) {
@ -87,18 +87,17 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
@Override
public Expression visitMethodCall(DecafParser.MethodCallContext ctx) {
boolean isField = ctx.methCall().THIS() != null;
Expression recipient = null;
Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
recipient = generateRecipient(recipientList, null);
recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null);
}
Id id = new Id(ctx.methCall().methName().id().getText());
List<Expression> args = new ArrayList<>();
for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(this);
args.add(astExpr);
}
return new MethodCall(isField, recipient, id, args);
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
}
@Override
@ -112,18 +111,25 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
return new New(type, args);
}
public static Expression generateRecipient(List<DecafParser.RecipientContext> ctxList, Expression recipient) {
public static Expression generateRecursiveOwnerChain(List<DecafParser.RecipientContext> ctxList, FieldVarAccess recipient) {
if (ctxList.isEmpty()) {
return recipient;
}
DecafParser.RecipientContext ctx = ctxList.get(0);
ctxList.remove(0);
Id id = new Id(ctx.methName().id().getText());
if (ctx.id() != null) {
return new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().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);
}
return new MethodCall(null, generateRecipient(ctxList, recipient), id, args);
return new MethodCall(new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().getText()), args);
}
throw new RuntimeException();
}
}

View File

@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser;
import de.maishai.ast.Operator;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.Statement;
import de.maishai.ast.Type;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList;
import java.util.List;
@ -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
@ -67,27 +67,32 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
}
private Assignment generateAssign(DecafParser.AssignContext ctx) {
Id id = new Id(ctx.id().getText());
Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(id, expr);
FieldVarAccess fieldVarAccess = generateField(ctx.fieldId());
Expression expr = resolveFancyAssign(ctx.assignSign(), fieldVarAccess, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(fieldVarAccess, expr);
}
private FieldVarAccess generateField(DecafParser.FieldIdContext fieldIdContext) {
return new FieldVarAccess(fieldIdContext.THIS() != null,
ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(),
null), fieldIdContext.id().getText());
}
//StatementExpression
@Override
public Statement visitMethodCall(DecafParser.MethodCallContext ctx) {
boolean isField = ctx.methCall().THIS() != null;
Expression recipient = null;
Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
recipient = ExpressionGenerator.generateRecipient(recipientList, null);
recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, null);
}
Id id = new Id(ctx.methCall().methName().id().getText());
List<Expression> args = new ArrayList<>();
for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(new ExpressionGenerator());
args.add(astExpr);
}
return new MethodCall(isField, recipient, id, args);
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
}
@Override
@ -101,15 +106,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
return new New(type, args);
}
public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, Id id, Expression expression) {
public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, FieldVarAccess fieldVarAccess, Expression expression) {
if (ctx.ASSIGN() != null)
return expression;
if (ctx.ADD_ASSIGN() != null)
return new Binary(id, Operator.ADD, expression);
return new Binary(fieldVarAccess, Operator.ADD, expression);
if (ctx.SUB_ASSIGN() != null)
return new Binary(id, Operator.SUB, expression);
return new Binary(fieldVarAccess, Operator.SUB, expression);
if (ctx.MUL_ASSIGN() != null)
return new Binary(id, Operator.MUL, expression);
return new Binary(fieldVarAccess, Operator.MUL, expression);
throw new RuntimeException("No assign sign found!");
}
}

View File

@ -3,16 +3,14 @@ package de.maishai;
import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.Type;
import de.maishai.ast.records.Id;
import de.maishai.ast.records.LocalVariable;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.Type;
public class VariableGenerator extends DecafBaseVisitor<LocalVariable> {
public class VariableGenerator extends DecafBaseVisitor<Declaration> {
@Override
public LocalVariable visitLocalVar(DecafParser.LocalVarContext ctx) {
Id id = new Id(ctx.id().getText());
public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) {
Type type = ASTGenerator.getType(ctx.type());
return new LocalVariable(id, type);
return new Declaration(ctx.id().getText(), type);
}
}

File diff suppressed because one or more lines are too long

View File

@ -35,7 +35,7 @@ public class DecafParser extends Parser {
return new String[] {
"class", "field", "localVar", "assignSign", "returntype", "type", "meth",
"mainmeth", "constructor", "params", "param", "block", "stmt", "stmtexpr",
"expr", "binaryOp", "unaryOp", "fieldId", "assign", "methCall", "recipient",
"expr", "binaryOp", "unaryOp", "fieldId", "assign", "methCall", "recursiveOwnerChain",
"methName", "args", "literal", "id"
};
}
@ -1904,8 +1904,8 @@ public class DecafParser extends Parser {
@SuppressWarnings("CheckReturnValue")
public static class AssignContext extends ParserRuleContext {
public IdContext id() {
return getRuleContext(IdContext.class,0);
public FieldIdContext fieldId() {
return getRuleContext(FieldIdContext.class,0);
}
public AssignSignContext assignSign() {
return getRuleContext(AssignSignContext.class,0);
@ -1939,7 +1939,7 @@ public class DecafParser extends Parser {
enterOuterAlt(_localctx, 1);
{
setState(238);
id();
fieldId();
setState(239);
assignSign();
setState(240);
@ -2494,7 +2494,7 @@ public class DecafParser extends Parser {
"\u00eb\u0001\u0000\u0000\u0000\u00e9\u00e7\u0001\u0000\u0000\u0000\u00e9"+
"\u00ea\u0001\u0000\u0000\u0000\u00ea\u00ec\u0001\u0000\u0000\u0000\u00eb"+
"\u00e9\u0001\u0000\u0000\u0000\u00ec\u00ed\u00030\u0018\u0000\u00ed#\u0001"+
"\u0000\u0000\u0000\u00ee\u00ef\u00030\u0018\u0000\u00ef\u00f0\u0003\u0006"+
"\u0000\u0000\u0000\u00ee\u00ef\u0003\"\u0011\u0000\u00ef\u00f0\u0003\u0006"+
"\u0003\u0000\u00f0\u00f1\u0003\u001c\u000e\u0000\u00f1%\u0001\u0000\u0000"+
"\u0000\u00f2\u00f3\u0005\u0016\u0000\u0000\u00f3\u00f5\u0005\u0012\u0000"+
"\u0000\u00f4\u00f2\u0001\u0000\u0000\u0000\u00f4\u00f5\u0001\u0000\u0000"+

View File

@ -1,8 +0,0 @@
package de.maishai.ast;
public enum AssignSign {
ASSIGN,
ADD_ASSIGN,
SUB_ASSIGN,
MUL_ASSIGN,
}

View File

@ -1,26 +0,0 @@
package de.maishai.ast;
import de.maishai.ast.records.Id;
public enum ReturnType {
INT,
BOOL,
CHAR,
VOID,
OBJECT;
Id objectType;
public Id getObjectType() {
if(this == ReturnType.OBJECT){
return objectType;
}
return null;
}
public void setObjectType(Id objectType) {
if(this == ReturnType.OBJECT){
this.objectType = objectType;
}
}
}

View File

@ -1,27 +0,0 @@
package de.maishai.ast;
import de.maishai.ast.records.Id;
public enum Type {
INT,
BOOL,
CHAR,
OBJECT;
Id objectType;
public Id getObjectType() {
if(this == Type.OBJECT){
return objectType;
}
return null;
}
public void setObjectType(Id objectType) {
if(this == Type.OBJECT){
this.objectType = objectType;
}
}
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
public record Assignment(Id loc, Expression value) implements Statement {
public record Assignment(FieldVarAccess location, Expression value) implements Statement {
}

View File

@ -4,5 +4,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Block(List<LocalVariable> vars, List<Statement> stmts) implements Node {
public record Block(List<Declaration> localVariables, List<Statement> stmts) implements Node {
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Class(Id id , List<Field> fields, List<Method> methods, MainMethod mainMethod, List<Constructor> constructors) implements Node {
public record Class(String classname , List<Declaration> fieldDeclarations, List<Method> methods, List<Constructor> constructors) implements Node {
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Constructor(Boolean isPublic, Id id, List<Parameter> params, Block block) implements Node {
public record Constructor(String className, List<Parameter> params, Block block) implements Node {
}

View File

@ -0,0 +1,6 @@
package de.maishai.ast.records;
import de.maishai.typedast.Type;
public record Declaration(String name, Type type) implements Node {
}

View File

@ -1,5 +1,5 @@
package de.maishai.ast.records;
public sealed interface Expression extends Node permits Binary, BoolLiteral, CharLiteral, FieldId, Id, IntLiteral, MethodCall, New, Unary {
public sealed interface Expression extends Node permits Binary, BoolLiteral, CharLiteral, FieldVarAccess, IntLiteral, MethodCall, New, Unary {
}

View File

@ -1,6 +0,0 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
public record Field(Id name, Type type) implements Node {
}

View File

@ -1,4 +0,0 @@
package de.maishai.ast.records;
public record FieldId(Boolean field, Expression recipient, Id id) implements Expression {
}

View File

@ -0,0 +1,4 @@
package de.maishai.ast.records;
public record FieldVarAccess(Boolean field, Expression recursiveOwnerChain, String id) implements Expression {
}

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record Id(String name) implements Expression {
}

View File

@ -1,7 +0,0 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
public record LocalVariable(Id name, Type type) implements Node {
}

View File

@ -1,5 +0,0 @@
package de.maishai.ast.records;
public record MainMethod(Block block) implements Node {
}

View File

@ -2,9 +2,9 @@ package de.maishai.ast.records;
import de.maishai.ast.ReturnType;
import de.maishai.typedast.Type;
import java.util.List;
public record Method(ReturnType type, Id id, List<Parameter> params, Block block) implements Node {
public record Method(Type type, String methodName, List<Parameter> params, Block block) implements Node {
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List;
public record MethodCall(Boolean field, Expression recipient, Id id, List<Expression> args) implements Expression, Statement {
public record MethodCall(FieldVarAccess recipient, List<Expression> args) implements Expression, Statement {
}

View File

@ -2,7 +2,7 @@ package de.maishai.ast.records;
import de.maishai.ast.Type;
import de.maishai.typedast.Type;
import java.util.List;

View File

@ -1,4 +1,4 @@
package de.maishai.ast.records;
public sealed interface Node permits Block, Class, Constructor, Expression, Field, LocalVariable, MainMethod, Method, Parameter, Statement {
public sealed interface Node permits Block, Class, Constructor, Expression, Declaration, Method, Parameter, Statement {
}

View File

@ -1,7 +1,7 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
import de.maishai.typedast.Type;
public record Parameter(String name, Type type) implements Node {
}

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

@ -1,28 +0,0 @@
package de.maishai.typedast;
public class TypeMapper {
public static Type getType(de.maishai.ast.Type type) {
if (type == de.maishai.ast.Type.CHAR) {
return Type.CHAR;
} else if (type == de.maishai.ast.Type.BOOL) {
return Type.BOOL;
} else if (type == de.maishai.ast.Type.INT) {
return Type.INT;
}
throw new RuntimeException("Unknown type");
}
public static Type getReturnType(de.maishai.ast.ReturnType type) {
if (type == de.maishai.ast.ReturnType.CHAR) {
return Type.CHAR;
} else if (type == de.maishai.ast.ReturnType.BOOL) {
return Type.BOOL;
} else if (type == de.maishai.ast.ReturnType.INT) {
return Type.INT;
} else if (type == de.maishai.ast.ReturnType.VOID) {
return Type.VOID;
}
throw new RuntimeException("Unknown type");
}
}

View File

@ -43,7 +43,7 @@ public class TypedAssignment implements TypedStatement {
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.loc().name());
typedAssignment.setVarName(untyped.location().id());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
return typedAssignment;
}

View File

@ -1,7 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Block;
import de.maishai.ast.records.LocalVariable;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
@ -22,23 +22,10 @@ public class TypedBlock implements TypedNode {
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
TypedBlock typedBlock = new TypedBlock();
for (LocalVariable var : unTypedBlock.vars()) {
for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.setName(var.name().name());
switch (var.type()) {
case INT:
typedVar.setType(Type.INT);
break;
case BOOL:
typedVar.setType(Type.BOOL);
break;
case CHAR:
typedVar.setType(Type.CHAR);
break;
case OBJECT:
typedVar.setType(Type.REFERENCE(var.name().name()));
break;
}
typedVar.setName(var.name());
typedVar.setType(var.type());
typedBlock.getVars().add(typedVar);
}
@ -65,7 +52,7 @@ public class TypedBlock implements TypedNode {
Block untyped = (Block) unTypedAST;
TypedBlock typedBlock = new TypedBlock();
for (LocalVariable var : untyped.vars()) {
for (Declaration var : untyped.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable();
typedVar.convertToTypedAST(localVar, classes, var);
typedBlock.getVars().add(typedVar);

View File

@ -17,11 +17,9 @@ import java.util.Map;
@Data
public class TypedClass implements TypedNode {
private Boolean isPublic;
private String className;
private List<TypedField> typedFields;
private List<TypedMethod> typedMethods;
private TypedMainMethod typedMainMethod;
private List<TypedConstructor> typedConstructors;
@Override
@ -55,7 +53,7 @@ public class TypedClass implements TypedNode {
TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST;
for (Field field : c.fields()) {
for (Declaration field : c.fieldDeclarations()) {
TypedField typedField = new TypedField();
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
}

View File

@ -45,7 +45,7 @@ public class TypedConstructor implements TypedNode {
public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.id().name());
typedConstructor.setName(unTypedConstructor.className());
if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(null);
@ -54,21 +54,7 @@ public class TypedConstructor implements TypedNode {
for (Parameter param : unTypedConstructor.params()) {
TypedParameter typedParam = new TypedParameter();
typedParam.setParaName(param.name());
switch(param.type()) {
case INT:
typedParam.setType(Type.INT);
break;
case BOOL:
typedParam.setType(Type.BOOL);
break;
case CHAR:
typedParam.setType(Type.CHAR);
break;
case OBJECT:
typedParam.setType(Type.REFERENCE(param.type().name()));
break;
}
typedParam.setType(param.type());
typedConstructor.getTypedParameters().add(typedParam);
}
@ -83,7 +69,7 @@ public class TypedConstructor implements TypedNode {
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.id().name());
typedConstructor.setName(untyped.className());
if (untyped.params().isEmpty()) {
typedConstructor.setTypedParameters(null);

View File

@ -1,9 +1,8 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Field;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@ -20,23 +19,10 @@ public class TypedField implements TypedNode {
private Type type;
public TypedField unTypedFieldToTypedField(Field field){
public TypedField unTypedFieldToTypedField(Declaration declaration){
TypedField typedField = new TypedField();
switch(field.type()) {
case INT:
typedField.setType(Type.INT);
break;
case BOOL:
typedField.setType(Type.BOOL);
break;
case CHAR:
typedField.setType(Type.CHAR);
break;
case OBJECT:
typedField.setType(Type.REFERENCE(typedField.varName));
break;
}
typedField.setVarName(field.name().name());
typedField.setType(declaration.type());
typedField.setVarName(declaration.name());
return typedField;
}
@ -51,10 +37,10 @@ public class TypedField implements TypedNode {
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Field untypedField = (Field) unTypedAST;
Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField();
typedField.setVarName(untypedField.name().name());
typedField.setType(TypeMapper.getType(untypedField.type()));
typedField.setVarName(untypedDeclaration.name());
typedField.setType(untypedDeclaration.type());
return typedField;
}

View File

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

View File

@ -1,10 +1,9 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.LocalVariable;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -29,11 +28,11 @@ public final class TypedLocalVariable implements TypedNode {
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST;
Declaration untypedLocalVariable = (Declaration) unTypedAST;
TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
typedLocalVariable.setName(untypedLocalVariable.name().name());
typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type()));
typedLocalVariable.setName(untypedLocalVariable.name());
typedLocalVariable.setType(untypedLocalVariable.type());
return typedLocalVariable;
}

View File

@ -1,31 +0,0 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.MainMethod;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TypedMainMethod implements TypedNode {
private TypedBlock typedBlock;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return typedBlock.typeCheck(localVar, classes);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
MainMethod untyped = (MainMethod) unTypedAST;
TypedMainMethod typedMainMethod = new TypedMainMethod();
typedMainMethod.setTypedBlock((TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untyped.block()));
return typedMainMethod;
}
}

View File

@ -21,25 +21,12 @@ public class TypedMethod implements TypedNode {
public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) {
TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(unTypedMethod.id().name());
typedMethod.setName(unTypedMethod.methodName());
for(Parameter parameter : unTypedMethod.params()){
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
switch(parameter.type()) {
case INT:
typedParameter.setType(Type.INT);
break;
case BOOL:
typedParameter.setType(Type.BOOL);
break;
case CHAR:
typedParameter.setType(Type.CHAR);
break;
case OBJECT:
typedParameter.setType(Type.REFERENCE(parameter.name()));
break;
}
typedParameter.setType(parameter.type());
typedParameters.add(typedParameter);
}
TypedBlock block = new TypedBlock();
@ -63,8 +50,8 @@ public class TypedMethod implements TypedNode {
Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(untypedMethod.id().name());
typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type()));
typedMethod.setName(untypedMethod.methodName());
typedMethod.setReturnType(untypedMethod.type());
if(untypedMethod.params().isEmpty()){
//TODO: Implement this
@ -85,13 +72,13 @@ public class TypedMethod implements TypedNode {
TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name());
if(parameter.type() == de.maishai.ast.Type.CHAR){
if(parameter.type() == Type.CHAR){
typedParameter.setType(Type.CHAR);
}
if(parameter.type() == de.maishai.ast.Type.BOOL){
if(parameter.type() == Type.BOOL){
typedParameter.setType(Type.BOOL);
}
if(parameter.type() == de.maishai.ast.Type.INT){
if(parameter.type() == Type.INT){
typedParameter.setType(Type.INT);
}

View File

@ -38,7 +38,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
New untyped = (New) unTypedAST;
TypedNew typedNew = new TypedNew();
typedNew.setType(TypeMapper.getType(untyped.type()));
typedNew.setType(untyped.type());
if(untyped.args().isEmpty()){
//TODO: Implement this

View File

@ -20,7 +20,7 @@ public class TypedReturnVoid implements TypedStatement {
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Method untyped = (Method) unTypedAST;
if(untyped.type() != de.maishai.ast.ReturnType.VOID){
if(untyped.type() != Type.VOID){
throw new RuntimeException("Return type is not void");
}
return new TypedReturnVoid();

View File

@ -20,8 +20,8 @@ import java.util.List;
public class AbstractSyntax_ClassWithConstructor {
public static Class get() {
List<Field> fields = List.of(
new Field(
List<Declaration> declarations = List.of(
new Declaration(
new Id("x"),
Type.INT
)
@ -30,7 +30,7 @@ public class AbstractSyntax_ClassWithConstructor {
List<Constructor> constructors = getConstructors();
return new Class(
new Id("ClassWithConstructor"),
fields,
declarations,
methods,
null,
constructors
@ -52,21 +52,33 @@ public class AbstractSyntax_ClassWithConstructor {
);
List<Statement> statementList = List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new IntLiteral(10)
),
new For(
new Assignment(
new Id("i"),
new FieldId(
false,
null,
new Id("i")),
new IntLiteral(0)
),
new Binary(
new Id("i"),
new FieldId(
false,
null,
new Id("i")),
Operator.LT,
new IntLiteral(6)
),
new Assignment(
new Id("i"),
new FieldId(
false,
null,
new Id("i")),
new Binary(
new Id("i"),
Operator.ADD,
@ -83,7 +95,10 @@ public class AbstractSyntax_ClassWithConstructor {
List.of(
new For(
new Assignment(
new Id("j"),
new FieldId(
false,
null,
new Id("j")),
new IntLiteral(0)
),
new Binary(
@ -92,14 +107,27 @@ public class AbstractSyntax_ClassWithConstructor {
new Id("x")
),
new Assignment(
new Id("j"),
new FieldId(
false,
null,
new Id("j")),
new Binary(
new FieldId(
false,
null,
new Id("j")),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(),
List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new Binary(
new Id("x"),
Operator.MUL,
@ -112,6 +140,7 @@ public class AbstractSyntax_ClassWithConstructor {
)
)
)
);
Block block = new Block(
localVariables,

View File

@ -19,8 +19,6 @@
import de.maishai.ast.Operator;
import de.maishai.ast.ReturnType;
import de.maishai.ast.Type;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
@ -28,8 +26,8 @@ import java.util.List;
public class AbstractSyntax_ClassWithConstructorAndMethodCall {
public static Class get() {
List<Field> fieldList = List.of(
new Field(
List<Declaration> declarationList = List.of(
new Declaration(
new Id("x"),
Type.INT
)
@ -38,7 +36,7 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
List<Constructor> constructorList = getConstructors();
return new Class(
new Id("ClassWithConstructorAndMethodCall"),
fieldList,
declarationList,
methodList,
null,
constructorList
@ -54,7 +52,10 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
List.of(),
List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new IntLiteral(10)
),
new While(
@ -68,7 +69,10 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
List.of(),
List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new Binary(
new Id("x"),
Operator.MUL,

View File

@ -15,15 +15,14 @@
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.Type;
import de.maishai.ast.records.Class;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorWithParameters {
public static Class get() {
List<Field> fields = List.of(
new Field(
List<Declaration> declarations = List.of(
new Declaration(
new Id("x"),
Type.INT
)
@ -31,7 +30,7 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List<Constructor> constructors = getConstructors();
return new Class(
new Id("ClassWithConstructorWithParameters"),
fields,
declarations,
List.of(),
null,
constructors
@ -57,7 +56,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of(),
List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new Id("startValue")
),
new While(
@ -75,12 +77,15 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
),
List.of(
new Assignment(
new Id("innerRepetitions"),
new FieldId(
false,
null,
new Id("innerRepetitions")),
new Id("x")
),
new While(
new Binary(
new Id("repetitions"),
new Id("innerRepetitions"),
Operator.GT,
new IntLiteral(0)
),
@ -88,7 +93,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of(),
List.of(
new Assignment(
new Id("x"),
new FieldId(
true,
null,
new Id("x")),
new Binary(
new Id("x"),
Operator.MUL,
@ -96,7 +104,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
)
),
new Assignment(
new Id("innerRepetitions"),
new FieldId(
false,
null,
new Id("innerRepetitions")),
new Binary(new Id("innerRepetitions"),
Operator.SUB,
new IntLiteral(1))
@ -106,7 +117,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
)
),
new Assignment(
new Id("repetitions"),
new FieldId(
false,
null,
new Id("repetitions")),
new Binary(new Id("repetitions"),
Operator.SUB,
new IntLiteral(1)

View File

@ -3,18 +3,16 @@
//}
import de.maishai.ast.records.Class;
import de.maishai.ast.Type;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Field;
import de.maishai.ast.records.Id;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Method;
import java.util.List;
public class AbstractSyntax_ClassWithField {
public static Class get() {
List<Field> fields = List.of(
new Field(
List<Declaration> declarations = List.of(
new Declaration(
new Id("x"),
Type.INT
)
@ -23,7 +21,7 @@ public class AbstractSyntax_ClassWithField {
List<Constructor> constructors = List.of();
return new Class(
new Id("ClassWithAssignment"),
fields,
declarations,
methods,
null,
constructors

View File

@ -4,7 +4,6 @@
// }
//}
import de.maishai.ast.ReturnType;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;

View File

@ -18,8 +18,8 @@ import java.util.List;
class AbstractSyntax_ClassWithMethodAndField {
public static Class get() {
List<Field> fields = List.of(
new Field(
List<Declaration> declarations = List.of(
new Declaration(
new Id("c"),
Type.CHAR
)
@ -28,7 +28,7 @@ class AbstractSyntax_ClassWithMethodAndField {
List<Constructor> constructors = getConstructors();
return new Class(
new Id("ClassWithMethodAndField"),
fields,
declarations,
methods,
null,
constructors
@ -53,7 +53,10 @@ class AbstractSyntax_ClassWithMethodAndField {
List.of(),
List.of(
new Assignment(
new Id("c"),
new FieldId(
true,
null,
new Id("c")),
new Id("character")
)
)

View File

@ -28,8 +28,8 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
public static Class get() {
Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT;
TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain"));
List<Field> fields = List.of(
new Field(
List<Declaration> declarations = List.of(
new Declaration(
new Id("instance"),
TypeClassWithMoreComplexMethodAndMain
)
@ -38,7 +38,7 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List<Constructor> constructors = List.of();
return new Class(
new Id("ClassWithMoreComplexMethodAndMain"),
fields,
declarations,
methods,
getMainMethod(),
constructors
@ -63,11 +63,17 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
),
List.of(
new Assignment(
new Id("i"),
new FieldId(
false,
null,
new Id("i")),
new IntLiteral(11)
),
new Assignment(
new Id("returnValue"),
new FieldId(
false,
null,
new Id("returnValue")),
new BoolLiteral(false)
),
new While(
@ -80,14 +86,20 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List.of(),
List.of(
new Assignment(
new Id("i"),
new FieldId(
true,
null,
new Id("i")),
new Binary(new Id("i"),
Operator.SUB,
new IntLiteral(1))
),
new Assignment(
new Id("returnValue"),
new FieldId(
false,
null,
new Id("returnValue")),
new Unary(
UnaryOperator.NOT,
new Id("returnValue")
@ -115,7 +127,10 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain"));
List<Statement> statementList = List.of(
new Assignment(
new Id("instance"),
new FieldId(
true,
null,
new Id("instance")),
new New(
TypeClassWithMoreComplexMethodAndMain,
List.of()

View File

@ -2,7 +2,6 @@
//}
import de.maishai.ast.records.Class;
import de.maishai.ast.records.Id;
import java.util.List;

View File

@ -12,7 +12,6 @@
// }
//}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator;
import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*;

View File

@ -18,19 +18,10 @@
//}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator;
import de.maishai.ast.ReturnType;
import de.maishai.ast.Type;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructorAndMethodCall {
// public static Class get() {
// List<Field> fieldList = List.of(
// new Field(
// List<Declaration> fieldList = List.of(
// new Declaration(
// new Id("x"),
// Type.INT
// )

View File

@ -13,18 +13,11 @@
// }
// }
//}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.Type;
import de.maishai.ast.records.Class;
import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructorWithParameters {
// public static Class get() {
// List<Field> fields = List.of(
// new Field(
// List<Declaration> fieldDeclarations = List.of(
// new Declaration(
// new Id("x"),
// Type.INT
// )
@ -33,7 +26,7 @@ public class TypedAbstractSyntax_ClassWithConstructorWithParameters {
// return new Class(
// true,
// new Id("ClassWithConstructorWithParameters"),
// fields,
// fieldDeclarations,
// List.of(),
// null,
// constructors

View File

@ -18,8 +18,8 @@ import java.util.List;
class TypedAbstractSyntax_ClassWithMethodAndField {
// public static Class get() {
// List<Field> fields = List.of(
// new Field(
// List<Declaration> fieldDeclarations = List.of(
// new Declaration(
// new Id("c"),
// Type.CHAR
// )
@ -29,7 +29,7 @@ class TypedAbstractSyntax_ClassWithMethodAndField {
// return new Class(
// false,
// new Id("ClassWithMethodAndField"),
// fields,
// fieldDeclarations,
// methods,
// null,
// constructors

View File

@ -28,8 +28,8 @@ public class TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain {
// public static Class get() {
// Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT;
// TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain"));
// List<Field> fields = List.of(
// new Field(
// List<Declaration> fieldDeclarations = List.of(
// new Declaration(
// new Id("instance"),
// TypeClassWithMoreComplexMethodAndMain
// )
@ -39,7 +39,7 @@ public class TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain {
// return new Class(
// true,
// new Id("ClassWithMoreComplexMethodAndMain"),
// fields,
// fieldDeclarations,
// methods,
// getMainMethod(),
// constructors