issue #114 experiment with for loop

pull/168/head
Dibyendu Majumdar 5 years ago
parent 2aa2515fe1
commit b1396c8886

@ -1611,6 +1611,31 @@ static void emit_op_iforprep(struct function *fn, int A, int pc, int step_one, i
membuff_add_fstring(&fn->body, "goto Lbc_%d;\n", pc);
}
static void emit_op_forprep_int(struct function *fn, int A, int pc, int pc1) {
(void)pc1;
if (!fn->locals[A]) {
fn->locals[A] = 1; // Lua can reuse the same forloop vars if loop isn't nested
membuff_add_fstring(&fn->prologue, "lua_Integer i_%d = 0;\n", A);
membuff_add_fstring(&fn->prologue, "lua_Integer limit_%d = 0;\n", A);
membuff_add_fstring(&fn->prologue, "lua_Integer step_%d = 0;\n", A);
}
emit_reg(fn, "ra", A); // init
membuff_add_string(&fn->body, "rb = ra+1; /*limit*/\n");
membuff_add_string(&fn->body, "rc = ra+2; /*step*/\n");
membuff_add_fstring(
&fn->body, "if (ttisinteger(ra) && ttisinteger(rc) && ttisinteger(rb)) {\n", A);
membuff_add_fstring(&fn->body, " limit_%d = ivalue(rb);\n", A);
membuff_add_fstring(&fn->body, " i_%d = ivalue(ra);\n", A);
membuff_add_fstring(&fn->body, " step_%d = ivalue(rc);\n", A);
membuff_add_fstring(&fn->body, " i_%d -= step_%d;\n", A, A);
membuff_add_fstring(&fn->body, " goto Lbc_%d;\n", pc);
membuff_add_string(&fn->body, "}\n");
membuff_add_string(&fn->body, "else {\n");
membuff_add_fstring(&fn->body, " error_code = %d;\n", Error_for_limit_must_be_number);
membuff_add_string(&fn->body, " goto Lraise_error;\n");
membuff_add_string(&fn->body, "}\n");
}
static void emit_op_forprep(struct function *fn, int A, int pc, int pc1) {
(void)pc1;
if (!fn->locals[A]) {
@ -1680,6 +1705,14 @@ static void emit_op_iforloop(struct function *fn, int A, int pc, int step_one, i
A, A, A + 3, A, pc);
}
static void emit_op_forloop_int(struct function *fn, int A, int pc, int pc1) {
(void)pc1;
membuff_add_fstring(&fn->body, "i_%d += step_%d;\n", A, A);
membuff_add_fstring(&fn->body, "if ((0 < step_%d) ? (i_%d <= limit_%d) : (limit_%d <= i_%d)) {\n", A, A, A, A, A);
membuff_add_fstring(&fn->body, " ra = R(%d);\n setivalue(ra, i_%d);\n goto Lbc_%d;\n", A + 3, A, pc);
membuff_add_string(&fn->body, "}\n");
}
static void emit_op_forloop(struct function *fn, int A, int pc, int pc1) {
(void)pc1;
membuff_add_fstring(&fn->body, "if (intloop_%d) {\n", A);

Loading…
Cancel
Save