some more typecheckings and indexing on matrixes

This commit is contained in:
Irmen de Jong 2018-10-07 21:20:04 +02:00
parent 34d26e42e1
commit 7b51597fe9
3 changed files with 34 additions and 12 deletions

View File

@ -4,12 +4,13 @@
~ main { ~ main {
byte[2] barray = 0 byte[10,5] barray = 0
byte[2] barray2 =0
sub start() { sub start() {
X=barray[0] X=barray[2,3]
return barray[3,3]=X
} }

View File

@ -233,8 +233,6 @@ class AstChecker(private val namespace: INameScope,
* Also check data type compatibility * Also check data type compatibility
*/ */
override fun process(assignment: Assignment): IStatement { override fun process(assignment: Assignment): IStatement {
// todo deal with target.arrayindexed
if(assignment.target.identifier!=null) { if(assignment.target.identifier!=null) {
val targetName = assignment.target.identifier!!.nameInSource val targetName = assignment.target.identifier!!.nameInSource
val targetSymbol = namespace.lookup(targetName, assignment) val targetSymbol = namespace.lookup(targetName, assignment)
@ -273,7 +271,6 @@ class AstChecker(private val namespace: INameScope,
else if(assignment.target.identifier!=null) else if(assignment.target.identifier!=null)
assignment.target.identifier!! assignment.target.identifier!!
else if(assignment.target.arrayindexed!=null) { else if(assignment.target.arrayindexed!=null) {
// todo deal with target.arrayindexed
assignment.target.arrayindexed!! assignment.target.arrayindexed!!
} else throw FatalAstException("strange assignment") } else throw FatalAstException("strange assignment")
@ -575,7 +572,21 @@ class AstChecker(private val namespace: INameScope,
} }
} }
} else if(postIncrDecr.target.arrayindexed!=null) { } 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) return super.process(postIncrDecr)
} }

View File

@ -468,6 +468,7 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram,
DataType.ARRAY -> Opcode.READ_INDEXED_VAR DataType.ARRAY -> Opcode.READ_INDEXED_VAR
DataType.ARRAY_W -> Opcode.READ_INDEXED_VAR_W DataType.ARRAY_W -> Opcode.READ_INDEXED_VAR_W
DataType.ARRAY_F -> Opcode.READ_INDEXED_VAR_F DataType.ARRAY_F -> Opcode.READ_INDEXED_VAR_F
DataType.MATRIX -> Opcode.READ_INDEXED_VAR
else -> throw CompilerException("invalid dt for indexed $dt") 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 -> Opcode.WRITE_INDEXED_VAR
DataType.ARRAY_W -> Opcode.WRITE_INDEXED_VAR_W DataType.ARRAY_W -> Opcode.WRITE_INDEXED_VAR_W
DataType.ARRAY_F -> Opcode.WRITE_INDEXED_VAR_F DataType.ARRAY_F -> Opcode.WRITE_INDEXED_VAR_F
DataType.MATRIX -> Opcode.WRITE_INDEXED_VAR
else -> throw CompilerException("invalid dt for indexed $dt") else -> throw CompilerException("invalid dt for indexed $dt")
} }
} }
@ -1110,10 +1112,18 @@ private class StatementTranslator(private val stackvmProg: StackVmProgram,
stackvmProg.instr(Opcode.ADD_W) stackvmProg.instr(Opcode.ADD_W)
} }
if(write) if(variable!=null) {
stackvmProg.instr(opcodeWriteindexedvar(variable!!.datatype), callLabel = variableName) if (write)
else stackvmProg.instr(opcodeWriteindexedvar(variable.datatype), callLabel = variableName)
stackvmProg.instr(opcodeReadindexedvar(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) { private fun createSyscall(funcname: String) {