reduce number of similar errors for type problem in assignment

This commit is contained in:
Irmen de Jong 2021-12-02 17:44:52 +01:00
parent c3144a20db
commit fbcd9a0c1d
4 changed files with 14 additions and 61 deletions

View File

@ -81,7 +81,7 @@ private fun compileMain(args: Array<String>): Boolean {
val results = mutableListOf<CompilationResult>()
for(filepathRaw in moduleFiles) {
val filepath = pathFrom(filepathRaw).normalize()
val args = CompilerArguments(
val compilerArgs = CompilerArguments(
filepath,
dontOptimize != true,
optimizeFloatExpressions == true,
@ -92,7 +92,7 @@ private fun compileMain(args: Array<String>): Boolean {
srcdirs,
outputPath
)
val compilationResult = compileProgram(args)
val compilationResult = compileProgram(compilerArgs)
results.add(compilationResult)
}
@ -129,7 +129,7 @@ private fun compileMain(args: Array<String>): Boolean {
val filepath = pathFrom(filepathRaw).normalize()
val compilationResult: CompilationResult
try {
val args = CompilerArguments(
val compilerArgs = CompilerArguments(
filepath,
dontOptimize != true,
optimizeFloatExpressions == true,
@ -140,7 +140,7 @@ private fun compileMain(args: Array<String>): Boolean {
srcdirs,
outputPath
)
compilationResult = compileProgram(args)
compilationResult = compileProgram(compilerArgs)
if (!compilationResult.success)
return false
} catch (x: AstException) {

View File

@ -11,7 +11,6 @@ import prog8.ast.walk.IAstVisitor
import prog8.compilerinterface.*
import java.io.CharConversionException
import java.io.File
import java.util.*
import kotlin.io.path.Path
internal class AstChecker(private val program: Program,
@ -414,18 +413,6 @@ internal class AstChecker(private val program: Program,
}
override fun visit(assignment: Assignment) {
if(assignment.value is FunctionCall) {
val stmt = (assignment.value as FunctionCall).target.targetStatement(program)
if (stmt is Subroutine) {
val idt = assignment.target.inferType(program)
if(!idt.isKnown)
throw FatalAstException("assignment target invalid dt")
if(stmt.returntypes.isEmpty() || (stmt.returntypes.size == 1 && stmt.returntypes.single() isNotAssignableTo idt.getOr(DataType.BYTE))) {
errors.err("return type mismatch: ${stmt.returntypes.single()} expected $idt", assignment.value.position)
}
}
}
val targetDt = assignment.target.inferType(program)
val valueDt = assignment.value.inferType(program)
if(valueDt.isKnown && !(valueDt isAssignableTo targetDt)) {
@ -500,7 +487,7 @@ internal class AstChecker(private val program: Program,
errors.err("assignment value is invalid or has no proper datatype", assignment.value.position)
} else {
checkAssignmentCompatible(targetDatatype.getOr(DataType.BYTE),
sourceDatatype.getOr(DataType.BYTE), assignment.value, assignment.position)
sourceDatatype.getOr(DataType.BYTE), assignment.value)
}
}
}
@ -1371,8 +1358,8 @@ internal class AstChecker(private val program: Program,
private fun checkAssignmentCompatible(targetDatatype: DataType,
sourceDatatype: DataType,
sourceValue: Expression,
position: Position) : Boolean {
sourceValue: Expression) : Boolean {
val position = sourceValue.position
if(sourceValue is RangeExpr)
errors.err("can't assign a range value to something else", position)
@ -1400,15 +1387,9 @@ internal class AstChecker(private val program: Program,
errors.err("cannot assign float to ${targetDatatype.name.lowercase()}; possible loss of precision. Suggestion: round the value or revert to integer arithmetic", position)
else {
if(targetDatatype!=DataType.UWORD && sourceDatatype !in PassByReferenceDatatypes)
errors.err(
"cannot assign ${sourceDatatype.name.lowercase()} to ${
targetDatatype.name.lowercase(
Locale.getDefault()
)
}", position)
errors.err("type of value $sourceDatatype doesn't match target $targetDatatype", position)
}
return false
}
}

View File

@ -6,7 +6,6 @@ import java.io.File
import java.io.IOException
import java.nio.channels.Channels
import java.nio.charset.CodingErrorAction
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import kotlin.io.path.*
@ -132,7 +131,7 @@ sealed class SourceCode {
val inpStr = object {}.javaClass.getResourceAsStream(normalized)!!
// CharStreams.fromStream() doesn't allow us to set the stream name properly, so we use a lower level api
val channel = Channels.newChannel(inpStr)
return CharStreams.fromChannel(channel, StandardCharsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1)
return CharStreams.fromChannel(channel, Charsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1)
}
override fun readText(): String {

View File

@ -7,41 +7,14 @@ main {
sub start() {
string.copy()
ubyte @shared dummy
word b1 = 1111
byte b2 = 22
word b3 = 3333
dummy++
labelz()
labelz(1)
printz(1)
printz(1,2,3,4,5,6)
func(-b1,-b2,-b3 , 3, 4, 5)
labelz:
uword[10] ptrs
ubyte @shared xx
xx = compare("zzz", ptrs[2])
}
sub printz(word a1, byte a2, word a3) {
txt.print_w(a1)
txt.spc()
txt.print_b(a2)
txt.spc()
txt.print_w(a3)
txt.nl()
}
asmsub func(word a1 @XY, byte a2 @A, word a3 @R0) {
asmsub compare(uword string1 @R0, uword string2 @AY) clobbers(Y) -> byte @A {
%asm {{
stx printz.a1
sty printz.a1+1
sta printz.a2
lda cx16.r0
sta printz.a3
lda cx16.r0+1
sta printz.a3+1
jmp printz
rts
}}
}
}