fix that memory("a b c", ...) produces invalid symbol

This commit is contained in:
Irmen de Jong 2022-01-02 16:11:53 +01:00
parent a23281afab
commit 7bc75fd220
5 changed files with 34 additions and 48 deletions

View File

@ -418,6 +418,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
throw AssemblyError("should not discard result of memory allocation at $fcall")
val nameRef = fcall.args[0] as IdentifierReference
val name = (nameRef.targetVarDecl(program)!!.value as StringLiteralValue).value
require(name.all { it.isLetterOrDigit() || it=='_' }) {"memory name should be a valid symbol name"}
val size = (fcall.args[1] as NumericLiteralValue).number.toUInt()
val existingSize = asmgen.slabs[name]

View File

@ -145,6 +145,19 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter,
val pos = (if(call.args.any()) call.args[0] else (call as Node)).position
errors.err("invalid number of arguments", pos)
}
if(func.name=="memory") {
val name = call.args[0] as? StringLiteralValue
if(name!=null) {
val processed = name.value.map {
if(it.isLetterOrDigit())
it
else
'_'
}.joinToString("")
call.args[0] = StringLiteralValue(processed, false, name.position)
call.args[0].linkParents(call as Node)
}
}
}
is Label -> {
if(call.args.isNotEmpty()) {

View File

@ -16,9 +16,11 @@ import prog8.compiler.printProgram
import prog8.codegen.target.C64Target
import prog8.compilerinterface.isIOAddress
import prog8.parser.SourceCode
import prog8tests.helpers.*
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer
import prog8tests.helpers.DummyStringEncoder
import prog8tests.helpers.compileText
class TestMemory: FunSpec({
@ -220,4 +222,15 @@ class TestMemory: FunSpec({
.addModule(module)
target.isIOAddress(C64Target.machine) shouldBe true
}
test("memory() with spaces in name works") {
val result = compileText(C64Target, false, """
main {
sub start() {
uword @shared mem = memory("a b c", 100)
}
}
""", writeAssembly = true).assertSuccess()
}
})

View File

@ -3,6 +3,8 @@ TODO
For next compiler release (7.6)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- fix that memory("name", ...) allocates a STR variable with contents "name" in the assembly source
- make it possible to inline non-asmsub routines that just contain a single statement (return, functioncall, assignment)
...
@ -21,6 +23,7 @@ Blocked by an official Commander-x16 r39 release
Future
^^^^^^
- add pipe operator ``|>`` ?
- make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as ``v_``
then we can get rid of the instruction lists in the machinedefinitions as well?
- fix the asm-labels problem (github issue #62)

View File

@ -1,57 +1,13 @@
%import textio
%import floats
%zeropage basicsafe
%option no_sysinit
main {
ubyte bb = 1
uword ww = 1
float ff = 1.111
str derp = "zzzz"
uword buffer = memory("the quick brown fox jumps over the lazy dog", 2000)
uword buffer2 = memory("the quick brown fox jumps over the lazy dog", 2000)
sub start() {
func()
txt.print(derp)
txt.print_uwhex(buffer, true)
txt.print_uwhex(buffer2, true)
}
sub func() {
ubyte fbb = 1
uword fww = 1
float fff = 1.111
txt.print_ub(fbb)
txt.spc()
txt.print_uw(fww)
txt.spc()
floats.print_f(fff)
txt.nl()
txt.print_ub(bb)
txt.spc()
txt.print_uw(ww)
txt.spc()
floats.print_f(ff)
txt.nl()
txt.print_ub(block2.bb)
txt.spc()
txt.print_uw(block2.ww)
txt.spc()
floats.print_f(block2.ff)
txt.nl()
fbb++
fww++
fff += 1.1
bb++
ww++
ff += 1.1
block2.bb++
block2.ww++
block2.ff += 1.1
}
}
block2 {
ubyte bb = 1
uword ww = 1
float ff = 1.111
}