文件名称:scala-2.9.2.jar
文件大小:23.33MB
文件格式:ZIP
更新时间:2015-05-08 09:15:23
scala scala api jar scala.jar
// Contributed by Daniel Gronau import scala.annotation._ trait Func[T] { val zero: T def inc(t: T): T def dec(t: T): T def in: T def out(t: T): Unit } object ByteFunc extends Func[Byte] { override val zero: Byte = 0 override def inc(t: Byte) = ((t + 1) & 0xFF).toByte override def dec(t: Byte) = ((t - 1) & 0xFF).toByte override def in: Byte = readByte override def out(t: Byte) { print(t.toChar) } } case class Tape[T](left: List[T], cell: T, right: List[T])(implicit func: Func[T]) { private def headOf(list:List[T]) = if (list.isEmpty) func.zero else list.head private def tailOf(list:List[T]) = if (list.isEmpty) Nil else list.tail def isZero = cell == func.zero def execute(ch: Char) = (ch: @switch) match { case '+' => copy(cell = func.inc(cell)) case '-' => copy(cell = func.dec(cell)) case '<' => Tape(tailOf(left), headOf(left), cell :: right) case '>' => Tape(cell :: left, headOf(right), tailOf(right)) case '.' => func.out(cell); this case ',' => copy(cell = func.in) case '[' | ']' => this case _ => error("Unexpected token: " + ch) } } object Tape { def empty[T](func: Func[T]) = Tape(Nil, func.zero, Nil)(func) } class Brainfuck[T](func:Func[T]) { def execute(p: String) { val prog = p.replaceAll("[^\\+\\-\\[\\]\\.\\,\\>\\<]", "") @tailrec def braceMatcher(pos: Int, stack: List[Int], o2c: Map[Int, Int]): Map[Int,Int] = if(pos == prog.length) o2c else (prog(pos): @switch) match { case '[' => braceMatcher(pos + 1, pos :: stack, o2c) case ']' => braceMatcher(pos + 1, stack.tail, o2c + (stack.head -> pos)) case _ => braceMatcher(pos + 1, stack, o2c) } val open2close = braceMatcher(0, Nil, Map()) val close2open = open2close.map(_.swap) @tailrec def ex(pos:Int, tape:Tape[T]): Unit = if(pos < prog.length) ex((prog(pos): @switch) match { case '[' if tape.isZero => open2close(pos) case ']' if ! tape.isZero => close2open(pos) case _ => pos + 1 }, tape.execute(prog(pos))) println("---running---") ex(0, Tape.empty(func)) println("\n---done---") } } /* Run with: val bf = new Brainfuck(ByteFunc) bf.execute(""">+++++++++[<++++++++>-]<.>+++++++[<++ ++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.#>+++++++++++[<+++++>-]<.>++++++++[<++ +>-]<.+++.------.--------.[-]>++++++++[<++++> -]<+.[-]++++++++++.""") */
【文件预览】:
scala-2.9.2
----misc()
--------scala-devel()
----bin()
--------scalap(4KB)
--------scaladoc(4KB)
--------scaladoc.bat(2KB)
--------scala.bat(2KB)
--------scalap.bat(2KB)
--------fsc(4KB)
--------scala(4KB)
--------scalac.bat(2KB)
--------fsc.bat(2KB)
--------scalac(4KB)
----man()
--------man1()
----src()
--------scalap-src.jar(41KB)
--------scala-partest-src.jar(50KB)
--------scala-library-src.jar(1.11MB)
--------scala-swing-src.jar(132KB)
--------scala-dbc-src.jar(48KB)
--------scala-compiler-src.jar(1.63MB)
----doc()
--------tools()
--------LICENSE(2KB)
--------README(2KB)
----lib()
--------jline.jar(155KB)
--------scalacheck.jar(723KB)
--------scala-library.jar(8.45MB)
--------scala-swing.jar(882KB)
--------scalap.jar(822KB)
--------scala-partest.jar(399KB)
--------scala-dbc.jar(309KB)
--------scala-compiler.jar(10.92MB)