This commit is contained in:
Boolean-true 2024-07-02 17:18:33 +02:00
commit a5d13fd2a9
17 changed files with 33 additions and 25 deletions

View File

@ -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) {

View File

@ -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);
} }

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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()) {

View File

@ -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));

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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