Skip to content

Commit

Permalink
Merge pull request #51 from CASC-Lang/trait-impl
Browse files Browse the repository at this point in the history
Trait impl implementation phase II: Implement Trait declaration
  • Loading branch information
ChAoSUnItY authored May 9, 2022
2 parents e088543 + 55766dc commit a30f18b
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 143 deletions.
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

0 comments on commit a30f18b

Please sign in to comment.