Skip to content

Commit be1972a

Browse files
committed
Added type mutability for variables
1 parent 6dd65a7 commit be1972a

File tree

11 files changed

+1742
-972
lines changed

11 files changed

+1742
-972
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.ultreon.pythonc;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
public interface ConditionContext extends Context {
6+
@Nullable org.objectweb.asm.Label ifTrue();
7+
@Nullable org.objectweb.asm.Label ifFalse();
8+
}

compiler/src/main/java/dev/ultreon/pythonc/ImportedField.java

+54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ultreon.pythonc;
22

33
import org.objectweb.asm.MethodVisitor;
4+
import org.objectweb.asm.Opcodes;
45
import org.objectweb.asm.Type;
56

67
import static org.objectweb.asm.Opcodes.GETSTATIC;
@@ -23,6 +24,59 @@ public Type type(PythonCompiler compiler) {
2324

2425
@Override
2526
public void set(MethodVisitor mv, PythonCompiler compiler, PyExpr visit) {
27+
if (type.getSort() == Type.OBJECT) {
28+
visit.load(mv, compiler, visit.preload(mv, compiler, true), true);
29+
compiler.writer.getContext().pop();
2630

31+
mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, type.getDescriptor());
32+
} else {
33+
visit.load(mv, compiler, visit.preload(mv, compiler, false), false);
34+
if (compiler.writer.getContext().pop() != type) {
35+
int sort = visit.type(compiler).getSort();
36+
if (sort == Type.OBJECT) {
37+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
38+
} else if (sort == Type.ARRAY) {
39+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
40+
} else if (sort == Type.VOID) {
41+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
42+
} else if (sort == Type.LONG) {
43+
if (type.getSort() == Type.DOUBLE) {
44+
mv.visitInsn(Opcodes.D2L);
45+
} else if (type.getSort() == Type.INT) {
46+
mv.visitInsn(Opcodes.L2I);
47+
} else {
48+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
49+
}
50+
} else if (sort == Type.DOUBLE) {
51+
if (type.getSort() == Type.LONG) {
52+
mv.visitInsn(Opcodes.L2D);
53+
} else if (type.getSort() == Type.INT) {
54+
mv.visitInsn(Opcodes.I2D);
55+
} else {
56+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
57+
}
58+
} else if (sort == Type.INT) {
59+
if (type.getSort() == Type.LONG) {
60+
mv.visitInsn(Opcodes.I2L);
61+
} else if (type.getSort() == Type.DOUBLE) {
62+
mv.visitInsn(Opcodes.I2D);
63+
} else {
64+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
65+
}
66+
} else if (sort == Type.FLOAT) {
67+
if (type.getSort() == Type.DOUBLE) {
68+
mv.visitInsn(Opcodes.F2D);
69+
} else if (type.getSort() == Type.INT) {
70+
mv.visitInsn(Opcodes.F2I);
71+
} else {
72+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
73+
}
74+
} else {
75+
throw new RuntimeException("Cannot smart cast " + visit.type(compiler) + " to " + type);
76+
}
77+
}
78+
79+
mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, type.getDescriptor());
80+
}
2781
}
2882
}

0 commit comments

Comments
 (0)