<commit_before>// -*- mode: Scala;-*- 
// Filename:    Roselang.scala 
// Authors:     luciusmeredith                                                    
// Creation:    Thu Feb  2 14:34:16 2017 
// Copyright:   See site license 
// Description: 
// ------------------------------------------------------------------------

package coop.rchain.rho2rose

import coop.rchain.lib.term._
import coop.rchain.lib.zipper._

import coop.rchain.syntax.rholang._
import coop.rchain.syntax.rholang.Absyn._

trait StrTermNavigation extends TermNavigation[String,Either[String,String],String]
trait StrTermMutation extends TermMutation [String,Either[String,String],String]
trait StrTermZipperComposition extends TermZipperComposition[String,Either[String,String],String]
trait StrTermSubstitution extends TermSubstitution[String,Either[String,String],String]

object StrTermCtorAbbrevs {
  type StrTermCtxt = TermCtxt[String,Either[String,String],String] with Factual
  def V( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Right( Left( v ) ) )
  def K( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Right( Right( v ) ) )
  def G( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Left( v ) )
  def B( v : String )( terms : StrTermCtxt* ) = StrTermPtdCtxtBr( v, terms.toList )
}

object StrZipAbbrevs {
  type ValOrVar = Either[String,Either[String,String]]
  type LocVorV = Location[ValOrVar]
  def L( term : StrTermCtorAbbrevs.StrTermCtxt, ctxt : Context[ValOrVar] ) : LocVorV = Location( term, ctxt )   
  def HV( cv : String ) : LocVorV = 
    Location( StrTermCtorAbbrevs.V( cv ), Top[ValOrVar]() )
  def T() : Context[ValOrVar] = Top()
}

object VisitorTypes {
  type A = Option[StrZipAbbrevs.LocVorV]
  type R = Option[StrZipAbbrevs.LocVorV]    
}

object S2SImplicits {
  implicit def asLoc( 
    term : StrTermCtorAbbrevs.StrTermCtxt
  ) : StrZipAbbrevs.LocVorV = StrZipAbbrevs.L( term, StrZipAbbrevs.T() )
  implicit def asR( 
    loc : StrZipAbbrevs.LocVorV
  ) : VisitorTypes.R = Some( loc )
  implicit def asR(
    term : StrTermCtorAbbrevs.StrTermCtxt
  ) : VisitorTypes.R = asR( asLoc( term ) )
}

object CompilerExceptions {
  trait CompilerException
  trait SyntaxException
  trait SemanticsException

  case class UnexpectedContractType(
    c : Contr
  ) extends Exception( s"$c found in unexpected context" )
      with CompilerException with SyntaxException
  case class NoComprehensionBindings(
    p : PInput 
  ) extends Exception( s"$p has no bindings" )
      with CompilerException with SyntaxException
  case class UnexpectedBindingType(
    b : Bind
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
  case class UnexpectedBranchType(
    b : CBranch
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
  case class FailedQuotation(
    b : AnyRef
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
}

object ComprehensionOps {
  val _map = "map"
  val _unit = "unit"
  val _mult = "mult"
  val _join = "flatMap"
  val _filter = "filter"
}

object RosetteOps {
  val _abs = "proc"
  val _defActor = "defActor"
  val _method = "method"
  val _produce = "produce"
  val _block = "block"
  val _quote = "Q"
  val _rx = "RX"
  val _run = "run"
  val _compile = "compile"
}

trait StrFoldCtxtVisitor
extends FoldVisitor[VisitorTypes.R,VisitorTypes.A] {
  def zipr : StrTermNavigation with StrTermMutation with StrTermZipperComposition
  def theCtxtVar : String

  def wrap( context : VisitorTypes.R ) : VisitorTypes.R = context
  def leaf( context : VisitorTypes.R ) : VisitorTypes.R = wrap( context )    

  override def combine(
    x : VisitorTypes.R, 
    y : VisitorTypes.R, 
    context : VisitorTypes.R
  ) : VisitorTypes.R = {
    /*
     println(
      (
	"/* ------------------------------------------------------- */\n"
	+ "/* method: " + "combine" + " */\n"
	+ "/* x: " + x + " */\n"
	+ "/* y: " + y + " */\n"
	+ "/* context: " + context + " */\n"
	+ "/* ------------------------------------------------------- */\n"
      )
    )
    */
    
    val rslt =
      for( 
	xLoc@Location( xTerm : StrTermCtorAbbrevs.StrTermCtxt, xCtxt ) <- x;
	yLoc@Location( yTerm : StrTermCtorAbbrevs.StrTermCtxt, yCtxt ) <- y
      ) yield {
	/*
	 println(
	  (
	    "/* ------------------------------------------------------- */\n"
	    + "/* method: " + "combine" + " continued" + " */\n"
	    + "/* xLoc: " + xLoc + " */\n"
	    + "/* yLoc: " + yLoc + " */\n"
	    + "/* ------------------------------------------------------- */\n"
	  )
	)
	*/
	yLoc match {
	  case Location( StrTermPtdCtxtLf( Right( v ) ), Top( ) ) => xLoc
	  case Location( _, Top( ) ) => {
	    xCtxt match {
	      case Top() => {
		val loc = zipr.up( zipr.insertDown( yLoc, xTerm ) )
		/*
		 * println(
		  (
		    "/* ------------------------------------------------------- */\n"
		    + "/* method: " + "combine" + " continued" + " */\n"
		    + "/* loc: " + loc + " */\n"
		    + "/* ------------------------------------------------------- */\n"
		  )
		)
		*/
		    
		loc
	      }
	      case _ => {
		val loc = zipr.update( yLoc, xTerm )
		/*
		 println(
		  (
		    "/* ------------------------------------------------------- */\n"
		    + "/* method: " + "combine" + " continued" + " */\n"
		    + "/* loc: " + loc + " */\n"
		    + "/* ------------------------------------------------------- */\n"
		  )
		)
		*/
		
		loc
	      }
	    }	    
	  }
	  case _ => {
	    xLoc match {	      
	      case Location( StrTermPtdCtxtLf( Right( v ) ), Top() ) => {
		val loc = zipr.update( yLoc, xTerm )
		/*
		 println(<commit_msg>Updated combine and visit to use the differentiated variables<commit_after>// -*- mode: Scala;-*- 
// Filename:    Roselang.scala 
// Authors:     luciusmeredith                                                    
// Creation:    Thu Feb  2 14:34:16 2017 
// Copyright:   See site license 
// Description: 
// ------------------------------------------------------------------------

package coop.rchain.rho2rose

import coop.rchain.lib.term._
import coop.rchain.lib.zipper._

import coop.rchain.syntax.rholang._
import coop.rchain.syntax.rholang.Absyn._

trait StrTermNavigation extends TermNavigation[String,Either[String,String],String]
trait StrTermMutation extends TermMutation [String,Either[String,String],String]
trait StrTermZipperComposition extends TermZipperComposition[String,Either[String,String],String]
trait StrTermSubstitution extends TermSubstitution[String,Either[String,String],String]

object StrTermCtorAbbrevs {
  type StrTermCtxt = TermCtxt[String,Either[String,String],String] with Factual
  def V( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Right( Left( v ) ) )
  def K( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Right( Right( v ) ) )
  def G( v : String ) : StrTermCtxt = StrTermPtdCtxtLf( Left( v ) )
  def B( v : String )( terms : StrTermCtxt* ) = StrTermPtdCtxtBr( v, terms.toList )
}

object StrZipAbbrevs {
  type ValOrVar = Either[String,Either[String,String]]
  type LocVorV = Location[ValOrVar]
  def L( term : StrTermCtorAbbrevs.StrTermCtxt, ctxt : Context[ValOrVar] ) : LocVorV = Location( term, ctxt )   
  def HV( cv : String ) : LocVorV = 
    Location( StrTermCtorAbbrevs.K( cv ), Top[ValOrVar]() )
  def T() : Context[ValOrVar] = Top()
}

object VisitorTypes {
  type A = Option[StrZipAbbrevs.LocVorV]
  type R = Option[StrZipAbbrevs.LocVorV]    
}

object S2SImplicits {
  implicit def asLoc( 
    term : StrTermCtorAbbrevs.StrTermCtxt
  ) : StrZipAbbrevs.LocVorV = StrZipAbbrevs.L( term, StrZipAbbrevs.T() )
  implicit def asR( 
    loc : StrZipAbbrevs.LocVorV
  ) : VisitorTypes.R = Some( loc )
  implicit def asR(
    term : StrTermCtorAbbrevs.StrTermCtxt
  ) : VisitorTypes.R = asR( asLoc( term ) )
}

object CompilerExceptions {
  trait CompilerException
  trait SyntaxException
  trait SemanticsException

  case class UnexpectedContractType(
    c : Contr
  ) extends Exception( s"$c found in unexpected context" )
      with CompilerException with SyntaxException
  case class NoComprehensionBindings(
    p : PInput 
  ) extends Exception( s"$p has no bindings" )
      with CompilerException with SyntaxException
  case class UnexpectedBindingType(
    b : Bind
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
  case class UnexpectedBranchType(
    b : CBranch
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
  case class FailedQuotation(
    b : AnyRef
  ) extends Exception( s"$b found in unexpected context" )
      with CompilerException with SyntaxException
  case class UnexpectedCombination(
    xLoc : StrZipAbbrevs.LocVorV,
    yLoc : StrZipAbbrevs.LocVorV
  ) extends Exception( s"attempting to combine: $xLoc with $yLoc" )
      with CompilerException
}

object ComprehensionOps {
  val _map = "map"
  val _unit = "unit"
  val _mult = "mult"
  val _join = "flatMap"
  val _filter = "filter"
}

object RosetteOps {
  val _abs = "proc"
  val _defActor = "defActor"
  val _method = "method"
  val _produce = "produce"
  val _block = "block"
  val _quote = "Q"
  val _rx = "RX"
  val _run = "run"
  val _compile = "compile"
}

trait StrFoldCtxtVisitor
extends FoldVisitor[VisitorTypes.R,VisitorTypes.A] {
  def zipr : StrTermNavigation with StrTermMutation with StrTermZipperComposition
  def theCtxtVar : String

  def wrap( context : VisitorTypes.R ) : VisitorTypes.R = context
  def leaf( context : VisitorTypes.R ) : VisitorTypes.R = wrap( context )    

  override def combine(
    x : VisitorTypes.R, 
    y : VisitorTypes.R, 
    context : VisitorTypes.R
  ) : VisitorTypes.R = {
    /*
     println(
      (
	"/* ------------------------------------------------------- */\n"
	+ "/* method: " + "combine" + " */\n"
	+ "/* x: " + x + " */\n"
	+ "/* y: " + y + " */\n"
	+ "/* context: " + context + " */\n"
	+ "/* ------------------------------------------------------- */\n"
      )
    )
    */
    
    val rslt =
      for( 
	xLoc@Location( xTerm : StrTermCtorAbbrevs.StrTermCtxt, xCtxt ) <- x;
	yLoc@Location( yTerm : StrTermCtorAbbrevs.StrTermCtxt, yCtxt ) <- y
      ) yield {
	/*
	 println(
	  (
	    "/* ------------------------------------------------------- */\n"
	    + "/* method: " + "combine" + " continued" + " */\n"
	    + "/* xLoc: " + xLoc + " */\n"
	    + "/* yLoc: " + yLoc + " */\n"
	    + "/* ------------------------------------------------------- */\n"
	  )
	)
	*/
	yLoc match {
	  case Location( StrTermPtdCtxtLf( Right( Left( v ) ) ), Top( ) ) => {
            throw new CompilerExceptions.UnexpectedCombination( xLoc, yLoc )
          }
          case Location( StrTermPtdCtxtLf( Right( Right( v ) ) ), Top( ) ) => xLoc
	  case Location( _, Top( ) ) => {
	    xCtxt match {
	      case Top() => {
		val loc = zipr.up( zipr.insertDown( yLoc, xTerm ) )
		/*
		 * println(
		  (
		    "/* ------------------------------------------------------- */\n"
		    + "/* method: " + "combine" + " continued" + " */\n"
		    + "/* loc: " + loc + " */\n"
		    + "/* ------------------------------------------------------- */\n"
		  )
		)
		*/
		    
		loc
	      }
	      case _ => {
		val loc = zipr.update( yLoc, xTerm )
		/*
		 println(
		  (
		    "/* ------------------------------------------------------- */\n"
		    + "/* method: " + "combine" + " continued" + " */\n"
		    + "/* loc: " + loc + " */\n"
		    + "/* ------------------------------------------------------- */\n"
		  )
		)
		*/
		
		loc
	      }
	    }	    
	  }
	  case _ => {
	    xLoc match {
              case Location( StrTermPtdCtxtLf( Right( Left( v ) ) ), Top() ) => {
                throw new CompilerExceptions.UnexpectedCombination( xLoc, yLoc )
              }
	      case Location( StrTermPtdCtxtLf( Right( Right( v ) ) ), Top() ) => {
		val loc = zipr.update( yLoc, xTerm )
		/*
		 println(
{'fraction_non_alphanumeric': 0.20654533311070294, 'fraction_numerical': 0.0021706461846718985, 'mean_word_length': 1.9798507462686568, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '405315', 'n_tokens_mistral': 4175, 'n_tokens_neox': 3962, 'n_words': 912}