Compare commits
4 Commits
patternMat
...
tryCatch
Author | SHA1 | Date | |
---|---|---|---|
7b915df43d | |||
ccfd9a65b8 | |||
9c675ee860 | |||
15926e8e18 |
src/main
antlr4
de
dhbwstuttgart
parser
antlr
java
de
dhbwstuttgart
bytecode
core
environment
parser
SyntaxTreeGenerator
syntaxtree
target
generate
tree
typedeployment
typeinference
typeAlgo
@ -539,7 +539,7 @@ catchClause
|
||||
;
|
||||
|
||||
catchType
|
||||
: qualifiedName ('|' qualifiedName)*
|
||||
: typeType ('|' typeType)*
|
||||
;
|
||||
|
||||
finallyBlock
|
||||
|
@ -10,6 +10,7 @@ import de.dhbwstuttgart.target.tree.expression.*;
|
||||
import de.dhbwstuttgart.target.tree.type.*;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.invoke.*;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
@ -1158,11 +1159,48 @@ public class Codegen {
|
||||
mv.visitLabel(end);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new CodeGenException("Unexpected value: " + expr);
|
||||
case TargetPattern targetPattern:
|
||||
break;
|
||||
case TargetTryCatchFinally targetTryCatchFinally:
|
||||
generateTryCatchFinally(targetTryCatchFinally, state);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateTryCatchFinally(TargetTryCatchFinally tryCatchFinally, State state){
|
||||
MethodVisitor mv = state.mv;
|
||||
Label startTry = new Label();
|
||||
Label endTry = new Label();
|
||||
Label endOrFinally = new Label();
|
||||
|
||||
//Try
|
||||
mv.visitLabel(startTry);
|
||||
generate(state, tryCatchFinally.tryBlock());
|
||||
mv.visitLabel(endTry);
|
||||
mv.visitJumpInsn(GOTO, endOrFinally);
|
||||
|
||||
for (var catchClause : tryCatchFinally.catchClauses()){
|
||||
Label startCatch = new Label();
|
||||
Label endCatch = new Label();
|
||||
mv.visitTryCatchBlock(startTry, endOrFinally, startCatch, "java/lang/Exception"); //ignore all exception types except the first
|
||||
mv.visitLabel(startCatch);
|
||||
if (!catchClause.identifier().isEmpty()) {
|
||||
mv.visitFrame(F_SAME1, 0, null, 1, new Object[]{"java/lang/Exception"});
|
||||
LocalVar excep = state.createVariable(catchClause.identifier(), catchClause.exceptionNames().getFirst());
|
||||
mv.visitVarInsn(ASTORE, excep.index());
|
||||
}
|
||||
generate(state, catchClause.catchBlock());
|
||||
mv.visitLabel(endCatch);
|
||||
mv.visitJumpInsn(GOTO, endOrFinally);
|
||||
}
|
||||
|
||||
if (tryCatchFinally.finallyBlock().isPresent()){
|
||||
mv.visitLabel(endOrFinally);
|
||||
generate(state, tryCatchFinally.finallyBlock().get());
|
||||
}
|
||||
else mv.visitLabel(endOrFinally);
|
||||
|
||||
}
|
||||
|
||||
private void generateForEach(TargetForEach forEach, State state) {
|
||||
state.enterScope();
|
||||
TargetVarDecl vd = (TargetVarDecl) forEach.vardecl();
|
||||
|
@ -97,7 +97,7 @@ public class JavaTXCompiler {
|
||||
path.add(new File(System.getProperty("user.dir")));
|
||||
}
|
||||
if (outputPath != null) path.add(outputPath);
|
||||
classLoader = new DirectoryClassLoader(path, ClassLoader.getSystemClassLoader());
|
||||
classLoader = new DirectoryClassLoader(path, ClassLoader.getPlatformClassLoader());
|
||||
environment = new CompilationEnvironment(sources, classLoader);
|
||||
classPath = path;
|
||||
this.outputPath = outputPath;
|
||||
|
@ -16,7 +16,7 @@ public class DirectoryClassLoader extends URLClassLoader implements IByteArrayCl
|
||||
// }
|
||||
|
||||
public DirectoryClassLoader(List<File> directory, java.lang.ClassLoader parent) {
|
||||
super(directory.stream().map(DirectoryClassLoader::dirToURL).flatMap(List::stream).collect(Collectors.toList()).toArray(new URL[0]), parent.getParent());
|
||||
super(directory.stream().map(DirectoryClassLoader::dirToURL).flatMap(List::stream).toArray(URL[]::new), parent);
|
||||
}
|
||||
|
||||
private static URL[] generateURLArray(URL url) {
|
||||
|
@ -1,12 +1,7 @@
|
||||
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -16,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
||||
import de.dhbwstuttgart.target.tree.expression.TargetUnaryOp;
|
||||
import de.dhbwstuttgart.target.generate.StatementToTargetExpression;
|
||||
import org.antlr.v4.runtime.RuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
@ -638,8 +634,16 @@ public class StatementGenerator {
|
||||
}
|
||||
|
||||
private Statement convert(Java17Parser.TrycatchblockContext stmt) {
|
||||
// TODO
|
||||
throw new NotImplementedException();
|
||||
Block tryBlock = convert(stmt.block(), false);
|
||||
List<CatchClause> catchClauses = stmt.catchClause().stream().
|
||||
map(x ->
|
||||
new CatchClause(x.catchType().typeType().stream().map(y -> TypeGenerator.convert(y, reg, generics)).toList(),
|
||||
x.identifier().getText(),
|
||||
convert(x.block(), false)
|
||||
)).toList();
|
||||
|
||||
Optional<Block> finallyBlock = stmt.finallyBlock() != null ? Optional.of(convert(stmt.finallyBlock().block(), false)) : Optional.empty();
|
||||
return new TryCatchFinally(tryBlock, catchClauses, finallyBlock, stmt.getStart());
|
||||
}
|
||||
|
||||
private Statement convert(Java17Parser.TrycatchresourceContext stmt) {
|
||||
|
@ -39,6 +39,11 @@ public class SyntacticSugar {
|
||||
public void visit(LambdaExpression le) {
|
||||
//PL 2024-04-09 Do nothing, as in a LambdaExpression a return could be
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasReturn(Block block) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dhbwstuttgart.syntaxtree;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.statement.TryCatchFinally;
|
||||
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||
|
||||
public interface ASTVisitor extends StatementVisitor{
|
||||
@ -40,4 +41,6 @@ public interface ASTVisitor extends StatementVisitor{
|
||||
|
||||
void visit(GuardedPattern aGuardedPattern);
|
||||
|
||||
void visit(TryCatchFinally tryCatchFinally);
|
||||
|
||||
}
|
||||
|
19
src/main/java/de/dhbwstuttgart/syntaxtree/CatchClause.java
Normal file
19
src/main/java/de/dhbwstuttgart/syntaxtree/CatchClause.java
Normal file
@ -0,0 +1,19 @@
|
||||
package de.dhbwstuttgart.syntaxtree;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CatchClause {
|
||||
public List<RefTypeOrTPHOrWildcardOrGeneric> exceptionTypes;
|
||||
public String identifier;
|
||||
public Block catchBlock;
|
||||
|
||||
public CatchClause(List<RefTypeOrTPHOrWildcardOrGeneric> exceptionTypes, String identifier, Block catchBlock){
|
||||
this.exceptionTypes = exceptionTypes;
|
||||
this.identifier = identifier;
|
||||
this.catchBlock = catchBlock;
|
||||
}
|
||||
}
|
||||
|
@ -85,4 +85,6 @@ public interface StatementVisitor {
|
||||
void visit(Throw aThrow);
|
||||
|
||||
void visit(Ternary ternary);
|
||||
|
||||
void visit(TryCatchFinally tryCatchFinally);
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package de.dhbwstuttgart.syntaxtree.statement;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.CatchClause;
|
||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TryCatchFinally extends Statement{
|
||||
|
||||
public Block tryBlock;
|
||||
public List<CatchClause> catchClauses;
|
||||
public Optional<Block> finallyBlock;
|
||||
|
||||
public TryCatchFinally(Block tryBlock, List<CatchClause> catchClauses, Optional<Block> finallyBlock, Token offset){
|
||||
super(new Void(offset), offset);
|
||||
this.tryBlock = tryBlock;
|
||||
this.catchClauses = catchClauses;
|
||||
this.finallyBlock = finallyBlock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(StatementVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
@ -530,4 +530,9 @@ public class OutputGenerator implements ASTVisitor {
|
||||
out.append(" with ");
|
||||
aGuardedPattern.getCondition().accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
out.append("");
|
||||
}
|
||||
}
|
@ -434,6 +434,11 @@ public abstract class GenerateGenerics {
|
||||
whileStmt.loopBlock.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ArgumentList arglist) {
|
||||
for (int i = 0; i < arglist.getArguments().size(); i++) {
|
||||
@ -536,6 +541,11 @@ public abstract class GenerateGenerics {
|
||||
super.visit(methodCall);
|
||||
typeVariables.addAll(findTypeVariables(methodCall.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -76,6 +76,11 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
localVariables.add(varDecl.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LambdaExpression lambda) {
|
||||
} // Don't look at lambda expressions
|
||||
@ -591,4 +596,14 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
public void visit(GuardedPattern aGuardedPattern) {
|
||||
result = new TargetGuard((TargetPattern) converter.convert(aGuardedPattern.getNestedPattern()), converter.convert(aGuardedPattern.getCondition()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
result = new TargetTryCatchFinally(
|
||||
converter.convert(tryCatchFinally.tryBlock),
|
||||
tryCatchFinally.catchClauses.stream().map(x -> new TargetCatchClause(x.exceptionTypes.stream().map(converter::convert).toList(), x.identifier, converter.convert(x.catchBlock))).toList(),
|
||||
tryCatchFinally.finallyBlock.map(converter::convert)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package de.dhbwstuttgart.target.tree.expression;
|
||||
|
||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record TargetCatchClause(List<TargetType> exceptionNames, String identifier, TargetBlock catchBlock) {
|
||||
}
|
@ -3,7 +3,7 @@ package de.dhbwstuttgart.target.tree.expression;
|
||||
import de.dhbwstuttgart.target.tree.type.*;
|
||||
|
||||
public sealed interface TargetExpression
|
||||
permits TargetBinaryOp, TargetBlock, TargetBreak, TargetCast, TargetClassName, TargetContinue, TargetDo, TargetFieldVar, TargetFor, TargetForEach, TargetIf, TargetInstanceOf, TargetLambdaExpression, TargetLiteral, TargetLocalVar, TargetPattern, TargetReturn, TargetStatementExpression, TargetSuper, TargetSwitch, TargetTernary, TargetThis, TargetThrow, TargetUnaryOp, TargetVarDecl, TargetWhile, TargetYield {
|
||||
permits TargetBinaryOp, TargetBlock, TargetBreak, TargetCast, TargetClassName, TargetContinue, TargetDo, TargetFieldVar, TargetFor, TargetForEach, TargetIf, TargetInstanceOf, TargetLambdaExpression, TargetLiteral, TargetLocalVar, TargetPattern, TargetReturn, TargetStatementExpression, TargetSuper, TargetSwitch, TargetTernary, TargetThis, TargetThrow, TargetTryCatchFinally, TargetUnaryOp, TargetVarDecl, TargetWhile, TargetYield {
|
||||
|
||||
default TargetType type() {
|
||||
return null;
|
||||
|
@ -0,0 +1,7 @@
|
||||
package de.dhbwstuttgart.target.tree.expression;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record TargetTryCatchFinally(TargetBlock tryBlock, List<TargetCatchClause> catchClauses, Optional<TargetBlock> finallyBlock) implements TargetExpression{
|
||||
}
|
@ -2,6 +2,7 @@ package de.dhbwstuttgart.typedeployment;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.*;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.TryCatchFinally;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.target.generate.GenericsResult;
|
||||
import de.dhbwstuttgart.target.generate.GenericsResultSet;
|
||||
@ -32,6 +33,11 @@ public class TypeInsertPlacer extends AbstractASTWalker {
|
||||
TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, genericsResult);
|
||||
this.inserts.addAll(cl.inserts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class TypeInsertPlacerClass extends AbstractASTWalker{
|
||||
@ -63,6 +69,11 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
|
||||
super.visit(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Field field) {
|
||||
if(field.getType() instanceof TypePlaceholder){
|
||||
|
@ -500,6 +500,12 @@ public class TYPEStmt implements StatementVisitor {
|
||||
constraintsSet.addUndConstraint(new Pair(ternary.iffalse.getType(), ternary.getType(), PairOperator.SMALLERDOT));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchFinally tryCatchFinally) {
|
||||
tryCatchFinally.tryBlock.accept(this);
|
||||
tryCatchFinally.catchClauses.forEach(c -> c.catchBlock.accept(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Return returnExpr) {
|
||||
returnExpr.retexpr.accept(this);
|
||||
|
Reference in New Issue
Block a user