mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 17:08:03 +00:00
Implemented the statements in TypedBlock
This commit is contained in:
parent
622a0951f3
commit
db646c3399
@ -1,11 +1,7 @@
|
|||||||
package de.maishai.typedast.typedclass;
|
package de.maishai.typedast.typedclass;
|
||||||
|
|
||||||
import de.maishai.ast.records.Block;
|
import de.maishai.ast.records.*;
|
||||||
import de.maishai.ast.records.Declaration;
|
import de.maishai.typedast.*;
|
||||||
import de.maishai.typedast.MethodContext;
|
|
||||||
import de.maishai.typedast.TypedNode;
|
|
||||||
import de.maishai.typedast.TypedStatement;
|
|
||||||
import de.maishai.typedast.Type;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
@ -21,7 +17,18 @@ public class TypedBlock implements TypedNode {
|
|||||||
|
|
||||||
|
|
||||||
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
|
public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) {
|
||||||
|
|
||||||
TypedBlock typedBlock = new TypedBlock();
|
TypedBlock typedBlock = new TypedBlock();
|
||||||
|
typedBlock.setStmts(new ArrayList<>());
|
||||||
|
typedBlock.setVars(new ArrayList<>());
|
||||||
|
|
||||||
|
if(unTypedBlock.localVariables().isEmpty()){
|
||||||
|
typedBlock.setVars(List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(unTypedBlock.stmts().isEmpty()){
|
||||||
|
typedBlock.setStmts(List.of());
|
||||||
|
}
|
||||||
|
|
||||||
for (Declaration var : unTypedBlock.localVariables()) {
|
for (Declaration var : unTypedBlock.localVariables()) {
|
||||||
TypedLocalVariable typedVar = new TypedLocalVariable();
|
TypedLocalVariable typedVar = new TypedLocalVariable();
|
||||||
@ -30,7 +37,78 @@ public class TypedBlock implements TypedNode {
|
|||||||
typedBlock.getVars().add(typedVar);
|
typedBlock.getVars().add(typedVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedBlock.getStmts().add((TypedStatement) unTypedBlock.stmts());
|
for(var stmt : unTypedBlock.stmts()){
|
||||||
|
|
||||||
|
if(stmt instanceof Assignment assignment){
|
||||||
|
TypedAssignment typedAssignment = new TypedAssignment();
|
||||||
|
typedAssignment.setVarName(assignment.location().id());
|
||||||
|
typedAssignment.setValue(null);
|
||||||
|
typedBlock.getStmts().add(typedAssignment);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof For forStmt){
|
||||||
|
TypedFor typedFor = new TypedFor();
|
||||||
|
TypedBlock typedBlock1 = new TypedBlock();
|
||||||
|
typedFor.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock((forStmt.block())));
|
||||||
|
typedFor.setAssign(null);
|
||||||
|
typedFor.setCond(null);
|
||||||
|
typedFor.setInc(null);
|
||||||
|
typedBlock.getStmts().add(typedFor);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof IfElse ifElse){
|
||||||
|
TypedIfElse typedIfElse = new TypedIfElse();
|
||||||
|
TypedBlock typedBlockIf = new TypedBlock();
|
||||||
|
TypedBlock typedBlockElse = new TypedBlock();
|
||||||
|
typedIfElse.setIfTypedBlock(typedBlockIf.unTypedBlockToTypedBlock(ifElse.ifBlock()));
|
||||||
|
typedIfElse.setElseTypedBlock(typedBlockElse.unTypedBlockToTypedBlock(ifElse.elseBlock()));
|
||||||
|
typedIfElse.setTypedCon(null);
|
||||||
|
typedBlock.getStmts().add(typedIfElse);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof MethodCall methodCall){
|
||||||
|
TypedMethodCall typedMethodCall = new TypedMethodCall();
|
||||||
|
typedMethodCall.setArgs(null);
|
||||||
|
typedMethodCall.setRecipient(null);
|
||||||
|
typedBlock.getStmts().add(typedMethodCall);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof New newStmt){
|
||||||
|
TypedNew typedNew = new TypedNew();
|
||||||
|
typedNew.setArgs(null);
|
||||||
|
typedNew.setType(newStmt.type());
|
||||||
|
typedBlock.getStmts().add(typedNew);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof Return returnStmt){
|
||||||
|
TypedReturn typedReturn = new TypedReturn();
|
||||||
|
typedReturn.setRet(null);
|
||||||
|
typedBlock.getStmts().add(typedReturn);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof While whileStmt){
|
||||||
|
TypedWhile typedWhile = new TypedWhile();
|
||||||
|
TypedBlock typedBlock1 = new TypedBlock();
|
||||||
|
typedWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(whileStmt.block()));
|
||||||
|
typedWhile.setCond(null);
|
||||||
|
typedBlock.getStmts().add(typedWhile);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof DoWhile doWhile){
|
||||||
|
TypedDoWhile typedDoWhile = new TypedDoWhile();
|
||||||
|
TypedBlock typedBlock1 = new TypedBlock();
|
||||||
|
typedDoWhile.setTypedBlock(typedBlock1.unTypedBlockToTypedBlock(doWhile.block()));
|
||||||
|
typedDoWhile.setCond(null);
|
||||||
|
typedBlock.getStmts().add(typedDoWhile);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(stmt instanceof Break){
|
||||||
|
TypedBreak typedBreak = new TypedBreak();
|
||||||
|
typedBlock.getStmts().add(typedBreak);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
//System.out.println("stmts:" + typedBlock.getStmts().toString());
|
||||||
|
|
||||||
return typedBlock;
|
return typedBlock;
|
||||||
}
|
}
|
||||||
@ -62,6 +140,10 @@ public class TypedBlock implements TypedNode {
|
|||||||
typedBlock.getVars().add(typedVar);
|
typedBlock.getVars().add(typedVar);
|
||||||
}
|
}
|
||||||
typedBlock.getStmts().add((TypedStatement) untyped.stmts());
|
typedBlock.getStmts().add((TypedStatement) untyped.stmts());
|
||||||
|
int i = 0;
|
||||||
|
for(var stmt : untyped.stmts()){
|
||||||
|
System.out.println(i++ +" :" + stmt);
|
||||||
|
}
|
||||||
|
|
||||||
return typedBlock;
|
return typedBlock;
|
||||||
}
|
}
|
||||||
|
@ -67,21 +67,17 @@ public class TypedClass implements TypedNode {
|
|||||||
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
|
typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Constructor constructor : c.constructors()) {
|
|
||||||
TypedConstructor typedConstructor = new TypedConstructor();
|
|
||||||
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Constructor constructor : c.constructors()){
|
|
||||||
TypedConstructor typedConstructor = new TypedConstructor();
|
|
||||||
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Method method : c.methods()) {
|
for (Method method : c.methods()) {
|
||||||
TypedMethod typedMethod = new TypedMethod();
|
TypedMethod typedMethod = new TypedMethod();
|
||||||
typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method));
|
typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Constructor constructor : c.constructors()) {
|
||||||
|
TypedConstructor typedConstructor = new TypedConstructor();
|
||||||
|
typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor));
|
||||||
|
}
|
||||||
|
|
||||||
return typedClass;
|
return typedClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,9 +45,10 @@ 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.className());
|
typedConstructor.setName(unTypedConstructor.className());
|
||||||
|
typedConstructor.setTypedParameters(new ArrayList<>());
|
||||||
|
|
||||||
if (unTypedConstructor.params().isEmpty()) {
|
if (unTypedConstructor.params().isEmpty()) {
|
||||||
typedConstructor.setTypedParameters(null);
|
typedConstructor.setTypedParameters(List.of());
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
for (Parameter param : unTypedConstructor.params()) {
|
for (Parameter param : unTypedConstructor.params()) {
|
||||||
@ -58,9 +59,8 @@ public class TypedConstructor implements TypedNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Block block = unTypedConstructor.block();
|
|
||||||
TypedBlock typedBlock = new TypedBlock();
|
TypedBlock typedBlock = new TypedBlock();
|
||||||
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(block));
|
typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(unTypedConstructor.block()));
|
||||||
return typedConstructor;
|
return typedConstructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class TypedMethodCall implements TypedExpression, TypedStatement {
|
public class TypedMethodCall implements TypedExpression, TypedStatement {
|
||||||
private Boolean field;
|
|
||||||
private TypedExpression recipient;
|
private TypedExpression recipient;
|
||||||
private String name;
|
|
||||||
private List<TypedExpression> args;
|
private List<TypedExpression> args;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -12,7 +12,6 @@ import java.util.Map;
|
|||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@NoArgsConstructor
|
|
||||||
@Data
|
@Data
|
||||||
public class TypedParameter implements TypedNode {
|
public class TypedParameter implements TypedNode {
|
||||||
private String paraName;
|
private String paraName;
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
package de.maishai.typedast.typedclass;
|
|
||||||
|
|
||||||
import de.maishai.typedast.TypedNode;
|
|
||||||
import de.maishai.typedast.Type;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class TypedProgram implements TypedNode {
|
|
||||||
private List<TypedClass> typedClasses;
|
|
||||||
@Override
|
|
||||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
|
||||||
for(TypedClass c : this.typedClasses){
|
|
||||||
c.typeCheck(localVar, classes);
|
|
||||||
}
|
|
||||||
return Type.VOID;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
|
||||||
//TODO: Implement this
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package de.maishai.typedast.typedclass;
|
|
||||||
|
|
||||||
import de.maishai.ast.records.Method;
|
|
||||||
import de.maishai.typedast.MethodContext;
|
|
||||||
import de.maishai.typedast.TypedNode;
|
|
||||||
import de.maishai.typedast.TypedStatement;
|
|
||||||
import de.maishai.typedast.Type;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.objectweb.asm.MethodVisitor;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
@Data
|
|
||||||
public class TypedReturnVoid implements TypedStatement {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
|
||||||
return Type.VOID;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
|
||||||
Method untyped = (Method) unTypedAST;
|
|
||||||
if(untyped.type() != Type.VOID){
|
|
||||||
throw new RuntimeException("Return type is not void");
|
|
||||||
}
|
|
||||||
return new TypedReturnVoid();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
public class ClassWithConstructor {
|
public class ClassWithConstructor {
|
||||||
int x;
|
int x;
|
||||||
public classWithConstructor() {
|
public ClassWithConstructor() {
|
||||||
this.x = 10;
|
this.x = 10;
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 6; i = i + 1) {
|
for (i = 0; i < 6; i = i + 1) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
public class ClassWithConstructorWithParameters {
|
public class ClassWithConstructorWithParameters {
|
||||||
int x;
|
int x;
|
||||||
public classWithConstructorWithParameters(int startValue, int repetitions) {
|
public ClassWithConstructorWithParameters(int startValue, int repetitions) {
|
||||||
this.x = startValue;
|
this.x = startValue;
|
||||||
while (repetitions > 0) {
|
while (repetitions > 0) {
|
||||||
int innerRepetitions;
|
int innerRepetitions;
|
||||||
|
Loading…
Reference in New Issue
Block a user