nice error message if pop() argument is wrong

This commit is contained in:
Irmen de Jong 2021-11-28 01:36:44 +01:00
parent e8f4686430
commit 7c5ec1853d
5 changed files with 28 additions and 15 deletions

View File

@ -27,7 +27,7 @@ object C64Target: ICompilationTarget {
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
override fun asmsubArgsEvalOrder(sub: Subroutine): List<Int> = asmsub6502ArgsEvalOrder(sub)
override fun asmsubArgsHaveRegisterClobberRisk(args: List<Expression>): Boolean = asmsub6502ArgsHaveRegisterClobberRisk(args)
override fun asmsubArgsHaveRegisterClobberRisk(args: List<Expression>) = asmsub6502ArgsHaveRegisterClobberRisk(args)
override fun memorySize(dt: DataType): Int {
return when(dt) {

View File

@ -28,7 +28,7 @@ object Cx16Target: ICompilationTarget {
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
override fun asmsubArgsEvalOrder(sub: Subroutine): List<Int> = asmsub6502ArgsEvalOrder(sub)
override fun asmsubArgsHaveRegisterClobberRisk(args: List<Expression>): Boolean = asmsub6502ArgsHaveRegisterClobberRisk(args)
override fun asmsubArgsHaveRegisterClobberRisk(args: List<Expression>) = asmsub6502ArgsHaveRegisterClobberRisk(args)
override fun memorySize(dt: DataType): Int {
return when(dt) {

View File

@ -981,19 +981,31 @@ internal class AstChecker(private val program: Program,
checkUnusedReturnValues(functionCallStatement, targetStatement, program, errors)
}
if(functionCallStatement.target.nameInSource.last() == "sort") {
// sort is not supported on float arrays
val idref = functionCallStatement.args.singleOrNull() as? IdentifierReference
if(idref!=null && idref.inferType(program) istype DataType.ARRAY_F) {
errors.err("sorting a floating point array is not supported", functionCallStatement.args.first().position)
}
}
val funcName = functionCallStatement.target.nameInSource
if(functionCallStatement.target.nameInSource.last() in arrayOf("rol", "ror", "rol2", "ror2", "swap", "sort", "reverse")) {
// in-place modification, can't be done on literals
if(functionCallStatement.args.any { it !is IdentifierReference && it !is ArrayIndexedExpression && it !is DirectMemoryRead }) {
errors.err("invalid argument to a in-place modifying function", functionCallStatement.args.first().position)
if(funcName.size==1) {
// check some builtin function calls
if(funcName[0] == "sort") {
// sort is not supported on float arrays
val idref = functionCallStatement.args.singleOrNull() as? IdentifierReference
if(idref!=null && idref.inferType(program) istype DataType.ARRAY_F) {
errors.err("sorting a floating point array is not supported", functionCallStatement.args.first().position)
}
}
else if(funcName[0] in arrayOf("pop", "popw")) {
// can only pop into a variable, that has to have the correct type
val idref = functionCallStatement.args.singleOrNull() as? IdentifierReference
if(idref==null)
errors.err("invalid argument to pop, must be a variable with the correct type: ${functionCallStatement.args.first()}", functionCallStatement.args.first().position)
}
if(funcName[0] in arrayOf("rol", "ror", "rol2", "ror2", "swap", "sort", "reverse")) {
// in-place modification, can't be done on literals
if(functionCallStatement.args.any { it !is IdentifierReference && it !is ArrayIndexedExpression && it !is DirectMemoryRead }) {
errors.err("invalid argument to a in-place modifying function", functionCallStatement.args.first().position)
}
}
}
val error =

View File

@ -6,7 +6,8 @@ For next compiler release (7.4)
Use GoSub to call subroutines (statements):
- [DONE] allow separate assigns to subroutine's parameter variables / registers
- [DONE] turn a regular subroutine call into assignments to the parameters + GoSub (take code from gosub branch)
- also do this for asmsubs taking >0 parameters
- [DONE] also do this for asmsubs taking >0 parameters
- make that push(x+1) doesn't use stack evaluation, via a temp var?
Optimize Function calls in expressions:
- move args to assignments to params

View File

@ -8,9 +8,9 @@ main {
uword @shared uw
ubyte @shared ub
word @shared ww
push(127)
popw(ub) ; TODO give type error
pop(ub)
txt.print_ub(ub)
txt.nl()