Skip to content

Commit

Permalink
sigma-ast: fix Reflection for SigmaCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Sep 10, 2023
1 parent 0e45513 commit 1a86c84
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object SigmaCompiler extends js.Object {
* @return SigmaCompiler instance
*/
private def create(networkPrefix: Byte): SigmaCompiler = {
val compiler = new sigmastate.lang.SigmaCompiler(networkPrefix)
val compiler = sigmastate.lang.SigmaCompiler(networkPrefix)
new SigmaCompiler(compiler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object ErgoScriptPredef {

/** Compiles the given ErgoScript `code` into ErgoTree expression. */
def compileWithCosting(env: ScriptEnv, code: String, networkPrefix: NetworkPrefix)(implicit IR: IRContext): Value[SType] = {
val compiler = new SigmaCompiler(networkPrefix)
val compiler = SigmaCompiler(networkPrefix)
val res = compiler.compile(env, code)
res.buildTree
}
Expand Down
15 changes: 12 additions & 3 deletions sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fastparse.Parsed.Success
import sigma.kiama.rewriting.Rewriter.{everywherebu, rewrite, rule}
import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix
import org.ergoplatform.Global
import scalan.GraphIRReflection
import sigmastate.Values.{SValue, Value}
import sigmastate.eval.IRContext
import sigmastate.interpreter.Interpreter.ScriptEnv
Expand All @@ -14,7 +15,7 @@ import sigmastate.lang.syntax.ParserException
import sigmastate.utxo._
import sigma.ast._
import sigmastate.SCollectionMethods.{ExistsMethod, ForallMethod, MapMethod}
import sigmastate.{Exponentiate, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor}
import sigmastate.{Exponentiate, InterpreterReflection, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor}

/**
* @param networkPrefix network prefix to decode an ergo address from string (PK op)
Expand Down Expand Up @@ -51,9 +52,9 @@ case class CompilerResult[Ctx <: IRContext](
/** Compiler which compiles ErgoScript source code into ErgoTree.
* @param settings compilation parameters \
*/
class SigmaCompiler(settings: CompilerSettings) {
class SigmaCompiler private(settings: CompilerSettings) {
/** Constructs an instance for the given network type and with default settings. */
def this(networkPrefix: Byte) = this(
private def this(networkPrefix: Byte) = this(
CompilerSettings(networkPrefix, TransformingSigmaBuilder, lowerMethodCalls = true)
)

Expand Down Expand Up @@ -141,6 +142,14 @@ class SigmaCompiler(settings: CompilerSettings) {
}

object SigmaCompiler {
/** Force initialization of reflection before any instance of SigmaCompiler is used. */
val _ = (InterpreterReflection, GraphIRReflection)

/** Constructs an instance for the given settings. */
def apply(settings: CompilerSettings): SigmaCompiler =
new SigmaCompiler(settings)

/** Constructs an instance for the given network type. */
def apply(networkPrefix: Byte): SigmaCompiler =
new SigmaCompiler(networkPrefix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait ErgoScriptTestkit extends ContractsTestkit with LangTests
import Context._
import Liftables._

override lazy val compiler = new SigmaCompiler(CompilerSettings(
override lazy val compiler = SigmaCompiler(CompilerSettings(
TestnetNetworkPrefix,
IR.builder,
lowerMethodCalls = true
Expand Down

0 comments on commit 1a86c84

Please sign in to comment.