Skip to content

Commit c2905c7

Browse files
committed
WIP
1 parent 1779bbc commit c2905c7

4 files changed

Lines changed: 1687 additions & 55 deletions

File tree

compiler/shared/src/main/scala/Prims.scala

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import
99
JsOps.{ indented, jsString, jsStringEscaped }
1010

1111
import org.nlogo.core.{
12+
Application,
1213
AstNode,
1314
CommandBlock,
1415
Expression,
@@ -19,7 +20,7 @@ import org.nlogo.core.{
1920
Statement,
2021
Syntax
2122
}
22-
import org.nlogo.core.prim.Lambda
23+
import org.nlogo.core.prim.{ Lambda, _symbol }
2324

2425
import org.nlogo.tortoise.compiler.utils.CompilerErrors.failCompilation
2526

@@ -99,6 +100,44 @@ trait PrimUtils {
99100
}
100101
}
101102

103+
// Check if a type mask allows SymbolType
104+
private def allowsSymbolType(mask: Int): Boolean =
105+
Arguments.isSupported(mask, Syntax.SymbolType)
106+
107+
// Extract symbol value from an expression if it's a symbol primitive
108+
def compileExtensionArgs(app: Application, syntax: Syntax, useCompileArgs: Boolean)
109+
(implicit compilerFlags: CompilerFlags, compilerContext: CompilerContext, procContext: ProcedureContext): String = {
110+
111+
val argTypes = if (syntax.isInfix) {
112+
Seq(syntax.left) ++ syntax.right
113+
} else {
114+
syntax.right
115+
}
116+
117+
def compileArg(arg: Expression): String =
118+
arg match {
119+
case r: ReporterApp => handlers.reporter(r, useCompileArgs)
120+
case b: ReporterBlock => handlers.reporter(b, useCompileArgs)
121+
case c: CommandBlock => s"() => { ${handlers.commands(c)} }"
122+
case _ => handlers.reporter(arg, useCompileArgs)
123+
}
124+
125+
val compiledArgs = app.args.zipWithIndex.map { case (arg, i) =>
126+
val allowedTypes = if (i < argTypes.length) argTypes(i) else argTypes.last
127+
if (allowsSymbolType(allowedTypes)) {
128+
arg match {
129+
case r: ReporterApp if r.reporter.isInstanceOf[_symbol] =>
130+
jsString(r.reporter.token.text.toLowerCase)
131+
case _ => compileArg(arg)
132+
}
133+
} else {
134+
compileArg(arg)
135+
}
136+
}
137+
138+
compiledArgs.mkString(", ")
139+
}
140+
102141
}
103142

104143
object ReporterPrims {
@@ -244,7 +283,7 @@ trait ReporterPrims extends PrimUtils {
244283
val ExtensionPrimRegex = """_externreport\(([^:]+):([^)]+)\)""".r
245284
(x.toString: @unchecked) match {
246285
case ExtensionPrimRegex(extName, primName) =>
247-
s"Extensions[${jsString(extName)}].prims[${jsString(primName)}](${args.commas})"
286+
s"Extensions[${jsString(extName)}].prims[${jsString(primName)}](${compileExtensionArgs(r, x.syntax, useCompileArgs)})"
248287
}
249288

250289
case ra: prim.etc._range =>
@@ -621,7 +660,7 @@ trait CommandPrims extends PrimUtils {
621660
val ExtensionPrimRegex = """_extern\(([^:]+):([^)]+)\)""".r
622661
(x.toString: @unchecked) match {
623662
case ExtensionPrimRegex(extName, primName) =>
624-
s"Extensions[${jsString(extName)}].prims[${jsString(primName)}](${args.commas});"
663+
s"Extensions[${jsString(extName)}].prims[${jsString(primName)}](${compileExtensionArgs(s, x.syntax, useCompileArgs)});"
625664
}
626665

627666
case _ if compilerFlags.generateUnimplemented =>

0 commit comments

Comments
 (0)