Skip to content

Commit 6f81348

Browse files
committed
py: Allow viper to use ints as direct conditionals in jumps.
Allows things like: if 1: ...
1 parent a732961 commit 6f81348

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

py/emitnative.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,20 +1365,25 @@ STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
13651365

13661366
STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
13671367
vtype_kind_t vtype = peek_vtype(emit);
1368-
if (vtype == VTYPE_BOOL) {
1369-
emit_pre_pop_reg(emit, &vtype, REG_RET);
1370-
if (!pop) {
1371-
adjust_stack(emit, 1);
1372-
}
1373-
} else if (vtype == VTYPE_PYOBJ) {
1374-
emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1375-
if (!pop) {
1376-
adjust_stack(emit, 1);
1377-
}
1378-
emit_call(emit, MP_F_OBJ_IS_TRUE);
1379-
} else {
1380-
printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1381-
assert(0);
1368+
switch (vtype) {
1369+
case VTYPE_PYOBJ:
1370+
emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1371+
if (!pop) {
1372+
adjust_stack(emit, 1);
1373+
}
1374+
emit_call(emit, MP_F_OBJ_IS_TRUE);
1375+
break;
1376+
case VTYPE_BOOL:
1377+
case VTYPE_INT:
1378+
case VTYPE_UINT:
1379+
emit_pre_pop_reg(emit, &vtype, REG_RET);
1380+
if (!pop) {
1381+
adjust_stack(emit, 1);
1382+
}
1383+
break;
1384+
default:
1385+
printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1386+
assert(0);
13821387
}
13831388
// need to commit stack because we may jump elsewhere
13841389
need_stack_settled(emit);

tests/micropython/viper_cond.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# using a bool as a conditional
2+
@micropython.viper
3+
def f():
4+
x = True
5+
if x:
6+
print("x", x)
7+
f()
8+
9+
# using an int as a conditional
10+
@micropython.viper
11+
def g():
12+
y = 1
13+
if y:
14+
print("y", y)
15+
g()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x True
2+
y 1

0 commit comments

Comments
 (0)