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