summaryrefslogtreecommitdiff
path: root/test/files/run/interpreter.scala
blob: 9b270f380af4c2f83b3de6b59b27ebe14197734f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import scala.tools.nsc._
import java.io.{BufferedReader, StringReader, PrintWriter,
                Writer, OutputStreamWriter}

object Test {
  val testCodeString = <code>
// basics
3+4
def 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 = 1
x = 2
val three = x+1
type anotherint = Int
val four: anotherint = 4
val bogus: anotherint = "hello"
trait PointlessTrait

// implicit conversions
case 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 result
import bar._
val m = n

// stressing the imports mechanism
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1
val one = 1


val x1 = 1
val x2 = 1
val x3 = 1
val x4 = 1
val x5 = 1
val x6 = 1
val x7 = 1
val x8 = 1
val x9 = 1
val x10 = 1
val x11 = 1
val x12 = 1
val x13 = 1
val x14 = 1
val x15 = 1
val x16 = 1
val x17 = 1
val x18 = 1
val x19 = 1
val x20 = 1

val 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
"""
hello
there
"""

(1 +   // give up early by typing two blank lines



</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()
  }
}