mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 08:38:03 +00:00
make most expressions work
This commit is contained in:
parent
2fb9ecd668
commit
9a7d296ecc
@ -1,5 +1,6 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import de.maishai.VariableGenerator;
|
||||
import lombok.*;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
@ -7,11 +8,12 @@ import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Stack;
|
||||
|
||||
@Getter
|
||||
public class MethodContext {
|
||||
private record LocalVariable(String name, int index, Type type) {
|
||||
public record LocalVariable(String name, int index, Type type) {
|
||||
}
|
||||
|
||||
private Label startLabel;
|
||||
@ -39,12 +41,9 @@ public class MethodContext {
|
||||
variableIndex.put(name, new LocalVariable(name, index, type));
|
||||
}
|
||||
|
||||
public int getVariableIndex(String name) {
|
||||
int index = variableIndex.get(name).index;
|
||||
if (index == -1) {
|
||||
throw new RuntimeException("Variable not declared");
|
||||
}
|
||||
return index;
|
||||
public Optional<LocalVariable> getLocalVar(String name) {
|
||||
LocalVariable index = variableIndex.get(name);
|
||||
return Optional.ofNullable(index);
|
||||
}
|
||||
|
||||
public void pushStack(String varName) {
|
||||
@ -61,6 +60,14 @@ public class MethodContext {
|
||||
System.out.println("Pushed " + variableIndex.get(varName) + " to stack");
|
||||
}
|
||||
|
||||
public void pushInstantToStack () {
|
||||
stack.push(localVarIndex);
|
||||
if (stack.size() > maxStack) {
|
||||
maxStack = stack.size();
|
||||
}
|
||||
System.out.println("Pushed instant to stack");
|
||||
}
|
||||
|
||||
public void popStack() {
|
||||
stack.pop();
|
||||
}
|
||||
|
@ -64,11 +64,14 @@ public class TypedAssignment implements TypedStatement {
|
||||
@Override
|
||||
public void codeGen(MethodContext ctx) {
|
||||
ctx.pushStack("this");
|
||||
System.out.println("left: " + location);
|
||||
System.out.println("right: " + value);
|
||||
// load location recursively on stack
|
||||
location.codeGen(ctx);
|
||||
|
||||
// put value on stack (WIP!!)
|
||||
ctx.pushStack("x");
|
||||
//ctx.pushStack("x");
|
||||
value.codeGen(ctx);
|
||||
|
||||
//save value in field
|
||||
String receiver = ctx.getClassContext().getName();
|
||||
|
@ -8,6 +8,7 @@ import de.maishai.typedast.Type;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -68,6 +69,16 @@ public class TypedBinary implements TypedExpression {
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
left.codeGen(ctx);
|
||||
right.codeGen(ctx);
|
||||
if (op == Operator.ADD) {
|
||||
ctx.getMv().visitInsn(Opcodes.IADD);
|
||||
} else if (op == Operator.SUB) {
|
||||
ctx.getMv().visitInsn(Opcodes.ISUB);
|
||||
} else if (op == Operator.MUL) {
|
||||
ctx.getMv().visitInsn(Opcodes.IMUL);
|
||||
}
|
||||
//TODO: implement other operators
|
||||
ctx.popStack();
|
||||
}
|
||||
}
|
||||
|
@ -42,5 +42,6 @@ public class TypedBoolLiteral implements TypedExpression {
|
||||
}else{
|
||||
ctx.getMv().visitInsn(Opcodes.ICONST_0);
|
||||
}
|
||||
ctx.pushInstantToStack();
|
||||
}
|
||||
}
|
||||
|
@ -34,5 +34,6 @@ public class TypedCharLiteral implements TypedExpression {
|
||||
@Override
|
||||
public void codeGen(MethodContext ctx) {
|
||||
ctx.getMv().visitLdcInsn(value);
|
||||
ctx.pushInstantToStack();
|
||||
}
|
||||
}
|
||||
|
@ -77,6 +77,11 @@ public class TypedFieldVarAccess implements TypedExpression {
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodContext ctx) {
|
||||
if (!field && recursiveOwnerChain == null && ctx.getLocalVar(name).isPresent() && ctx.getLocalVar(name).get().type().equals(type)) {
|
||||
// load local variable
|
||||
ctx.pushStack(name);
|
||||
return;
|
||||
}
|
||||
if (recursiveOwnerChain != null) {
|
||||
recursiveOwnerChain.codeGen(ctx);
|
||||
}
|
||||
|
@ -42,5 +42,6 @@ public class TypedIntLiteral implements TypedExpression {
|
||||
@Override
|
||||
public void codeGen(MethodContext ctx) {
|
||||
ctx.getMv().visitLdcInsn(value);
|
||||
ctx.pushInstantToStack();
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ public class TypedUnary implements TypedExpression {
|
||||
public void convertToTypedUnary(TypedClass clas, Unary unTypedUnary) {
|
||||
op = unTypedUnary.op();
|
||||
right = convertExpression(clas, unTypedUnary.right());
|
||||
type = right.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,9 +1,11 @@
|
||||
public class ClassCanBeTyped {
|
||||
public ClassCanBeTyped c;
|
||||
public int x;
|
||||
public boolean y;
|
||||
|
||||
public ClassCanBeTyped(int x) {
|
||||
this.c.x = x;
|
||||
public ClassCanBeTyped(int x, boolean y) {
|
||||
this.c.x = 2 * x;
|
||||
this.y = !y;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user