mirror of
https://github.com/irmen/prog8.git
synced 2024-07-10 08:28:57 +00:00
fixed strlen() to work on arguments other than just a variable
This commit is contained in:
parent
add5bfa2ec
commit
43c0afdea0
@ -540,14 +540,21 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun funcStrlen(fcall: IFunctionCall, resultToStack: Boolean) {
|
private fun funcStrlen(fcall: IFunctionCall, resultToStack: Boolean) {
|
||||||
val name = asmgen.asmVariableName(fcall.args[0] as IdentifierReference)
|
if (fcall.args[0] is IdentifierReference) {
|
||||||
val type = fcall.args[0].inferType(program)
|
// use the address of the variable
|
||||||
when {
|
val name = asmgen.asmVariableName(fcall.args[0] as IdentifierReference)
|
||||||
type.istype(DataType.STR) -> asmgen.out(" lda #<$name | ldy #>$name")
|
val type = fcall.args[0].inferType(program)
|
||||||
type.istype(DataType.UWORD) -> asmgen.out(" lda $name | ldy $name+1")
|
when {
|
||||||
else -> throw AssemblyError("strlen requires str or uword arg")
|
type.istype(DataType.STR) -> asmgen.out(" lda #<$name | ldy #>$name")
|
||||||
|
type.istype(DataType.UWORD) -> asmgen.out(" lda $name | ldy $name+1")
|
||||||
|
else -> throw AssemblyError("strlen requires str or uword arg")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(resultToStack)
|
else {
|
||||||
|
// use the expression value as address of the string
|
||||||
|
asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY)
|
||||||
|
}
|
||||||
|
if (resultToStack)
|
||||||
asmgen.out(" jsr prog8_lib.func_strlen_stack")
|
asmgen.out(" jsr prog8_lib.func_strlen_stack")
|
||||||
else
|
else
|
||||||
asmgen.out(" jsr prog8_lib.func_strlen_into_A")
|
asmgen.out(" jsr prog8_lib.func_strlen_into_A")
|
||||||
|
@ -354,7 +354,7 @@ private fun builtinStrlen(args: List<Expression>, position: Position, program: P
|
|||||||
val argument=args[0]
|
val argument=args[0]
|
||||||
if(argument is StringLiteralValue)
|
if(argument is StringLiteralValue)
|
||||||
return NumericLiteralValue.optimalInteger(argument.value.length, argument.position)
|
return NumericLiteralValue.optimalInteger(argument.value.length, argument.position)
|
||||||
val vardecl = (argument as IdentifierReference).targetVarDecl(program.namespace)
|
val vardecl = (argument as? IdentifierReference)?.targetVarDecl(program.namespace)
|
||||||
if(vardecl!=null) {
|
if(vardecl!=null) {
|
||||||
if(vardecl.datatype!=DataType.STR && vardecl.datatype!=DataType.UWORD)
|
if(vardecl.datatype!=DataType.STR && vardecl.datatype!=DataType.UWORD)
|
||||||
throw SyntaxError("strlen must have string argument", position)
|
throw SyntaxError("strlen must have string argument", position)
|
||||||
|
Loading…
Reference in New Issue
Block a user