+ temporarily add PetsciiEncoding (and Petscii.kt copied from compiler) to parser; .linkParents for child nodes of Module

This commit is contained in:
meisl 2021-07-03 16:49:46 +02:00
parent 4eb61529f6
commit 46911a8905
4 changed files with 1217 additions and 14 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,24 @@
package prog8.parser
import prog8.ast.IStringEncoding
import java.io.CharConversionException
/**
* TODO: remove once [IStringEncoding] has been to compiler module
*/
object PetsciiEncoding : IStringEncoding {
override fun encodeString(str: String, altEncoding: Boolean) =
try {
if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
} catch (x: CharConversionException) {
throw CharConversionException("can't convert string to target machine's char encoding: ${x.message}")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean) =
try {
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
} catch (x: CharConversionException) {
throw CharConversionException("can't decode string: ${x.message}")
}
}

View File

@ -4,20 +4,8 @@ import org.antlr.v4.runtime.*
import org.antlr.v4.runtime.misc.ParseCancellationException
import prog8.ast.antlr.toAst
import prog8.ast.Module
import prog8.ast.base.Position
import prog8.ast.IStringEncoding
object DummyEncoding: IStringEncoding {
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
TODO("move StringEncoding out of compilerAst")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
TODO("move StringEncoding out of compilerAst")
}
}
class Prog8ErrorStrategy: BailErrorStrategy() {
override fun recover(recognizer: Parser?, e: RecognitionException?) {
try {
@ -56,10 +44,14 @@ class Prog8Parser(private val errorListener: ANTLRErrorListener = ThrowErrorList
parser.addErrorListener(errorListener)
val parseTree = parser.module()
val moduleName = "anonymous"
val module = parseTree.toAst(moduleName, pathFrom(""), PetsciiEncoding)
// TODO: use Module ctor directly
val moduleAst = parseTree.toAst("anonymous", pathFrom(""), DummyEncoding)
return moduleAst
for (statement in module.statements) {
statement.linkParents(module)
}
return module
}
}

View File

@ -0,0 +1,16 @@
package prog8.parser
import prog8.ast.IStringEncoding
/**
* TODO: remove once [IStringEncoding] has been to compiler module
*/
object ThrowTodoEncoding: IStringEncoding {
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
TODO("move StringEncoding out of compilerAst")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
TODO("move StringEncoding out of compilerAst")
}
}