Skip to content

Commit

Permalink
Prefactor: Lay groundwork for compiling just plots
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBizzle committed Nov 27, 2023
1 parent 48dc0e2 commit f7bf406
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 19 additions & 3 deletions compiler/shared/src/main/scala/CompiledModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package org.nlogo.tortoise.compiler

import
org.nlogo.{ core, parse },
core.{ model, CompilerException, Model, View },
core.{ model, CompilerException, Model, Plot, View, Widget },
model.ModelReader,
parse.CompilerUtilities

Expand Down Expand Up @@ -61,10 +61,13 @@ object CompiledModel {
CompiledModel(compiler.toJS(compilation), compilation, c)
}

def fromNlogoContents(contents: String, compiler: Compiler)
def fromNlogoContents(contents: String, compiler: Compiler, extraWidgets: Seq[Widget] = Seq())
(implicit compilerFlags: CompilerFlags): ValidationNel[Exception, CompiledModel] = {
val validation =
try fromModel(ModelReader.parseModel(contents, CompilerUtilities, Map()), compiler)
try {
val model = ModelReader.parseModel(contents, CompilerUtilities, Map())
fromModel(model.copy(widgets = model.widgets ++ extraWidgets), compiler)
}
catch {
case e: RuntimeException => e.failureNel
}
Expand All @@ -82,6 +85,19 @@ object CompiledModel {
fromModel(model.copy(code = netlogoCode), compiler)
}

def plotsToJS(plots: Seq[Plot], oldModel: CompiledModel)
(implicit compilerFlags: CompilerFlags): CompileResult[String] = {
val CompiledModel(_, compilation, compiler) = oldModel
val model = compilation.model
validate(compiler) {
(c) =>
val nonPlots = model.widgets.filterNot(_.isInstanceOf[Plot])
val newWidgets = nonPlots ++ plots
val compilation = c.compileProcedures(model.copy(widgets = newWidgets))
compiler.plotsToJS(compilation.widgets)
}
}

private def validate[T](compiler: Compiler)
(compileFunc: (Compiler) => T): CompileResult[T] =
try compileFunc(compiler).successNel
Expand Down
9 changes: 9 additions & 0 deletions compiler/shared/src/main/scala/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ class Compiler {

}

def plotsToJS(widgets: Seq[CompiledWidget]): String = {
val globalModelConfig = JsStatement("global.modelConfig", Polyfills.content)
TortoiseLoader.integrateSymbols(
PlotCompiler.formatPlots(widgets)
:+ globalModelConfig
:+ resolveModelConfig
)
}

def compileReporter(
logo: String,
oldProcedures: ProceduresMap = NoProcedures,
Expand Down

0 comments on commit f7bf406

Please sign in to comment.