repeat and when without translateExpression()

This commit is contained in:
Irmen de Jong 2020-11-17 23:51:54 +01:00
parent d212f69d89
commit 79c75adac1
2 changed files with 26 additions and 11 deletions

View File

@ -905,17 +905,16 @@ internal class AsmGen(private val program: Program,
}
}
else -> {
translateExpression(stmt.iterations!!) // todo directly into AY?
val dt = stmt.iterations!!.inferType(program)
if(!dt.isKnown)
throw AssemblyError("unknown dt")
when (dt.typeOrElse(DataType.STRUCT)) {
in ByteDatatypes -> {
out(" inx | lda P8ESTACK_LO,x")
assignExpressionToRegister(stmt.iterations!!, RegisterOrPair.A)
repeatByteCountInA(null, repeatLabel, endLabel, stmt.body)
}
in WordDatatypes -> {
out(" inx | lda P8ESTACK_LO,x | ldy P8ESTACK_HI,x")
assignExpressionToRegister(stmt.iterations!!, RegisterOrPair.AY)
repeatWordCountInAY(null, repeatLabel, endLabel, stmt.body)
}
else -> throw AssemblyError("invalid loop expression datatype $dt")
@ -1011,16 +1010,16 @@ $counterVar .byte 0""")
}
private fun translate(stmt: WhenStatement) {
expressionsAsmGen.translateExpression(stmt.condition) // TODO directly into AY?
val endLabel = makeLabel("choice_end")
val choiceBlocks = mutableListOf<Pair<String, AnonymousScope>>()
val conditionDt = stmt.condition.inferType(program)
if(!conditionDt.isKnown)
throw AssemblyError("unknown condition dt")
if(conditionDt.typeOrElse(DataType.BYTE) in ByteDatatypes)
out(" inx | lda P8ESTACK_LO,x")
assignExpressionToRegister(stmt.condition, RegisterOrPair.A)
else
out(" inx | lda P8ESTACK_LO,x | ldy P8ESTACK_HI,x")
assignExpressionToRegister(stmt.condition, RegisterOrPair.AY)
for(choice in stmt.choices) {
val choiceLabel = makeLabel("choice")
if(choice.values==null) {

View File

@ -6,12 +6,28 @@ main {
sub start() {
ubyte ff = 10
ubyte ub = 4
uword uw = 5
setflag(ff-10)
setflag(ff-9)
setflag(ff-10)
setflag(ff-9)
when ub {
1 -> txt.chrout('1')
2 -> txt.chrout('2')
3 -> txt.chrout('3')
4 -> txt.chrout('4')
else -> txt.chrout('?')
}
txt.chrout('\n')
when uw {
$0001 -> txt.chrout('1')
$0002 -> txt.chrout('2')
$0003 -> txt.chrout('3')
$0004 -> txt.chrout('4')
$0005 -> txt.chrout('5')
else -> txt.chrout('?')
}
txt.chrout('\n')
testX()
}