mirror of
https://github.com/irmen/prog8.git
synced 2024-12-27 05:29:38 +00:00
upgraded to Kotlin 1.4, fixed several compilation warnings
This commit is contained in:
parent
c0887b5f08
commit
e6ff87ecd0
@ -1,11 +1,11 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
// id "org.jetbrains.kotlin.jvm" version "1.3.72"
|
// id "org.jetbrains.kotlin.jvm" version "1.4.0"
|
||||||
id 'application'
|
id 'application'
|
||||||
id 'org.jetbrains.dokka' version "0.9.18"
|
id 'org.jetbrains.dokka' version "0.9.18"
|
||||||
id 'com.github.johnrengelman.shadow' version '5.2.0'
|
id 'com.github.johnrengelman.shadow' version '5.2.0'
|
||||||
|
@ -79,7 +79,7 @@ fun compileProgram(filepath: Path,
|
|||||||
|
|
||||||
private fun parseImports(filepath: Path, errors: ErrorReporter): Triple<Program, CompilationOptions, List<Path>> {
|
private fun parseImports(filepath: Path, errors: ErrorReporter): Triple<Program, CompilationOptions, List<Path>> {
|
||||||
println("Parsing...")
|
println("Parsing...")
|
||||||
val importer = ModuleImporter(errors)
|
val importer = ModuleImporter()
|
||||||
val programAst = Program(moduleName(filepath.fileName), mutableListOf())
|
val programAst = Program(moduleName(filepath.fileName), mutableListOf())
|
||||||
importer.importModule(programAst, filepath)
|
importer.importModule(programAst, filepath)
|
||||||
errors.handle()
|
errors.handle()
|
||||||
|
@ -39,13 +39,13 @@ abstract class Zeropage(protected val options: CompilationOptions) {
|
|||||||
|
|
||||||
if(free.size > 0) {
|
if(free.size > 0) {
|
||||||
if(size==1) {
|
if(size==1) {
|
||||||
for(candidate in free.min()!! .. free.max()!!+1) {
|
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) {
|
||||||
if(loneByte(candidate))
|
if(loneByte(candidate))
|
||||||
return makeAllocation(candidate, 1, datatype, scopedname)
|
return makeAllocation(candidate, 1, datatype, scopedname)
|
||||||
}
|
}
|
||||||
return makeAllocation(free[0], 1, datatype, scopedname)
|
return makeAllocation(free[0], 1, datatype, scopedname)
|
||||||
}
|
}
|
||||||
for(candidate in free.min()!! .. free.max()!!+1) {
|
for(candidate in free.minOrNull()!! .. free.maxOrNull()!!+1) {
|
||||||
if (sequentialFree(candidate, size))
|
if (sequentialFree(candidate, size))
|
||||||
return makeAllocation(candidate, size, datatype, scopedname)
|
return makeAllocation(candidate, size, datatype, scopedname)
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ internal class AsmGen(private val program: Program,
|
|||||||
private val forloopsAsmGen = ForLoopsAsmGen(program, this)
|
private val forloopsAsmGen = ForLoopsAsmGen(program, this)
|
||||||
private val postincrdecrAsmGen = PostIncrDecrAsmGen(program, this)
|
private val postincrdecrAsmGen = PostIncrDecrAsmGen(program, this)
|
||||||
private val functioncallAsmGen = FunctionCallAsmGen(program, this)
|
private val functioncallAsmGen = FunctionCallAsmGen(program, this)
|
||||||
private val assignmentAsmGen = AssignmentAsmGen(program, errors, this)
|
private val assignmentAsmGen = AssignmentAsmGen(program, this)
|
||||||
private val expressionsAsmGen = ExpressionsAsmGen(program, this)
|
private val expressionsAsmGen = ExpressionsAsmGen(program, this)
|
||||||
internal val loopEndLabels = ArrayDeque<String>()
|
internal val loopEndLabels = ArrayDeque<String>()
|
||||||
internal val blockLevelVarInits = mutableMapOf<Block, MutableSet<VarDecl>>()
|
internal val blockLevelVarInits = mutableMapOf<Block, MutableSet<VarDecl>>()
|
||||||
@ -1002,21 +1002,6 @@ $endLabel""")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun translateArrayIndexIntoY(expr: ArrayIndexedExpression) {
|
|
||||||
when (val index = expr.arrayspec.index) {
|
|
||||||
is NumericLiteralValue -> throw AssemblyError("this should be optimized directly")
|
|
||||||
is IdentifierReference -> {
|
|
||||||
val indexName = asmIdentifierName(index)
|
|
||||||
out(" ldy $indexName")
|
|
||||||
}
|
|
||||||
// TODO optimize more cases, see translateArrayIndexIntoA
|
|
||||||
else -> {
|
|
||||||
expressionsAsmGen.translateExpression(index)
|
|
||||||
out(" inx | ldy $ESTACK_LO_HEX,x")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun translateExpression(expression: Expression) =
|
internal fun translateExpression(expression: Expression) =
|
||||||
expressionsAsmGen.translateExpression(expression)
|
expressionsAsmGen.translateExpression(expression)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
|
|||||||
import prog8.compiler.toHex
|
import prog8.compiler.toHex
|
||||||
|
|
||||||
|
|
||||||
internal class AssignmentAsmGen(private val program: Program, private val errors: ErrorReporter, private val asmgen: AsmGen) {
|
internal class AssignmentAsmGen(private val program: Program, private val asmgen: AsmGen) {
|
||||||
|
|
||||||
internal fun translate(assign: Assignment) {
|
internal fun translate(assign: Assignment) {
|
||||||
if(assign.isInplace)
|
if(assign.isInplace)
|
||||||
|
@ -194,7 +194,6 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
|
|||||||
|
|
||||||
private val optimizedByteMultiplications = setOf(3,5,6,7,9,10,11,12,13,14,15,20,25,40)
|
private val optimizedByteMultiplications = setOf(3,5,6,7,9,10,11,12,13,14,15,20,25,40)
|
||||||
private val optimizedWordMultiplications = setOf(3,5,6,7,9,10,12,15,20,25,40)
|
private val optimizedWordMultiplications = setOf(3,5,6,7,9,10,12,15,20,25,40)
|
||||||
private val powersOfTwo = setOf(0,1,2,4,8,16,32,64,128,256)
|
|
||||||
|
|
||||||
private fun translateExpression(expr: BinaryExpression) {
|
private fun translateExpression(expr: BinaryExpression) {
|
||||||
val leftIDt = expr.left.inferType(program)
|
val leftIDt = expr.left.inferType(program)
|
||||||
|
@ -103,9 +103,9 @@ val BuiltinFunctions = mapOf(
|
|||||||
FParam("length", setOf(DataType.UBYTE))), null)
|
FParam("length", setOf(DataType.UBYTE))), null)
|
||||||
)
|
)
|
||||||
|
|
||||||
fun builtinMax(array: List<Number>): Number = array.maxBy { it.toDouble() }!!
|
fun builtinMax(array: List<Number>): Number = array.maxByOrNull { it.toDouble() }!!
|
||||||
|
|
||||||
fun builtinMin(array: List<Number>): Number = array.minBy { it.toDouble() }!!
|
fun builtinMin(array: List<Number>): Number = array.minByOrNull { it.toDouble() }!!
|
||||||
|
|
||||||
fun builtinSum(array: List<Number>): Number = array.sumByDouble { it.toDouble() }
|
fun builtinSum(array: List<Number>): Number = array.sumByDouble { it.toDouble() }
|
||||||
|
|
||||||
@ -312,7 +312,7 @@ private fun builtinSin8(args: List<Expression>, position: Position, program: Pro
|
|||||||
throw SyntaxError("sin8 requires one argument", position)
|
throw SyntaxError("sin8 requires one argument", position)
|
||||||
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
||||||
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
||||||
return NumericLiteralValue(DataType.BYTE, (127.0 * sin(rad)).toShort(), position)
|
return NumericLiteralValue(DataType.BYTE, (127.0 * sin(rad)).toInt().toShort(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun builtinSin8u(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
private fun builtinSin8u(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
||||||
@ -320,7 +320,7 @@ private fun builtinSin8u(args: List<Expression>, position: Position, program: Pr
|
|||||||
throw SyntaxError("sin8u requires one argument", position)
|
throw SyntaxError("sin8u requires one argument", position)
|
||||||
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
||||||
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
||||||
return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * sin(rad)).toShort(), position)
|
return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * sin(rad)).toInt().toShort(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun builtinCos8(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
private fun builtinCos8(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
||||||
@ -328,7 +328,7 @@ private fun builtinCos8(args: List<Expression>, position: Position, program: Pro
|
|||||||
throw SyntaxError("cos8 requires one argument", position)
|
throw SyntaxError("cos8 requires one argument", position)
|
||||||
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
||||||
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
||||||
return NumericLiteralValue(DataType.BYTE, (127.0 * cos(rad)).toShort(), position)
|
return NumericLiteralValue(DataType.BYTE, (127.0 * cos(rad)).toInt().toShort(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun builtinCos8u(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
private fun builtinCos8u(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
||||||
@ -336,7 +336,7 @@ private fun builtinCos8u(args: List<Expression>, position: Position, program: Pr
|
|||||||
throw SyntaxError("cos8u requires one argument", position)
|
throw SyntaxError("cos8u requires one argument", position)
|
||||||
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
||||||
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
val rad = constval.number.toDouble() /256.0 * 2.0 * PI
|
||||||
return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * cos(rad)).toShort(), position)
|
return NumericLiteralValue(DataType.UBYTE, (128.0 + 127.5 * cos(rad)).toInt().toShort(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun builtinSin16(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
private fun builtinSin16(args: List<Expression>, position: Position, program: Program): NumericLiteralValue {
|
||||||
@ -375,7 +375,7 @@ private fun builtinSgn(args: List<Expression>, position: Position, program: Prog
|
|||||||
if (args.size != 1)
|
if (args.size != 1)
|
||||||
throw SyntaxError("sgn requires one argument", position)
|
throw SyntaxError("sgn requires one argument", position)
|
||||||
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
|
||||||
return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toShort(), position)
|
return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun numericLiteral(value: Number, position: Position): NumericLiteralValue {
|
private fun numericLiteral(value: Number, position: Position): NumericLiteralValue {
|
||||||
|
@ -179,7 +179,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal class ConstantFoldingOptimizer(private val program: Program, private val errors: ErrorReporter) : AstWalker() {
|
internal class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
|
||||||
private val noModifications = emptyList<IAstModification>()
|
private val noModifications = emptyList<IAstModification>()
|
||||||
|
|
||||||
override fun before(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
|
override fun before(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
|
||||||
|
@ -6,7 +6,6 @@ import prog8.ast.base.*
|
|||||||
import prog8.ast.expressions.*
|
import prog8.ast.expressions.*
|
||||||
import prog8.ast.processing.AstWalker
|
import prog8.ast.processing.AstWalker
|
||||||
import prog8.ast.processing.IAstModification
|
import prog8.ast.processing.IAstModification
|
||||||
import prog8.ast.statements.Assignment
|
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.log2
|
import kotlin.math.log2
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
|
@ -10,7 +10,7 @@ internal fun Program.constantFold(errors: ErrorReporter) {
|
|||||||
if(errors.isEmpty()) {
|
if(errors.isEmpty()) {
|
||||||
replacer.applyModifications()
|
replacer.applyModifications()
|
||||||
|
|
||||||
val optimizer = ConstantFoldingOptimizer(this, errors)
|
val optimizer = ConstantFoldingOptimizer(this)
|
||||||
optimizer.visit(this)
|
optimizer.visit(this)
|
||||||
while (errors.isEmpty() && optimizer.applyModifications() > 0) {
|
while (errors.isEmpty() && optimizer.applyModifications() > 0) {
|
||||||
optimizer.visit(this)
|
optimizer.visit(this)
|
||||||
|
@ -34,7 +34,7 @@ internal class CustomLexer(val modulePath: Path, input: CharStream?) : prog8Lexe
|
|||||||
internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
|
internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
|
||||||
|
|
||||||
|
|
||||||
internal class ModuleImporter(private val errors: ErrorReporter) {
|
internal class ModuleImporter() {
|
||||||
|
|
||||||
internal fun importModule(program: Program, filePath: Path): Module {
|
internal fun importModule(program: Program, filePath: Path): Module {
|
||||||
print("importing '${moduleName(filePath.fileName)}'")
|
print("importing '${moduleName(filePath.fileName)}'")
|
||||||
|
Loading…
Reference in New Issue
Block a user