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

View File

@ -1,10 +1,9 @@
package de.maishai; package de.maishai;
import de.maishai.antlr.DecafParser; 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.*;
import de.maishai.ast.records.Class; import de.maishai.ast.records.Class;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -13,13 +12,9 @@ public class ASTGenerator {
public static Class generateAST(DecafParser.ClassContext ctx) { public static Class generateAST(DecafParser.ClassContext ctx) {
List<Field> fields = new ArrayList<>(); List<Declaration> declarations = new ArrayList<>();
if (ctx.field() != null) { if (ctx.field() != null) {
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); declarations = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
}
MainMethod mainMethod = null;
if(ctx.mainmeth() != null){
mainMethod = generateMainMethod(ctx.mainmeth());
} }
List<Constructor> constructors = new ArrayList<>(); List<Constructor> constructors = new ArrayList<>();
if (ctx.constructor() != null) { if (ctx.constructor() != null) {
@ -29,14 +24,12 @@ public class ASTGenerator {
if (ctx.meth() != null) { if (ctx.meth() != null) {
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList(); meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
} }
Id classId = new Id(ctx.id().getText()); return new Class(ctx.id().getText(), declarations, meths, constructors);
return new Class(classId, fields, meths, mainMethod, constructors);
} }
public static Field generateFieldVariable(DecafParser.FieldContext ctx) { public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) {
Id id = new Id(ctx.id().getText());
Type type = ASTGenerator.getType(ctx.type()); 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) { public static Parameter generateParameter(DecafParser.ParamContext ctx) {
@ -49,24 +42,16 @@ public class ASTGenerator {
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
} }
Block block = new BlockGenerator().visit(ctx.block()); Block block = new BlockGenerator().visit(ctx.block());
Id methId = new Id(ctx.id().getText()); return new Method(getType(ctx.returntype()), ctx.id().getText(), params, block);
return new Method(getReturnType(ctx.returntype()), methId, params, block);
} }
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) { public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
Boolean isPublic = ctx.PUBLIC() != null;
List<Parameter> params = new ArrayList<>(); List<Parameter> params = new ArrayList<>();
if (ctx.params() != null) { if (ctx.params() != null) {
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
} }
Block block = new BlockGenerator().visit(ctx.block()); Block block = new BlockGenerator().visit(ctx.block());
Id constructorId = new Id(ctx.id().getText()); return new Constructor( ctx.id().getText(), params, block);
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);
} }
public static Type getType(DecafParser.TypeContext ctx) { public static Type getType(DecafParser.TypeContext ctx) {
@ -77,28 +62,17 @@ public class ASTGenerator {
if (ctx.CHAR() != null) if (ctx.CHAR() != null)
return Type.CHAR; return Type.CHAR;
if (ctx.id() != null) { if (ctx.id() != null) {
Type type = Type.OBJECT; return Type.REFERENCE(ctx.id().getText());
type.setObjectType(new Id(ctx.id().getText()));
return type;
} }
throw new RuntimeException("No type found!"); throw new RuntimeException("No type found!");
} }
public static Type getType(DecafParser.ReturntypeContext ctx) {
public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) { if (ctx.type() != null){
if (ctx.type().INT() != null) return getType(ctx.type());
return ReturnType.INT;
if (ctx.type().BOOL() != null)
return ReturnType.BOOL;
if (ctx.type().CHAR() != null)
return ReturnType.CHAR;
if (ctx.VOID() != null) {
return ReturnType.VOID;
} }
if (ctx.type().id() != null){ if (ctx.VOID() != null){
ReturnType type = ReturnType.OBJECT; return Type.VOID;
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.DecafBaseVisitor;
import de.maishai.antlr.DecafParser; import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Statement; import de.maishai.ast.records.Statement;
import de.maishai.ast.records.Block; import de.maishai.ast.records.Block;
import de.maishai.ast.records.LocalVariable;
import java.util.List; import java.util.List;
public class BlockGenerator extends DecafBaseVisitor<Block> { public class BlockGenerator extends DecafBaseVisitor<Block> {
@Override @Override
public Block visitBlock(DecafParser.BlockContext ctx) { 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(); List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
return new Block(vars, statements); 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.UnaryOperator;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -47,9 +47,9 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
Expression recipient = null; Expression recipient = null;
if (ctx.fieldId().recipient() != null) { if (ctx.fieldId().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.fieldId().recipient(); 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) { public static Expression generateConstant(DecafParser.LiteralContext ctx) {
@ -87,18 +87,17 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
@Override @Override
public Expression visitMethodCall(DecafParser.MethodCallContext ctx) { public Expression visitMethodCall(DecafParser.MethodCallContext ctx) {
boolean isField = ctx.methCall().THIS() != null; boolean isField = ctx.methCall().THIS() != null;
Expression recipient = null; Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) { if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient(); 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<>(); List<Expression> args = new ArrayList<>();
for (var expr : ctx.methCall().methName().args().expr()) { for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(this); Expression astExpr = expr.accept(this);
args.add(astExpr); args.add(astExpr);
} }
return new MethodCall(isField, recipient, id, args); return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
} }
@Override @Override
@ -112,18 +111,25 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
return new New(type, args); 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()) { if (ctxList.isEmpty()) {
return recipient; return recipient;
} }
DecafParser.RecipientContext ctx = ctxList.get(0); DecafParser.RecipientContext ctx = ctxList.get(0);
ctxList.remove(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<>(); List<Expression> args = new ArrayList<>();
for (var expr : ctx.methName().args().expr()) { for (var expr : ctx.methName().args().expr()) {
Expression astExpr = expr.accept(new ExpressionGenerator()); Expression astExpr = expr.accept(new ExpressionGenerator());
args.add(astExpr); 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.Operator;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.records.Statement; import de.maishai.ast.records.Statement;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -53,7 +53,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
@Override @Override
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) { public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
return new ReturnVoid(); return new Return(null);
} }
@Override @Override
@ -67,27 +67,32 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
} }
private Assignment generateAssign(DecafParser.AssignContext ctx) { private Assignment generateAssign(DecafParser.AssignContext ctx) {
Id id = new Id(ctx.id().getText()); FieldVarAccess fieldVarAccess = generateField(ctx.fieldId());
Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr())); Expression expr = resolveFancyAssign(ctx.assignSign(), fieldVarAccess, new ExpressionGenerator().visit(ctx.expr()));
return new Assignment(id, 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 //StatementExpression
@Override @Override
public Statement visitMethodCall(DecafParser.MethodCallContext ctx) { public Statement visitMethodCall(DecafParser.MethodCallContext ctx) {
boolean isField = ctx.methCall().THIS() != null; boolean isField = ctx.methCall().THIS() != null;
Expression recipient = null; Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) { if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient(); 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<>(); List<Expression> args = new ArrayList<>();
for (var expr : ctx.methCall().methName().args().expr()) { for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(new ExpressionGenerator()); Expression astExpr = expr.accept(new ExpressionGenerator());
args.add(astExpr); args.add(astExpr);
} }
return new MethodCall(isField, recipient, id, args); return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
} }
@Override @Override
@ -101,15 +106,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
return new New(type, args); 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) if (ctx.ASSIGN() != null)
return expression; return expression;
if (ctx.ADD_ASSIGN() != null) if (ctx.ADD_ASSIGN() != null)
return new Binary(id, Operator.ADD, expression); return new Binary(fieldVarAccess, Operator.ADD, expression);
if (ctx.SUB_ASSIGN() != null) if (ctx.SUB_ASSIGN() != null)
return new Binary(id, Operator.SUB, expression); return new Binary(fieldVarAccess, Operator.SUB, expression);
if (ctx.MUL_ASSIGN() != null) 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!"); 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.DecafBaseVisitor;
import de.maishai.antlr.DecafParser; import de.maishai.antlr.DecafParser;
import de.maishai.ast.Type; import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Id; import de.maishai.typedast.Type;
import de.maishai.ast.records.LocalVariable;
public class VariableGenerator extends DecafBaseVisitor<LocalVariable> { public class VariableGenerator extends DecafBaseVisitor<Declaration> {
@Override @Override
public LocalVariable visitLocalVar(DecafParser.LocalVarContext ctx) { public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) {
Id id = new Id(ctx.id().getText());
Type type = ASTGenerator.getType(ctx.type()); 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[] { return new String[] {
"class", "field", "localVar", "assignSign", "returntype", "type", "meth", "class", "field", "localVar", "assignSign", "returntype", "type", "meth",
"mainmeth", "constructor", "params", "param", "block", "stmt", "stmtexpr", "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" "methName", "args", "literal", "id"
}; };
} }
@ -1904,8 +1904,8 @@ public class DecafParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class AssignContext extends ParserRuleContext { public static class AssignContext extends ParserRuleContext {
public IdContext id() { public FieldIdContext fieldId() {
return getRuleContext(IdContext.class,0); return getRuleContext(FieldIdContext.class,0);
} }
public AssignSignContext assignSign() { public AssignSignContext assignSign() {
return getRuleContext(AssignSignContext.class,0); return getRuleContext(AssignSignContext.class,0);
@ -1939,7 +1939,7 @@ public class DecafParser extends Parser {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(238); setState(238);
id(); fieldId();
setState(239); setState(239);
assignSign(); assignSign();
setState(240); setState(240);
@ -2494,7 +2494,7 @@ public class DecafParser extends Parser {
"\u00eb\u0001\u0000\u0000\u0000\u00e9\u00e7\u0001\u0000\u0000\u0000\u00e9"+ "\u00eb\u0001\u0000\u0000\u0000\u00e9\u00e7\u0001\u0000\u0000\u0000\u00e9"+
"\u00ea\u0001\u0000\u0000\u0000\u00ea\u00ec\u0001\u0000\u0000\u0000\u00eb"+ "\u00ea\u0001\u0000\u0000\u0000\u00ea\u00ec\u0001\u0000\u0000\u0000\u00eb"+
"\u00e9\u0001\u0000\u0000\u0000\u00ec\u00ed\u00030\u0018\u0000\u00ed#\u0001"+ "\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"+ "\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\u00f2\u00f3\u0005\u0016\u0000\u0000\u00f3\u00f5\u0005\u0012\u0000"+
"\u0000\u00f4\u00f2\u0001\u0000\u0000\u0000\u00f4\u00f5\u0001\u0000\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; 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; 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; 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; 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; 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; 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; import java.util.List;

View File

@ -1,4 +1,4 @@
package de.maishai.ast.records; 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; package de.maishai.ast.records;
import de.maishai.ast.Type; import de.maishai.typedast.Type;
public record Parameter(String name, Type type) implements Node { 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; 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) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Assignment untyped = (Assignment) unTypedAST; Assignment untyped = (Assignment) unTypedAST;
TypedAssignment typedAssignment = new TypedAssignment(); TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(untyped.loc().name()); typedAssignment.setVarName(untyped.location().id());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
return typedAssignment; return typedAssignment;
} }

View File

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

View File

@ -17,11 +17,9 @@ 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;
private TypedMainMethod typedMainMethod;
private List<TypedConstructor> typedConstructors; private List<TypedConstructor> typedConstructors;
@Override @Override
@ -55,7 +53,7 @@ public class TypedClass implements TypedNode {
TypedClass typedClass = new TypedClass(); TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST; Class c = (Class) unTypedAST;
for (Field field : c.fields()) { for (Declaration field : c.fieldDeclarations()) {
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field)); 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) { public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) {
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.id().name()); typedConstructor.setName(unTypedConstructor.className());
if (unTypedConstructor.params().isEmpty()) { if (unTypedConstructor.params().isEmpty()) {
typedConstructor.setTypedParameters(null); typedConstructor.setTypedParameters(null);
@ -54,21 +54,7 @@ public class TypedConstructor implements TypedNode {
for (Parameter param : unTypedConstructor.params()) { for (Parameter param : unTypedConstructor.params()) {
TypedParameter typedParam = new TypedParameter(); TypedParameter typedParam = new TypedParameter();
typedParam.setParaName(param.name()); typedParam.setParaName(param.name());
typedParam.setType(param.type());
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;
}
typedConstructor.getTypedParameters().add(typedParam); 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) { 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;
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(untyped.id().name()); typedConstructor.setName(untyped.className());
if (untyped.params().isEmpty()) { if (untyped.params().isEmpty()) {
typedConstructor.setTypedParameters(null); typedConstructor.setTypedParameters(null);

View File

@ -1,9 +1,8 @@
package de.maishai.typedast.typedclass; 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.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -20,23 +19,10 @@ public class TypedField implements TypedNode {
private Type type; private Type type;
public TypedField unTypedFieldToTypedField(Field field){ public TypedField unTypedFieldToTypedField(Declaration declaration){
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
switch(field.type()) { typedField.setType(declaration.type());
case INT: typedField.setVarName(declaration.name());
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());
return typedField; return typedField;
} }
@ -51,10 +37,10 @@ public class TypedField 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) {
Field untypedField = (Field) unTypedAST; Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
typedField.setVarName(untypedField.name().name()); typedField.setVarName(untypedDeclaration.name());
typedField.setType(TypeMapper.getType(untypedField.type())); typedField.setType(untypedDeclaration.type());
return typedField; return typedField;
} }

View File

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

View File

@ -1,10 +1,9 @@
package de.maishai.typedast.typedclass; 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.MethodContext;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -29,11 +28,11 @@ public final class TypedLocalVariable 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) {
LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST; Declaration untypedLocalVariable = (Declaration) unTypedAST;
TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
typedLocalVariable.setName(untypedLocalVariable.name().name()); typedLocalVariable.setName(untypedLocalVariable.name());
typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); typedLocalVariable.setType(untypedLocalVariable.type());
return typedLocalVariable; 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) { public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) {
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(unTypedMethod.id().name()); typedMethod.setName(unTypedMethod.methodName());
for(Parameter parameter : unTypedMethod.params()){ for(Parameter parameter : unTypedMethod.params()){
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name()); typedParameter.setParaName(parameter.name());
switch(parameter.type()) { typedParameter.setType(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;
}
typedParameters.add(typedParameter); typedParameters.add(typedParameter);
} }
TypedBlock block = new TypedBlock(); TypedBlock block = new TypedBlock();
@ -63,8 +50,8 @@ public class TypedMethod implements TypedNode {
Method untypedMethod = (Method) unTypedAST; Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(untypedMethod.id().name()); typedMethod.setName(untypedMethod.methodName());
typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type())); typedMethod.setReturnType(untypedMethod.type());
if(untypedMethod.params().isEmpty()){ if(untypedMethod.params().isEmpty()){
//TODO: Implement this //TODO: Implement this
@ -85,13 +72,13 @@ public class TypedMethod implements TypedNode {
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name()); typedParameter.setParaName(parameter.name());
if(parameter.type() == de.maishai.ast.Type.CHAR){ if(parameter.type() == Type.CHAR){
typedParameter.setType(Type.CHAR); typedParameter.setType(Type.CHAR);
} }
if(parameter.type() == de.maishai.ast.Type.BOOL){ if(parameter.type() == Type.BOOL){
typedParameter.setType(Type.BOOL); typedParameter.setType(Type.BOOL);
} }
if(parameter.type() == de.maishai.ast.Type.INT){ if(parameter.type() == Type.INT){
typedParameter.setType(Type.INT); typedParameter.setType(Type.INT);
} }

View File

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

View File

@ -20,7 +20,7 @@ public class TypedReturnVoid implements TypedStatement {
@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) {
Method untyped = (Method) 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"); throw new RuntimeException("Return type is not void");
} }
return new TypedReturnVoid(); return new TypedReturnVoid();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,7 +12,6 @@
// } // }
//} //}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*; 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 class TypedAbstractSyntax_ClassWithConstructorAndMethodCall {
// public static Class get() { // public static Class get() {
// List<Field> fieldList = List.of( // List<Declaration> fieldList = List.of(
// new Field( // new Declaration(
// new Id("x"), // new Id("x"),
// Type.INT // 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 class TypedAbstractSyntax_ClassWithConstructorWithParameters {
// public static Class get() { // public static Class get() {
// List<Field> fields = List.of( // List<Declaration> fieldDeclarations = List.of(
// new Field( // new Declaration(
// new Id("x"), // new Id("x"),
// Type.INT // Type.INT
// ) // )
@ -33,7 +26,7 @@ public class TypedAbstractSyntax_ClassWithConstructorWithParameters {
// return new Class( // return new Class(
// true, // true,
// new Id("ClassWithConstructorWithParameters"), // new Id("ClassWithConstructorWithParameters"),
// fields, // fieldDeclarations,
// List.of(), // List.of(),
// null, // null,
// constructors // constructors

View File

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

View File

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