diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 5f5dc75b7..69bb694a8 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -390,12 +390,22 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val private fun funcStrlen(fcall: IFunctionCall) { val name = asmgen.asmVariableName(fcall.args[0] as IdentifierReference) - asmgen.out(""" - lda #<$name - ldy #>$name - jsr prog8_lib.strlen - sta P8ESTACK_LO,x - dex""") + val type = fcall.args[0].inferType(program) + when { + type.istype(DataType.STR) -> asmgen.out(""" + lda #<$name + ldy #>$name + jsr prog8_lib.strlen + sta P8ESTACK_LO,x + dex""") + type.istype(DataType.UWORD) -> asmgen.out(""" + lda $name + ldy $name+1 + jsr prog8_lib.strlen + sta P8ESTACK_LO,x + dex""") + else -> throw AssemblyError("strlen requires str or uword arg") + } } private fun funcSwap(fcall: IFunctionCall) { diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 0f238840d..0b48fa23e 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -285,9 +285,9 @@ private fun builtinStrlen(args: List, position: Position, program: P return NumericLiteralValue.optimalInteger(argument.value.length, argument.position) val vardecl = (argument as IdentifierReference).targetVarDecl(program.namespace) if(vardecl!=null) { - if(vardecl.datatype!=DataType.STR) + if(vardecl.datatype!=DataType.STR && vardecl.datatype!=DataType.UWORD) throw SyntaxError("strlen must have string argument", position) - if(vardecl.autogeneratedDontRemove) { + if(vardecl.autogeneratedDontRemove && vardecl.value!=null) { return NumericLiteralValue.optimalInteger((vardecl.value as StringLiteralValue).value.length, argument.position) } } diff --git a/examples/test.p8 b/examples/test.p8 index 879f5003e..8cb3761e0 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,10 +8,25 @@ main { sub start() { ; TODO fix multi- string concatenation: - txt.print("\nCommands are:\n"+ - "buy jump inf cash\n" + - "sell teleport market hold\n" + - "fuel galhyp local quit\n") +; txt.print("\nCommands are:\n"+ +; "buy jump inf cash\n" + +; "sell teleport market hold\n" + +; "fuel galhyp local quit\n") + + str name = "irmen de jong" + uword strptr = &name + + + txt.print_ub(strlen("1234")) + txt.chrout('\n') + txt.print_ub(strlen(name)) + txt.chrout('\n') + txt.print_uwhex(strptr, 1) + txt.chrout('\n') + txt.print(strptr) + txt.chrout('\n') + txt.print_ub(strlen(strptr)) + txt.chrout('\n') }