Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait impl implementation phase II: Implement Trait declaration #51

Merged
merged 8 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/kotlin/org/casc/lang/ast/ClassInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import org.objectweb.asm.Opcodes
data class ClassInstance(
override val packageReference: Reference?,
val accessorToken: Token?,
val abstrToken: Token?, // Unused
val mutKeyword: Token?,
val classKeyword: Token?,
override val typeReference: Reference,
override val fields: List<Field>,
override val accessor: Accessor = Accessor.fromString(accessorToken?.literal)
) : TypeInstance(packageReference, typeReference), HasFlag {
) : TypeInstance(), HasFlag {
override val flag: Int by lazy {
var flag = Opcodes.ACC_SUPER
flag += accessor.access
flag += abstrToken.getOrElse(Opcodes.ACC_ABSTRACT)
flag += mutKeyword.getOrElse(0, Opcodes.ACC_FINAL)
flag
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/casc/lang/ast/Constructor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ data class Constructor(
val superKeyword: Token?,
val selfKeyword: Token?,
val parentConstructorArguments: List<Expression?>,
var ownerType: Type? = null,
var parentType: Type? = null,
var ownerType: ClassType? = null,
var parentType: ClassType? = null,
val accessor: Accessor = Accessor.fromString(accessorToken?.literal),
var parameterTypes: List<Type?> = listOf(),
var parentConstructorArgumentsTypes: List<Type?> = listOf(),
Expand All @@ -31,6 +31,7 @@ data class Constructor(
override fun asSignature(): FunctionSignature =
FunctionSignature(
ownerReference!!,
ownerType,
companion = true,
mutable = false,
accessor,
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/org/casc/lang/ast/Field.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.objectweb.asm.Opcodes
data class Field(
val ownerReference: Reference?,
val accessorToken: Token?,
val abstrKeyword: Token?,
val mutKeyword: Token?,
val compKeyword: Token?,
val name: Token?,
Expand All @@ -16,8 +17,13 @@ data class Field(
) : HasDescriptor, HasFlag {
override val descriptor: String
get() = type?.descriptor ?: ""
override val flag: Int =
mutKeyword.getOrElse(0, Opcodes.ACC_FINAL) + accessor.access + compKeyword.getOrElse(Opcodes.ACC_STATIC)
override val flag: Int by lazy {
var flag = accessor.access
flag += compKeyword.getOrElse(Opcodes.ACC_STATIC)
flag += abstrKeyword.getOrElse(Opcodes.ACC_ABSTRACT)
flag += mutKeyword.getOrElse(0, Opcodes.ACC_FINAL)
flag
}

fun asClassField(): TypeField =
TypeField(
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/org/casc/lang/ast/Function.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package org.casc.lang.ast
import org.casc.lang.table.*
import org.casc.lang.utils.getOrElse
import org.objectweb.asm.Opcodes
import java.lang.reflect.Modifier

data class Function(
val ownerReference: Reference?,
val accessorToken: Token?,
val ovrdKeyword: Token?,
val abstrKeyword: Token?,
val mutKeyword: Token?,
val selfKeyword: Token?, // determine whether function is companion
val name: Token?,
val parameters: List<Parameter>,
val returnTypeReference: Reference?,
val statements: List<Statement>,
val statements: List<Statement>?,
val accessor: Accessor = Accessor.fromString(accessorToken?.literal),
var ownerType: Type? = null,
var ownerType: ClassType? = null,
var parameterTypes: List<Type?>? = listOf(),
var returnType: Type? = null
) : Method(), HasDescriptor, HasFlag, HasSignature {
Expand All @@ -25,12 +27,19 @@ data class Function(
s + type?.descriptor
}
})${returnType?.descriptor}"
override val flag: Int =
mutKeyword.getOrElse(0, Opcodes.ACC_FINAL) + accessor.access + selfKeyword.getOrElse(0, Opcodes.ACC_STATIC)
override val flag: Int by lazy {
var flag = accessor.access
flag += selfKeyword.getOrElse(0, Opcodes.ACC_STATIC)
flag += statements.getOrElse(abstrKeyword.getOrElse(Opcodes.ACC_ABSTRACT), Opcodes.ACC_ABSTRACT)
if (ownerType?.isTrait == false)
flag += mutKeyword.getOrElse(0, Opcodes.ACC_FINAL)
flag
}

override fun asSignature() =
FunctionSignature(
ownerReference!!,
ownerType,
selfKeyword == null,
mutKeyword != null,
accessor,
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/org/casc/lang/ast/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ data class Token(var literal: String, val type: TokenType, val pos: Position) {

companion object {
val keywords = arrayOf(
"package", "use", "class", "impl", "comp", "pub", "prot", "intl", "priv", "true", "false", "null",
"mut", "fn", "if", "else", "for", "return", "as", "is", "new", "self", "super", "ovrd"
"package", "use", "class", "trait", "impl", "comp", "pub", "prot", "intl", "priv", "true", "false", "null",
"mut", "fn", "if", "else", "for", "return", "as", "is", "new", "self", "super", "ovrd", "abstr"
)
}

Expand All @@ -19,6 +19,9 @@ data class Token(var literal: String, val type: TokenType, val pos: Position) {
fun isClassKeyword(): Boolean =
literal == "class"

fun isTraitKeyword(): Boolean =
literal == "trait"

fun isImplKeyword(): Boolean =
literal == "impl"

Expand Down Expand Up @@ -61,6 +64,9 @@ data class Token(var literal: String, val type: TokenType, val pos: Position) {
fun isOvrdKeyword(): Boolean =
literal == "ovrd"

fun isAbstrKeyword(): Boolean =
literal == "abstr"

fun isAccessorKeyword(): Boolean =
Accessor.validKeywords.contains(literal)
}
18 changes: 18 additions & 0 deletions src/main/kotlin/org/casc/lang/ast/TraitInstance.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.casc.lang.ast

import org.casc.lang.table.HasFlag
import org.casc.lang.table.Reference
import org.objectweb.asm.Opcodes

data class TraitInstance(
override val packageReference: Reference?,
val accessorToken: Token?,
val traitKeyword: Token?,
override val typeReference: Reference,
override val fields: List<Field>,
override val accessor: Accessor = Accessor.fromString(accessorToken?.literal)
) : TypeInstance(), HasFlag {
override val flag: Int by lazy {
accessor.access + Opcodes.ACC_ABSTRACT + Opcodes.ACC_INTERFACE
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/org/casc/lang/ast/TypeInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.casc.lang.ast

import org.casc.lang.table.Reference

sealed class TypeInstance(open val packageReference: Reference?, open val typeReference: Reference) {
sealed class TypeInstance {
abstract val packageReference: Reference?
abstract val typeReference: Reference
abstract val fields: List<Field>
var impl: Impl? = null
var traitImpls: List<TraitImpl>? = null
Expand Down
Loading