mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 08:38:03 +00:00
Merge branch 'main' of https://github.com/JonathanFleischmann/CompilerULTIMATE
This commit is contained in:
commit
a5d13fd2a9
@ -90,7 +90,7 @@ public class Compiler {
|
|||||||
options.addOption("d", "debug", false, "Enable debug logging");
|
options.addOption("d", "debug", false, "Enable debug logging");
|
||||||
|
|
||||||
CommandLineParser parser = new DefaultParser();
|
CommandLineParser parser = new DefaultParser();
|
||||||
CommandLine cmd = null;
|
CommandLine cmd;
|
||||||
try {
|
try {
|
||||||
cmd = parser.parse(options, args);
|
cmd = parser.parse(options, args);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
|
@ -28,8 +28,7 @@ public class MethodContext {
|
|||||||
private final Deque<Label> continueLabels = new ArrayDeque<>();
|
private final Deque<Label> continueLabels = new ArrayDeque<>();
|
||||||
|
|
||||||
public MethodContext(ClassContext classContext, MethodVisitor mv, Type returnType) {
|
public MethodContext(ClassContext classContext, MethodVisitor mv, Type returnType) {
|
||||||
// if DEBUG env variable is set, log all method calls
|
if (Objects.equals(System.getenv("DEBUG"), "true")) {
|
||||||
if (System.getenv("DEBUG") != null) {
|
|
||||||
this.mv = new LoggingMethodVisitor(mv);
|
this.mv = new LoggingMethodVisitor(mv);
|
||||||
} else {
|
} else {
|
||||||
this.mv = mv;
|
this.mv = mv;
|
||||||
@ -85,7 +84,6 @@ public class MethodContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void wrapUp() {
|
public void wrapUp() {
|
||||||
//if return type is void, add return instruction
|
|
||||||
if (returnType.getKind() == Type.Kind.VOID) {
|
if (returnType.getKind() == Type.Kind.VOID) {
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package de.maishai.typedast.Help;
|
package de.maishai.typedast.Util;
|
||||||
|
|
||||||
import de.maishai.ast.records.*;
|
import de.maishai.ast.records.*;
|
||||||
import de.maishai.typedast.TypedExpression;
|
import de.maishai.typedast.TypedExpression;
|
||||||
import de.maishai.typedast.typedclass.*;
|
import de.maishai.typedast.typedclass.*;
|
||||||
|
|
||||||
public class TypedExpressionHelp {
|
public class TypedExpressionUtil {
|
||||||
|
|
||||||
public static TypedExpression convertExpression( TypedProgram typedProgram, Expression expression) {
|
public static TypedExpression convertExpression( TypedProgram typedProgram, Expression expression) {
|
||||||
if (expression instanceof BoolLiteral boolLiteral) {
|
if (expression instanceof BoolLiteral boolLiteral) {
|
@ -13,7 +13,7 @@ import java.util.Optional;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -11,7 +11,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ -52,13 +52,20 @@ public class TypedBinary implements TypedExpression {
|
|||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Type mismatch in " + op);
|
throw new RuntimeException("Type mismatch in " + op);
|
||||||
}
|
}
|
||||||
} else if (op == Operator.EQ || op == Operator.NE || op == Operator.AND || op == Operator.OR) {
|
} else if (op == Operator.EQ || op == Operator.NE ) {
|
||||||
if (leftType == Type.INT && rightType == Type.INT || leftType == Type.BOOL && rightType == Type.BOOL) {
|
if (leftType == Type.INT && rightType == Type.INT || leftType == Type.BOOL && rightType == Type.BOOL) {
|
||||||
type = Type.BOOL;
|
type = Type.BOOL;
|
||||||
return Type.BOOL;
|
return Type.BOOL;
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Type mismatch in " + op);
|
throw new RuntimeException("Type mismatch in " + op);
|
||||||
}
|
}
|
||||||
|
} else if (op == Operator.AND || op == Operator.OR ){
|
||||||
|
if (leftType == Type.BOOL && rightType == Type.BOOL) {
|
||||||
|
type = Type.BOOL;
|
||||||
|
return Type.BOOL;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Type mismatch in " + op);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftType == rightType && leftType != Type.VOID) {
|
if (leftType == rightType && leftType != Type.VOID) {
|
||||||
|
@ -56,7 +56,7 @@ public class TypedClass implements TypedNode {
|
|||||||
typedDeclarations.add(new TypedDeclaration(typedProgram, declaration));
|
typedDeclarations.add(new TypedDeclaration(typedProgram, declaration));
|
||||||
}
|
}
|
||||||
for (Constructor constructor : c.constructors()) {
|
for (Constructor constructor : c.constructors()) {
|
||||||
typedConstructors.add(new TypedConstructor(typedProgram, constructor));
|
typedConstructors.add(new TypedConstructor(typedProgram, constructor, this.className));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Method method : c.methods()) {
|
for (Method method : c.methods()) {
|
||||||
|
@ -32,9 +32,14 @@ public class TypedConstructor implements TypedNode {
|
|||||||
this.typedParameters = typedParameters;
|
this.typedParameters = typedParameters;
|
||||||
this.typedBlock = typedBlock;
|
this.typedBlock = typedBlock;
|
||||||
}
|
}
|
||||||
|
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
|
||||||
|
convertToTypedConstructor(typedProgram, unTypedConstructor, className);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
|
||||||
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
|
if(!unTypedConstructor.className().equals(className)) {
|
||||||
|
throw new RuntimeException("Constructor name "+ unTypedConstructor.className() +" must be the same as class name" + className);
|
||||||
|
}
|
||||||
name = unTypedConstructor.className();
|
name = unTypedConstructor.className();
|
||||||
convertToTypedParameter(typedProgram, unTypedConstructor.params());
|
convertToTypedParameter(typedProgram, unTypedConstructor.params());
|
||||||
type = Type.VOID;
|
type = Type.VOID;
|
||||||
@ -59,9 +64,7 @@ public class TypedConstructor implements TypedNode {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
|
|
||||||
convertToTypedConstructor(typedProgram, unTypedConstructor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isLocalVariablePresent(String localVarName) {
|
public boolean isLocalVariablePresent(String localVarName) {
|
||||||
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
|
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
|
||||||
|
@ -7,7 +7,7 @@ import lombok.Data;
|
|||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -13,7 +13,7 @@ import java.util.Optional;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ -14,7 +14,7 @@ import java.util.Optional;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@ -12,7 +12,7 @@ import java.util.List;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -7,7 +7,7 @@ import org.objectweb.asm.Opcodes;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class TypedPrint implements TypedStatement {
|
public class TypedPrint implements TypedStatement {
|
||||||
|
@ -10,7 +10,7 @@ import org.objectweb.asm.Opcodes;
|
|||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ -9,7 +9,7 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -10,7 +10,7 @@ import lombok.Data;
|
|||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
|
import static de.maishai.typedast.Util.TypedExpressionUtil.convertExpression;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
Loading…
Reference in New Issue
Block a user