From ebe443a10d0998ce3556b89556fc9c98c23d0f61 Mon Sep 17 00:00:00 2001 From: Pavel Shirshov Date: Fri, 20 Oct 2023 20:39:17 +0100 Subject: [PATCH] cs bullshit --- .../io/septimalmind/baboon/BaboonModule.scala | 4 +-- .../translator/AbstractBaboonTranslator.scala | 2 +- .../csharp/CSBaboonTranslator.scala | 32 ++++++++++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/scala/io/septimalmind/baboon/BaboonModule.scala b/src/main/scala/io/septimalmind/baboon/BaboonModule.scala index 2f369c0..06a964b 100644 --- a/src/main/scala/io/septimalmind/baboon/BaboonModule.scala +++ b/src/main/scala/io/septimalmind/baboon/BaboonModule.scala @@ -3,7 +3,7 @@ package io.septimalmind.baboon import distage.{DIKey, ModuleDef} import io.septimalmind.baboon.BaboonCompiler.CompilerOptions import io.septimalmind.baboon.parser.BaboonParser -import io.septimalmind.baboon.translator.AbstractBaboonTranslator +import io.septimalmind.baboon.translator.BaboonAbstractTranslator import io.septimalmind.baboon.translator.csharp.CSValue.CSPackageId import io.septimalmind.baboon.translator.csharp.{ CSBaboonTranslator, @@ -38,7 +38,7 @@ class BaboonModule(options: CompilerOptions) extends ModuleDef { make[BLogger].from[BLogger.BLoggerImpl] - many[AbstractBaboonTranslator].ref[CSBaboonTranslator] + many[BaboonAbstractTranslator].ref[CSBaboonTranslator] make[CSBaboonTranslator] make[CSDefnTools].from[CSDefnTools.CSDefnToolsImpl] make[CSDefnTranslator].from[CSDefnTranslator.CSDefnTranslatorImpl] diff --git a/src/main/scala/io/septimalmind/baboon/translator/AbstractBaboonTranslator.scala b/src/main/scala/io/septimalmind/baboon/translator/AbstractBaboonTranslator.scala index b1c06a5..56aa386 100644 --- a/src/main/scala/io/septimalmind/baboon/translator/AbstractBaboonTranslator.scala +++ b/src/main/scala/io/septimalmind/baboon/translator/AbstractBaboonTranslator.scala @@ -7,7 +7,7 @@ import izumi.fundamentals.collections.nonempty.NEList case class Sources(files: Map[String, String]) case class DomainSources(domain: Domain, files: Map[String, String]) -trait AbstractBaboonTranslator { +trait BaboonAbstractTranslator { def translate( family: BaboonFamily ): Either[NEList[BaboonIssue.TranslationIssue], Sources] diff --git a/src/main/scala/io/septimalmind/baboon/translator/csharp/CSBaboonTranslator.scala b/src/main/scala/io/septimalmind/baboon/translator/csharp/CSBaboonTranslator.scala index 45ef3ee..9dc4bc6 100644 --- a/src/main/scala/io/septimalmind/baboon/translator/csharp/CSBaboonTranslator.scala +++ b/src/main/scala/io/septimalmind/baboon/translator/csharp/CSBaboonTranslator.scala @@ -6,7 +6,7 @@ import io.septimalmind.baboon.RuntimeGenOpt import io.septimalmind.baboon.parser.model.issues.BaboonIssue import io.septimalmind.baboon.translator.csharp.CSBaboonTranslator.* import io.septimalmind.baboon.translator.csharp.CSValue.{CSPackageId, CSType} -import io.septimalmind.baboon.translator.{AbstractBaboonTranslator, Sources} +import io.septimalmind.baboon.translator.{BaboonAbstractTranslator, Sources} import io.septimalmind.baboon.typer.model.* import izumi.distage.LocalContext import izumi.functional.IzEither.* @@ -23,7 +23,7 @@ class CSBaboonTranslator( options: CompilerOptions, codecs: Set[CSCodecTranslator], tools: CSDefnTools, -) extends AbstractBaboonTranslator { +) extends BaboonAbstractTranslator { type Out[T] = Either[NEList[BaboonIssue.TranslationIssue], T] @@ -184,12 +184,17 @@ class CSBaboonTranslator( | public $csTpe TypeTo(); |} | + |public interface IBaboonGeneratedConversion : IConversion + |{ + | public IBaboonGenerated Convert(C? context, AbstractBaboonConversions conversions, IBaboonGenerated from); + |} + | |public interface IDynamicConversion : IConversion |{ | public To Convert(C? context, AbstractBaboonConversions conversions, dynamic from); |} | - |public abstract class AbstractConversion : IDynamicConversion + |public abstract class AbstractConversion : IDynamicConversion, IBaboonGeneratedConversion |{ | public abstract To Convert(C? context, AbstractBaboonConversions conversions, From from); | @@ -198,6 +203,23 @@ class CSBaboonTranslator( | return Convert(context, conversions, (From)from); | } | + | IBaboonGenerated IBaboonGeneratedConversion.Convert(C? context, $abstractBaboonConversions conversions, $iBaboonGenerated from) where C : default + | { + | if (from is not From fr) + | { + | throw new Exception( + | $"Can't use IBaboonGeneratedConversion interface when from is not of type {typeof(To).FullName}"); + | } + | var res = Convert(context, conversions, fr); + | + | if (res is not $iBaboonGenerated bg) + | { + | throw new $csArgumentException( + | $$"Can't use IBaboonGeneratedConversion interface for non IBaboonGenerated return type To = {typeof(To).FullName}"); + | } + | return bg; + | } + | | public $csTpe TypeFrom() { | return typeof(From); | } @@ -332,13 +354,13 @@ class CSBaboonTranslator( | | public IBaboonGenerated ConvertWithContext(C? c, IBaboonGenerated from, IConversion conversion) | { - | var tconv = ((IDynamicConversion)conversion); + | var tconv = (IBaboonGeneratedConversion)conversion; | return tconv.Convert(c, this, from); | } | | public IBaboonGenerated Convert(IBaboonGenerated from, IConversion conversion) | { - | var tconv = ((IDynamicConversion)conversion); + | var tconv = (IBaboonGeneratedConversion)conversion; | return tconv.Convert(null, this, from); | } |