simplistic repeat and while loops

This commit is contained in:
Irmen de Jong 2019-08-01 21:22:42 +02:00
parent b740b079db
commit fb00ff74d1
2 changed files with 48 additions and 28 deletions

View File

@ -836,14 +836,44 @@ internal class AsmGen2(val program: Program,
}
private fun translate(stmt: WhileLoop) {
TODO("while $stmt")
val whileLabel = makeLabel("while")
val endLabel = makeLabel("whileend")
out(whileLabel)
// TODO optimize for the simple cases, can we avoid stack use?
translateExpression(stmt.condition)
if(stmt.condition.inferType(program) in ByteDatatypes) {
out(" inx | lda $ESTACK_LO_HEX,x | beq $endLabel")
} else {
out("""
inx
lda $ESTACK_LO_HEX,x
bne +
lda $ESTACK_HI_HEX,x
beq $endLabel
+ """)
}
translate(stmt.body)
out(" jmp $whileLabel")
out(endLabel)
}
private fun translate(stmt: RepeatLoop) {
// TODO("repeat $stmt")
out(";------ TODO REPEAT")
val repeatLabel = makeLabel("repeat")
out(repeatLabel)
// TODO optimize this for the simple cases, can we avoid stack use?
translate(stmt.body)
out(";------ TODO REPEAT END")
translateExpression(stmt.untilCondition)
if(stmt.untilCondition.inferType(program) in ByteDatatypes) {
out(" inx | lda $ESTACK_LO_HEX,x | beq $repeatLabel")
} else {
out("""
inx
lda $ESTACK_LO_HEX,x
bne +
lda $ESTACK_HI_HEX,x
beq $repeatLabel
+ """)
}
}
private fun translate(stmt: WhenStatement) {

View File

@ -4,32 +4,22 @@ main {
sub start() {
byte b1
byte b2
word w1
word w2
ubyte ub1
ubyte ub2
uword uw1
uword uw2
byte bb=10
b1 = 5
b2= 122
b1 = b2+b1
c64scr.print_b(b1)
c64.CHROUT('\n')
while bb<15 {
bb++
c64scr.print_b(bb)
c64.CHROUT('\n')
}
word ww=5
while ww > -5 {
ww--
c64scr.print_w(ww)
c64.CHROUT('\n')
}
w1 = -1111
w2 = 11231
w1 = w2+w1
c64scr.print_w(w1)
c64.CHROUT('\n')
uw1 = 55555
uw2 = 1123
uw1 = uw2+uw1
c64scr.print_uw(uw1)
c64.CHROUT('\n')
c64.CHROUT('\n')
}
}