mirror of
https://github.com/irmen/prog8.git
synced 2025-02-18 05:30:34 +00:00
-/* remove IStringEncoding as param in compilerAst, and all other uses that were only because of that.
For good measure we also turn on *all* compiler tests with examples (they do take some time). Note that the total *mentions* of IStringEncoding in the entire project went down from ~50 to 6, only 3 of which are *actual uses* (the others are 2 imports and 1 supertype ref in ICompilationTarget : IStringEncoding)!
This commit is contained in:
parent
de92740e87
commit
db76c8d7f4
@ -1,6 +1,9 @@
|
|||||||
package prog8.compiler
|
package prog8.compiler
|
||||||
|
|
||||||
import prog8.ast.*
|
import prog8.ast.AstToSourceCode
|
||||||
|
import prog8.ast.IBuiltinFunctions
|
||||||
|
import prog8.ast.IMemSizer
|
||||||
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.AstException
|
import prog8.ast.base.AstException
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.ast.expressions.Expression
|
import prog8.ast.expressions.Expression
|
||||||
@ -171,13 +174,12 @@ private fun parseImports(filepath: Path,
|
|||||||
errors: IErrorReporter,
|
errors: IErrorReporter,
|
||||||
compTarget: ICompilationTarget,
|
compTarget: ICompilationTarget,
|
||||||
libdirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
|
libdirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
|
||||||
val compilationTargetName = compTarget.name
|
println("Compiler target: ${compTarget.name}. Parsing...")
|
||||||
println("Compiler target: $compilationTargetName. Parsing...")
|
|
||||||
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
|
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
|
||||||
val programAst = Program(moduleName(filepath.fileName), mutableListOf(), bf, compTarget)
|
val programAst = Program(moduleName(filepath.fileName), mutableListOf(), bf, compTarget)
|
||||||
bf.program = programAst
|
bf.program = programAst
|
||||||
|
|
||||||
val importer = ModuleImporter(programAst, compTarget, compilationTargetName, libdirs)
|
val importer = ModuleImporter(programAst, compTarget.name, libdirs)
|
||||||
importer.importModule(filepath)
|
importer.importModule(filepath)
|
||||||
errors.report()
|
errors.report()
|
||||||
|
|
||||||
@ -190,7 +192,7 @@ private fun parseImports(filepath: Path,
|
|||||||
throw ParsingFailedError("${programAst.modules.first().position} BASIC launcher requires output type PRG.")
|
throw ParsingFailedError("${programAst.modules.first().position} BASIC launcher requires output type PRG.")
|
||||||
|
|
||||||
// depending on the machine and compiler options we may have to include some libraries
|
// depending on the machine and compiler options we may have to include some libraries
|
||||||
for(lib in compTarget.machine.importLibs(compilerOptions, compilationTargetName))
|
for(lib in compTarget.machine.importLibs(compilerOptions, compTarget.name))
|
||||||
importer.importLibraryModule(lib)
|
importer.importLibraryModule(lib)
|
||||||
|
|
||||||
// always import prog8_lib and math
|
// always import prog8_lib and math
|
||||||
@ -266,7 +268,7 @@ private fun processAst(programAst: Program, errors: IErrorReporter, compilerOpti
|
|||||||
programAst.checkIdentifiers(errors, compilerOptions)
|
programAst.checkIdentifiers(errors, compilerOptions)
|
||||||
errors.report()
|
errors.report()
|
||||||
// TODO: turning char literals into UBYTEs via an encoding should really happen in code gen - but for that we'd need DataType.CHAR
|
// TODO: turning char literals into UBYTEs via an encoding should really happen in code gen - but for that we'd need DataType.CHAR
|
||||||
programAst.charLiteralsToUByteLiterals(errors, compilerOptions.compTarget as IStringEncoding)
|
programAst.charLiteralsToUByteLiterals(errors, compilerOptions.compTarget)
|
||||||
errors.report()
|
errors.report()
|
||||||
programAst.constantFold(errors, compilerOptions.compTarget)
|
programAst.constantFold(errors, compilerOptions.compTarget)
|
||||||
errors.report()
|
errors.report()
|
||||||
|
@ -9,11 +9,10 @@ import prog8.ast.statements.ForLoop
|
|||||||
import prog8.ast.statements.VarDecl
|
import prog8.ast.statements.VarDecl
|
||||||
import prog8.ast.walk.AstWalker
|
import prog8.ast.walk.AstWalker
|
||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
import prog8.compiler.target.ICompilationTarget
|
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
|
|
||||||
|
|
||||||
internal class ConstantFoldingOptimizer(private val program: Program, private val compTarget: ICompilationTarget) : AstWalker() {
|
internal class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
|
||||||
|
|
||||||
override fun before(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
|
override fun before(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
|
||||||
// @( &thing ) --> thing
|
// @( &thing ) --> thing
|
||||||
|
@ -21,7 +21,7 @@ internal fun Program.constantFold(errors: IErrorReporter, compTarget: ICompilati
|
|||||||
if(errors.noErrors()) {
|
if(errors.noErrors()) {
|
||||||
valuetypefixer.applyModifications()
|
valuetypefixer.applyModifications()
|
||||||
|
|
||||||
val optimizer = ConstantFoldingOptimizer(this, compTarget)
|
val optimizer = ConstantFoldingOptimizer(this)
|
||||||
optimizer.visit(this)
|
optimizer.visit(this)
|
||||||
while (errors.noErrors() && optimizer.applyModifications() > 0) {
|
while (errors.noErrors() && optimizer.applyModifications() > 0) {
|
||||||
optimizer.visit(this)
|
optimizer.visit(this)
|
||||||
|
@ -6,7 +6,6 @@ import java.nio.file.Path
|
|||||||
|
|
||||||
import prog8.ast.IBuiltinFunctions
|
import prog8.ast.IBuiltinFunctions
|
||||||
import prog8.ast.IMemSizer
|
import prog8.ast.IMemSizer
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
import prog8.ast.base.DataType
|
import prog8.ast.base.DataType
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.ast.expressions.Expression
|
import prog8.ast.expressions.Expression
|
||||||
@ -127,16 +126,6 @@ fun <A, B, C, D, R> mapCombinations(dim1: Iterable<A>, dim2: Iterable<B>, dim3:
|
|||||||
}.toList()
|
}.toList()
|
||||||
|
|
||||||
|
|
||||||
val DummyEncoding = object : IStringEncoding {
|
|
||||||
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
|
|
||||||
throw Exception("just a dummy - should not be called")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
|
|
||||||
throw Exception("just a dummy - should not be called")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val DummyFunctions = object : IBuiltinFunctions {
|
val DummyFunctions = object : IBuiltinFunctions {
|
||||||
override val names: Set<String> = emptySet()
|
override val names: Set<String> = emptySet()
|
||||||
override val purefunctionNames: Set<String> = emptySet()
|
override val purefunctionNames: Set<String> = emptySet()
|
||||||
|
@ -54,7 +54,7 @@ class TestCompilerOnExamples {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@TestFactory
|
@TestFactory
|
||||||
@Disabled
|
// @Disabled
|
||||||
fun bothCx16AndC64() = mapCombinations(
|
fun bothCx16AndC64() = mapCombinations(
|
||||||
dim1 = listOf(
|
dim1 = listOf(
|
||||||
"animals",
|
"animals",
|
||||||
|
@ -2,26 +2,16 @@ package prog8.ast.antlr
|
|||||||
|
|
||||||
import org.antlr.v4.runtime.ParserRuleContext
|
import org.antlr.v4.runtime.ParserRuleContext
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode
|
import org.antlr.v4.runtime.tree.TerminalNode
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
import prog8.ast.Module
|
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.*
|
||||||
import prog8.ast.expressions.*
|
import prog8.ast.expressions.*
|
||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
import prog8.parser.Prog8ANTLRParser
|
import prog8.parser.Prog8ANTLRParser
|
||||||
import prog8.parser.SourceCode
|
|
||||||
|
|
||||||
|
|
||||||
/***************** Antlr Extension methods to create AST ****************/
|
/***************** Antlr Extension methods to create AST ****************/
|
||||||
|
|
||||||
private data class NumericLiteral(val number: Number, val datatype: DataType)
|
private data class NumericLiteral(val number: Number, val datatype: DataType)
|
||||||
|
|
||||||
internal fun Prog8ANTLRParser.ModuleContext.toAst(name: String, source: SourceCode, encoding: IStringEncoding) : Module {
|
|
||||||
val nameWithoutSuffix = if(name.endsWith(".p8")) name.substringBeforeLast('.') else name
|
|
||||||
val directives = this.directive().map { it.toAst() }
|
|
||||||
val blocks = this.block().map { it.toAst(isInLibrary = source.isFromResources, encoding) }
|
|
||||||
return Module(nameWithoutSuffix, (directives + blocks).toMutableList(), toPosition(), source)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun ParserRuleContext.toPosition() : Position {
|
private fun ParserRuleContext.toPosition() : Position {
|
||||||
/*
|
/*
|
||||||
val customTokensource = this.start.tokenSource as? CustomLexer
|
val customTokensource = this.start.tokenSource as? CustomLexer
|
||||||
@ -38,11 +28,11 @@ private fun ParserRuleContext.toPosition() : Position {
|
|||||||
return Position(filename, start.line, start.charPositionInLine, stop.charPositionInLine + stop.text.length)
|
return Position(filename, start.line, start.charPositionInLine, stop.charPositionInLine + stop.text.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun Prog8ANTLRParser.BlockContext.toAst(isInLibrary: Boolean, encoding: IStringEncoding) : Block {
|
internal fun Prog8ANTLRParser.BlockContext.toAst(isInLibrary: Boolean) : Block {
|
||||||
val blockstatements = block_statement().map {
|
val blockstatements = block_statement().map {
|
||||||
when {
|
when {
|
||||||
it.variabledeclaration()!=null -> it.variabledeclaration().toAst(encoding)
|
it.variabledeclaration()!=null -> it.variabledeclaration().toAst()
|
||||||
it.subroutinedeclaration()!=null -> it.subroutinedeclaration().toAst(encoding)
|
it.subroutinedeclaration()!=null -> it.subroutinedeclaration().toAst()
|
||||||
it.directive()!=null -> it.directive().toAst()
|
it.directive()!=null -> it.directive().toAst()
|
||||||
it.inlineasm()!=null -> it.inlineasm().toAst()
|
it.inlineasm()!=null -> it.inlineasm().toAst()
|
||||||
it.labeldef()!=null -> it.labeldef().toAst()
|
it.labeldef()!=null -> it.labeldef().toAst()
|
||||||
@ -52,11 +42,11 @@ internal fun Prog8ANTLRParser.BlockContext.toAst(isInLibrary: Boolean, encoding:
|
|||||||
return Block(identifier().text, integerliteral()?.toAst()?.number?.toInt(), blockstatements.toMutableList(), isInLibrary, toPosition())
|
return Block(identifier().text, integerliteral()?.toAst()?.number?.toInt(), blockstatements.toMutableList(), isInLibrary, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Statement_blockContext.toAst(encoding: IStringEncoding): MutableList<Statement> =
|
private fun Prog8ANTLRParser.Statement_blockContext.toAst(): MutableList<Statement> =
|
||||||
statement().asSequence().map { it.toAst(encoding) }.toMutableList()
|
statement().asSequence().map { it.toAst() }.toMutableList()
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.VariabledeclarationContext.toAst(encoding: IStringEncoding) : Statement {
|
private fun Prog8ANTLRParser.VariabledeclarationContext.toAst() : Statement {
|
||||||
vardecl()?.let { return it.toAst(encoding) }
|
vardecl()?.let { return it.toAst() }
|
||||||
|
|
||||||
varinitializer()?.let {
|
varinitializer()?.let {
|
||||||
val vd = it.vardecl()
|
val vd = it.vardecl()
|
||||||
@ -64,9 +54,9 @@ private fun Prog8ANTLRParser.VariabledeclarationContext.toAst(encoding: IStringE
|
|||||||
VarDeclType.VAR,
|
VarDeclType.VAR,
|
||||||
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
||||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||||
vd.arrayindex()?.toAst(encoding),
|
vd.arrayindex()?.toAst(),
|
||||||
vd.varname.text,
|
vd.varname.text,
|
||||||
it.expression().toAst(encoding),
|
it.expression().toAst(),
|
||||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||||
false,
|
false,
|
||||||
vd.SHARED()!=null,
|
vd.SHARED()!=null,
|
||||||
@ -81,9 +71,9 @@ private fun Prog8ANTLRParser.VariabledeclarationContext.toAst(encoding: IStringE
|
|||||||
VarDeclType.CONST,
|
VarDeclType.CONST,
|
||||||
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
||||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||||
vd.arrayindex()?.toAst(encoding),
|
vd.arrayindex()?.toAst(),
|
||||||
vd.varname.text,
|
vd.varname.text,
|
||||||
cvarinit.expression().toAst(encoding),
|
cvarinit.expression().toAst(),
|
||||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||||
false,
|
false,
|
||||||
vd.SHARED() != null,
|
vd.SHARED() != null,
|
||||||
@ -98,9 +88,9 @@ private fun Prog8ANTLRParser.VariabledeclarationContext.toAst(encoding: IStringE
|
|||||||
VarDeclType.MEMORY,
|
VarDeclType.MEMORY,
|
||||||
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
vd.datatype()?.toAst() ?: DataType.UNDEFINED,
|
||||||
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
if (vd.ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||||
vd.arrayindex()?.toAst(encoding),
|
vd.arrayindex()?.toAst(),
|
||||||
vd.varname.text,
|
vd.varname.text,
|
||||||
mvarinit.expression().toAst(encoding),
|
mvarinit.expression().toAst(),
|
||||||
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
vd.ARRAYSIG() != null || vd.arrayindex() != null,
|
||||||
false,
|
false,
|
||||||
vd.SHARED()!=null,
|
vd.SHARED()!=null,
|
||||||
@ -111,33 +101,33 @@ private fun Prog8ANTLRParser.VariabledeclarationContext.toAst(encoding: IStringE
|
|||||||
throw FatalAstException("weird variable decl $this")
|
throw FatalAstException("weird variable decl $this")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.SubroutinedeclarationContext.toAst(encoding: IStringEncoding) : Subroutine {
|
private fun Prog8ANTLRParser.SubroutinedeclarationContext.toAst() : Subroutine {
|
||||||
return when {
|
return when {
|
||||||
subroutine()!=null -> subroutine().toAst(encoding)
|
subroutine()!=null -> subroutine().toAst()
|
||||||
asmsubroutine()!=null -> asmsubroutine().toAst(encoding)
|
asmsubroutine()!=null -> asmsubroutine().toAst()
|
||||||
romsubroutine()!=null -> romsubroutine().toAst()
|
romsubroutine()!=null -> romsubroutine().toAst()
|
||||||
else -> throw FatalAstException("weird subroutine decl $this")
|
else -> throw FatalAstException("weird subroutine decl $this")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.StatementContext.toAst(encoding: IStringEncoding) : Statement {
|
private fun Prog8ANTLRParser.StatementContext.toAst() : Statement {
|
||||||
val vardecl = variabledeclaration()?.toAst(encoding)
|
val vardecl = variabledeclaration()?.toAst()
|
||||||
if(vardecl!=null) return vardecl
|
if(vardecl!=null) return vardecl
|
||||||
|
|
||||||
assignment()?.let {
|
assignment()?.let {
|
||||||
return Assignment(it.assign_target().toAst(encoding), it.expression().toAst(encoding), it.toPosition())
|
return Assignment(it.assign_target().toAst(), it.expression().toAst(), it.toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
augassignment()?.let {
|
augassignment()?.let {
|
||||||
// replace A += X with A = A + X
|
// replace A += X with A = A + X
|
||||||
val target = it.assign_target().toAst(encoding)
|
val target = it.assign_target().toAst()
|
||||||
val oper = it.operator.text.substringBefore('=')
|
val oper = it.operator.text.substringBefore('=')
|
||||||
val expression = BinaryExpression(target.toExpression(), oper, it.expression().toAst(encoding), it.expression().toPosition())
|
val expression = BinaryExpression(target.toExpression(), oper, it.expression().toAst(), it.expression().toPosition())
|
||||||
return Assignment(it.assign_target().toAst(encoding), expression, it.toPosition())
|
return Assignment(it.assign_target().toAst(), expression, it.toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
postincrdecr()?.let {
|
postincrdecr()?.let {
|
||||||
return PostIncrDecr(it.assign_target().toAst(encoding), it.operator.text, it.toPosition())
|
return PostIncrDecr(it.assign_target().toAst(), it.operator.text, it.toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
val directive = directive()?.toAst()
|
val directive = directive()?.toAst()
|
||||||
@ -149,49 +139,49 @@ private fun Prog8ANTLRParser.StatementContext.toAst(encoding: IStringEncoding) :
|
|||||||
val jump = unconditionaljump()?.toAst()
|
val jump = unconditionaljump()?.toAst()
|
||||||
if(jump!=null) return jump
|
if(jump!=null) return jump
|
||||||
|
|
||||||
val fcall = functioncall_stmt()?.toAst(encoding)
|
val fcall = functioncall_stmt()?.toAst()
|
||||||
if(fcall!=null) return fcall
|
if(fcall!=null) return fcall
|
||||||
|
|
||||||
val ifstmt = if_stmt()?.toAst(encoding)
|
val ifstmt = if_stmt()?.toAst()
|
||||||
if(ifstmt!=null) return ifstmt
|
if(ifstmt!=null) return ifstmt
|
||||||
|
|
||||||
val returnstmt = returnstmt()?.toAst(encoding)
|
val returnstmt = returnstmt()?.toAst()
|
||||||
if(returnstmt!=null) return returnstmt
|
if(returnstmt!=null) return returnstmt
|
||||||
|
|
||||||
val subroutine = subroutinedeclaration()?.toAst(encoding)
|
val subroutine = subroutinedeclaration()?.toAst()
|
||||||
if(subroutine!=null) return subroutine
|
if(subroutine!=null) return subroutine
|
||||||
|
|
||||||
val asm = inlineasm()?.toAst()
|
val asm = inlineasm()?.toAst()
|
||||||
if(asm!=null) return asm
|
if(asm!=null) return asm
|
||||||
|
|
||||||
val branchstmt = branch_stmt()?.toAst(encoding)
|
val branchstmt = branch_stmt()?.toAst()
|
||||||
if(branchstmt!=null) return branchstmt
|
if(branchstmt!=null) return branchstmt
|
||||||
|
|
||||||
val forloop = forloop()?.toAst(encoding)
|
val forloop = forloop()?.toAst()
|
||||||
if(forloop!=null) return forloop
|
if(forloop!=null) return forloop
|
||||||
|
|
||||||
val untilloop = untilloop()?.toAst(encoding)
|
val untilloop = untilloop()?.toAst()
|
||||||
if(untilloop!=null) return untilloop
|
if(untilloop!=null) return untilloop
|
||||||
|
|
||||||
val whileloop = whileloop()?.toAst(encoding)
|
val whileloop = whileloop()?.toAst()
|
||||||
if(whileloop!=null) return whileloop
|
if(whileloop!=null) return whileloop
|
||||||
|
|
||||||
val repeatloop = repeatloop()?.toAst(encoding)
|
val repeatloop = repeatloop()?.toAst()
|
||||||
if(repeatloop!=null) return repeatloop
|
if(repeatloop!=null) return repeatloop
|
||||||
|
|
||||||
val breakstmt = breakstmt()?.toAst()
|
val breakstmt = breakstmt()?.toAst()
|
||||||
if(breakstmt!=null) return breakstmt
|
if(breakstmt!=null) return breakstmt
|
||||||
|
|
||||||
val whenstmt = whenstmt()?.toAst(encoding)
|
val whenstmt = whenstmt()?.toAst()
|
||||||
if(whenstmt!=null) return whenstmt
|
if(whenstmt!=null) return whenstmt
|
||||||
|
|
||||||
throw FatalAstException("unprocessed source text (are we missing ast conversion rules for parser elements?): $text")
|
throw FatalAstException("unprocessed source text (are we missing ast conversion rules for parser elements?): $text")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.AsmsubroutineContext.toAst(encoding: IStringEncoding): Subroutine {
|
private fun Prog8ANTLRParser.AsmsubroutineContext.toAst(): Subroutine {
|
||||||
val inline = this.inline()!=null
|
val inline = this.inline()!=null
|
||||||
val subdecl = asmsub_decl().toAst()
|
val subdecl = asmsub_decl().toAst()
|
||||||
val statements = statement_block()?.toAst(encoding) ?: mutableListOf()
|
val statements = statement_block()?.toAst() ?: mutableListOf()
|
||||||
return Subroutine(subdecl.name, subdecl.parameters, subdecl.returntypes,
|
return Subroutine(subdecl.name, subdecl.parameters, subdecl.returntypes,
|
||||||
subdecl.asmParameterRegisters, subdecl.asmReturnvaluesRegisters,
|
subdecl.asmParameterRegisters, subdecl.asmReturnvaluesRegisters,
|
||||||
subdecl.asmClobbers, null, true, inline, statements, toPosition())
|
subdecl.asmClobbers, null, true, inline, statements, toPosition())
|
||||||
@ -272,28 +262,28 @@ private fun Prog8ANTLRParser.Asmsub_paramsContext.toAst(): List<AsmSubroutinePar
|
|||||||
AsmSubroutineParameter(vardecl.varname.text, datatype, registerorpair, statusregister, toPosition())
|
AsmSubroutineParameter(vardecl.varname.text, datatype, registerorpair, statusregister, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Functioncall_stmtContext.toAst(encoding: IStringEncoding): Statement {
|
private fun Prog8ANTLRParser.Functioncall_stmtContext.toAst(): Statement {
|
||||||
val void = this.VOID() != null
|
val void = this.VOID() != null
|
||||||
val location = scoped_identifier().toAst()
|
val location = scoped_identifier().toAst()
|
||||||
return if(expression_list() == null)
|
return if(expression_list() == null)
|
||||||
FunctionCallStatement(location, mutableListOf(), void, toPosition())
|
FunctionCallStatement(location, mutableListOf(), void, toPosition())
|
||||||
else
|
else
|
||||||
FunctionCallStatement(location, expression_list().toAst(encoding).toMutableList(), void, toPosition())
|
FunctionCallStatement(location, expression_list().toAst().toMutableList(), void, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.FunctioncallContext.toAst(encoding: IStringEncoding): FunctionCall {
|
private fun Prog8ANTLRParser.FunctioncallContext.toAst(): FunctionCall {
|
||||||
val location = scoped_identifier().toAst()
|
val location = scoped_identifier().toAst()
|
||||||
return if(expression_list() == null)
|
return if(expression_list() == null)
|
||||||
FunctionCall(location, mutableListOf(), toPosition())
|
FunctionCall(location, mutableListOf(), toPosition())
|
||||||
else
|
else
|
||||||
FunctionCall(location, expression_list().toAst(encoding).toMutableList(), toPosition())
|
FunctionCall(location, expression_list().toAst().toMutableList(), toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.InlineasmContext.toAst() =
|
private fun Prog8ANTLRParser.InlineasmContext.toAst() =
|
||||||
InlineAssembly(INLINEASMBLOCK().text, toPosition())
|
InlineAssembly(INLINEASMBLOCK().text, toPosition())
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ReturnstmtContext.toAst(encoding: IStringEncoding) : Return {
|
private fun Prog8ANTLRParser.ReturnstmtContext.toAst() : Return {
|
||||||
return Return(expression()?.toAst(encoding), toPosition())
|
return Return(expression()?.toAst(), toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.UnconditionaljumpContext.toAst(): Jump {
|
private fun Prog8ANTLRParser.UnconditionaljumpContext.toAst(): Jump {
|
||||||
@ -305,14 +295,14 @@ private fun Prog8ANTLRParser.UnconditionaljumpContext.toAst(): Jump {
|
|||||||
private fun Prog8ANTLRParser.LabeldefContext.toAst(): Statement =
|
private fun Prog8ANTLRParser.LabeldefContext.toAst(): Statement =
|
||||||
Label(children[0].text, toPosition())
|
Label(children[0].text, toPosition())
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.SubroutineContext.toAst(encoding: IStringEncoding) : Subroutine {
|
private fun Prog8ANTLRParser.SubroutineContext.toAst() : Subroutine {
|
||||||
// non-asm subroutine
|
// non-asm subroutine
|
||||||
val inline = inline()!=null
|
val inline = inline()!=null
|
||||||
val returntypes = sub_return_part()?.toAst() ?: emptyList()
|
val returntypes = sub_return_part()?.toAst() ?: emptyList()
|
||||||
return Subroutine(identifier().text,
|
return Subroutine(identifier().text,
|
||||||
sub_params()?.toAst() ?: emptyList(),
|
sub_params()?.toAst() ?: emptyList(),
|
||||||
returntypes,
|
returntypes,
|
||||||
statement_block()?.toAst(encoding) ?: mutableListOf(),
|
statement_block()?.toAst() ?: mutableListOf(),
|
||||||
inline,
|
inline,
|
||||||
toPosition())
|
toPosition())
|
||||||
}
|
}
|
||||||
@ -328,12 +318,12 @@ private fun Prog8ANTLRParser.Sub_paramsContext.toAst(): List<SubroutineParameter
|
|||||||
SubroutineParameter(it.varname.text, datatype, it.toPosition())
|
SubroutineParameter(it.varname.text, datatype, it.toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Assign_targetContext.toAst(encoding: IStringEncoding) : AssignTarget {
|
private fun Prog8ANTLRParser.Assign_targetContext.toAst() : AssignTarget {
|
||||||
val identifier = scoped_identifier()
|
val identifier = scoped_identifier()
|
||||||
return when {
|
return when {
|
||||||
identifier!=null -> AssignTarget(identifier.toAst(), null, null, toPosition())
|
identifier!=null -> AssignTarget(identifier.toAst(), null, null, toPosition())
|
||||||
arrayindexed()!=null -> AssignTarget(null, arrayindexed().toAst(encoding), null, toPosition())
|
arrayindexed()!=null -> AssignTarget(null, arrayindexed().toAst(), null, toPosition())
|
||||||
directmemory()!=null -> AssignTarget(null, null, DirectMemoryWrite(directmemory().expression().toAst(encoding), toPosition()), toPosition())
|
directmemory()!=null -> AssignTarget(null, null, DirectMemoryWrite(directmemory().expression().toAst(), toPosition()), toPosition())
|
||||||
else -> AssignTarget(scoped_identifier()?.toAst(), null, null, toPosition())
|
else -> AssignTarget(scoped_identifier()?.toAst(), null, null, toPosition())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -345,8 +335,8 @@ private fun Prog8ANTLRParser.ClobberContext.toAst() : Set<CpuRegister> {
|
|||||||
|
|
||||||
private fun Prog8ANTLRParser.DatatypeContext.toAst() = DataType.valueOf(text.uppercase())
|
private fun Prog8ANTLRParser.DatatypeContext.toAst() = DataType.valueOf(text.uppercase())
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ArrayindexContext.toAst(encoding: IStringEncoding) : ArrayIndex =
|
private fun Prog8ANTLRParser.ArrayindexContext.toAst() : ArrayIndex =
|
||||||
ArrayIndex(expression().toAst(encoding), toPosition())
|
ArrayIndex(expression().toAst(), toPosition())
|
||||||
|
|
||||||
internal fun Prog8ANTLRParser.DirectiveContext.toAst() : Directive =
|
internal fun Prog8ANTLRParser.DirectiveContext.toAst() : Directive =
|
||||||
Directive(directivename.text, directivearg().map { it.toAst() }, toPosition())
|
Directive(directivename.text, directivearg().map { it.toAst() }, toPosition())
|
||||||
@ -354,7 +344,7 @@ internal fun Prog8ANTLRParser.DirectiveContext.toAst() : Directive =
|
|||||||
private fun Prog8ANTLRParser.DirectiveargContext.toAst() : DirectiveArg {
|
private fun Prog8ANTLRParser.DirectiveargContext.toAst() : DirectiveArg {
|
||||||
val str = stringliteral()
|
val str = stringliteral()
|
||||||
if(str?.ALT_STRING_ENCODING() != null)
|
if(str?.ALT_STRING_ENCODING() != null)
|
||||||
throw AstException("${toPosition()} can't use alternate string encodings for directive arguments")
|
throw AstException("${toPosition()} can't use alternate string s for directive arguments")
|
||||||
|
|
||||||
return DirectiveArg(stringliteral()?.text, identifier()?.text, integerliteral()?.toAst()?.number?.toInt(), toPosition())
|
return DirectiveArg(stringliteral()?.text, identifier()?.text, integerliteral()?.toAst()?.number?.toInt(), toPosition())
|
||||||
}
|
}
|
||||||
@ -410,7 +400,7 @@ private fun Prog8ANTLRParser.IntegerliteralContext.toAst(): NumericLiteral {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ExpressionContext.toAst(encoding: IStringEncoding) : Expression {
|
private fun Prog8ANTLRParser.ExpressionContext.toAst() : Expression {
|
||||||
|
|
||||||
val litval = literalvalue()
|
val litval = literalvalue()
|
||||||
if(litval!=null) {
|
if(litval!=null) {
|
||||||
@ -433,7 +423,7 @@ private fun Prog8ANTLRParser.ExpressionContext.toAst(encoding: IStringEncoding)
|
|||||||
litval.stringliteral()!=null -> litval.stringliteral().toAst()
|
litval.stringliteral()!=null -> litval.stringliteral().toAst()
|
||||||
litval.charliteral()!=null -> litval.charliteral().toAst()
|
litval.charliteral()!=null -> litval.charliteral().toAst()
|
||||||
litval.arrayliteral()!=null -> {
|
litval.arrayliteral()!=null -> {
|
||||||
val array = litval.arrayliteral().toAst(encoding)
|
val array = litval.arrayliteral().toAst()
|
||||||
// the actual type of the arraysize can not yet be determined here (missing namespace & heap)
|
// the actual type of the arraysize can not yet be determined here (missing namespace & heap)
|
||||||
// the ConstantFold takes care of that and converts the type if needed.
|
// the ConstantFold takes care of that and converts the type if needed.
|
||||||
ArrayLiteralValue(InferredTypes.InferredType.unknown(), array, position = litval.toPosition())
|
ArrayLiteralValue(InferredTypes.InferredType.unknown(), array, position = litval.toPosition())
|
||||||
@ -447,31 +437,31 @@ private fun Prog8ANTLRParser.ExpressionContext.toAst(encoding: IStringEncoding)
|
|||||||
return scoped_identifier().toAst()
|
return scoped_identifier().toAst()
|
||||||
|
|
||||||
if(bop!=null)
|
if(bop!=null)
|
||||||
return BinaryExpression(left.toAst(encoding), bop.text, right.toAst(encoding), toPosition())
|
return BinaryExpression(left.toAst(), bop.text, right.toAst(), toPosition())
|
||||||
|
|
||||||
if(prefix!=null)
|
if(prefix!=null)
|
||||||
return PrefixExpression(prefix.text, expression(0).toAst(encoding), toPosition())
|
return PrefixExpression(prefix.text, expression(0).toAst(), toPosition())
|
||||||
|
|
||||||
val funcall = functioncall()?.toAst(encoding)
|
val funcall = functioncall()?.toAst()
|
||||||
if(funcall!=null) return funcall
|
if(funcall!=null) return funcall
|
||||||
|
|
||||||
if (rangefrom!=null && rangeto!=null) {
|
if (rangefrom!=null && rangeto!=null) {
|
||||||
val defaultstep = if(rto.text == "to") 1 else -1
|
val defaultstep = if(rto.text == "to") 1 else -1
|
||||||
val step = rangestep?.toAst(encoding) ?: NumericLiteralValue(DataType.UBYTE, defaultstep, toPosition())
|
val step = rangestep?.toAst() ?: NumericLiteralValue(DataType.UBYTE, defaultstep, toPosition())
|
||||||
return RangeExpr(rangefrom.toAst(encoding), rangeto.toAst(encoding), step, toPosition())
|
return RangeExpr(rangefrom.toAst(), rangeto.toAst(), step, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
if(childCount==3 && children[0].text=="(" && children[2].text==")")
|
if(childCount==3 && children[0].text=="(" && children[2].text==")")
|
||||||
return expression(0).toAst(encoding) // expression within ( )
|
return expression(0).toAst() // expression within ( )
|
||||||
|
|
||||||
if(arrayindexed()!=null)
|
if(arrayindexed()!=null)
|
||||||
return arrayindexed().toAst(encoding)
|
return arrayindexed().toAst()
|
||||||
|
|
||||||
if(typecast()!=null)
|
if(typecast()!=null)
|
||||||
return TypecastExpression(expression(0).toAst(encoding), typecast().datatype().toAst(), false, toPosition())
|
return TypecastExpression(expression(0).toAst(), typecast().datatype().toAst(), false, toPosition())
|
||||||
|
|
||||||
if(directmemory()!=null)
|
if(directmemory()!=null)
|
||||||
return DirectMemoryRead(directmemory().expression().toAst(encoding), toPosition())
|
return DirectMemoryRead(directmemory().expression().toAst(), toPosition())
|
||||||
|
|
||||||
if(addressof()!=null)
|
if(addressof()!=null)
|
||||||
return AddressOf(addressof().scoped_identifier().toAst(), toPosition())
|
return AddressOf(addressof().scoped_identifier().toAst(), toPosition())
|
||||||
@ -485,13 +475,13 @@ private fun Prog8ANTLRParser.CharliteralContext.toAst(): CharLiteral =
|
|||||||
private fun Prog8ANTLRParser.StringliteralContext.toAst(): StringLiteralValue =
|
private fun Prog8ANTLRParser.StringliteralContext.toAst(): StringLiteralValue =
|
||||||
StringLiteralValue(unescape(this.STRING().text, toPosition()), ALT_STRING_ENCODING()!=null, toPosition())
|
StringLiteralValue(unescape(this.STRING().text, toPosition()), ALT_STRING_ENCODING()!=null, toPosition())
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ArrayindexedContext.toAst(encoding: IStringEncoding): ArrayIndexedExpression {
|
private fun Prog8ANTLRParser.ArrayindexedContext.toAst(): ArrayIndexedExpression {
|
||||||
return ArrayIndexedExpression(scoped_identifier().toAst(),
|
return ArrayIndexedExpression(scoped_identifier().toAst(),
|
||||||
arrayindex().toAst(encoding),
|
arrayindex().toAst(),
|
||||||
toPosition())
|
toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Expression_listContext.toAst(encoding: IStringEncoding) = expression().map{ it.toAst(encoding) }
|
private fun Prog8ANTLRParser.Expression_listContext.toAst() = expression().map{ it.toAst() }
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.IdentifierContext.toAst() : IdentifierReference =
|
private fun Prog8ANTLRParser.IdentifierContext.toAst() : IdentifierReference =
|
||||||
IdentifierReference(listOf(text), toPosition())
|
IdentifierReference(listOf(text), toPosition())
|
||||||
@ -507,27 +497,27 @@ private fun Prog8ANTLRParser.BooleanliteralContext.toAst() = when(text) {
|
|||||||
else -> throw FatalAstException(text)
|
else -> throw FatalAstException(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ArrayliteralContext.toAst(encoding: IStringEncoding) : Array<Expression> =
|
private fun Prog8ANTLRParser.ArrayliteralContext.toAst() : Array<Expression> =
|
||||||
expression().map { it.toAst(encoding) }.toTypedArray()
|
expression().map { it.toAst() }.toTypedArray()
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.If_stmtContext.toAst(encoding: IStringEncoding): IfStatement {
|
private fun Prog8ANTLRParser.If_stmtContext.toAst(): IfStatement {
|
||||||
val condition = expression().toAst(encoding)
|
val condition = expression().toAst()
|
||||||
val trueStatements = statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
val trueStatements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
val elseStatements = else_part()?.toAst(encoding) ?: mutableListOf()
|
val elseStatements = else_part()?.toAst() ?: mutableListOf()
|
||||||
val trueScope = AnonymousScope(trueStatements, statement_block()?.toPosition()
|
val trueScope = AnonymousScope(trueStatements, statement_block()?.toPosition()
|
||||||
?: statement().toPosition())
|
?: statement().toPosition())
|
||||||
val elseScope = AnonymousScope(elseStatements, else_part()?.toPosition() ?: toPosition())
|
val elseScope = AnonymousScope(elseStatements, else_part()?.toPosition() ?: toPosition())
|
||||||
return IfStatement(condition, trueScope, elseScope, toPosition())
|
return IfStatement(condition, trueScope, elseScope, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Else_partContext.toAst(encoding: IStringEncoding): MutableList<Statement> {
|
private fun Prog8ANTLRParser.Else_partContext.toAst(): MutableList<Statement> {
|
||||||
return statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
return statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.Branch_stmtContext.toAst(encoding: IStringEncoding): BranchStatement {
|
private fun Prog8ANTLRParser.Branch_stmtContext.toAst(): BranchStatement {
|
||||||
val branchcondition = branchcondition().toAst()
|
val branchcondition = branchcondition().toAst()
|
||||||
val trueStatements = statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
val trueStatements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
val elseStatements = else_part()?.toAst(encoding) ?: mutableListOf()
|
val elseStatements = else_part()?.toAst() ?: mutableListOf()
|
||||||
val trueScope = AnonymousScope(trueStatements, statement_block()?.toPosition()
|
val trueScope = AnonymousScope(trueStatements, statement_block()?.toPosition()
|
||||||
?: statement().toPosition())
|
?: statement().toPosition())
|
||||||
val elseScope = AnonymousScope(elseStatements, else_part()?.toPosition() ?: toPosition())
|
val elseScope = AnonymousScope(elseStatements, else_part()?.toPosition() ?: toPosition())
|
||||||
@ -538,65 +528,65 @@ private fun Prog8ANTLRParser.BranchconditionContext.toAst() = BranchCondition.va
|
|||||||
text.substringAfter('_').uppercase()
|
text.substringAfter('_').uppercase()
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.ForloopContext.toAst(encoding: IStringEncoding): ForLoop {
|
private fun Prog8ANTLRParser.ForloopContext.toAst(): ForLoop {
|
||||||
val loopvar = identifier().toAst()
|
val loopvar = identifier().toAst()
|
||||||
val iterable = expression()!!.toAst(encoding)
|
val iterable = expression()!!.toAst()
|
||||||
val scope =
|
val scope =
|
||||||
if(statement()!=null)
|
if(statement()!=null)
|
||||||
AnonymousScope(mutableListOf(statement().toAst(encoding)), statement().toPosition())
|
AnonymousScope(mutableListOf(statement().toAst()), statement().toPosition())
|
||||||
else
|
else
|
||||||
AnonymousScope(statement_block().toAst(encoding), statement_block().toPosition())
|
AnonymousScope(statement_block().toAst(), statement_block().toPosition())
|
||||||
return ForLoop(loopvar, iterable, scope, toPosition())
|
return ForLoop(loopvar, iterable, scope, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.BreakstmtContext.toAst() = Break(toPosition())
|
private fun Prog8ANTLRParser.BreakstmtContext.toAst() = Break(toPosition())
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.WhileloopContext.toAst(encoding: IStringEncoding): WhileLoop {
|
private fun Prog8ANTLRParser.WhileloopContext.toAst(): WhileLoop {
|
||||||
val condition = expression().toAst(encoding)
|
val condition = expression().toAst()
|
||||||
val statements = statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
val statements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
||||||
?: statement().toPosition())
|
?: statement().toPosition())
|
||||||
return WhileLoop(condition, scope, toPosition())
|
return WhileLoop(condition, scope, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.RepeatloopContext.toAst(encoding: IStringEncoding): RepeatLoop {
|
private fun Prog8ANTLRParser.RepeatloopContext.toAst(): RepeatLoop {
|
||||||
val iterations = expression()?.toAst(encoding)
|
val iterations = expression()?.toAst()
|
||||||
val statements = statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
val statements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
||||||
?: statement().toPosition())
|
?: statement().toPosition())
|
||||||
return RepeatLoop(iterations, scope, toPosition())
|
return RepeatLoop(iterations, scope, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.UntilloopContext.toAst(encoding: IStringEncoding): UntilLoop {
|
private fun Prog8ANTLRParser.UntilloopContext.toAst(): UntilLoop {
|
||||||
val untilCondition = expression().toAst(encoding)
|
val untilCondition = expression().toAst()
|
||||||
val statements = statement_block()?.toAst(encoding) ?: mutableListOf(statement().toAst(encoding))
|
val statements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||||
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
val scope = AnonymousScope(statements, statement_block()?.toPosition()
|
||||||
?: statement().toPosition())
|
?: statement().toPosition())
|
||||||
return UntilLoop(scope, untilCondition, toPosition())
|
return UntilLoop(scope, untilCondition, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.WhenstmtContext.toAst(encoding: IStringEncoding): WhenStatement {
|
private fun Prog8ANTLRParser.WhenstmtContext.toAst(): WhenStatement {
|
||||||
val condition = expression().toAst(encoding)
|
val condition = expression().toAst()
|
||||||
val choices = this.when_choice()?.map { it.toAst(encoding) }?.toMutableList() ?: mutableListOf<WhenChoice>()
|
val choices = this.when_choice()?.map { it.toAst() }?.toMutableList() ?: mutableListOf()
|
||||||
return WhenStatement(condition, choices, toPosition())
|
return WhenStatement(condition, choices, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.When_choiceContext.toAst(encoding: IStringEncoding): WhenChoice {
|
private fun Prog8ANTLRParser.When_choiceContext.toAst(): WhenChoice {
|
||||||
val values = expression_list()?.toAst(encoding)
|
val values = expression_list()?.toAst()
|
||||||
val stmt = statement()?.toAst(encoding)
|
val stmt = statement()?.toAst()
|
||||||
val stmtBlock = statement_block()?.toAst(encoding)?.toMutableList() ?: mutableListOf()
|
val stmtBlock = statement_block()?.toAst()?.toMutableList() ?: mutableListOf()
|
||||||
if(stmt!=null)
|
if(stmt!=null)
|
||||||
stmtBlock.add(stmt)
|
stmtBlock.add(stmt)
|
||||||
val scope = AnonymousScope(stmtBlock, toPosition())
|
val scope = AnonymousScope(stmtBlock, toPosition())
|
||||||
return WhenChoice(values?.toMutableList(), scope, toPosition())
|
return WhenChoice(values?.toMutableList(), scope, toPosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Prog8ANTLRParser.VardeclContext.toAst(encoding: IStringEncoding): VarDecl {
|
private fun Prog8ANTLRParser.VardeclContext.toAst(): VarDecl {
|
||||||
return VarDecl(
|
return VarDecl(
|
||||||
VarDeclType.VAR,
|
VarDeclType.VAR,
|
||||||
datatype()?.toAst() ?: DataType.UNDEFINED,
|
datatype()?.toAst() ?: DataType.UNDEFINED,
|
||||||
if(ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
if(ZEROPAGE() != null) ZeropageWish.PREFER_ZEROPAGE else ZeropageWish.DONTCARE,
|
||||||
arrayindex()?.toAst(encoding),
|
arrayindex()?.toAst(),
|
||||||
varname.text,
|
varname.text,
|
||||||
null,
|
null,
|
||||||
ARRAYSIG() != null || arrayindex() != null,
|
ARRAYSIG() != null || arrayindex() != null,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package prog8.parser
|
package prog8.parser
|
||||||
|
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
import prog8.ast.Module
|
import prog8.ast.Module
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
@ -17,7 +16,6 @@ fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
|
|||||||
|
|
||||||
|
|
||||||
class ModuleImporter(private val program: Program,
|
class ModuleImporter(private val program: Program,
|
||||||
private val encoder: IStringEncoding,
|
|
||||||
private val compilationTargetName: String,
|
private val compilationTargetName: String,
|
||||||
private val libdirs: List<String>) {
|
private val libdirs: List<String>) {
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,24 +0,0 @@
|
|||||||
package prog8.parser
|
|
||||||
|
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
import java.io.CharConversionException
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: remove once [IStringEncoding] has been moved 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}")
|
|
||||||
}
|
|
||||||
}
|
|
@ -37,7 +37,7 @@ object Prog8Parser {
|
|||||||
// .linkParents called in ParsedModule.add
|
// .linkParents called in ParsedModule.add
|
||||||
parseTree.directive().forEach { module.add(it.toAst()) }
|
parseTree.directive().forEach { module.add(it.toAst()) }
|
||||||
// TODO: remove Encoding
|
// TODO: remove Encoding
|
||||||
parseTree.block().forEach { module.add(it.toAst(module.isLibrary(), PetsciiEncoding)) }
|
parseTree.block().forEach { module.add(it.toAst(module.isLibrary())) }
|
||||||
|
|
||||||
return module
|
return module
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package prog8.parser
|
|
||||||
|
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: remove once [IStringEncoding] has been moved 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")
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,7 +5,6 @@ import kotlin.io.path.*
|
|||||||
|
|
||||||
import prog8.ast.IBuiltinFunctions
|
import prog8.ast.IBuiltinFunctions
|
||||||
import prog8.ast.IMemSizer
|
import prog8.ast.IMemSizer
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
import prog8.ast.base.DataType
|
import prog8.ast.base.DataType
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.ast.expressions.Expression
|
import prog8.ast.expressions.Expression
|
||||||
@ -46,16 +45,6 @@ fun sanityCheckDirectories(workingDirName: String? = null) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val DummyEncoding = object : IStringEncoding {
|
|
||||||
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val DummyFunctions = object : IBuiltinFunctions {
|
val DummyFunctions = object : IBuiltinFunctions {
|
||||||
override val names: Set<String> = emptySet()
|
override val names: Set<String> = emptySet()
|
||||||
override val purefunctionNames: Set<String> = emptySet()
|
override val purefunctionNames: Set<String> = emptySet()
|
||||||
|
@ -19,7 +19,7 @@ class TestModuleImporter {
|
|||||||
fun testImportModuleWithNonExistingPath() {
|
fun testImportModuleWithNonExistingPath() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
|
|
||||||
val srcPath = fixturesDir.resolve("i_do_not_exist")
|
val srcPath = fixturesDir.resolve("i_do_not_exist")
|
||||||
assumeNotExists(srcPath)
|
assumeNotExists(srcPath)
|
||||||
@ -31,7 +31,7 @@ class TestModuleImporter {
|
|||||||
fun testImportModuleWithDirectoryPath() {
|
fun testImportModuleWithDirectoryPath() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
|
|
||||||
val srcPath = fixturesDir
|
val srcPath = fixturesDir
|
||||||
assumeDirectory(srcPath)
|
assumeDirectory(srcPath)
|
||||||
@ -46,7 +46,7 @@ class TestModuleImporter {
|
|||||||
fun testImportModuleWithSyntaxError() {
|
fun testImportModuleWithSyntaxError() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
|
|
||||||
val filename = "file_with_syntax_error.p8"
|
val filename = "file_with_syntax_error.p8"
|
||||||
val path = fixturesDir.resolve(filename)
|
val path = fixturesDir.resolve(filename)
|
||||||
@ -67,7 +67,7 @@ class TestModuleImporter {
|
|||||||
fun testImportModuleWithImportingModuleWithSyntaxError() {
|
fun testImportModuleWithImportingModuleWithSyntaxError() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
|
|
||||||
val importing = fixturesDir.resolve("import_file_with_syntax_error.p8")
|
val importing = fixturesDir.resolve("import_file_with_syntax_error.p8")
|
||||||
val imported = fixturesDir.resolve("file_with_syntax_error.p8")
|
val imported = fixturesDir.resolve("file_with_syntax_error.p8")
|
||||||
@ -92,7 +92,7 @@ class TestModuleImporter {
|
|||||||
fun testImportLibraryModuleWithNonExistingName() {
|
fun testImportLibraryModuleWithNonExistingName() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
val filenameNoExt = "i_do_not_exist"
|
val filenameNoExt = "i_do_not_exist"
|
||||||
val filenameWithExt = filenameNoExt + ".p8"
|
val filenameWithExt = filenameNoExt + ".p8"
|
||||||
val srcPathNoExt = fixturesDir.resolve(filenameNoExt)
|
val srcPathNoExt = fixturesDir.resolve(filenameNoExt)
|
||||||
@ -108,7 +108,7 @@ class TestModuleImporter {
|
|||||||
fun testImportLibraryModuleWithSyntaxError() {
|
fun testImportLibraryModuleWithSyntaxError() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
val srcPath = fixturesDir.resolve("file_with_syntax_error.p8")
|
val srcPath = fixturesDir.resolve("file_with_syntax_error.p8")
|
||||||
assumeReadableFile(srcPath)
|
assumeReadableFile(srcPath)
|
||||||
|
|
||||||
@ -130,7 +130,7 @@ class TestModuleImporter {
|
|||||||
fun testImportLibraryModuleWithImportingBadModule() {
|
fun testImportLibraryModuleWithImportingBadModule() {
|
||||||
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
|
||||||
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
|
||||||
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf(searchIn))
|
val importer = ModuleImporter(program, "blah", listOf(searchIn))
|
||||||
|
|
||||||
val importing = fixturesDir.resolve("import_file_with_syntax_error.p8")
|
val importing = fixturesDir.resolve("import_file_with_syntax_error.p8")
|
||||||
val imported = fixturesDir.resolve("file_with_syntax_error.p8")
|
val imported = fixturesDir.resolve("file_with_syntax_error.p8")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user