From 512ddd1694911f4255c95ec6ac67c0727ae3b04f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 20 Dec 2024 22:47:05 +0100 Subject: [PATCH] cleanups --- codeCore/src/prog8/code/SymbolTable.kt | 3 + codeCore/src/prog8/code/ast/AstBase.kt | 2 +- codeCore/src/prog8/code/core/Position.kt | 4 +- .../prog8/code/{core => source}/SourceCode.kt | 56 +++++-------------- .../src/prog8/code/source/SourceLineCache.kt | 33 +++++++++++ .../src/prog8/codegen/cpu6502/AsmGen.kt | 2 + codeGenCpu6502/test/TestCodegen.kt | 1 + codeGenIntermediate/test/TestVmCodeGen.kt | 1 + .../src/prog8/optimizer/UnusedCodeRemover.kt | 2 +- compiler/src/prog8/compiler/ModuleImporter.kt | 2 +- .../astprocessing/IntermediateAstMaker.kt | 6 +- .../astprocessing/VerifyFunctionArgTypes.kt | 3 +- compiler/test/ModuleImporterTests.kt | 4 +- compiler/test/TestCallgraph.kt | 2 +- .../TestImportedModulesOrderAndOptions.kt | 2 +- compiler/test/TestMemory.kt | 1 + compiler/test/TestSymbolTable.kt | 1 + compiler/test/ast/TestAstToSourceText.kt | 4 +- compiler/test/ast/TestIdentifierRef.kt | 2 +- compiler/test/ast/TestProg8Parser.kt | 6 +- compiler/test/ast/TestProgram.kt | 4 +- compiler/test/ast/TestSourceCode.kt | 13 ++--- compiler/test/ast/TestSubroutines.kt | 2 +- .../test/codegeneration/TestAsmGenSymbols.kt | 1 + compilerAst/src/prog8/ast/AstToplevel.kt | 1 + compilerAst/src/prog8/ast/Program.kt | 2 + compilerAst/src/prog8/ast/SymbolDumper.kt | 1 + .../src/prog8/ast/antlr/Antlr2Kotlin.kt | 1 + .../prog8/ast/expressions/AstExpressions.kt | 1 + compilerAst/src/prog8/parser/Prog8Parser.kt | 2 +- docs/source/todo.rst | 1 + docs/source/variables.rst | 11 ++-- examples/test.p8 | 21 ++----- .../src/prog8/intermediate/IRFileWriter.kt | 6 +- .../src/prog8/intermediate/IRSymbolTable.kt | 4 +- intermediate/src/prog8/intermediate/Utils.kt | 2 +- virtualmachine/src/prog8/vm/Memory.kt | 2 +- 37 files changed, 112 insertions(+), 100 deletions(-) rename codeCore/src/prog8/code/{core => source}/SourceCode.kt (77%) create mode 100644 codeCore/src/prog8/code/source/SourceLineCache.kt diff --git a/codeCore/src/prog8/code/SymbolTable.kt b/codeCore/src/prog8/code/SymbolTable.kt index 7757223a9..272d2f57e 100644 --- a/codeCore/src/prog8/code/SymbolTable.kt +++ b/codeCore/src/prog8/code/SymbolTable.kt @@ -6,6 +6,9 @@ import prog8.code.ast.PtProgram import prog8.code.core.* +const val internedStringsModuleName = "prog8_interned_strings" + + /** * Tree structure containing all symbol definitions in the program * (blocks, subroutines, variables (all types), memoryslabs, and labels). diff --git a/codeCore/src/prog8/code/ast/AstBase.kt b/codeCore/src/prog8/code/ast/AstBase.kt index fde73562b..76e2d7dfb 100644 --- a/codeCore/src/prog8/code/ast/AstBase.kt +++ b/codeCore/src/prog8/code/ast/AstBase.kt @@ -3,7 +3,7 @@ package prog8.code.ast import prog8.code.core.IMemSizer import prog8.code.core.IStringEncoding import prog8.code.core.Position -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode import java.nio.file.Path // New simplified AST for the code generator. diff --git a/codeCore/src/prog8/code/core/Position.kt b/codeCore/src/prog8/code/core/Position.kt index d749c0d1a..7527de6a9 100644 --- a/codeCore/src/prog8/code/core/Position.kt +++ b/codeCore/src/prog8/code/core/Position.kt @@ -1,6 +1,6 @@ package prog8.code.core -import prog8.code.core.SourceCode.Companion.LIBRARYFILEPREFIX +import prog8.code.source.SourceCode import java.nio.file.InvalidPathException import kotlin.io.path.Path import kotlin.io.path.absolute @@ -10,7 +10,7 @@ data class Position(val file: String, val line: Int, val startCol: Int, val endC fun toClickableStr(): String { if(this===DUMMY) return "" - if(file.startsWith(LIBRARYFILEPREFIX)) + if(SourceCode.isLibraryResource(file)) return "$file:$line:$startCol:" return try { val path = Path(file).absolute().normalize().toString() diff --git a/codeCore/src/prog8/code/core/SourceCode.kt b/codeCore/src/prog8/code/source/SourceCode.kt similarity index 77% rename from codeCore/src/prog8/code/core/SourceCode.kt rename to codeCore/src/prog8/code/source/SourceCode.kt index 3dc52b9f6..d8d2de72c 100644 --- a/codeCore/src/prog8/code/core/SourceCode.kt +++ b/codeCore/src/prog8/code/source/SourceCode.kt @@ -1,16 +1,11 @@ -package prog8.code.core +package prog8.code.source -import java.io.File import java.io.IOException import java.nio.file.Path import java.text.Normalizer import kotlin.io.path.Path import kotlin.io.path.readText - -const val internedStringsModuleName = "prog8_interned_strings" - - /** * Encapsulates - and ties together - actual source code (=text) and its [origin]. */ @@ -55,14 +50,21 @@ sealed class SourceCode { /** * filename prefix to designate library files that will be retreived from internal resources rather than disk */ - const val LIBRARYFILEPREFIX = "library:" - const val STRINGSOURCEPREFIX = "string:" + private const val LIBRARYFILEPREFIX = "library:" + private const val STRINGSOURCEPREFIX = "string:" val curdir: Path = Path(".").toAbsolutePath() fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath()) - fun isRegularFilesystemPath(pathString: String) = - !(pathString.startsWith(LIBRARYFILEPREFIX) || pathString.startsWith(STRINGSOURCEPREFIX)) - + fun isRegularFilesystemPath(pathString: String) = !isLibraryResource(pathString) && !isStringResource(pathString) fun isLibraryResource(path: String) = path.startsWith(LIBRARYFILEPREFIX) + fun isStringResource(path: String) = path.startsWith(STRINGSOURCEPREFIX) + fun withoutPrefix(path: String): String { + return if(isLibraryResource(path)) + path.removePrefix(LIBRARYFILEPREFIX) + else if(isStringResource(path)) + path.removePrefix(STRINGSOURCEPREFIX) + else + path + } } /** @@ -124,7 +126,7 @@ sealed class SourceCode { if (rscURL == null) { val rscRoot = object {}.javaClass.getResource("/") throw NoSuchFileException( - File(normalized), + java.io.File(normalized), reason = "looked in resources rooted at $rscRoot" ) } @@ -144,34 +146,4 @@ sealed class SourceCode { override val origin: String = name override val text: String = "" } -} - - -object SourceLineCache { - private val cache = mutableMapOf>() - - private fun getCachedFile(file: String): List { - val existing = cache[file] - if(existing!=null) - return existing - if (SourceCode.isRegularFilesystemPath(file)) { - val source = SourceCode.File(Path(file)) - cache[file] = source.text.split('\n', '\r').map { it.trim() } - return cache.getValue(file) - } else if(file.startsWith(SourceCode.LIBRARYFILEPREFIX)) { - val source = SourceCode.Resource(file.drop(SourceCode.LIBRARYFILEPREFIX.length)) - cache[file] = source.text.split('\n', '\r').map { it.trim()} - return cache.getValue(file) - } - return emptyList() - } - - fun retrieveLine(position: Position): String? { - if (position.line>0) { - val lines = getCachedFile(position.file) - if(lines.isNotEmpty()) - return lines[position.line-1] - } - return null - } } \ No newline at end of file diff --git a/codeCore/src/prog8/code/source/SourceLineCache.kt b/codeCore/src/prog8/code/source/SourceLineCache.kt new file mode 100644 index 000000000..8c5dd513e --- /dev/null +++ b/codeCore/src/prog8/code/source/SourceLineCache.kt @@ -0,0 +1,33 @@ +package prog8.code.source + +import prog8.code.core.Position +import kotlin.io.path.Path + +object SourceLineCache { + private val cache = mutableMapOf>() + + private fun getCachedFile(file: String): List { + val existing = cache[file] + if(existing!=null) + return existing + if (SourceCode.isRegularFilesystemPath(file)) { + val source = SourceCode.File(Path(file)) + cache[file] = source.text.split('\n', '\r').map { it.trim() } + return cache.getValue(file) + } else if(SourceCode.isLibraryResource(file)) { + val source = SourceCode.Resource(SourceCode.withoutPrefix(file)) + cache[file] = source.text.split('\n', '\r').map { it.trim()} + return cache.getValue(file) + } + return emptyList() + } + + fun retrieveLine(position: Position): String? { + if (position.line>0) { + val lines = getCachedFile(position.file) + if(lines.isNotEmpty()) + return lines[position.line-1] + } + return null + } +} \ No newline at end of file diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index e15dbdbac..8292e1b3f 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -7,6 +7,8 @@ import prog8.code.SymbolTable import prog8.code.SymbolTableMaker import prog8.code.ast.* import prog8.code.core.* +import prog8.code.source.SourceCode +import prog8.code.source.SourceLineCache import prog8.code.target.Cx16Target import prog8.codegen.cpu6502.assignment.* import kotlin.io.path.Path diff --git a/codeGenCpu6502/test/TestCodegen.kt b/codeGenCpu6502/test/TestCodegen.kt index d4e836c45..bafe3f760 100644 --- a/codeGenCpu6502/test/TestCodegen.kt +++ b/codeGenCpu6502/test/TestCodegen.kt @@ -8,6 +8,7 @@ import io.kotest.matchers.shouldBe import prog8.code.SymbolTableMaker import prog8.code.ast.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.code.target.C64Target import prog8.codegen.cpu6502.AsmGen6502 import java.nio.file.Files diff --git a/codeGenIntermediate/test/TestVmCodeGen.kt b/codeGenIntermediate/test/TestVmCodeGen.kt index a82547763..3a19e49b7 100644 --- a/codeGenIntermediate/test/TestVmCodeGen.kt +++ b/codeGenIntermediate/test/TestVmCodeGen.kt @@ -4,6 +4,7 @@ import io.kotest.matchers.shouldBe import prog8.code.SymbolTableMaker import prog8.code.ast.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.code.target.VMTarget import prog8.codegen.vm.VmAssemblyProgram import prog8.codegen.vm.VmCodeGen diff --git a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt index 150da2152..740dec3f6 100644 --- a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt @@ -10,7 +10,7 @@ import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification import prog8.code.core.ICompilationTarget import prog8.code.core.IErrorReporter -import prog8.code.core.internedStringsModuleName +import prog8.code.internedStringsModuleName import prog8.compiler.CallGraph diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index c09ff13fc..aef9d28f1 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -8,7 +8,7 @@ import prog8.ast.statements.Directive import prog8.ast.statements.DirectiveArg import prog8.code.core.IErrorReporter import prog8.code.core.Position -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode import prog8.parser.Prog8Parser import java.io.File import java.nio.file.Path diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 7f25fa1b4..0dd99891b 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -10,6 +10,7 @@ import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.code.ast.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.compiler.builtinFunctionReturnType import java.io.File import kotlin.io.path.Path @@ -765,9 +766,10 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr private fun loadAsmIncludeFile(filename: String, source: SourceCode): Result { - return if (filename.startsWith(SourceCode.LIBRARYFILEPREFIX)) { + return if (SourceCode.isLibraryResource(filename)) { return com.github.michaelbull.result.runCatching { - SourceCode.Resource("/prog8lib/${filename.substring(SourceCode.LIBRARYFILEPREFIX.length)}").text + val physFilename = SourceCode.withoutPrefix(filename) + SourceCode.Resource("/prog8lib/$physFilename").text }.mapError { NoSuchFileException(File(filename)) } } else { val sib = Path(source.origin).resolveSibling(filename) diff --git a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt index 4e6a1c33e..aa43db879 100644 --- a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt +++ b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt @@ -6,6 +6,7 @@ import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.code.core.* +import prog8.code.internedStringsModuleName internal class VerifyFunctionArgTypes(val program: Program, val options: CompilationOptions, val errors: IErrorReporter) : IAstVisitor { @@ -21,7 +22,7 @@ internal class VerifyFunctionArgTypes(val program: Program, val options: Compila } // remove unused strings from interned strings block - val internedBlock = program.allBlocks.singleOrNull { it.name== internedStringsModuleName } + val internedBlock = program.allBlocks.singleOrNull { it.name == internedStringsModuleName } internedBlock?.statements?.withIndex()?.reversed()?.forEach { (index, st) -> if(st is VarDecl && st.scopedName !in allStringRefs) { internedBlock.statements.removeAt(index) diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index 9dc63d68a..acc58b9d0 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -11,8 +11,8 @@ import io.kotest.matchers.shouldBe import io.kotest.matchers.string.shouldContain import prog8.ast.Program import prog8.code.core.IErrorReporter -import prog8.code.core.SourceCode -import prog8.code.core.internedStringsModuleName +import prog8.code.source.SourceCode +import prog8.code.internedStringsModuleName import prog8.compiler.ModuleImporter import prog8.parser.ParseError import prog8tests.helpers.* diff --git a/compiler/test/TestCallgraph.kt b/compiler/test/TestCallgraph.kt index 63e626b03..9a351d214 100644 --- a/compiler/test/TestCallgraph.kt +++ b/compiler/test/TestCallgraph.kt @@ -10,7 +10,7 @@ import io.kotest.matchers.string.shouldContain import prog8.ast.Program import prog8.ast.statements.Block import prog8.ast.statements.Subroutine -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode import prog8.code.target.C64Target import prog8.code.target.VMTarget import prog8.compiler.CallGraph diff --git a/compiler/test/TestImportedModulesOrderAndOptions.kt b/compiler/test/TestImportedModulesOrderAndOptions.kt index 3f9420c47..b94de0008 100644 --- a/compiler/test/TestImportedModulesOrderAndOptions.kt +++ b/compiler/test/TestImportedModulesOrderAndOptions.kt @@ -7,7 +7,7 @@ import io.kotest.matchers.shouldNotBe import io.kotest.matchers.string.shouldContain import io.kotest.matchers.string.shouldStartWith import prog8.code.core.ZeropageType -import prog8.code.core.internedStringsModuleName +import prog8.code.internedStringsModuleName import prog8.code.target.C64Target import prog8.code.target.Cx16Target import prog8.code.target.VMTarget diff --git a/compiler/test/TestMemory.kt b/compiler/test/TestMemory.kt index 006486d58..f3816a12f 100644 --- a/compiler/test/TestMemory.kt +++ b/compiler/test/TestMemory.kt @@ -13,6 +13,7 @@ import prog8.ast.expressions.NumericLiteral import prog8.ast.expressions.PrefixExpression import prog8.ast.statements.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.code.target.* import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer diff --git a/compiler/test/TestSymbolTable.kt b/compiler/test/TestSymbolTable.kt index f63482d2c..8dc6d30ac 100644 --- a/compiler/test/TestSymbolTable.kt +++ b/compiler/test/TestSymbolTable.kt @@ -8,6 +8,7 @@ import io.kotest.matchers.types.shouldBeSameInstanceAs import prog8.code.* import prog8.code.ast.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8tests.helpers.DummyMemsizer import prog8tests.helpers.DummyStringEncoder diff --git a/compiler/test/ast/TestAstToSourceText.kt b/compiler/test/ast/TestAstToSourceText.kt index 26ae9ca2e..debfd359d 100644 --- a/compiler/test/ast/TestAstToSourceText.kt +++ b/compiler/test/ast/TestAstToSourceText.kt @@ -6,8 +6,8 @@ import io.kotest.matchers.string.shouldContain import prog8.ast.AstToSourceTextConverter import prog8.ast.Module import prog8.ast.Program -import prog8.code.core.SourceCode -import prog8.code.core.internedStringsModuleName +import prog8.code.source.SourceCode +import prog8.code.internedStringsModuleName import prog8.parser.ParseError import prog8.parser.Prog8Parser.parseModule import prog8tests.helpers.DummyFunctions diff --git a/compiler/test/ast/TestIdentifierRef.kt b/compiler/test/ast/TestIdentifierRef.kt index 0ecd10ad7..295e01177 100644 --- a/compiler/test/ast/TestIdentifierRef.kt +++ b/compiler/test/ast/TestIdentifierRef.kt @@ -13,7 +13,7 @@ import prog8.ast.statements.Block import prog8.ast.statements.Subroutine import prog8.ast.statements.VarDecl import prog8.code.core.Position -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode import prog8.parser.Prog8Parser import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index 98801de0b..3706a5192 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -9,7 +9,6 @@ import io.kotest.matchers.or import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import io.kotest.matchers.string.shouldContain -import io.kotest.matchers.string.shouldStartWith import io.kotest.matchers.types.instanceOf import prog8.ast.IFunctionCall import prog8.ast.Module @@ -18,6 +17,7 @@ import prog8.ast.Program import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.code.target.C64Target import prog8.code.target.VMTarget import prog8.code.target.encodings.PetsciiEncoding @@ -379,7 +379,7 @@ class TestProg8Parser: FunSpec( { """.trimIndent() val module = parseModule(SourceCode.Text(srcText)) assertSomethingForAllNodes(module) { - it.position.file shouldStartWith SourceCode.STRINGSOURCEPREFIX + SourceCode.isStringResource(it.position.file) shouldBe true } } @@ -388,7 +388,7 @@ class TestProg8Parser: FunSpec( { val resource = SourceCode.Resource("prog8lib/math.p8") val module = parseModule(resource) assertSomethingForAllNodes(module) { - it.position.file shouldStartWith SourceCode.LIBRARYFILEPREFIX + SourceCode.isLibraryResource(it.position.file) shouldBe true } } } diff --git a/compiler/test/ast/TestProgram.kt b/compiler/test/ast/TestProgram.kt index 0a309eeca..550f0d26f 100644 --- a/compiler/test/ast/TestProgram.kt +++ b/compiler/test/ast/TestProgram.kt @@ -14,8 +14,8 @@ import prog8.ast.Program import prog8.ast.statements.Block import prog8.code.ast.PtBlock import prog8.code.core.Position -import prog8.code.core.SourceCode -import prog8.code.core.internedStringsModuleName +import prog8.code.source.SourceCode +import prog8.code.internedStringsModuleName import prog8.code.target.C64Target import prog8tests.helpers.DummyFunctions import prog8tests.helpers.DummyMemsizer diff --git a/compiler/test/ast/TestSourceCode.kt b/compiler/test/ast/TestSourceCode.kt index 2bebb0df9..4d73d226d 100644 --- a/compiler/test/ast/TestSourceCode.kt +++ b/compiler/test/ast/TestSourceCode.kt @@ -5,8 +5,7 @@ import io.kotest.core.spec.style.AnnotationSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import io.kotest.matchers.string.shouldContain -import prog8.code.core.SourceCode -import prog8.code.core.SourceCode.Companion.LIBRARYFILEPREFIX +import prog8.code.source.SourceCode import prog8tests.helpers.* import kotlin.io.path.Path @@ -99,7 +98,7 @@ class TestSourceCode: AnnotationSpec() { val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.Resource(pathString) - src.origin shouldBe "$LIBRARYFILEPREFIX/$pathString" + src.origin shouldBe "library:/$pathString" src.text shouldBe normalizeLineEndings(srcFile.readText()) src.isFromResources shouldBe true src.isFromFilesystem shouldBe false @@ -111,7 +110,7 @@ class TestSourceCode: AnnotationSpec() { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.Resource(pathString) - src.origin shouldBe "$LIBRARYFILEPREFIX$pathString" + src.origin shouldBe "library:$pathString" src.text shouldBe normalizeLineEndings(srcFile.readText()) } @@ -121,7 +120,7 @@ class TestSourceCode: AnnotationSpec() { val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.Resource(pathString) - src.origin shouldBe "$LIBRARYFILEPREFIX/$pathString" + src.origin shouldBe "library:/$pathString" src.text shouldBe normalizeLineEndings(srcFile.readText()) src.isFromResources shouldBe true } @@ -132,7 +131,7 @@ class TestSourceCode: AnnotationSpec() { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.Resource(pathString) - src.origin shouldBe "$LIBRARYFILEPREFIX$pathString" + src.origin shouldBe "library:$pathString" src.text shouldBe normalizeLineEndings(srcFile.readText()) } @@ -142,7 +141,7 @@ class TestSourceCode: AnnotationSpec() { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.Resource(pathString) - src.origin shouldBe "$LIBRARYFILEPREFIX/prog8lib/math.p8" + src.origin shouldBe "library:/prog8lib/math.p8" src.text shouldBe normalizeLineEndings(srcFile.readText()) src.isFromResources shouldBe true } diff --git a/compiler/test/ast/TestSubroutines.kt b/compiler/test/ast/TestSubroutines.kt index d2b8e1719..a87da84c6 100644 --- a/compiler/test/ast/TestSubroutines.kt +++ b/compiler/test/ast/TestSubroutines.kt @@ -10,7 +10,7 @@ import prog8.code.ast.PtAssignTarget import prog8.code.ast.PtAssignment import prog8.code.core.BaseDataType import prog8.code.core.DataType -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode import prog8.code.target.C64Target import prog8.code.target.Cx16Target import prog8.parser.Prog8Parser.parseModule diff --git a/compiler/test/codegeneration/TestAsmGenSymbols.kt b/compiler/test/codegeneration/TestAsmGenSymbols.kt index a65f5b43d..842981121 100644 --- a/compiler/test/codegeneration/TestAsmGenSymbols.kt +++ b/compiler/test/codegeneration/TestAsmGenSymbols.kt @@ -15,6 +15,7 @@ import prog8.code.ast.PtAssignment import prog8.code.ast.PtIdentifier import prog8.code.ast.PtProgram import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.code.target.C64Target import prog8.code.target.VMTarget import prog8.codegen.cpu6502.AsmGen6502Internal diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index 107d1fad1..c32c2c907 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -9,6 +9,7 @@ import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstVisitor import prog8.code.core.* +import prog8.code.source.SourceCode object ParentSentinel : Node { diff --git a/compilerAst/src/prog8/ast/Program.kt b/compilerAst/src/prog8/ast/Program.kt index 59ff9d929..319f9342c 100644 --- a/compilerAst/src/prog8/ast/Program.kt +++ b/compilerAst/src/prog8/ast/Program.kt @@ -7,6 +7,8 @@ import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.code.ast.PtLabel import prog8.code.core.* +import prog8.code.internedStringsModuleName +import prog8.code.source.SourceCode /*********** Everything starts from here, the Program; zero or more modules *************/ diff --git a/compilerAst/src/prog8/ast/SymbolDumper.kt b/compilerAst/src/prog8/ast/SymbolDumper.kt index 0111da6af..88b7880e8 100644 --- a/compilerAst/src/prog8/ast/SymbolDumper.kt +++ b/compilerAst/src/prog8/ast/SymbolDumper.kt @@ -4,6 +4,7 @@ import prog8.ast.expressions.NumericLiteral import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.code.core.* +import prog8.code.source.SourceCode import java.io.PrintStream diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index d0e7a6382..7c1e69535 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -8,6 +8,7 @@ import prog8.ast.base.SyntaxError import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.code.core.* +import prog8.code.source.SourceCode import prog8.parser.Prog8ANTLRParser.* import kotlin.io.path.Path import kotlin.io.path.isRegularFile diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index a24b80234..0ff19581d 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -9,6 +9,7 @@ import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstVisitor import prog8.code.core.* +import prog8.code.internedStringsModuleName import prog8.code.target.encodings.JapaneseCharacterConverter import java.io.CharConversionException import java.util.* diff --git a/compilerAst/src/prog8/parser/Prog8Parser.kt b/compilerAst/src/prog8/parser/Prog8Parser.kt index 387c3d196..130068495 100644 --- a/compilerAst/src/prog8/parser/Prog8Parser.kt +++ b/compilerAst/src/prog8/parser/Prog8Parser.kt @@ -6,7 +6,7 @@ import prog8.ast.antlr.toAst import prog8.ast.statements.Block import prog8.ast.statements.Directive import prog8.code.core.Position -import prog8.code.core.SourceCode +import prog8.code.source.SourceCode class ParseError(override var message: String, val position: Position, cause: RuntimeException): Exception(message, cause) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index efc357a1f..09c76b4d1 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -20,6 +20,7 @@ TODO Future Things and Ideas ^^^^^^^^^^^^^^^^^^^^^^^ +- SourceLineCache should be removed as a separate thing, it reloads all source files again and splits them by line. It should re-use the already loaded Sources. Wrap it all in a ImporterFileSystem? - Compiling Libraries: improve ability to create library files in prog8; for instance there's still stuff injected into the start of the start() routine AND there is separate setup logic going on before calling it. Make up our mind! Maybe all setup does need to be put into start() ? because the program cannot function correctly when the variables aren't initialized properly bss is not cleared etc. etc. Add a -library $xxxx command line option (and/or some directive) to preselect every setting that is required to make a library at $xxxx rather than a normal loadable and runnable program? diff --git a/docs/source/variables.rst b/docs/source/variables.rst index 91a8df8ec..8a1848573 100644 --- a/docs/source/variables.rst +++ b/docs/source/variables.rst @@ -372,9 +372,10 @@ but they have some special properties because they are considered to be *text*. Strings (without encoding prefix) will be encoded (translated from ASCII/UTF-8) into bytes via the *default encoding* for the target platform. On the CBM machines, this is CBM PETSCII. -Strings in the default encoding are stored in the machine's default character encoding (which is PETSCII on the CBM machines). -You can choose to store it in another encodings such as ``sc`` (screencodes) or ``iso`` (iso-8859-15). -Here are examples of the possible encodings: +Strings without an encoding prefix are stored in the machine's default character encoding (which is PETSCII on the CBM machines, +but can be something else on other targets). +There are ways to change the encoding: prefix the string with an encoding name, or use the ``%encoding`` directive to +change it for the whole file at once. Here are examples of the possible encodings: - ``"hello"`` a string translated into the default character encoding (PETSCII on the CBM machines) - ``petscii:"hello"`` string in CBM PETSCII encoding @@ -386,9 +387,9 @@ Here are examples of the possible encodings: - ``cp437:"≈ IBM Pc ≈ ♂♀♪☺¶"`` string in "cp437" encoding (IBM PC codepage 437) - ``kata:"アノ ニホンジン ワ ガイコクジン。 # が # ガ"`` string in "kata" encoding (Katakana) -So the following is a string literal that will be encoded into memory bytes using the iso encoding. +So what follows below is a string literal that will be encoded into memory bytes using the iso encoding. It can be correctly displayed on the screen only if a iso-8859-15 charset has been activated first -(the Commander X16 has this feature built in):: +(the Commander X16 has this capability):: iso:"Käse, Straße" diff --git a/examples/test.p8 b/examples/test.p8 index 058f56f05..e8c414a6e 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,22 +1,9 @@ -%import textio -%zeropage basicsafe -%option no_sysinit +%import compression main { sub start() { - ubyte[] array = [ - 11, - 22, - 33, - 44, - ] - for cx16.r0L in [1,2,3,4,] { - txt.print_ub(cx16.r0L) - txt.nl() - } - for cx16.r0L in array { - txt.print_ub(cx16.r0L) - txt.nl() - } + ; compression.decode_rle(0,0,0) + ; compression.decode_zx0(0,0) + compression.decode_tscrunch(0,0) } } diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index 515c3f801..6ecef4b8f 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -2,8 +2,8 @@ package prog8.intermediate import prog8.code.core.InternalCompilerException import prog8.code.core.Position -import prog8.code.core.SourceLineCache import prog8.code.core.toHex +import prog8.code.source.SourceLineCache import java.nio.file.Path import javax.xml.stream.XMLOutputFactory import javax.xml.stream.XMLStreamWriter @@ -102,12 +102,12 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { is IRSubroutine -> { xml.writeStartElement("SUB") xml.writeAttribute("NAME", child.label) - xml.writeAttribute("RETURNTYPE", child.returnType?.typeString(null)?.lowercase() ?: "") + xml.writeAttribute("RETURNTYPE", child.returnType?.irTypeString(null)?.lowercase() ?: "") xml.writeAttribute("POS", child.position.toString()) xml.writeCharacters("\n") xml.writeStartElement("PARAMS") xml.writeCharacters("\n") - child.parameters.forEach { param -> xml.writeCharacters("${param.dt.typeString(null)} ${param.name}\n") } + child.parameters.forEach { param -> xml.writeCharacters("${param.dt.irTypeString(null)} ${param.name}\n") } xml.writeEndElement() xml.writeCharacters("\n") child.chunks.forEach { chunk -> diff --git a/intermediate/src/prog8/intermediate/IRSymbolTable.kt b/intermediate/src/prog8/intermediate/IRSymbolTable.kt index 76c50690a..7e063b69b 100644 --- a/intermediate/src/prog8/intermediate/IRSymbolTable.kt +++ b/intermediate/src/prog8/intermediate/IRSymbolTable.kt @@ -169,7 +169,7 @@ class IRStMemVar(name: String, require(!dt.isString) } - val typeString: String = dt.typeString(length) + val typeString: String = dt.irTypeString(length) } class IRStMemorySlab( @@ -219,7 +219,7 @@ class IRStStaticVariable(name: String, val uninitialized = onetimeInitializationArrayValue==null && onetimeInitializationStringValue==null && onetimeInitializationNumericValue==null - val typeString: String = dt.typeString(length) + val typeString: String = dt.irTypeString(length) } class IRStArrayElement(val bool: Boolean?, val number: Double?, val addressOfSymbol: String?) { diff --git a/intermediate/src/prog8/intermediate/Utils.kt b/intermediate/src/prog8/intermediate/Utils.kt index d667892d5..bf2841cdf 100644 --- a/intermediate/src/prog8/intermediate/Utils.kt +++ b/intermediate/src/prog8/intermediate/Utils.kt @@ -6,7 +6,7 @@ import prog8.code.left import prog8.code.right -fun DataType.typeString(length: Int?): String { +fun DataType.irTypeString(length: Int?): String { val lengthStr = if(length==0) "" else length.toString() return when (this.base) { BaseDataType.BOOL -> "bool" diff --git a/virtualmachine/src/prog8/vm/Memory.kt b/virtualmachine/src/prog8/vm/Memory.kt index 62750ed92..387d3a435 100644 --- a/virtualmachine/src/prog8/vm/Memory.kt +++ b/virtualmachine/src/prog8/vm/Memory.kt @@ -78,7 +78,7 @@ class Memory { return Double.fromBits(bits) } - // for now, no LONG 32-bits and no FLOAT support. +// for now, no LONG 32-bits support // fun getL(address: Int): UInt { // return mem[address+3] + 256u*mem[address+2] + 65536u*mem[address+1] + 16777216u*mem[address] // }