diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index 7d88f1510..2d36818c7 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -4,12 +4,13 @@ ~ main { - byte[2] barray = 0 - byte[2] barray2 =0 + byte[10,5] barray = 0 sub start() { - X=barray[0] - return + X=barray[2,3] + barray[3,3]=X + + } diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index ce3d4534e..f82b83635 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -233,8 +233,6 @@ class AstChecker(private val namespace: INameScope, * Also check data type compatibility */ override fun process(assignment: Assignment): IStatement { - // todo deal with target.arrayindexed - if(assignment.target.identifier!=null) { val targetName = assignment.target.identifier!!.nameInSource val targetSymbol = namespace.lookup(targetName, assignment) @@ -273,7 +271,6 @@ class AstChecker(private val namespace: INameScope, else if(assignment.target.identifier!=null) assignment.target.identifier!! else if(assignment.target.arrayindexed!=null) { - // todo deal with target.arrayindexed assignment.target.arrayindexed!! } else throw FatalAstException("strange assignment") @@ -575,7 +572,21 @@ class AstChecker(private val namespace: INameScope, } } } else if(postIncrDecr.target.arrayindexed!=null) { - // todo deal with target.arrayindexed + val indexedRegister = postIncrDecr.target.arrayindexed?.register + if(indexedRegister!=null) { + if(indexedRegister==Register.A || indexedRegister==Register.X || indexedRegister==Register.Y) + checkResult.add(SyntaxError("array indexing on registers requires register pair variable", postIncrDecr.position)) + } else { + val target = postIncrDecr.target.arrayindexed?.identifier?.targetStatement(namespace) + if(target==null) { + checkResult.add(SyntaxError("undefined symbol", postIncrDecr.position)) + } + else { + val dt = (target as VarDecl).datatype + if(dt!=DataType.ARRAY && dt!=DataType.ARRAY_W && dt!=DataType.ARRAY_F) + checkResult.add(SyntaxError("can only increment or decrement a byte/float/word", postIncrDecr.position)) + } + } } return super.process(postIncrDecr) } diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 5323f4734..0ded59a61 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -468,6 +468,7 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram, DataType.ARRAY -> Opcode.READ_INDEXED_VAR DataType.ARRAY_W -> Opcode.READ_INDEXED_VAR_W DataType.ARRAY_F -> Opcode.READ_INDEXED_VAR_F + DataType.MATRIX -> Opcode.READ_INDEXED_VAR else -> throw CompilerException("invalid dt for indexed $dt") } } @@ -477,6 +478,7 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram, DataType.ARRAY -> Opcode.WRITE_INDEXED_VAR DataType.ARRAY_W -> Opcode.WRITE_INDEXED_VAR_W DataType.ARRAY_F -> Opcode.WRITE_INDEXED_VAR_F + DataType.MATRIX -> Opcode.WRITE_INDEXED_VAR else -> throw CompilerException("invalid dt for indexed $dt") } } @@ -1110,10 +1112,18 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram, stackvmProg.instr(Opcode.ADD_W) } - if(write) - stackvmProg.instr(opcodeWriteindexedvar(variable!!.datatype), callLabel = variableName) - else - stackvmProg.instr(opcodeReadindexedvar(variable!!.datatype), callLabel = variableName) + if(variable!=null) { + if (write) + stackvmProg.instr(opcodeWriteindexedvar(variable.datatype), callLabel = variableName) + else + stackvmProg.instr(opcodeReadindexedvar(variable.datatype), callLabel = variableName) + } else { + // register indexed + if (write) + stackvmProg.instr(opcodeWriteindexedvar(DataType.ARRAY), callLabel = arrayindexed.register!!.toString()) + else + stackvmProg.instr(opcodeReadindexedvar(DataType.ARRAY), callLabel = arrayindexed.register!!.toString()) + } } private fun createSyscall(funcname: String) {