Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jan 19, 2019
1 parent 670a4e0 commit 8b9490c
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 16 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,5 @@
#Sun Jan 13 22:31:17 CET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
2 changes: 1 addition & 1 deletion gradlew
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS=""

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/soywiz/ktcc/NodeVisitor.kt
Expand Up @@ -85,10 +85,18 @@ open class NodeVisitor {
is BasicTypeSpecifier -> visit(it)
is TypeName -> visit(it)
is StructUnionTypeSpecifier -> visit(it)
is StorageClassSpecifier -> visit(it)
is TypedefTypeSpecifier -> visit(it)
else -> error("Unknown TypeSpecifier ${it::class.java}: $it")
}
}

open fun visit(it: TypedefTypeSpecifier) {
}

open fun visit(it: StorageClassSpecifier) {
}

open fun visit(it: List<Node>?) {
if (it != null) for (v in it) visit(v)
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/kotlin/com/soywiz/ktcc/Parser.kt
Expand Up @@ -228,9 +228,9 @@ fun ProgramParser.tryPrimaryExpr(): Expr? = tag {
}
"_Generic" -> {
expect("_Generic", "(")
TODO()
TODO("_Generic")
expect(")")
TODO()
TODO("_Generic")
}
else -> {
when {
Expand Down Expand Up @@ -587,14 +587,14 @@ fun ProgramParser.tryDirectAbstractDeclarator(): Node? {
loop@while (true) {
out = when (peek()) {
"(" -> {
TODO()
TODO("tryDirectAbstractDeclarator")
expect("(")
val adc = tryAbstractDeclarator()
expect(")")
adc
}
"[" -> {
TODO()
TODO("tryDirectAbstractDeclarator")
}
else -> break@loop
}
Expand Down Expand Up @@ -622,6 +622,12 @@ fun ProgramParser.declarationSpecifiers(): ListTypeSpecifier? {
//if (spec is TypedefTypeSpecifier) break // @TODO: Check this!
out += spec
}
if (hasTypedef) {
val name = out.filterIsInstance<TypedefTypeSpecifier>().firstOrNull() ?: error("Typedef doesn't include a name")
typedefTypes[name.id] = Unit
//out.firstIsInstance<TypedefTypeSpecifier>().id
//println("hasTypedef: $hasTypedef")
}
return if (out.isEmpty()) null else ListTypeSpecifier(out)
}

Expand Down Expand Up @@ -905,7 +911,8 @@ fun ProgramParser.tryDeclaration(): Decl? = tag {

data class Declaration(val specs: ListTypeSpecifier, val initDeclaratorList: List<InitDeclarator>): Decl()

fun ProgramParser.declaration(): Decl = tryDeclaration() ?: throw ExpectException("TODO")
fun ProgramParser.declaration(): Decl = tryDeclaration()
?: throw ExpectException("TODO: ProgramParser.declaration")

// (6.8.2) compound-statement:
fun ProgramParser.compoundStatement(): Stms = tag {
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/com/soywiz/ktcc/Types.kt
Expand Up @@ -14,7 +14,7 @@ data class IntFType(val signed: Boolean?, val long: Int, var size: Int?) : FType
} else {
if (rsigned) "Int" else "UInt"
}
else -> TODO()
else -> TODO("IntFType")
}
}
}
Expand All @@ -31,6 +31,10 @@ class UnknownFType(val reason: Any?) : FType() {
override fun toString(): String = "UnknownFType${reason}"
}

class TypedefFType(val id: String) : FType() {
override fun toString(): String = id
}

fun combine(l: FType, r: FType): FType {
if (l is IntFType && r is IntFType) {
return IntFType(r.signed ?: l.signed, l.long + r.long, r.size ?: l.size)
Expand Down Expand Up @@ -61,6 +65,12 @@ fun generateFinalType(type: TypeSpecifier): FType {
is StructUnionTypeSpecifier -> {
return StructFType(type)
}
is StorageClassSpecifier -> {
return IntFType(null, 0, null)
}
is TypedefTypeSpecifier -> {
return TypedefFType(type.id)
}
}
TODO("${type::class}: $type")
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/kotlin/com/soywiz/ktcc/gen/KotlinGenerator.kt
Expand Up @@ -161,16 +161,16 @@ class KotlinGenerator {
else -> TODO(spec.id)
}
}
else -> TODO()
else -> TODO("toKotlinType")
}
}
when {
void -> "Unit"
integral -> "Int"
else -> TODO()
else -> TODO("toKotlinType")
}
}
else -> TODO()
else -> TODO("toKotlinType")
}

fun generate(it: CType): String = it.toKotlinType()
Expand Down Expand Up @@ -213,7 +213,7 @@ class KotlinGenerator {
}
}
is ArrayInitExpr -> {
val structType = (leftType!! as StructFType).getProgramType()
val structType = leftType!!.getProgramType()
val structName = structType.name
val inits = LinkedHashMap<String, String>()
var index = 0
Expand All @@ -235,4 +235,9 @@ class KotlinGenerator {
}

fun StructFType.getProgramType() = analyzer.getType(this.spec)
fun FType.getProgramType() = when (this) {
is StructFType -> getProgramType()
is TypedefFType -> analyzer.getType(this.id)
else -> error("$this")
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/soywiz/ktcc/util/Quote.kt
Expand Up @@ -33,7 +33,7 @@ val String.cunescaped: String get() {
'x' -> {
val h0 = this[n++]
val h1 = this[n++]
TODO()
TODO("cunescaped")
}
}
}
Expand Down
@@ -1 +1,3 @@
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
#org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory
#org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonCompileScriptEngine
14 changes: 14 additions & 0 deletions src/test/kotlin/com/soywiz/ktcc/KotlinGeneratorTest.kt
Expand Up @@ -63,6 +63,20 @@ class KotlinGeneratorTest {
""".trimIndent().tokenize().program()))
}

@Test
//@Ignore
fun struct2() {
println(KotlinGenerator().generate("""
typedef struct Demo {
int a;
char *b;
} Demo;
void main() {
Demo demo = {1};
}
""".trimIndent().tokenize().program()))
}

@Test
fun test() {
println(KotlinGenerator().generate("""
Expand Down

0 comments on commit 8b9490c

Please sign in to comment.