Skip to content

Commit 66abcf2

Browse files
committed
fix: fix optimization for FOR loop
1 parent 1da9b30 commit 66abcf2

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed

src/api/optimize.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,12 @@ def visit_WHILE(self, node):
421421
yield node
422422

423423
def visit_FOR(self, node):
424-
from_ = yield node.children[1]
425-
to_ = yield node.children[2]
426-
step_ = yield node.children[3]
427-
body_ = yield node.children[4]
424+
node = yield self.generic_visit(node)
425+
426+
from_ = node.children[1]
427+
to_ = node.children[2]
428+
step_ = node.children[3]
429+
body_ = node.children[4]
428430

429431
if self.O_LEVEL > 0 and chk.is_number(from_, to_, step_) and not chk.is_block_accessed(body_):
430432
if from_ > to_ and step_ > 0:
@@ -434,8 +436,6 @@ def visit_FOR(self, node):
434436
yield self.NOP
435437
return
436438

437-
for i, child in enumerate((from_, to_, step_, body_), start=1):
438-
node.children[i] = child
439439
yield node
440440

441441
# TODO: ignore unused labels
@@ -446,8 +446,8 @@ def _visit_LABEL(self, node):
446446
yield node
447447

448448
def generic_visit(self, node: symbols.SYMBOL):
449-
for i in range(len(node.children)):
450-
node.children[i] = yield ToVisit(node.children[i])
449+
for i, child in enumerate(node.children):
450+
node.children[i] = yield ToVisit(child)
451451

452452
yield node
453453

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
org 32768
2+
.core.__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (.core.__CALL_BACK__), hl
12+
ei
13+
jp .core.__MAIN_PROGRAM__
14+
.core.__CALL_BACK__:
15+
DEFW 0
16+
.core.ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
19+
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
20+
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
21+
.core.ZXBASIC_USER_DATA_END:
22+
.core.__MAIN_PROGRAM__:
23+
call _main
24+
ld hl, 0
25+
ld b, h
26+
ld c, l
27+
.core.__END_PROGRAM:
28+
di
29+
ld hl, (.core.__CALL_BACK__)
30+
ld sp, hl
31+
exx
32+
pop hl
33+
pop iy
34+
pop ix
35+
exx
36+
ei
37+
ret
38+
_saludar:
39+
push ix
40+
ld ix, 0
41+
add ix, sp
42+
ld l, (ix+4)
43+
ld h, (ix+5)
44+
inc hl
45+
_saludar__leave:
46+
ld sp, ix
47+
pop ix
48+
exx
49+
pop hl
50+
ex (sp), hl
51+
exx
52+
ret
53+
_main:
54+
push ix
55+
ld ix, 0
56+
add ix, sp
57+
ld hl, 0
58+
push hl
59+
ld (ix-2), 1
60+
ld (ix-1), 0
61+
jp .LABEL.__LABEL0
62+
.LABEL.__LABEL3:
63+
ld l, (ix-2)
64+
ld h, (ix-1)
65+
push hl
66+
call _saludar
67+
ld l, (ix-2)
68+
ld h, (ix-1)
69+
inc hl
70+
ld (ix-2), l
71+
ld (ix-1), h
72+
.LABEL.__LABEL0:
73+
ld l, (ix-2)
74+
ld h, (ix-1)
75+
ex de, hl
76+
ld hl, 2
77+
or a
78+
sbc hl, de
79+
jp nc, .LABEL.__LABEL3
80+
_main__leave:
81+
ld sp, ix
82+
pop ix
83+
ret
84+
;; --- end of user code ---
85+
END
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
main()
2+
3+
function saludar(x as Uinteger) as Uinteger
4+
return x + 1
5+
end function
6+
7+
sub main()
8+
dim i, result as uinteger
9+
for i = 1 to 2
10+
result = saludar(i)
11+
next
12+
end sub

0 commit comments

Comments
 (0)