Skip to content

Commit

Permalink
Introduce StreamArg to better typecheck the io type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Oct 5, 2024
1 parent ed494ef commit 9896a8a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ object MethodArgType {
case object ArrayArg extends MethodArgType {
override def toString = "array"
}
case object StreamArg extends MethodArgType {
override def toString = "io stream"
}

def byDataType(dataType: DataType): Option[MethodArgType] = {
dataType match {
Expand All @@ -35,6 +38,7 @@ object MethodArgType {
case _: BooleanType => Some(BooleanArg)
case _: BytesType => Some(BytesArg)
case _: ArrayType => Some(ArrayArg)
case _: StreamType => Some(StreamArg)
case _ => None
}
}
Expand Down Expand Up @@ -133,6 +137,12 @@ abstract trait CommonMethods[T] extends TypeDetector {
MethodSig0("min", AnyType, arrayMin),
MethodSig0("max", AnyType, arrayMax),
),

StreamArg -> List(
MethodSig0("eof", CalcBooleanType, kaitaiStreamEof),
MethodSig0("pos", CalcIntType, kaitaiStreamPos),
MethodSig0("size", CalcIntType, kaitaiStreamSize),
),
)

/**
Expand Down Expand Up @@ -170,12 +180,6 @@ abstract trait CommonMethods[T] extends TypeDetector {
}
case ut: UserType =>
userTypeField(ut, value, attr.name)
case _: StreamType =>
attr.name match {
case "size" => kaitaiStreamSize(value)
case "eof" => kaitaiStreamEof(value)
case "pos" => kaitaiStreamPos(value)
}
case et: EnumType =>
attr.name match {
case "to_i" => enumToInt(value, et)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ class TypeDetector(provider: TypeProvider) {
case "size" => CalcIntType
case "pos" => CalcIntType
case "eof" => CalcBooleanType
case _ => throw new MethodNotFoundError(attr.name, valType)
// MethodArgType.byDataType returns Some(...) in that case
case _ => throw new MethodNotFoundErrorWithArg(attr.name, MethodArgType.byDataType(valType).get)
}
case et: EnumType =>
attr.name match {
Expand Down

0 comments on commit 9896a8a

Please sign in to comment.