Skip to content

Commit

Permalink
Merge pull request #987 from ScorexFoundation/v6.0.0-refactor-ir-cake
Browse files Browse the repository at this point in the history
[6.0] Merge with refactored SigmaCompiler
  • Loading branch information
kushti authored May 29, 2024
2 parents f0c34e6 + a63631f commit 3fe08a0
Show file tree
Hide file tree
Showing 92 changed files with 723 additions and 661 deletions.
4 changes: 2 additions & 2 deletions data/shared/src/main/scala/sigma/ast/ErgoTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ case class UnparsedErgoTree(bytes: mutable.WrappedArray[Byte], error: Validation
* ErgoTreeSerializer defines top-level serialization format of the scripts.
* The interpretation of the byte array depend on the first `header` byte, which uses VLQ encoding up to 30 bits.
* Currently we define meaning for only first byte, which may be extended in future versions.
* 7 6 5 4 3 2 1 0
* 7 6 5 4 3 2 1 0
* -------------------------
* | | | | | | | | |
* -------------------------
* Bit 7 == 1 if the header contains more than 1 byte (default == 0)
* Bit 6 - reserved for GZIP compression (should be 0)
* Bit 5 == 1 - reserved for context dependent costing (should be = 0)
* Bit 5 == 1 - reserved (should be = 0)
* Bit 4 == 1 if constant segregation is used for this ErgoTree (default = 0)
* (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/264)
* Bit 3 == 1 if size of the whole tree is serialized after the header byte (default = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigmastate.lang
import fastparse._
import fastparse.NoWhitespace._
import SigmaParser._
import sigma.ast.SType
import sigma.ast.{Constant, SType}
import sigma.ast.syntax.SValue
import sigmastate.lang.parsers.Basic

Expand Down Expand Up @@ -187,9 +187,9 @@ object ContractParser {

def annotation[_: P] = P("@contract")

def paramDefault[_: P] = P(WL.? ~ `=` ~ WL.? ~ ExprLiteral).map(s => s.asWrappedType)
def paramDefault[_: P] = P(WL.? ~ `=` ~ WL.? ~ ExprLiteral)

def param[_: P] = P(WL.? ~ Id.! ~ ":" ~ Type ~ paramDefault.?).map(s => ContractParam(s._1, s._2, s._3))
def param[_: P] = P(WL.? ~ Id.! ~ ":" ~ Type ~ paramDefault.?).map(s => ContractParam(s._1, s._2, s._3.map(_.value)))

def params[_: P] = P("(" ~ param.rep(1, ",").? ~ ")")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sigmastate.lang
import org.scalatest.matchers.should.Matchers
import org.scalatest.propspec.AnyPropSpec
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import sigma.ast.SType.AnyOps
import sigma.ast._

class ContractParserSpec extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers {
Expand Down Expand Up @@ -34,8 +35,8 @@ class ContractParserSpec extends AnyPropSpec with ScalaCheckPropertyChecks with

parsed.name shouldBe "contractName"
parsed.params should contain theSameElementsInOrderAs Seq(
ContractParam("p1", SInt, Some(IntConstant(5).asWrappedType)),
ContractParam("p2", SString, Some(StringConstant("default string").asWrappedType)),
ContractParam("p1", SInt, Some(5.asWrappedType)),
ContractParam("p2", SString, Some("default string".asWrappedType)),
ContractParam("param3", SLong, None)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package sigmastate.lang

import org.scalatest.matchers.should.Matchers
import sigma.{Coll, _}
import sigma.ast.SCollection.SByteArray
import sigma.ast.syntax.{SValue, ValueOps}
import sigma.ast._
import sigma.crypto.CryptoConstants
import sigma.data.{CAnyValue, CSigmaDslBuilder, ProveDHTuple, ProveDlog, SigmaBoolean}
import sigma.util.Extensions.BigIntegerOps
import sigma._
import sigmastate.helpers.NegativeTesting
import sigmastate.interpreter.Interpreter.ScriptEnv
import sigma.ast.{Ident, MethodCallLike}

import java.math.BigInteger

Expand Down
6 changes: 3 additions & 3 deletions sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel
import sigma.js.Value
import sigma.ast.ErgoTree.HeaderType
import sigmastate.eval.CompiletimeIRContext
import sigma.compiler.ir.CompiletimeIRContext
import sigma.ast.syntax.ValueOps


/** Wrapper exported to JS. */
@JSExportTopLevel("SigmaCompiler")
class SigmaCompiler(_compiler: sigmastate.lang.SigmaCompiler) extends js.Object {
class SigmaCompiler(_compiler: sigma.compiler.SigmaCompiler) extends js.Object {

/** Compiles ErgoScript code to ErgoTree.
*
Expand Down Expand Up @@ -59,7 +59,7 @@ object SigmaCompiler extends js.Object {
* @return SigmaCompiler instance
*/
private def create(networkPrefix: Byte): SigmaCompiler = {
val compiler = sigmastate.lang.SigmaCompiler(networkPrefix)
val compiler = sigma.compiler.SigmaCompiler(networkPrefix)
new SigmaCompiler(compiler)
}
}
6 changes: 4 additions & 2 deletions sc/js/src/test/scala/scalan/Platform.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package scalan

import sigma.compiler.ir.IRContext

import scala.annotation.unused

object Platform {
/** In JS tests do nothing. The corresponding JVM method outputs graphs into files. */
def stage[Ctx <: Scalan](scalan: Ctx)(
def stage[Ctx <: IRContext](ctx: Ctx)(
@unused prefix: String,
@unused testName: String,
@unused name: String,
@unused sfs: Seq[() => scalan.Sym]): Unit = {
@unused sfs: Seq[() => ctx.Sym]): Unit = {
}

/** On JS it is no-operation. */
Expand Down
11 changes: 6 additions & 5 deletions sc/jvm/src/test/scala/scalan/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package scalan
import scalan.compilation.GraphVizExport
import sigma.util.FileUtil
import org.scalatest.Assertions
import sigma.compiler.ir.IRContext

object Platform {
/** Output graph given by symbols in `sfs` to files.
* @param scalan The Scalan context
* @param ctx The Scalan context
* @param prefix The prefix of the directory where the graphs will be stored.
* @param testName The name of the test.
* @param name The name of the graph.
* @param sfs A sequence of functions that return symbols of the graph roots.
*/
def stage[Ctx <: Scalan](scalan: Ctx)
(prefix: String, testName: String, name: String, sfs: Seq[() => scalan.Sym]): Unit = {
def stage[Ctx <: IRContext](ctx: Ctx)
(prefix: String, testName: String, name: String, sfs: Seq[() => ctx.Sym]): Unit = {
val directory = FileUtil.file(prefix, testName)
val gv = new GraphVizExport(scalan)
val gv = new GraphVizExport(ctx)
implicit val graphVizConfig = gv.defaultGraphVizConfig
try {
val ss = sfs.map(_.apply()).asInstanceOf[Seq[gv.scalan.Sym]]
val ss = sfs.map(_.apply()).asInstanceOf[Seq[gv.ctx.Sym]]
gv.emitDepGraph(ss, directory, name)(graphVizConfig)
} catch {
case e: Exception =>
Expand Down
6 changes: 3 additions & 3 deletions sc/jvm/src/test/scala/scalan/compilation/GraphVizExport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package scalan.compilation

import java.awt.Desktop
import java.io.{File, PrintWriter}
import scalan.Scalan
import scalan.core.ScalaNameUtil
import sigma.compiler.ir.IRContext
import sigma.util.{FileUtil, ProcessUtil, StringUtil}

import scala.annotation.unused
Expand All @@ -12,8 +12,8 @@ import scala.collection.immutable.StringOps
// TODO implement this outside of the cake

/** Implementation of Graphviz's dot file generator. */
class GraphVizExport[Ctx <: Scalan](val scalan: Ctx) {
import scalan._
class GraphVizExport[Ctx <: IRContext](val ctx: Ctx) {
import ctx._
case class GraphFile(file: File, fileType: String) {
def open() = {
Desktop.getDesktop.open(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sigmastate

import org.scalatest.matchers.should.Matchers
import org.scalatest.propspec.AnyPropSpec
import scalan.TypeDescs
import sigma.compiler.ir.{IRContext, TypeDescs}
import sigma.reflection.ReflectionData.registerClassEntry
import sigma.reflection.SRConstructor

Expand Down Expand Up @@ -37,7 +37,7 @@ class InterpreterReflectionGeneratorTests extends AnyPropSpec with Matchers {
}

property("inner class") {
val ctx = null.asInstanceOf[scalan.Library] // ok! type level only
val ctx = null.asInstanceOf[IRContext] // ok! type level only
val clazz = classOf[ctx.Coll.CollElem[_, _]]
registerClassEntry(clazz,
constructors = Array(
Expand Down
21 changes: 16 additions & 5 deletions sc/jvm/src/test/scala/sigmastate/ReflectionGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package sigmastate

import scalan.{Base, TypeDescs}
import sigma.compiler.ir.{Base, TypeDescs}
import sigma.reflection._
import scala.annotation.unused
import scala.collection.mutable

/** Generates code for registering classes in the ReflectionData.
* It is not used in the runtime.
*
* The generated invocations of `registerClassEntry`, `mkMethod`, `mkConstructor` may
* require manual adjustments.
*
* It uses [[sigma.reflection.Platform.unknownClasses]] to collect classes which were
* accessed during runtime
*
* @see [[ReflectionData]]
*/
object ReflectionGenerator {

def normalizeName(name: String): String = {
Expand All @@ -30,17 +41,17 @@ object ReflectionGenerator {
}

val knownPackages = Array(
"scalan.primitives.",
"sigma.compiler.ir.primitives.",
"sigma.",
"sigma.",
"special.wrappers.",
"sigma.compiler.ir.wrappers.",
"sigma.ast.",
"sigmastate.lang.Terms.",
"sigmastate.interpreter.",
"sigmastate.utxo.",
"sigmastate.",
"wrappers.scala.",
"scalan.",
"sigma.compiler.ir.wrappers.scala.",
"sigma.compiler.ir.",
"scala.collection.",
"scala."
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.ergoplatform

import sigmastate.lang.SigmaCompiler
import sigmastate.eval.IRContext
import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix
import sigma.ast.SType
import sigma.ast.syntax.SigmaPropValue
import sigma.ast.Value
import sigma.ast.syntax.ValueOps
import sigma.compiler.SigmaCompiler
import sigma.compiler.ir.IRContext

object ErgoScriptPredef {
import sigmastate.interpreter.Interpreter._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import sigma.interpreter.{CostedProverResult, ProverResult}
import sigma.data.{CSigmaDslBuilder, RType}
import org.ergoplatform.{ErgoBox, ErgoLikeContext}
import sigma.{Coll, SigmaDslBuilder, SigmaProp}
import sigmastate.eval.IRContext

import scala.util.Try
import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token}
import sigma.ast.{ErgoTree, EvaluatedValue, SType}
import sigma.compiler.ir.IRContext

import scala.language.implicitConversions

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.ergoplatform.dsl

import sigma.Coll
import sigmastate.eval.IRContext
import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token}
import org.ergoplatform.ErgoBox.{BoxId, NonMandatoryRegisterId, TokenId}
import sigma.compiler.ir.IRContext
import sigma.interpreter.CostedProverResult

class ErgoContractSpec(implicit val IR: IRContext) extends ContractSpec {
Expand Down
36 changes: 0 additions & 36 deletions sc/shared/src/main/scala/scalan/Scalan.scala

This file was deleted.

92 changes: 0 additions & 92 deletions sc/shared/src/main/scala/scalan/primitives/UniversalOps.scala

This file was deleted.

Loading

0 comments on commit 3fe08a0

Please sign in to comment.