Skip to content

Commit

Permalink
Update bootcompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
h7x4 committed Sep 2, 2022
1 parent b17889a commit beb5784
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 138 deletions.
6 changes: 3 additions & 3 deletions bootcompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ project(MOZARTBOOTCOMPILER NONE)
find_package(Java COMPONENTS Runtime REQUIRED)

set(SBT_JAVA_OPTS
-Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M
-Xms512M -Xmx1024M -Xss1M
CACHE STRING "Options passed to the Java executable when running sbt")

set(SBT "${Java_JAVA_EXECUTABLE}" ${SBT_JAVA_OPTS} -Dfile.encoding=UTF-8
-jar "${CMAKE_CURRENT_SOURCE_DIR}/sbt-launch.jar")

file(GLOB_RECURSE bootcompiler_sources src/*)
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/bootcompiler.jar"
COMMAND ${SBT} oneJar
COMMAND ${SBT} assembly
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/target/scala-2.11/bootcompiler_2.11-2.0-SNAPSHOT-one-jar.jar"
"${CMAKE_CURRENT_SOURCE_DIR}/target/scala-2.13/bootcompiler-assembly-2.0-SNAPSHOT.jar"
"${CMAKE_CURRENT_BINARY_DIR}/bootcompiler.jar"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS "sbt-launch.jar" "build.sbt" "project/plugins.sbt" ${bootcompiler_sources}
Expand Down
16 changes: 9 additions & 7 deletions bootcompiler/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ name := "bootcompiler"

version := "2.0-SNAPSHOT"

scalaVersion := "2.11.2"
scalaVersion := "2.13.8"

scalacOptions ++= Seq("-deprecation", "-optimize")
scalacOptions ++= Seq("-language:postfixOps", "-deprecation", "-optimize")

libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.2"

libraryDependencies += "com.github.scopt" %% "scopt" % "3.2.0"

seq(com.github.retronym.SbtOneJar.oneJarSettings: _*)
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1"
libraryDependencies += "com.github.scopt" %% "scopt" % "4.1.0"
libraryDependencies += "io.spray" %% "spray-json" % "1.3.6"

// Work around a bug that prevents generating documentation
unmanagedClasspath in Compile +=
Attributed.blank(new java.io.File("doesnotexist"))

// Added during migration to the java 9 module system
Compile / packageBin / packageOptions +=
Package.ManifestAttributes("Add-Exports" -> "java.base/jdk.internal.math")
2 changes: 1 addition & 1 deletion bootcompiler/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.6
sbt.version=1.6.2
2 changes: 1 addition & 1 deletion bootcompiler/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.github.retronym" % "sbt-onejar" % "0.8")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
4 changes: 2 additions & 2 deletions bootcompiler/sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh

java ${DEBUG_PARAM} -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled \
-XX:MaxPermSize=384M ${JAVA_OPTS} -Dfile.encoding=UTF-8 \
java ${DEBUG_PARAM} -Xms512M -Xmx1536M -Xss1M \
${JAVA_OPTS} -Dfile.encoding=UTF-8 \
-jar `dirname $0`/sbt-launch.jar "$@"
2 changes: 1 addition & 1 deletion bootcompiler/sbt.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off

set SCRIPT_DIR=%~dp0
java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M %JAVA_OPTS% -Dfile.encoding=UTF-8 -jar "%SCRIPT_DIR%sbt-launch.jar" %*
java -Xms512M -Xmx1024M -Xss1M %JAVA_OPTS% -Dfile.encoding=UTF-8 -jar "%SCRIPT_DIR%sbt-launch.jar" %*
172 changes: 94 additions & 78 deletions bootcompiler/src/main/scala/org/mozartoz/bootcompiler/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package org.mozartoz.bootcompiler
import java.io.{ Console => _, _ }

import scala.collection.mutable.ListBuffer
import scala.collection.immutable.PagedSeq
import scala.util.parsing.input.PagedSeqReader
import scala.util.parsing.combinator._
import scala.util.parsing.input._
import scala.util.parsing.json._

import scopt.{ OParser, OParserSetup, DefaultOParserSetup }
import spray.json._

import oz._
import parser._
Expand Down Expand Up @@ -38,31 +40,38 @@ case class Config(
/** Entry point for the Mozart2 bootstrap compiler */
object Main {
/** Executes the Mozart2 bootstrap compiler */
def main(args: Array[String]) {
def main(args: Array[String]): Unit = {
// Define command-line options
val optParser = new scopt.OptionParser[Config]("bootcompiler") {
head("bootcompiler", "2.0.x")
opt[Unit]("baseenv") action {
(_, c) => c.copy(mode = Config.Mode.BaseEnv)
} text("switch to base environment mode")
opt[String]('o', "output") action {
(v, c) => c.copy(outputStream =
() => new BufferedOutputStream(new FileOutputStream(v)))
} text("output file")
opt[String]('m', "module") action {
(v, c) => c.copy(moduleDefs = v :: c.moduleDefs)
} text("module definition file or directory")
opt[String]('b', "base") action {
(v, c) => c.copy(baseDeclsFileName = v)
} text("path to the base declarations file")
opt[String]('D', "define") action {
(v, c) => c.copy(defines = c.defines + v)
} text("add a symbol to the conditional defines")
arg[String]("<file>") action {
(v, c) => c.copy(fileName = v)
} text("input file")

opt[Unit]("baseenv")
.action((_, c) => c.copy(mode = Config.Mode.BaseEnv))
.text("switch to base environment mode")

opt[String]('o', "output")
.action((v, c) =>
c.copy(outputStream = () => new BufferedOutputStream(new FileOutputStream(v))))
.text("output file")

opt[String]('m', "module")
.action((v, c) => c.copy(moduleDefs = v :: c.moduleDefs))
.text("module definition file or directory")

opt[String]('b', "base")
.action((v, c) => c.copy(baseDeclsFileName = v))
.text("path to the base declarations file")

opt[String]('D', "define")
.action((v, c) => c.copy(defines = c.defines + v))
.text("add a symbol to the conditional defines")

arg[String]("<file>")
.action((v, c) => c.copy(fileName = v))
.text("input file")
}


// Parse the options
optParser.parse(args, Config()) map { config =>
// OK, we're good to go
Expand All @@ -80,15 +89,11 @@ object Main {
th.printStackTrace()
sys.exit(2)
}
} getOrElse {
// Bad command-line arguments
optParser.showUsage
sys.exit(1)
}
}

/** Performs the Module mode */
private def mainModule(config: Config) {
private def mainModule(config: Config): Unit = {
import config._

val (program, _) = createProgram(moduleDefs, Some(baseDeclsFileName))
Expand All @@ -103,7 +108,7 @@ object Main {
}

/** Performs the BaseEnv mode */
private def mainBaseEnv(config: Config) {
private def mainBaseEnv(config: Config): Unit = {
import config._

val (program, bootModules) = createProgram(moduleDefs, None, true)
Expand All @@ -120,8 +125,10 @@ object Main {
}

/** Creates a new Program */
private def createProgram(moduleDefs: List[String],
baseDeclsFileName: Option[String], isBaseEnvironment: Boolean = false) = {
private def createProgram(
moduleDefs: List[String],
baseDeclsFileName: Option[String],
isBaseEnvironment: Boolean = false): (Program, Map[String, Expression]) = {
val program = new Program(isBaseEnvironment)
val bootModules = loadModuleDefs(program, moduleDefs)

Expand All @@ -140,8 +147,10 @@ object Main {
* @param reader input reader
* @return The statement AST
*/
private def parseStatement(reader: PagedSeqReader, file: File,
defines: Set[String]) =
private def parseStatement(
reader: PagedSeqReader,
file: File,
defines: Set[String]): Statement =
new ParserWrapper().parseStatement(reader, file, defines)

/** Parses an Oz expression from a reader
Expand All @@ -153,7 +162,7 @@ object Main {
* @return The expression AST
*/
private def parseExpression(reader: PagedSeqReader, file: File,
defines: Set[String]) =
defines: Set[String]): Expression =
new ParserWrapper().parseExpression(reader, file, defines)

/** Utility wrapper for an [[org.mozartoz.bootcompiler.parser.OzParser]]
Expand All @@ -165,11 +174,11 @@ object Main {
private val parser = new OzParser()

def parseStatement(reader: PagedSeqReader, file: File,
defines: Set[String]) =
defines: Set[String]): Statement =
processResult(parser.parseStatement(reader, file, defines))

def parseExpression(reader: PagedSeqReader, file: File,
defines: Set[String]) =
defines: Set[String]): Expression =
processResult(parser.parseExpression(reader, file, defines))

/** Processes a parse result
Expand Down Expand Up @@ -200,7 +209,7 @@ object Main {
*
* @param fileName name of the file to be read
*/
private def readerForFile(fileName: String) = {
private def readerForFile(fileName: String): PagedSeqReader = {
new PagedSeqReader(PagedSeq.fromReader(
new BufferedReader(new FileReader(fileName))))
}
Expand All @@ -211,7 +220,7 @@ object Main {
* @param moduleDefs list of files that define builtin modules
*/
private def loadModuleDefs(prog: Program, moduleDefs: List[String]) = {
JSON.globalNumberParser = (_.toInt)
// JSON.globalNumberParser = (_.toInt)

val result = new scala.collection.mutable.HashMap[String, Expression]

Expand All @@ -235,53 +244,60 @@ object Main {
}

/** Loads one builtin module definition */
private def loadModuleDef(prog: Program, moduleDef: File) = {
class CC[T] {
def unapply(a: Any): Option[T] = Some(a.asInstanceOf[T])
private def loadModuleDef(prog: Program, moduleDef: File): List[(String, Record)] = {
case class JsParameter(val kind: String)

case class JsBuiltin(
val name: String,
val inlineable: Boolean,
val inlineOpCode: Option[Int],
val params: List[JsParameter]
)

case class JsModule(
val name: String,
val builtins: List[JsBuiltin]
)

object ModuleJsonProtocol extends DefaultJsonProtocol {
implicit val parameterJsonFormat = jsonFormat1(JsParameter)
implicit val builtinJsonFormat = jsonFormat4(JsBuiltin)
implicit val moduleJsonFormat = jsonFormat2(JsModule)
}

object M extends CC[Map[String, Any]]
object L extends CC[List[Any]]
object S extends CC[String]
object D extends CC[Double]
object B extends CC[Boolean]
import ModuleJsonProtocol._

val jsModule: JsModule =
readFileToString(moduleDef)
.parseJson
.convertTo[JsModule]

val modules = JSON.parseFull(readFileToString(moduleDef)).toList
val exportFields = new ListBuffer[RecordField]

for {
M(module) <- modules
S(modName) = module("name")
L(builtins) = module("builtins")
jsBuiltin <- jsModule.builtins
} yield {
val exportFields = new ListBuffer[RecordField]

for {
M(bi) <- builtins
S(biName) = bi("name")
B(inlineable) = bi("inlineable")
L(params) = bi("params")
} {
val inlineAs =
if (inlineable) Some(bi("inlineOpCode").asInstanceOf[Int])
else None

val paramKinds = for {
M(param) <- params
S(paramKind) = param("kind")
} yield {
Builtin.ParamKind.withName(paramKind)
}
val inlineAs: Option[Int] =
if (jsBuiltin.inlineable) jsBuiltin.inlineOpCode
else None
// if (inlineable) Some(bi("inlineOpCode").asInstanceOf[Int])
// else None

val paramKinds = for {
jsParam <- jsBuiltin.params
} yield {
Builtin.ParamKind.withName(jsParam.kind)
}

val builtin = new Builtin(
modName, biName, paramKinds, inlineAs)
val builtin = new Builtin(
jsModule.name, jsBuiltin.name, paramKinds, inlineAs)

prog.builtins.register(builtin)
prog.builtins.register(builtin)

exportFields += RecordField(
Constant(OzAtom(biName)), Constant(OzBuiltin(builtin)))
}
exportFields += RecordField(
Constant(OzAtom(builtin.name)), Constant(OzBuiltin(builtin)))

val moduleURL = "x-oz://boot/" + modName
val moduleURL = "x-oz://boot/" + jsModule.name
val moduleExport = Record(Constant(OzAtom("export")), exportFields.toList)

moduleURL -> moduleExport
Expand All @@ -293,7 +309,7 @@ object Main {
* @param prog program to compile
* @param fileName top-level file that is being processed
*/
private def compile(prog: Program, fileName: String) {
private def compile(prog: Program, fileName: String): Unit = {
applyTransforms(prog)

if (prog.hasErrors) {
Expand All @@ -311,7 +327,7 @@ object Main {
}

/** Applies the successive transformation phases to a program */
private def applyTransforms(prog: Program) {
private def applyTransforms(prog: Program): Unit = {
Namer(prog)
DesugarFunctor(prog)
DesugarClass(prog)
Expand All @@ -328,7 +344,7 @@ object Main {
* @param file file to read
* @return the contents of the file
*/
private def readFileToString(file: File) = {
private def readFileToString(file: File): String = {
val source = io.Source.fromFile(file)
try source.mkString
finally source.close()
Expand All @@ -339,7 +355,7 @@ object Main {
* @param file file to read
* @return the lines in the file
*/
private def readFileLines(file: File) = {
private def readFileLines(file: File): List[String] = {
val source = io.Source.fromFile(file)
try source.getLines().toList
finally source.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object ProgramBuilder extends TreeDSL with TransformUtils {
* end
* }}}
*/
def buildModuleProgram(prog: Program, functor: Expression) {
def buildModuleProgram(prog: Program, functor: Expression): Unit = {
prog.rawCode = {
prog.topLevelResultSymbol === functor
}
Expand Down Expand Up @@ -65,7 +65,7 @@ object ProgramBuilder extends TreeDSL with TransformUtils {
*/
def buildBaseEnvProgram(prog: Program,
bootModulesMap: Map[String, Expression],
baseFunctor0: Expression) {
baseFunctor0: Expression): Unit = {

val baseFunctor = baseFunctor0.asInstanceOf[FunctorExpression]

Expand Down
Loading

0 comments on commit beb5784

Please sign in to comment.