Skip to content

Commit

Permalink
Show correct error for foreach in command center
Browse files Browse the repository at this point in the history
  • Loading branch information
mrerrormessage committed Jan 6, 2017
1 parent 4fcc4f4 commit 568152e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
7 changes: 5 additions & 2 deletions netlogo-gui/src/main/compile/prim/LegacyPrims.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ package etc {
}

case class _english() extends Command {
def syntax = Syntax.commandSyntax(agentClassString = "O---")
def syntax = Syntax.commandSyntax(
agentClassString = "O---",
canBeConcise = false)
}

case class _extracthsbold() extends Reporter {
Expand Down Expand Up @@ -82,7 +84,8 @@ package etc {
}

case class _git() extends Command {
def syntax = Syntax.commandSyntax(agentClassString = "O---", right = List(StringType))
def syntax = Syntax.commandSyntax(agentClassString = "O---", right = List(StringType),
canBeConcise = false)
}

case class _hsbold() extends Reporter {
Expand Down
12 changes: 8 additions & 4 deletions parser-core/src/main/core/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ case class Syntax private(
isRightAssociative: Boolean, // only relevant if infix
agentClassString: String,
blockAgentClassString: Option[String],
introducesContext: Boolean)
introducesContext: Boolean,
canBeConcise: Boolean)
{

import Syntax._
Expand Down Expand Up @@ -137,7 +138,8 @@ object Syntax {
minimumOption: Option[Int] = None, // minimum number of args might be different than the default
agentClassString: String = "OTPL",
blockAgentClassString: Option[String] = None,
introducesContext: Boolean = false
introducesContext: Boolean = false,
canBeConcise: Boolean = true
): Syntax =
new Syntax(
precedence = CommandPrecedence,
Expand All @@ -149,7 +151,8 @@ object Syntax {
isRightAssociative = false,
agentClassString = agentClassString,
blockAgentClassString = blockAgentClassString,
introducesContext = introducesContext || blockAgentClassString.nonEmpty
introducesContext = introducesContext || blockAgentClassString.nonEmpty,
canBeConcise = canBeConcise
)

def reporterSyntax(
Expand All @@ -172,7 +175,8 @@ object Syntax {
isRightAssociative = isRightAssociative,
agentClassString = agentClassString,
blockAgentClassString = blockAgentClassString,
introducesContext = blockAgentClassString.nonEmpty
introducesContext = blockAgentClassString.nonEmpty,
canBeConcise = true
)

/** <i>Unsupported. Do not use.</i> */
Expand Down
12 changes: 8 additions & 4 deletions parser-core/src/main/core/prim/etc/etc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ case class _link() extends Reporter {
case class _linkcode() extends Command {
override def syntax =
Syntax.commandSyntax(
agentClassString = "---L")
agentClassString = "---L",
canBeConcise = false)
}
case class _linklength() extends Reporter {
override def syntax =
Expand Down Expand Up @@ -673,7 +674,8 @@ case class _nvalues() extends Reporter {
case class _observercode() extends Command {
override def syntax =
Syntax.commandSyntax(
agentClassString = "O---")
agentClassString = "O---",
canBeConcise = false)
}
case class _patch() extends Reporter {
override def syntax =
Expand All @@ -684,7 +686,8 @@ case class _patch() extends Reporter {
case class _patchcode() extends Command {
override def syntax =
Syntax.commandSyntax(
agentClassString = "--P-")
agentClassString = "--P-",
canBeConcise = false)
}
case class _patchhere() extends Reporter {
override def syntax =
Expand Down Expand Up @@ -1029,7 +1032,8 @@ case class _tostring() extends Reporter with Pure {
case class _turtlecode() extends Command {
override def syntax =
Syntax.commandSyntax(
agentClassString = "-T--")
agentClassString = "-T--",
canBeConcise = false)
}
case class _untie() extends Command {
override def syntax =
Expand Down
2 changes: 1 addition & 1 deletion parser-core/src/main/core/prim/misc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ case class _createturtles(breedName: String) extends Command {
blockAgentClassString = Option("-T--"))
}
case class _done() extends Command {
override def syntax = Syntax.commandSyntax()
override def syntax = Syntax.commandSyntax(canBeConcise = false)
}
case class _equal() extends Reporter with Pure {
override def syntax =
Expand Down
2 changes: 2 additions & 0 deletions parser-core/src/main/parse/ExpressionParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ object ExpressionParser {
// expand e.g. "foreach xs print" -> "foreach xs [[x] -> print x]"
private def expandConciseCommandLambda(token: Token, scope: SymbolTable): core.ReporterApp = {
val coreCommand = token.value.asInstanceOf[core.Command]
if (! coreCommand.syntax.canBeConcise)
throw new UnexpectedTokenException(token)
val (varNames, varApps) = syntheticVariables(coreCommand.syntax.totalDefault, coreCommand.token, scope)
val stmtArgs =
if (coreCommand.syntax.takesOptionalCommandBlock)
Expand Down
3 changes: 3 additions & 0 deletions parser-core/src/test/parse/FrontEndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ class FrontEndTests extends FunSuite {
runTest("foreach [1 2 3] print",
"_foreach()[_const([1.0, 2.0, 3.0])[], _commandlambda(_0)[[_print()[_lambdavariable(_0)[]]]]]")
}
test("DoParseForeachWithDone") {
runFailure("foreach [1 2 3] __done", "FOREACH expected at least 2 inputs, a list and an anonymous command.", 0, 7)
}
test("DoParselet") {
runTest("let x 5 __ignore x",
"_let(Let(X))[_const(5.0)[]] _ignore()[_letvariable(Let(X))[]]")
Expand Down
4 changes: 4 additions & 0 deletions test/commands/ControlStructures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Foreach2
O> (foreach [1 2 3] [4 5 6] [[x y] -> crt (x + y) ])
count turtles => 21

ForeachWithDone
to foo foreach [ 1 2 3 ] __done end
COMPILE> COMPILER ERROR FOREACH expected at least 2 inputs, a list and an anonymous command.

Run1
O> run "crt 10"
count turtles => 10
Expand Down

0 comments on commit 568152e

Please sign in to comment.