struct fixes

This commit is contained in:
Irmen de Jong 2019-07-12 17:57:56 +02:00
parent 717b5f3b07
commit 7500c6efd0
6 changed files with 53 additions and 42 deletions

View File

@ -76,17 +76,6 @@ artifacts {
archives shadowJar archives shadowJar
} }
task p8vmScript(type: CreateStartScripts) {
mainClassName = "prog8.vm.stackvm.MainKt"
applicationName = "p8vm"
outputDir = new File(project.buildDir, 'scripts')
classpath = jar.outputs.files + project.configurations.runtime
}
applicationDistribution.into("bin") {
from(p8vmScript)
fileMode = 0755
}
// To create a fat-jar use the 'create_compiler_jar' script for now // To create a fat-jar use the 'create_compiler_jar' script for now
// @todo investigate https://imperceptiblethoughts.com/shadow/introduction/ // @todo investigate https://imperceptiblethoughts.com/shadow/introduction/

View File

@ -62,8 +62,9 @@ internal class AstIdentifiersChecker(private val namespace: INameScope) : IAstMo
if(decl.structHasBeenFlattened) if(decl.structHasBeenFlattened)
return decl // don't do this multiple times return decl // don't do this multiple times
val decls: MutableList<IStatement> = decl.struct!!.statements.map { val decls: MutableList<IStatement> = decl.struct!!.statements.withIndex().map {
val member = it as VarDecl val member = it.value as VarDecl
val initvalue = if(decl.value!=null) (decl.value as LiteralValue).arrayvalue!![it.index] else null
VarDecl( VarDecl(
VarDeclType.VAR, VarDeclType.VAR,
member.datatype, member.datatype,
@ -71,7 +72,7 @@ internal class AstIdentifiersChecker(private val namespace: INameScope) : IAstMo
member.arraysize, member.arraysize,
mangledStructMemberName(decl.name, member.name), mangledStructMemberName(decl.name, member.name),
null, null,
member.value, initvalue,
member.isArray, member.isArray,
true, true,
member.position member.position

View File

@ -62,22 +62,23 @@ internal class VarInitValueAndAddressOfCreator(private val namespace: INameScope
) )
} }
if(decl.datatype==DataType.STRUCT) { // if(decl.datatype==DataType.STRUCT) {
// a struct initialization value // println("STRUCT INIT DECL $decl")
// flatten it to assignment statements // // a struct initialization value perhaps
val sourceArray = (decl.value as LiteralValue).arrayvalue!! // // flatten it to assignment statements
val memberAssignments = decl.struct!!.statements.zip(sourceArray).map { member -> // val sourceArray = (decl.value as LiteralValue).arrayvalue!!
val memberDecl = member.first as VarDecl // val memberAssignments = decl.struct!!.statements.zip(sourceArray).map { member ->
val mangled = mangledStructMemberName(decl.name, memberDecl.name) // val memberDecl = member.first as VarDecl
val idref = IdentifierReference(listOf(mangled), decl.position) // val mangled = mangledStructMemberName(decl.name, memberDecl.name)
val target = AssignTarget(null, idref, null, null, decl.position) // val idref = IdentifierReference(listOf(mangled), decl.position)
val assign = VariableInitializationAssignment(target, null, member.second, member.second.position) // val target = AssignTarget(null, idref, null, null, decl.position)
assign // val assign = VariableInitializationAssignment(target, null, member.second, member.second.position)
} // assign
val scope = AnonymousScope(memberAssignments.toMutableList(), decl.position) // }
scope.linkParents(decl.parent) // val scope = AnonymousScope(memberAssignments.toMutableList(), decl.position)
return scope // scope.linkParents(decl.parent)
} // return scope
// }
return decl return decl
} }

View File

@ -63,6 +63,7 @@ fun compileProgram(filepath: Path,
} }
//println(" time2: $time2") //println(" time2: $time2")
val time3 = measureTimeMillis { val time3 = measureTimeMillis {
programAst.removeNopsFlattenAnonScopes()
programAst.reorderStatements() // reorder statements and add type casts, to please the compiler later programAst.reorderStatements() // reorder statements and add type casts, to please the compiler later
} }
//println(" time3: $time3") //println(" time3: $time3")
@ -88,7 +89,7 @@ fun compileProgram(filepath: Path,
programAst.checkValid(compilerOptions) // check if final tree is valid programAst.checkValid(compilerOptions) // check if final tree is valid
programAst.checkRecursion() // check if there are recursive subroutine calls programAst.checkRecursion() // check if there are recursive subroutine calls
printAst(programAst) // printAst(programAst)
// namespace.debugPrint() // namespace.debugPrint()
if(generateVmCode) { if(generateVmCode) {

View File

@ -54,14 +54,10 @@ The compiler will link everything together into one output program at the end.
If you start the compiler without arguments, it will print a short usage text. If you start the compiler without arguments, it will print a short usage text.
For normal use the compiler is invoked with the command: For normal use the compiler is invoked with the command:
``$ java -jar prog8compiler.jar sourcefile.p8`` if you're using the packaged release version, or ``$ java -jar prog8compiler.jar sourcefile.p8``
``$ ./p8compile.sh sourcefile.p8`` if you're running an unpackaged development version. Other options are also available, see the introduction page about how
to build and run the compiler.
If you tell it to save the intermediate code for the prog8 virtual machine, you can
actually run this code with the command:
``$ ./p8vm.sh sourcefile.vm.txt``
By default, assembly code is generated and written to ``sourcefile.asm``. By default, assembly code is generated and written to ``sourcefile.asm``.

View File

@ -3,8 +3,11 @@
~ main { ~ main {
Color blocklevelcolor
sub start() { sub start() {
uword derp
uword derp =44
ubyte[] v = [22,33,44] ubyte[] v = [22,33,44]
Color foreground = [1,2,3] Color foreground = [1,2,3]
@ -16,15 +19,15 @@
c64.CHROUT('\n') c64.CHROUT('\n')
Color background = [255,255,255] ; @todo make zeros if no value is given Color background
Color cursor Color cursor = [255,255,255]
foreground.red=99 foreground.red=99
background.blue=foreground.red background.blue=foreground.red
cursor = [1,2,3] ; assign all members at once cursor = [1,2,3] ; assign all members at once
cursor = v cursor = v
;cursor=foreground ; @todo memberwise assignment cursor = foreground ; @todo memberwise assignment
c64scr.print_ub(foreground.red) c64scr.print_ub(foreground.red)
c64.CHROUT(':') c64.CHROUT(':')
@ -45,9 +48,29 @@
c64scr.print_ub(cursor.blue) c64scr.print_ub(cursor.blue)
c64.CHROUT('\n') c64.CHROUT('\n')
foo()
foo()
foo()
foo()
foo()
foo()
foo()
return return
} }
sub foo() {
Color localcolor
localcolor.red++
c64scr.print_ub(localcolor.red)
c64.CHROUT(':')
c64scr.print_ub(localcolor.green)
c64.CHROUT(':')
c64scr.print_ub(localcolor.blue)
c64.CHROUT('\n')
}
struct Color { struct Color {
ubyte red ubyte red
ubyte green ubyte green