aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-06 02:05:58 +0200
committerMel <einebeere@gmail.com>2022-07-06 14:05:28 +0200
commit32ec3e1186b3ed29e910ad60a18f956b823ddc23 (patch)
tree413c4613059b9d870ac631ad7daa24b1369e5f2c
parent16899111516fbff736dedacb2804e8ea83267968 (diff)
downloadjinx-32ec3e1186b3ed29e910ad60a18f956b823ddc23.tar.zst
jinx-32ec3e1186b3ed29e910ad60a18f956b823ddc23.zip
Increment hidden i variable in ForIn code
-rw-r--r--pkg/lang/compiler/compiler.go16
-rw-r--r--pkg/lang/compiler/compiler_test.go13
2 files changed, 25 insertions, 4 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index 3a5efd9..c51728f 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -204,6 +204,11 @@ func (comp *Compiler) compileForInStmt(t *code.Builder, forInStmt ast.StmtForIn)
// get_local 1
// index
// set_local 2
+
+ // get_local 1
+ // push_int 1
+ // add
+ // set_local 1
// 3. Do block
// ...
// 4. Repeat jump:
@@ -259,6 +264,17 @@ func (comp *Compiler) compileForInStmt(t *code.Builder, forInStmt ast.StmtForIn)
doPreparationTarget.AppendOp(code.OpSetLocal)
doPreparationTarget.AppendInt(int64(nameLocal))
+ doPreparationTarget.AppendOp(code.OpGetLocal)
+ doPreparationTarget.AppendInt(int64(iLocal))
+
+ doPreparationTarget.AppendOp(code.OpPushInt)
+ doPreparationTarget.AppendInt(1)
+
+ doPreparationTarget.AppendOp(code.OpAdd)
+
+ doPreparationTarget.AppendOp(code.OpSetLocal)
+ doPreparationTarget.AppendInt(int64(iLocal))
+
// Do block
doTarget := code.NewBuilder()
if err := comp.compileBlockNode(&doTarget, forInStmt.Do); err != nil {
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index 326ad68..f3a20a5 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -302,17 +302,17 @@ func TestForIn(t *testing.T) {
push_array
get_local 0
- get_member "$push"
+ get_member "push"
push_int 1
call 1
get_local 0
- get_member "$push"
+ get_member "push"
push_int 2
call 1
get_local 0
- get_member "$push"
+ get_member "push"
push_int 3
call 1
@@ -322,7 +322,7 @@ func TestForIn(t *testing.T) {
@check:
get_local 1
get_local 0
- get_member "$length"
+ get_member "length"
call 0
lt
@@ -333,6 +333,11 @@ func TestForIn(t *testing.T) {
index
set_local 2
+ get_local 1
+ push_int 1
+ add
+ set_local 1
+
push_string "say"
get_local 2
call 1