Skip to content

Commit 10768a3

Browse files
committed
WIP
1 parent 562cfdd commit 10768a3

6 files changed

Lines changed: 1692 additions & 60 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 =>

compiler/shared/src/main/scala/json/DrawingActionToJson.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ class StampImageConverter(override protected val target: StampImage) extends Dra
134134
"size" -> JsDouble(size),
135135
"x" -> JsDouble(x),
136136
"y" -> JsDouble(y),
137-
"shapeName" -> JsString(shapeName),
138-
"stampMode" -> JsString(stampMode)
137+
"shapeName" -> JsString(shapeName)
139138
))
140139

141140
("turtle", obj)
@@ -156,8 +155,7 @@ class StampImageConverter(override protected val target: StampImage) extends Dra
156155
"thickness" -> JsDouble(thickness),
157156
"directed?" -> JsBool(isDirected),
158157
"size" -> JsDouble(size),
159-
"hidden?" -> JsBool(isHidden),
160-
"stampMode" -> JsString(stampMode)
158+
"hidden?" -> JsBool(isHidden)
161159
))
162160

163161
("link", obj)

compiler/shared/src/main/scala/utils/CompilerUtils.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package org.nlogo.tortoise.compiler.utils
55
import org.nlogo.core.{ CompilerException, Token }
66

77
object CompilerErrors {
8+
val eof = Token.eof("")
9+
810
def failCompilation(msg: String): Nothing =
9-
failCompilation(msg, Token.Eof.start, Token.Eof.end, Token.Eof.filename)
11+
failCompilation(msg, eof.start, eof.end, eof.filename)
1012

1113
def failCompilation(msg: String, token: Token): Nothing =
1214
failCompilation(msg, token.start, token.end, token.filename)

0 commit comments

Comments
 (0)