Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Jun 13, 2023
1 parent 45c44b3 commit 7f4e77f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
7 changes: 6 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ name := "aqua-hll"
val commons = Seq(
version := {
val aquaSnapshot = sys.env.getOrElse("SNAPSHOT", "")
if (aquaSnapshot.isEmpty()) aquaVersion else aquaVersion + "-" + aquaSnapshot,
if (aquaSnapshot.isEmpty()) aquaVersion else aquaVersion + "-" + aquaSnapshot
},
scalaVersion := dottyVersion,
libraryDependencies ++= Seq(
Expand Down Expand Up @@ -251,6 +251,11 @@ lazy val compiler = crossProject(JVMPlatform, JSPlatform)
.settings(commons: _*)
.dependsOn(semantics, linker, backend, transform % Test)

lazy val `compiler-native-lib` = project
.in(file("compiler-native-lib"))
.settings(commons: _*)
.dependsOn(compiler.jvm, io.jvm, transform.jvm, `backend-air`.jvm)

lazy val backend = crossProject(JVMPlatform, JSPlatform)
.withoutSuffixFor(JVMPlatform)
.crossType(CrossType.Pure)
Expand Down
81 changes: 81 additions & 0 deletions compiler-native-lib/src/main/scala/aqua/compiler/Library.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aqua.compiler

import aqua.AquaIO
import org.graalvm.nativeimage.IsolateThread
import org.graalvm.nativeimage.c.function.CEntryPoint
import org.graalvm.nativeimage.c.`type`.CCharPointer
import org.graalvm.nativeimage.c.`type`.CTypeConversion
import aqua.io.AquaFileError
import aqua.files.{AquaFileSources, FileModuleId}
import cats.{Applicative, Functor, Monad}
import cats.data.{Chain, ValidatedNec}
import cats.data.Validated.Valid
import cats.data.Validated.validNec
import cats.effect.IO
import fs2.io.file.{Files, Path}
import cats.implicits.*
import aqua.SpanParser
import aqua.model.transform.TransformConfig
import aqua.model.AquaContext
import aqua.model.transform.Transform
import aqua.backend.Backend
import aqua.backend.AirFunction
import aqua.backend.Generated
import aqua.res.AquaRes
import aqua.parser.lift.FileSpan
import aqua.backend.air.AirBackend
import aqua.files.AquaFilesIO.summon
import cats.effect.unsafe.implicits.global
object Library {


private final val path = Path("")

private class RawAquaSource[F[_] : AquaIO : Monad : Files](input: String, imports: List[String]) extends AquaFileSources[F](path, imports.map(Path.apply)):
override def sources: F[ValidatedNec[AquaFileError, Chain[(FileModuleId, String)]]] = {
Applicative[F].pure(Valid(Chain.one((FileModuleId(path), input))))
}


private class LocalBackendTransform(transformConfig: TransformConfig) extends Backend.Transform:
override def transform(ex: AquaContext): AquaRes =
Transform.contextRes(ex, transformConfig)

override def generate(aqua: AquaRes): Seq[Generated] = AirBackend.generate(aqua)


private class LocalAirValidator[F[_] : Applicative]() extends AirValidator[F]:

override def init(): F[Unit] = Applicative[F].pure(())

override def validate(airs: List[AirFunction]): F[ValidatedNec[String, Unit]] =
Applicative[F].pure(validNec(()))

private def compileF[F[_] : AquaIO : Monad : Files](input: String, imports: List[String]): F[ValidatedNec[AquaError[FileModuleId, AquaFileError, FileSpan.F], Chain[AquaCompiled[FileModuleId]]]] = for {
sources <- new RawAquaSource[F](input, imports).pure
transformConfig <- TransformConfig().pure
backendTransform <- new LocalBackendTransform(transformConfig).pure
validator <- new LocalAirValidator[F]().pure
result <- CompilerAPI
.compile[F, AquaFileError, FileModuleId, FileSpan.F](
sources,
SpanParser.parser,
validator,
backendTransform,
AquaCompilerConf(transformConfig.constantsList))
} yield result

@CEntryPoint(name = "compile") def compile(thread: IsolateThread, codePointer: CCharPointer): Int = {
val code = CTypeConversion.toJavaString(codePointer)

val program = compileF[IO](code, List.empty)
val result = program.unsafeRunSync()

if (result.isValid) {
0
} else {
1
}
}

}

0 comments on commit 7f4e77f

Please sign in to comment.