Skip to content

Commit

Permalink
Reorganize EtsIR DTOs (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen authored Jan 15, 2025
1 parent d42737d commit 983d353
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 592 deletions.
38 changes: 0 additions & 38 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsConstant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,41 +89,3 @@ object EtsUndefinedConstant : EtsConstant {
return visitor.visit(this)
}
}

data class EtsArrayLiteral(
val elements: List<EtsEntity>,
override val type: EtsType,
) : EtsConstant {

init {
if (type is EtsArrayType) {
require(type.dimensions == 1) {
"Array type of array literal must have exactly one dimension"
}
}
}

override fun toString(): String {
return elements.joinToString(prefix = "[", postfix = "]")
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

// TODO: see TypeScript reference for different types of elements inside object literals, such as property assignment, shorthand property assignment, method definition, spread assignment, and getter/setter accessors.
data class EtsObjectLiteral(
val properties: List<Pair<String, EtsEntity>>,
override val type: EtsType,
) : EtsConstant {
override fun toString(): String {
return properties.joinToString(prefix = "{", postfix = "}") { (name, value) ->
"$name: $value"
}
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}
42 changes: 42 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,48 @@ data class EtsParameterRef(
}
}

data class EtsCaughtExceptionRef(
override val type: EtsType,
) : EtsValue {
override fun toString(): String {
return "catch($type)"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsGlobalRef(
val name: String,
val ref: EtsValue?, // TODO: check whether it could be EtsEntity at best
) : EtsValue {
override val type: EtsType
get() = ref?.type ?: EtsUnknownType

override fun toString(): String {
return "global $name"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsClosureFieldRef(
val base: EtsLocal,
val fieldName: String,
override val type: EtsType,
) : EtsValue {
override fun toString(): String {
return "$base.$fieldName"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsArrayAccess(
val array: EtsValue,
val index: EtsValue,
Expand Down
16 changes: 16 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsStmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface EtsStmt : CommonInst {
fun visit(stmt: EtsGotoStmt): R
fun visit(stmt: EtsIfStmt): R
fun visit(stmt: EtsSwitchStmt): R
fun visit(stmt: EtsRawStmt): R

interface Default<out R> : Visitor<R> {
override fun visit(stmt: EtsNopStmt): R = defaultVisit(stmt)
Expand All @@ -52,6 +53,7 @@ interface EtsStmt : CommonInst {
override fun visit(stmt: EtsGotoStmt): R = defaultVisit(stmt)
override fun visit(stmt: EtsIfStmt): R = defaultVisit(stmt)
override fun visit(stmt: EtsSwitchStmt): R = defaultVisit(stmt)
override fun visit(stmt: EtsRawStmt): R = defaultVisit(stmt)

fun defaultVisit(stmt: EtsStmt): R
}
Expand Down Expand Up @@ -167,3 +169,17 @@ data class EtsSwitchStmt(
return visitor.visit(this)
}
}

data class EtsRawStmt(
override val location: EtsInstLocation,
val type: String,
val text: String,
) : EtsStmt {
override fun toString(): String {
return text
}

override fun <R> accept(visitor: EtsStmt.Visitor<R>): R {
return visitor.visit(this)
}
}
16 changes: 16 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface EtsType : CommonType, CommonTypeName {
fun visit(type: EtsAliasType): R
fun visit(type: EtsAnnotationNamespaceType): R
fun visit(type: EtsAnnotationTypeQueryType): R
fun visit(type: EtsLexicalEnvType): R

interface Default<R> : Visitor<R> {
override fun visit(type: EtsAnyType): R = defaultVisit(type)
Expand All @@ -74,6 +75,7 @@ interface EtsType : CommonType, CommonTypeName {
override fun visit(type: EtsAliasType): R = defaultVisit(type)
override fun visit(type: EtsAnnotationNamespaceType): R = defaultVisit(type)
override fun visit(type: EtsAnnotationTypeQueryType): R = defaultVisit(type)
override fun visit(type: EtsLexicalEnvType): R = defaultVisit(type)

fun defaultVisit(type: EtsType): R
}
Expand Down Expand Up @@ -372,3 +374,17 @@ data class EtsAnnotationTypeQueryType(
return visitor.visit(this)
}
}

data class EtsLexicalEnvType(
val nestedMethod: EtsMethodSignature,
val closures: List<EtsLocal>,
) : EtsType {
override val typeName: String
get() = closures.joinToString(prefix = "[", postfix = "]")

override fun toString(): String = typeName

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}
10 changes: 6 additions & 4 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ interface EtsValue : EtsEntity, CommonValue {
fun visit(value: EtsNumberConstant): R
fun visit(value: EtsNullConstant): R
fun visit(value: EtsUndefinedConstant): R
fun visit(value: EtsArrayLiteral): R
fun visit(value: EtsObjectLiteral): R

// Ref
fun visit(value: EtsThis): R
fun visit(value: EtsParameterRef): R
fun visit(value: EtsCaughtExceptionRef): R
fun visit(value: EtsGlobalRef): R
fun visit(value: EtsClosureFieldRef): R
fun visit(value: EtsArrayAccess): R
fun visit(value: EtsInstanceFieldRef): R
fun visit(value: EtsStaticFieldRef): R
Expand All @@ -46,11 +47,12 @@ interface EtsValue : EtsEntity, CommonValue {
override fun visit(value: EtsNumberConstant): R = defaultVisit(value)
override fun visit(value: EtsNullConstant): R = defaultVisit(value)
override fun visit(value: EtsUndefinedConstant): R = defaultVisit(value)
override fun visit(value: EtsArrayLiteral): R = defaultVisit(value)
override fun visit(value: EtsObjectLiteral): R = defaultVisit(value)

override fun visit(value: EtsThis): R = defaultVisit(value)
override fun visit(value: EtsParameterRef): R = defaultVisit(value)
override fun visit(value: EtsCaughtExceptionRef): R = defaultVisit(value)
override fun visit(value: EtsGlobalRef): R = defaultVisit(value)
override fun visit(value: EtsClosureFieldRef): R = defaultVisit(value)
override fun visit(value: EtsArrayAccess): R = defaultVisit(value)
override fun visit(value: EtsInstanceFieldRef): R = defaultVisit(value)
override fun visit(value: EtsStaticFieldRef): R = defaultVisit(value)
Expand Down
Loading

0 comments on commit 983d353

Please sign in to comment.