Main.scala 2.13 KB
Newer Older
Sapphie's avatar
Sapphie committed
1
2
3
4
5
6
7
package l3

import java.io.PrintWriter
import java.nio.file.{ Files, Paths }

import l3.SymbolicCL3TreeModule.Tree

Sapphie's avatar
Sapphie committed
8
9
10
11
import CL3TreeFormatter._ // Implicits required for CL3 tree printing
import CPSTreeFormatter._ // Implicits required for CPS tree printing
import CPSTreeChecker._   // Implicits required for CPS tree checking

Sapphie's avatar
Sapphie committed
12
13
14
object Main {
  def main(args: Array[String]): Unit = {
    val backEnd: Tree => TerminalPhaseResult = (
Sapphie's avatar
Sapphie committed
15
      CL3ToCPSTranslator
Sapphie's avatar
Fix bug    
Sapphie committed
16
        andThen treePrinter("---------- After CPS translation")
17
18
19
        andThen CPSOptimizerHigh
        andThen treePrinter("---------- After Optimization (High)")
        andThen treeChecker
Sapphie's avatar
Sapphie committed
20
21
        andThen CPSValueRepresenter
        andThen treePrinter("---------- After value representation")
Sapphie's avatar
Sapphie committed
22
        andThen treeChecker
Sapphie's avatar
Sapphie committed
23
        andThen CPSHoister
24
25
26
27
        andThen treePrinter("---------- After hoisting")
        andThen CPSOptimizerLow
        andThen treePrinter("---------- After Optimization (Low)")
        andThen treeChecker
Sapphie's avatar
Sapphie committed
28
        andThen CPSInterpreterLow
Sapphie's avatar
Sapphie committed
29
30
    )

Luca Bataillard's avatar
Luca Bataillard committed
31

Sapphie's avatar
Sapphie committed
32
    val basePath = Paths.get(".").toAbsolutePath
33
    Either.cond(! args.isEmpty, args.toIndexedSeq, "no input file given")
Sapphie's avatar
Sapphie committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      .flatMap(L3FileReader.readFilesExpandingModules(basePath, _))
      .flatMap(p => L3Parser.parse(p._1, p._2))
      .flatMap(CL3NameAnalyzer)
      .flatMap(backEnd) match {
      case Right((retCode, maybeMsg)) =>
        maybeMsg foreach println
        sys.exit(retCode)
      case Left(errMsg) =>
        println(s"Error: $errMsg")
        sys.exit(1)
    }
  }

  private lazy val outPrintWriter =
    new PrintWriter(System.out, true)

Sapphie's avatar
Sapphie committed
50
51
52
  private def treeChecker[T <: CPSTreeModule](implicit c: CPSTreeChecker[T]) =
    passThrough(c)

Sapphie's avatar
Sapphie committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  private def treePrinter[T](msg: String)(implicit f: Formatter[T]): T => T =
    passThrough { tree =>
      outPrintWriter.println(msg)
      f.toDoc(tree).writeTo(80, outPrintWriter)
      outPrintWriter.println()
    }

  private def seqPrinter[T](msg: String): Seq[T] => Seq[T] =
    passThrough { program =>
      outPrintWriter.println(msg)
      program foreach outPrintWriter.println
    }

  private def passThrough[T](f: T => Unit): T => T =
    { t: T => f(t); t }
}