Skip to content

Commit

Permalink
extract virtual methods as well
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Mar 9, 2024
1 parent 9ce911b commit d672aa0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 59 deletions.
58 changes: 3 additions & 55 deletions fluent-generator/src/main/scala/AugmentedNamespace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,62 +51,10 @@ trait ClassLike:
lazy val methods: Seq[Method] =
collect[Method]

lazy val virtualMethods: Seq[Virtualu45method] =
collect[Virtualu45method]

lazy val constructors: Seq[Constructor] =
collect[Constructor]
end ClassLike

private def extractParams(
c: Seq[DataRecord[Any]]
): Seq[Parameter | Instanceu45parameter] =
c
.collect:
case d if d.value.isInstanceOf[Parameters] =>
d.as[Parameters]
.parametersoption
.collect[Parameter | Instanceu45parameter]:
case d if d.value.isInstanceOf[Parameter] => d.as[Parameter]
case d if d.value.isInstanceOf[Instanceu45parameter] =>
d.as[Instanceu45parameter]
.flatten

extension (c: Constructor) def parameters = extractParams(c.constructoroption)

extension (c: Method)
def isThrowing = c.throws.exists(_.toString == "1")
def parameters: Seq[Parameter | Instanceu45parameter] = extractParams(
c.methodoption
)
def instanceParameter: Option[Instanceu45parameter] =
c.methodoption.collectFirst:
case d if d.value.isInstanceOf[Instanceu45parameter] =>
d.as[Instanceu45parameter]

def returnType: Option[Type | ArrayType] = c.methodoption
.collectFirst:
case d if d.value.isInstanceOf[Returnu45value] => d.as[Returnu45value]
.flatMap: rv =>
rv.returnu45valueoption.collectFirst:
case d if d.value.isInstanceOf[Type] => d.as[Type]
case d if d.value.isInstanceOf[ArrayType] => d.as[ArrayType]
end extension

extension [T](c: Seq[DataRecord[T]])
def doc =
c.collectFirst:
case d if d.value.isInstanceOf[Doc] => d.as[Doc]

extension (c: Seq[DataRecord[Any]])
def tpe =
c.collectFirst:
case d if d.value.isInstanceOf[Type] => d.as[Type]

extension (c: Parameter | Instanceu45parameter)
def tpe: Option[Type | ArrayType] =
val data =
c match
case p: Parameter => p.parameteroption
case p: Instanceu45parameter => p.instanceu45parameteroption

data.collectFirst:
case d if d.value.isInstanceOf[Type] => d.as[Type]
case d if d.value.isInstanceOf[ArrayType] => d.as[ArrayType]
3 changes: 0 additions & 3 deletions fluent-generator/src/main/scala/renderClassMethod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def renderClassMethod(cls: AugmentedClass, meth: Method)(using
val camelName = camelify(meth.name)
val cMethod = meth.identifier

scribe.info(s"Method: ${meth.name} ${meth.isThrowing}")

if meth.isThrowing then coll.add(importGResultEffect)

val renderedParameters =
Expand Down Expand Up @@ -45,7 +43,6 @@ def renderClassMethod(cls: AugmentedClass, meth: Method)(using
val body = s"$cMethod(${serialisedArguments})"

val massagedBody = returnType.fromUnsafeForm(body)
scribe.info(s"Method: ${meth.name}, ${returnType.massageFromUnsafe}")

val returnTypeRepr =
if meth.isThrowing then s"GResult[${returnType.scalaRepr}]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def renderTrait(ns: AugmentedNamespace, iface: AugmentedInterface)(using

emptyLine()

// if iface.name == "Icon" then println(iface.virtualMethods)

block(s"object ${iface.name}:", s"end ${iface.name}"):
block(s"class Abstract(raw: Ptr[Byte]) extends ${iface.name}:", "end Abstract"):
block(
s"class Abstract(raw: Ptr[Byte]) extends ${iface.name}:",
"end Abstract"
):
line("override def getUnsafeRawPointer(): Ptr[Byte] = raw")
end renderTrait
18 changes: 18 additions & 0 deletions fluent-generator/src/main/scala/syntaxExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ extension (c: Constructor)
def parameters = extractParams(c.constructoroption)
def isThrowing = c.throws.exists(_.toString == "1")

extension (c: Virtualu45method)
def isThrowing = c.throws.exists(_.toString == "1")
def parameters: Seq[Parameter | Instanceu45parameter] = extractParams(
c.virtualu45methodoption
)
def instanceParameter: Option[Instanceu45parameter] =
c.virtualu45methodoption.collectFirst:
case d if d.value.isInstanceOf[Instanceu45parameter] =>
d.as[Instanceu45parameter]

def returnType: Option[Type | ArrayType] = c.virtualu45methodoption
.collectFirst:
case d if d.value.isInstanceOf[Returnu45value] => d.as[Returnu45value]
.flatMap: rv =>
rv.returnu45valueoption.collectFirst:
case d if d.value.isInstanceOf[Type] => d.as[Type]
case d if d.value.isInstanceOf[ArrayType] => d.as[ArrayType]
end extension

extension (c: Method)
def isThrowing = c.throws.exists(_.toString == "1")
Expand Down

0 comments on commit d672aa0

Please sign in to comment.