<commit_before>package lancet
package interpreter

import lancet.api._

class TestInterpreter5 extends FileDiffSuite {

  val prefix = "test-out/test-interpreter-5"

  // TODO: delimit scope of slowpath/fastpath

  def test1a = withOutFileChecked(prefix+"slowpath1a") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.slowpath

    val f = it.interpret { (x:Int) => // fully interpreted
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def test1 = withOutFileChecked(prefix+"slowpath1") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.slowpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def testFastpath1 = withOutFileChecked(prefix+"fastpath1") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }


  def testFastpath2 = withOutFileChecked(prefix+"fastpath2") {
    val it = new Decompiler

    def compute(i: Int) = if (i % 10 == 0) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }



  def testCps1 = withOutFileChecked(prefix+"cps1") {
    val it = new Decompiler

    def compute(i: Int) = it.shift[Int,Int](k => k(7) + k(9))

    val f = it.compile { (x:Int) => 
      it.reset[Int](compute(x) * 100)
    }
    printcheck(f(100), 1600)
  }



  def xxtest10 = withOutFileChecked(prefix+"language10") {
    assert(false)
    val it = new Decompiler

    abstract class Exp
    case class IntVal(x: Int) extends Exp
    case class DoubleVal(x: Double) extends Exp
    case class StringVal(x: String) extends Exp
    case class Plus(x: Exp, y: Exp) extends Exp
    case class Minus(x: Exp, y: Exp) extends Exp
    case class Var(x: String) extends Exp
    
    abstract class Stm
    case class Assign(x: String, y: Exp) extends Stm
    case class While(x: Exp, y: Stm) extends Stm
    case class Print(x: Exp) extends Stm
    case class Block(xs: List[Stm]) extends Stm

    val env = new scala.collection.mutable.HashMap[String,Any]
    def eval(e: Exp): Any = e match {
      case IntVal(x) => x
      case DoubleVal(x) => x
      case StringVal(x) => x
      case Plus(x,y) => (eval(x),eval(y)) match {
        case (x: Int, y: Int) => x + y
        case (x: Double, y: Double) => x + y
        case (x: String, y: String) => x + y 
        // todo widen
      }
      case Minus(x,y) => (eval(x),eval(y)) match {
        case (x: Int, y: Int) => x + y
        case (x: Double, y: Double) => x + y
        case (x: String, y: String) => x + y 
        // todo widen
      }
      case Var(x) => env(x)
    }
    def exec(s: Stm): Unit = s match {
      case Assign(x,y) => env(x) = eval(y)
      case While(x,y) => while (eval(x).asInstanceOf[Int] > 0) exec(y) // test int
      case Print(x) => println(eval(x))
      case Block(xs) => xs.foreach(exec(_)) // unroll
    }

    // GOAL 1: switch plus/minus nodes
    // GOAL 2: switch environment if new keys added

    val p = Block(List(
      //Assign("max", IntVal(100)),
      Assign("x", IntVal(0)),
      While(Minus(Var("max"), Var("x")), Block(List(
        Print(Var("x")),
        Assign("x", Plus(Var("x"), IntVal(1)))
      )))
    ))

    val f = it.compile { (x:Int) => 
      env("max") = x
      exec(p)
    }
    println(f(100))
  }





  class Decompiler extends BytecodeInterpreter_LIR_Opt with DefaultMacros {

    initialize()
    emitUniqueOpt = true
    debugBlockKeys = false
    debugReadWrite = false
    debugMethods = false
    debugReturns = true

  }

}<commit_msg>test5 lms<commit_after>package lancet
package interpreter

import lancet.api._

class TestInterpreter5 extends FileDiffSuite {

  val prefix = "test-out/test-interpreter-5"

  // TODO: delimit scope of slowpath/fastpath

  def test1a = withOutFileChecked(prefix+"slowpath1a") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.slowpath

    val f = it.interpret { (x:Int) => // fully interpreted
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def test1 = withOutFileChecked(prefix+"slowpath1") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.slowpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def testFastpath1 = withOutFileChecked(prefix+"fastpath1") {
    val it = new Decompiler

    def compute(i: Int) = if (i == 50) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }


  def testFastpath2 = withOutFileChecked(prefix+"fastpath2") {
    val it = new Decompiler

    def compute(i: Int) = if (i % 10 == 0) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def testCps1 = withOutFileChecked(prefix+"cps1") {
    val it = new Decompiler

    def compute(i: Int) = it.shift[Int,Int](k => k(7) + k(9))

    val f = it.compile { (x:Int) => 
      it.reset[Int](compute(x) * 100)
    }
    printcheck(f(100), 1600)
  }


  def test1LMS = withOutFileChecked(prefix+"slowpath1LMS") {
    val it = new DecompilerLMS

    def compute(i: Int) = if (i == 50) it.slowpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }

  def testFastpath1LMS = withOutFileChecked(prefix+"fastpath1LMS") {
    val it = new DecompilerLMS

    def compute(i: Int) = if (i == 50) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }


  def testFastpath2LMS = withOutFileChecked(prefix+"fastpath2LMS") {
    val it = new DecompilerLMS

    def compute(i: Int) = if (i % 10 == 0) it.fastpath

    val f = it.compile { (x:Int) => 
      var i = 0
      while (i < x) {
        compute(i)
        i += 1
      }
      i
    }
    printcheck(f(100), 100)
  }





  def testCps1LMS = withOutFileChecked(prefix+"cps1LMS") {
    val it = new DecompilerLMS

    def compute(i: Int) = it.shift[Int,Int](k => k(7) + k(9))

    val f = it.compile { (x:Int) => 
      it.reset[Int](compute(x) * 100)
    }
    printcheck(f(100), 1600)
  }



  def xxtest10 = withOutFileChecked(prefix+"language10") {
    assert(false)
    val it = new Decompiler

    abstract class Exp
    case class IntVal(x: Int) extends Exp
    case class DoubleVal(x: Double) extends Exp
    case class StringVal(x: String) extends Exp
    case class Plus(x: Exp, y: Exp) extends Exp
    case class Minus(x: Exp, y: Exp) extends Exp
    case class Var(x: String) extends Exp
    
    abstract class Stm
    case class Assign(x: String, y: Exp) extends Stm
    case class While(x: Exp, y: Stm) extends Stm
    case class Print(x: Exp) extends Stm
    case class Block(xs: List[Stm]) extends Stm

    val env = new scala.collection.mutable.HashMap[String,Any]
    def eval(e: Exp): Any = e match {
      case IntVal(x) => x
      case DoubleVal(x) => x
      case StringVal(x) => x
      case Plus(x,y) => (eval(x),eval(y)) match {
        case (x: Int, y: Int) => x + y
        case (x: Double, y: Double) => x + y
        case (x: String, y: String) => x + y 
        // todo widen
      }
      case Minus(x,y) => (eval(x),eval(y)) match {
        case (x: Int, y: Int) => x + y
        case (x: Double, y: Double) => x + y
        case (x: String, y: String) => x + y 
        // todo widen
      }
      case Var(x) => env(x)
    }
    def exec(s: Stm): Unit = s match {
      case Assign(x,y) => env(x) = eval(y)
      case While(x,y) => while (eval(x).asInstanceOf[Int] > 0) exec(y) // test int
      case Print(x) => println(eval(x))
      case Block(xs) => xs.foreach(exec(_)) // unroll
    }

    // GOAL 1: switch plus/minus nodes
    // GOAL 2: switch environment if new keys added

    val p = Block(List(
      //Assign("max", IntVal(100)),
      Assign("x", IntVal(0)),
      While(Minus(Var("max"), Var("x")), Block(List(
        Print(Var("x")),
        Assign("x", Plus(Var("x"), IntVal(1)))
      )))
    ))

    val f = it.compile { (x:Int) => 
      env("max") = x
      exec(p)
    }
    println(f(100))
  }




  class DecompilerLMS extends BytecodeInterpreter_LMS_Opt with DefaultMacrosLMS {

    //def fastpath() = {} // XXX

    initialize()
    emitUniqueOpt = true
    debugBlockKeys = false
    debugReadWrite = false
    //debugMethods = false
    //debugReturns = true

  }

  class Decompiler extends BytecodeInterpreter_LIR_Opt with DefaultMacros {

    initialize()
    emitUniqueOpt = true
    debugBlockKeys = false
    debugReadWrite = false
    debugMethods = false
    debugReturns = true

  }

}
{'fraction_non_alphanumeric': 0.1390643400064233, 'fraction_numerical': 0.022695642864789638, 'mean_word_length': 2.1370047011417057, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '666127', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3296, 'n_words': 927}