interpreter.scala

来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 159 行

SCALA
159
字号
import scala.tools.nsc._import java.io.{BufferedReader, StringReader, PrintWriter,                Writer, OutputStreamWriter}object Test {  val testCodeString = <code>// basics3+4def gcd(x: int, y: int): int = {{          if (x == 0) y          else if (y == 0) x          else gcd(y%x, x)}}val five = gcd(15,35)var x = 1x = 2val three = x+1type anotherint = Intval four: anotherint = 4val bogus: anotherint = "hello"trait PointlessTraitval (x,y) = (2,3)println("hello")// implicit conversionscase class Foo(n: int)case class Bar(n: int)implicit def foo2bar(foo: Foo) = Bar(foo.n)val bar: Bar = Foo(3)// importing from a previous resultimport bar._val m = n// stressing the imports mechanismval one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val one = 1val x1 = 1val x2 = 1val x3 = 1val x4 = 1val x5 = 1val x6 = 1val x7 = 1val x8 = 1val x9 = 1val x10 = 1val x11 = 1val x12 = 1val x13 = 1val x14 = 1val x15 = 1val x16 = 1val x17 = 1val x18 = 1val x19 = 1val x20 = 1val two = one + x5// interior syntax errors should *not* go into multi-line input mode.// both of the following should abort immediately:def x => y => z[1,2,3]// multi-line XML&lt;a>&lt;b  c="c"  d="dd"/>&lt;/a>/*  /*    multi-line comment  */*/// multi-line string"""hellothere"""(1 +   // give up early by typing two blank lines// defining and using quoted names should work (ticket #323)def `match` = 1 val x = `match` // multiple classes defined on one linesealed class Exp; class Fact extends Exp; class Term extends Expdef f(e: Exp) = e match {{  // non-exhaustive warning here  case _:Fact => 3}}</code>.text  /** A writer that skips the first line of text.  The first   *  line of interpreter output is skipped because it includes   *  a version number. */  class Skip1Writer(writer: Writer) extends Writer {    var seenNL = false    def write(cbuf: Array[Char], off: Int, len: Int) {      if (seenNL) 	writer.write(cbuf, off, len)       else {	val slice : Array[Char] = cbuf.slice(off, off+len)	val i = slice.indexOf('\n')	if (i >= 0) {	  seenNL = true	  writer.write(slice, i+1, slice.length-(i+1))	} else {	  // skip it	}      }    }    def close() { writer.close() }    def flush() { writer.flush() }  }  def main(args: Array[String]) {    val input = new BufferedReader(new StringReader(testCodeString))    val output = new PrintWriter(      new Skip1Writer(new OutputStreamWriter(Console.out)))    val repl = new InterpreterLoop(input, output)    repl.main(new Settings)    println()  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?