mirror of
https://github.com/irmen/prog8.git
synced 2024-12-25 23:29:55 +00:00
tweaks about initialization values
This commit is contained in:
parent
61af72b906
commit
4dbf4b2005
@ -80,8 +80,7 @@ class GlobalNamespace(val modules: List<Module>): Node, INameScope {
|
||||
if(struct.statements.any { (it as VarDecl).name == scopedName.last()}) {
|
||||
// return ref to the mangled name variable
|
||||
val mangled = mangledStructMemberName(thing.name, scopedName.last())
|
||||
val mangledVar = thing.definingScope().getLabelOrVariable(mangled)
|
||||
return mangledVar
|
||||
return thing.definingScope().getLabelOrVariable(mangled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,8 +144,7 @@ interface INameScope {
|
||||
if(struct.statements.any { (it as VarDecl).name == scopedName.last()}) {
|
||||
// return ref to the mangled name variable
|
||||
val mangled = mangledStructMemberName(thing.name, scopedName.last())
|
||||
val mangledVar = thing.definingScope().getLabelOrVariable(mangled)
|
||||
return mangledVar
|
||||
return thing.definingScope().getLabelOrVariable(mangled)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,9 +102,9 @@ private fun prog8Parser.StatementContext.toAst() : IStatement {
|
||||
it.varname.text,
|
||||
it.structname.text,
|
||||
null,
|
||||
false,
|
||||
false,
|
||||
it.toPosition()
|
||||
isArray = false,
|
||||
autogeneratedDontRemove = false,
|
||||
position = it.toPosition()
|
||||
)
|
||||
}
|
||||
|
||||
@ -522,10 +522,6 @@ private fun prog8Parser.BooleanliteralContext.toAst() = when(text) {
|
||||
private fun prog8Parser.ArrayliteralContext.toAst() : Array<IExpression> =
|
||||
expression().map { it.toAst() }.toTypedArray()
|
||||
|
||||
private fun prog8Parser.StructliteralContext.toAst() : Array<IExpression> =
|
||||
expression().map { it.toAst() }.toTypedArray()
|
||||
|
||||
|
||||
private fun prog8Parser.If_stmtContext.toAst(): IfStatement {
|
||||
val condition = expression().toAst()
|
||||
val trueStatements = statement_block()?.toAst() ?: mutableListOf(statement().toAst())
|
||||
|
@ -210,10 +210,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi
|
||||
val lval = returnStmt.value as? NumericLiteralValue
|
||||
if(lval!=null) {
|
||||
val adjusted = lval.cast(subroutine.returntypes.single())
|
||||
if(adjusted!=null && adjusted !== lval)
|
||||
newValue = adjusted
|
||||
else
|
||||
newValue = lval
|
||||
newValue = if(adjusted!=null && adjusted !== lval) adjusted else lval
|
||||
} else {
|
||||
newValue = returnStmt.value!!
|
||||
}
|
||||
|
@ -232,14 +232,14 @@ internal class StatementReorderer(private val program: Program): IAstModifyingVi
|
||||
return assg // do NOT flatten it at this point!! (the compiler will take care if it, later, if needed)
|
||||
|
||||
val assignments = flattenStructAssignmentFromIdentifier(assg, program) // 'structvar1 = structvar2'
|
||||
if(assignments.isEmpty()) {
|
||||
return if(assignments.isEmpty()) {
|
||||
// something went wrong (probably incompatible struct types)
|
||||
// we'll get an error later from the AstChecker
|
||||
return assg
|
||||
assg
|
||||
} else {
|
||||
val scope = AnonymousScope(assignments.toMutableList(), assg.position)
|
||||
scope.linkParents(assg.parent)
|
||||
return scope
|
||||
scope
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,17 @@ internal class VarInitValueAndAddressOfCreator(private val namespace: INameScope
|
||||
|
||||
override fun visit(decl: VarDecl): IStatement {
|
||||
super.visit(decl)
|
||||
|
||||
if(decl.isArray && decl.value==null) {
|
||||
// array datatype without initialization value, add list of zeros
|
||||
val arraysize = decl.arraysize!!.size()!!
|
||||
val array = ReferenceLiteralValue(decl.datatype, null,
|
||||
Array(arraysize) { NumericLiteralValue.optimalInteger(0, decl.position) },
|
||||
null, decl.position)
|
||||
array.addToHeap(heap)
|
||||
decl.value = array
|
||||
}
|
||||
|
||||
if(decl.type!= VarDeclType.VAR || decl.value==null)
|
||||
return decl
|
||||
|
||||
@ -47,7 +58,7 @@ internal class VarInitValueAndAddressOfCreator(private val namespace: INameScope
|
||||
}
|
||||
else
|
||||
declvalue
|
||||
val identifierName = listOf(decl.name) // // TODO this was: (scoped name) decl.scopedname.split(".")
|
||||
val identifierName = listOf(decl.name) // this was: (scoped name) decl.scopedname.split(".")
|
||||
return VariableInitializationAssignment(
|
||||
AssignTarget(null, IdentifierReference(identifierName, decl.position), null, null, decl.position),
|
||||
null,
|
||||
@ -55,6 +66,7 @@ internal class VarInitValueAndAddressOfCreator(private val namespace: INameScope
|
||||
decl.position
|
||||
)
|
||||
}
|
||||
|
||||
return decl
|
||||
}
|
||||
|
||||
|
@ -356,24 +356,6 @@ data class AssignTarget(val register: Register?,
|
||||
return null
|
||||
}
|
||||
|
||||
fun shortString(withTypePrefix: Boolean=false): String {
|
||||
if(register!=null)
|
||||
return (if(withTypePrefix) "0register::" else "") + register.name
|
||||
if(identifier!=null)
|
||||
return (if(withTypePrefix) "3identifier::" else "") + identifier.nameInSource.last()
|
||||
if(arrayindexed!=null)
|
||||
return (if(withTypePrefix) "2arrayidx::" else "") + arrayindexed.identifier.nameInSource.last()
|
||||
val address = memoryAddress?.addressExpression
|
||||
if(address is NumericLiteralValue)
|
||||
return (if(withTypePrefix) "1address::" else "") +address.number.toString()
|
||||
else if(address is ReferenceLiteralValue)
|
||||
throw FatalAstException("assigntarget is a reference literal")
|
||||
return if(withTypePrefix) "???::???" else "???"
|
||||
}
|
||||
|
||||
fun isMemoryMapped(namespace: INameScope): Boolean =
|
||||
memoryAddress!=null || (identifier?.targetVarDecl(namespace)?.type== VarDeclType.MEMORY)
|
||||
|
||||
infix fun isSameAs(value: IExpression): Boolean {
|
||||
return when {
|
||||
this.memoryAddress!=null -> false
|
||||
|
@ -8,12 +8,12 @@ import prog8.ast.processing.IAstVisitor
|
||||
import prog8.ast.statements.*
|
||||
|
||||
class AstToSourceCode(val output: (text: String) -> Unit, val program: Program): IAstVisitor {
|
||||
var scopelevel = 0
|
||||
private var scopelevel = 0
|
||||
|
||||
fun indent(s: String) = " ".repeat(scopelevel) + s
|
||||
fun outputln(text: String) = output(text + "\n")
|
||||
fun outputlni(s: Any) = outputln(indent(s.toString()))
|
||||
fun outputi(s: Any) = output(indent(s.toString()))
|
||||
private fun indent(s: String) = " ".repeat(scopelevel) + s
|
||||
private fun outputln(text: String) = output(text + "\n")
|
||||
private fun outputlni(s: Any) = outputln(indent(s.toString()))
|
||||
private fun outputi(s: Any) = output(indent(s.toString()))
|
||||
|
||||
override fun visit(program: Program) {
|
||||
outputln("============= PROGRAM ${program.name} (FROM AST) ===============")
|
||||
@ -73,7 +73,7 @@ class AstToSourceCode(val output: (text: String) -> Unit, val program: Program):
|
||||
output("\n")
|
||||
}
|
||||
|
||||
fun datatypeString(dt: DataType): String {
|
||||
private fun datatypeString(dt: DataType): String {
|
||||
return when(dt) {
|
||||
in NumericDatatypes -> dt.toString().toLowerCase()
|
||||
in StringDatatypes -> dt.toString().toLowerCase()
|
||||
|
@ -63,6 +63,11 @@ fun compileProgram(filepath: Path,
|
||||
//println(" time2: $time2")
|
||||
val time3 = measureTimeMillis {
|
||||
programAst.removeNopsFlattenAnonScopes()
|
||||
|
||||
// if you want to print the AST, do it before shuffling the statements around below
|
||||
//printAst(programAst)
|
||||
|
||||
|
||||
programAst.reorderStatements() // reorder statements and add type casts, to please the compiler later
|
||||
}
|
||||
//println(" time3: $time3")
|
||||
@ -88,9 +93,6 @@ fun compileProgram(filepath: Path,
|
||||
programAst.checkValid(compilerOptions) // check if final tree is valid
|
||||
programAst.checkRecursion() // check if there are recursive subroutine calls
|
||||
|
||||
// printAst(programAst)
|
||||
// namespace.debugPrint()
|
||||
|
||||
if(generateVmCode) {
|
||||
// compile the syntax tree into stackvmProg form, and optimize that
|
||||
val compiler = Compiler(programAst)
|
||||
|
@ -421,21 +421,7 @@ class IntermediateProgram(val name: String, var loadAddress: Int, val heap: Heap
|
||||
if(litval!=null){
|
||||
RuntimeValue(decl.datatype, heapId = litval.heapId)
|
||||
} else {
|
||||
// uninitialized array. fill it with zeros.
|
||||
val arraysize = decl.arraysize!!.size()!!
|
||||
val heapId =
|
||||
when(decl.datatype){
|
||||
DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W -> {
|
||||
val array = Array(arraysize) { IntegerOrAddressOf(0, null) }
|
||||
heap.addIntegerArray(decl.datatype, array)
|
||||
}
|
||||
DataType.ARRAY_F -> {
|
||||
val array = DoubleArray(arraysize) { 0.0 }
|
||||
heap.addDoublesArray(array)
|
||||
}
|
||||
else -> throw CompilerException("weird array dt")
|
||||
}
|
||||
RuntimeValue(decl.datatype, heapId=heapId)
|
||||
throw CompilerException("initialization value expected")
|
||||
}
|
||||
}
|
||||
DataType.STRUCT -> {
|
||||
|
@ -6,7 +6,6 @@ import prog8.ast.expressions.DirectMemoryRead
|
||||
import prog8.ast.expressions.IdentifierReference
|
||||
import prog8.ast.expressions.NumericLiteralValue
|
||||
import prog8.ast.expressions.ReferenceLiteralValue
|
||||
import prog8.ast.statements.VarDecl
|
||||
import prog8.compiler.CompilerException
|
||||
import kotlin.math.*
|
||||
|
||||
|
@ -2,7 +2,6 @@ package prog8.optimizer
|
||||
|
||||
import prog8.ast.*
|
||||
import prog8.ast.base.AstException
|
||||
import prog8.ast.statements.NopStatement
|
||||
import prog8.parser.ParsingFailedError
|
||||
|
||||
|
||||
|
@ -3,8 +3,6 @@ package prog8.vm
|
||||
import prog8.ast.base.*
|
||||
import prog8.ast.expressions.NumericLiteralValue
|
||||
import prog8.ast.expressions.ReferenceLiteralValue
|
||||
import prog8.ast.statements.StructDecl
|
||||
import prog8.ast.statements.ZeropageWish
|
||||
import prog8.compiler.HeapValues
|
||||
import prog8.compiler.target.c64.Petscii
|
||||
import kotlin.math.abs
|
||||
@ -119,20 +117,6 @@ open class RuntimeValue(val type: DataType, num: Number?=null, val str: String?=
|
||||
}
|
||||
}
|
||||
|
||||
fun asNumericLiteralValue(): NumericLiteralValue {
|
||||
return when(type) {
|
||||
in ByteDatatypes -> NumericLiteralValue(type, byteval!!, Position("", 0, 0, 0))
|
||||
in WordDatatypes -> NumericLiteralValue(type, wordval!!, Position("", 0, 0, 0))
|
||||
DataType.FLOAT -> NumericLiteralValue(type, floatval!!, Position("", 0, 0, 0))
|
||||
in PassByReferenceDatatypes -> TODO("passbyref???")
|
||||
// in StringDatatypes -> NumericLiteralValue(type, strvalue = str, Position("", 0, 0, 0))
|
||||
// in ArrayDatatypes -> NumericLiteralValue(type,
|
||||
// arrayvalue = array?.map { NumericLiteralValue.optimalNumeric(it, Position("", 0, 0, 0)) }?.toTypedArray(),
|
||||
// Position("", 0, 0, 0))
|
||||
else -> throw IllegalArgumentException("weird source value $this")
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when(type) {
|
||||
DataType.UBYTE -> "ub:%02x".format(byteval)
|
||||
|
@ -413,9 +413,9 @@ class AstVm(val program: Program) {
|
||||
val elementType = stmt.target.arrayindexed!!.inferType(program)!!
|
||||
val index = evaluate(stmt.target.arrayindexed!!.arrayspec.index, evalCtx).integerValue()
|
||||
var value = RuntimeValue(elementType, arrayvalue.array!![index].toInt())
|
||||
when {
|
||||
stmt.operator == "++" -> value=value.inc()
|
||||
stmt.operator == "--" -> value=value.dec()
|
||||
value = when {
|
||||
stmt.operator == "++" -> value.inc()
|
||||
stmt.operator == "--" -> value.dec()
|
||||
else -> throw VmExecutionException("strange postincdec operator $stmt")
|
||||
}
|
||||
arrayvalue.array[index] = value.numericValue()
|
||||
|
@ -1,18 +0,0 @@
|
||||
package prog8.vm.astvm
|
||||
|
||||
import prog8.ast.INameScope
|
||||
import java.util.*
|
||||
|
||||
class CallStack {
|
||||
|
||||
private val stack = Stack<Pair<INameScope, Int>>()
|
||||
|
||||
fun pop(): Pair<INameScope, Int> {
|
||||
return stack.pop()
|
||||
}
|
||||
|
||||
fun push(scope: INameScope, index: Int) {
|
||||
stack.push(Pair(scope, index))
|
||||
}
|
||||
|
||||
}
|
@ -18,7 +18,7 @@ class BitmapScreenPanel : KeyListener, JPanel() {
|
||||
private val g2d = image.graphics as Graphics2D
|
||||
private var cursorX: Int=0
|
||||
private var cursorY: Int=0
|
||||
val keyboardBuffer: Deque<Char> = LinkedList<Char>()
|
||||
val keyboardBuffer: Deque<Char> = LinkedList()
|
||||
|
||||
init {
|
||||
val size = Dimension(image.width * SCALING, image.height * SCALING)
|
||||
|
@ -5,24 +5,22 @@
|
||||
|
||||
~ main {
|
||||
|
||||
struct Color {
|
||||
word red
|
||||
byte green
|
||||
float blue
|
||||
}
|
||||
float[5] flarray
|
||||
byte[5] barray
|
||||
uword[5] uwarray
|
||||
|
||||
sub start() {
|
||||
Color rgb1 = {1,2,3.44}
|
||||
Color rgb2
|
||||
|
||||
rgb2 = {22233, 33, 1.1} ; @todo implicit type conversion
|
||||
c64scr.print_b(rgb1.green)
|
||||
c64scr.print_uw(flarray)
|
||||
c64.CHROUT('=')
|
||||
c64flt.print_f(flarray[0])
|
||||
c64.CHROUT('\n')
|
||||
c64scr.print_b(rgb2.green)
|
||||
c64scr.print_uw(barray)
|
||||
c64.CHROUT('=')
|
||||
c64scr.print_b(barray[0])
|
||||
c64.CHROUT('\n')
|
||||
|
||||
rgb1=rgb2
|
||||
c64scr.print_b(rgb1.green)
|
||||
c64scr.print_uw(uwarray)
|
||||
c64.CHROUT('=')
|
||||
c64scr.print_uw(uwarray[0])
|
||||
c64.CHROUT('\n')
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user