scala 0.1.0

A experimental Scala interpreter written in Rust: lexer, parser, type inference, and tree-walking evaluation with a REPL.
Documentation
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# scala Language Specification

This document defines the language subset of Scala that scala implements.

## 1. Lexical Structure

### 1.1 Keywords

```
abstract  case      catch     class     def       do        else
extends   false     final     finally   for       forSome   if
implicit  import    lazy      match     new       null      object
override  package   private   protected return    sealed    super
this      throw     trait     true      try       type      val
var       while     with      yield
```

### 1.2 Operators and Delimiters

```
+  -  *  /  %        arithmetic
&  |  ^  ~           bitwise
=  !  <  >           comparison
<< >> >>>            shift
&& ||                logical short-circuit
== != <= >=          equality/comparison
=> ->                arrow
<-                   for-comprehension
:  ;  .  ,  _        punctuation
(  )  [  ]  {  }     brackets
```

### 1.3 Literals

| Type        | Examples                                    |
|-------------|---------------------------------------------|
| Integer     | `42`, `0xFF`, `0o77`, `0b1010`             |
| Long        | `42L`, `0xFFL`                              |
| Double      | `3.14`, `1.0e10`, `.5`                     |
| Float       | `3.14f`, `1.0f`                            |
| Boolean     | `true`, `false`                             |
| Character   | `'a'`, `'\n'`, `'\u0041'`                  |
| String      | `"hello"`, `"""multi\nline"""`             |
| Null        | `null`                                      |
| Unit        | `()`                                        |
| Symbol      | `'symbol`                                   |

### 1.4 Identifiers

- Alphanumeric: letter followed by letters, digits, or underscores
- Operator: one or more operator characters
- Backtick-quoted: `` `any string` ``

### 1.5 Comments

```scala
// single line
/* block comment */
/** doc comment */
```

### 1.6 String Interpolation

```scala
s"Hello, $name!"
s"Result: ${expr}"
raw"no\nescaping"
f"Pi is $pi%.2f"
```

## 2. Types

### 2.1 Primitive Types

| Type     | Description                |
|----------|----------------------------|
| `Int`    | 32-bit signed integer      |
| `Long`   | 64-bit signed integer      |
| `Double` | 64-bit floating point      |
| `Float`  | 32-bit floating point      |
| `Boolean`| `true` or `false`          |
| `Char`   | 16-bit Unicode character   |
| `String` | UTF-16 string              |
| `Unit`   | No meaningful value        |
| `Any`    | Top of type hierarchy      |
| `AnyVal` | Parent of value types      |
| `AnyRef` | Parent of reference types  |
| `Nothing`| Bottom type                |
| `Null`   | Bottom of reference types  |

### 2.2 Function Types

```scala
(A, B) => C           // Function2[A, B, C]
A => B                // Function1[A, B]
() => A               // Function0[A]
```

### 2.3 Tuple Types

```scala
(A, B)                // Tuple2[A, B]
(A, B, C)             // Tuple3[A, B, C]
```

### 2.4 Parameterized Types

```scala
List[Int]
Map[String, Int]
Option[Double]
Either[String, Int]
```

### 2.5 Type Parameters (Generics)

```scala
class Pair[A, B](val first: A, val second: B)
def identity[T](x: T): T = x
```

Variance annotations:
- `+T` — covariant
- `-T` — contravariant
- `T` — invariant (default)

Type bounds:
- `T <: Upper` — upper bound
- `T >: Lower` — lower bound

## 3. Declarations

### 3.1 Values

```scala
val x = 42
val x: Int = 42
```

Immutable bindings. Single assignment.

### 3.2 Variables

```scala
var x = 42
var x: Int = 42
x = 43
```

Mutable bindings.

### 3.3 Functions

```scala
def f(x: Int): Int = x + 1
def f(x: Int): Int = {          // block body
  val y = x + 1
  y
}
def f(x: Int)(y: Int): Int = x + y   // curried
def f(x: Int*): Int = x.sum           // varargs
```

### 3.4 Type Aliases

```scala
type StringMap[V] = Map[String, V]
```

### 3.5 Imports

```scala
import scala.collection.mutable
import scala.collection.mutable.{HashMap, HashSet}
import scala.collection.mutable._
```

## 4. Expressions

### 4.1 Literals

All literal types listed in section 1.3.

### 4.2 Arithmetic

```scala
a + b   a - b   a * b   a / b   a % b
a | b   a & b   a ^ b
a << n  a >> n  a >>> n
```

### 4.3 Comparison

```scala
a == b  a != b  a < b  a > b  a <= b  a >= b
a eq b  a ne b                            // reference equality
```

### 4.4 Boolean

```scala
a && b  a || b  !a
```

### 4.5 String Concatenation

```scala
"hello" + " " + "world"
```

### 4.6 If/Else

```scala
if (cond) expr
if (cond) expr1 else expr2
```

Both branches must be expressions when used as an expression. The type is the least upper bound of both branches.

### 4.7 Match Expression

```scala
expr match {
  case pattern1 => body1
  case pattern2 if guard2 => body2
  case _ => defaultBody
}
```

Patterns:
- Wildcard: `_`
- Variable: `x`
- Literal: `42`, `"hello"`, `true`
- Constructor: `Point(0, y)`
- Tuple: `(a, b, c)`
- Typed: `x: Int`
- Sequence wildcard: `xs*` (in constructor patterns)
- Alternative: `a | b`
- Stable identifier: `` `x` ``

### 4.8 Block Expression

```scala
{
  val x = 1
  val y = 2
  x + y     // last expression is the result
}
```

### 4.9 Function Literals (Lambdas)

```scala
(x: Int) => x + 1
(x: Int, y: Int) => x + y
_ + 1                    // placeholder syntax
(x: Int) => {
  val y = x + 1
  y
}
```

### 4.10 Method Invocation

```scala
obj.method(arg)
obj method arg            // infix style
obj.method                // no-arg
obj.method()              // empty parens
```

### 4.11 Field Access

```scala
obj.field
```

### 4.12 Tuples

```scala
(1, "hello", true)
```

### 4.13 New Expression

```scala
new ClassName(args)
new ClassName[A, B](args)
new TraitName { body }     // anonymous class
```

### 4.14 Type Cast

```scala
expr.asInstanceOf[Type]
expr.isInstanceOf[Type]
```

### 4.15 Throw / Try / Catch

```scala
throw new Exception("msg")

try {
  expr
} catch {
  case e: IOException => handle(e)
} finally {
  cleanup()
}
```

### 4.16 Return

```scala
return expr
return
```

### 4.17 For-Comprehension

```scala
for (x <- collection) yield expr
for (x <- collection) body             // imperative
for {
  x <- collection1
  y <- collection2
  if guard
} yield expr
```

### 4.18 While Loop

```scala
while (cond) body
do body while (cond)
```

### 4.19 Assignment

```scala
x = expr
obj.field = expr
```

## 5. Classes

### 5.1 Class Definition

```scala
class Point(val x: Int, val y: Int)
class Point(var x: Int, var y: Int)
class Point(x: Int, y: Int) {         // private constructor params
  def show: String = s"($x, $y)"
}
```

### 5.2 Auxiliary Constructors

```scala
class Point(val x: Int, val y: Int) {
  def this() = this(0, 0)
  def this(x: Int) = this(x, 0)
}
```

### 5.3 Type Parameters

```scala
class Box[T](val content: T)
```

### 5.4 Inheritance

```scala
class Dog(val name: String) extends Animal(name) {
  override def speak: String = "Woof!"
}
```

### 5.5 Abstract Class

```scala
abstract class Shape {
  def area: Double
  def perimeter: Double
}
```

## 6. Traits

```scala
trait Greeter {
  def greet(name: String): String
  def defaultGreet: String = greet("World")
}

trait with Logging {
  def log(msg: String): Unit = println(msg)
}
```

Traits can:
- Declare abstract members
- Provide default implementations
- Be stacked (linearization)

## 7. Objects (Companions)

```scala
object Math {
  def square(x: Int): Int = x * x
}

object Point {
  def origin: Point = new Point(0, 0)
}

case class Point(x: Int, y: Int)
object Point {                              // companion object
  def origin: Point = Point(0, 0)
}
```

## 8. Case Classes

```scala
case class Point(x: Int, y: Int)
```

Auto-generated:
- `apply` factory method on companion
- `unapply` extractor for pattern matching
- `equals`, `hashCode`, `toString`
- `copy` method
- Serializable

## 9. Sealed Traits / Classes

```scala
sealed trait Expr
case class Literal(value: Int) extends Expr
case class Add(left: Expr, right: Expr) extends Expr
case class Mul(left: Expr, right: Expr) extends Expr
```

## 10. Implicit Parameters and Conversions

```scala
implicit val ord: Ordering[Int] = Ordering.Int

def max[A](a: A, b: A)(implicit ord: Ordering[A]): A =
  if (ord.gt(a, b)) a else b

implicit def intToString(x: Int): String = x.toString
```

## 11. Lazy Values

```scala
lazy val x = {
  println("computing")
  42
}
```

## 12. Pattern Matching (Full)

```scala
x match {
  case 0 => "zero"
  case 1 | 2 | 3 => "small"
  case n if n > 10 => "big"
  case List(1, _*) => "starts with 1"
  case head :: tail => s"head=$head"
  case _ => "other"
}
```

## 13. Standard Library

### 13.1 Collections

- `List[A]` — immutable linked list
- `Vector[A]` — immutable indexed sequence
- `Map[K, V]` — immutable hash map
- `Set[A]` — immutable set
- `Option[A]``Some(value)` or `None`
- `Either[L, R]``Left(value)` or `Right(value)`

### 13.2 Predef Functions

- `println(x: Any): Unit`
- `print(x: Any): Unit`
- `assert(condition: Boolean): Unit`
- `require(condition: Boolean): Unit`
- `throwf(msg: String): Nothing`
- `identity[T](x: T): T`
- `implicitly[T]: T`

### 13.3 Type Members

- `type String = java.lang.String`
- `type RuntimeException = java.lang.RuntimeException`

## 14. Operator Overloading

Methods with symbolic names are operators:

```scala
class Vec(val x: Int, val y: Int) {
  def +(that: Vec): Vec = new Vec(this.x + that.x, this.y + that.y)
  def unary_- : Vec = new Vec(-x, -y)
}
```

## 15. Type Inference

scala supports local type inference:

- `val x = 42` infers `Int`
- `def f(x: Int) = x + 1` infers return type `Int`
- Lambda parameter types may be inferred from context
- Recursive functions require explicit return types

## 16. Semantics

- Everything is an expression (statements are expressions of type `Unit`)
- Evaluation is eager (strict), except `lazy val` and by-name parameters (`=> T`)
- Parameters are passed by value by default
- `val` is evaluated at definition time
- `var` is reassignable
- Blocks return their last expression
- `Unit` has exactly one value: `()`