From fbcd9a0c1d3bfb81a07e8cf177b70aac9d81f0da Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 2 Dec 2021 17:44:52 +0100 Subject: [PATCH] reduce number of similar errors for type problem in assignment --- compiler/src/prog8/CompilerMain.kt | 8 ++-- .../compiler/astprocessing/AstChecker.kt | 27 ++------------ compilerAst/src/prog8/parser/SourceCode.kt | 3 +- examples/test.p8 | 37 +++---------------- 4 files changed, 14 insertions(+), 61 deletions(-) diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 7178640f3..3d050217f 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -81,7 +81,7 @@ private fun compileMain(args: Array): Boolean { val results = mutableListOf() 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): Boolean { srcdirs, outputPath ) - val compilationResult = compileProgram(args) + val compilationResult = compileProgram(compilerArgs) results.add(compilationResult) } @@ -129,7 +129,7 @@ private fun compileMain(args: Array): 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): Boolean { srcdirs, outputPath ) - compilationResult = compileProgram(args) + compilationResult = compileProgram(compilerArgs) if (!compilationResult.success) return false } catch (x: AstException) { diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index d341c8a11..93d880ccc 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -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 } } diff --git a/compilerAst/src/prog8/parser/SourceCode.kt b/compilerAst/src/prog8/parser/SourceCode.kt index ddbf51bd7..e20dd9221 100644 --- a/compilerAst/src/prog8/parser/SourceCode.kt +++ b/compilerAst/src/prog8/parser/SourceCode.kt @@ -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 { diff --git a/examples/test.p8 b/examples/test.p8 index a90ee2799..ec9650f94 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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 }} } }