diff --git a/compiler/src/prog8/ast/base/Base.kt b/compiler/src/prog8/ast/base/Base.kt index 3ee5c821b..a382b22ae 100644 --- a/compiler/src/prog8/ast/base/Base.kt +++ b/compiler/src/prog8/ast/base/Base.kt @@ -30,8 +30,8 @@ enum class DataType { UWORD -> targetType in setOf(UWORD, FLOAT) WORD -> targetType in setOf(WORD, FLOAT) FLOAT -> targetType == FLOAT - STR -> targetType == STR || targetType == UWORD - in ArrayDatatypes -> targetType == this || targetType == UWORD + STR -> targetType == STR + in ArrayDatatypes -> targetType == this else -> false } diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 71e78296f..e4227bd66 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -309,18 +309,21 @@ read the syntax reference on strings. .. hint:: - Strings and uwords (=memory address) can often be interchanged. + Strings/arrays and uwords (=memory address) can often be interchanged. An array of strings is actually an array of uwords where every element is the memory address of the string. You can pass a memory address to assembly functions that require a string as an argument. + For regular assignments you still need to use an explicit ``&`` (address-of) to take + the address of the string or array. .. caution:: - It's probably best to avoid changing strings after they've been created. This - includes changing certain letters by index, or by assigning a new value, or by + It's probably best to avoid changing the contents in strings and treat them as static. + This includes changing certain letters by index, or by assigning a new value, or by modifying the string via other means for example ``substr`` function and its cousins. - This is because if your program exits and is restarted (without loading it again), - it will then start working with the changed strings instead of the original ones! - The same is true for arrays. + This is because the changes persist in memory. If your program exits and is restarted + (without reloading it from disk), it will then start working with the modified strings + instead of the original ones! + The same is true for arrays! So be careful to (re)initialize them if needed. Structs diff --git a/examples/test.p8 b/examples/test.p8 index 5a6ae6e78..02c1d3998 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,11 +5,24 @@ main { sub start() { - uword foo = [1,2,3,4] ; TODO SYNTAX ERROR - uword bar = "sdfadsaf" ; TODO SYNTAX ERROR + uword foo + uword bar + uword[] arra = [1,2,3] + str nom = "omnom" + foo = &arra + foo++ + foo = &nom + foo++ + + ding(nom) + ding("sdfsdfd") txt.print("hello\n") } + + sub ding(uword ss) { + txt.print(ss) + } }