Skip to content

Commit

Permalink
Implement if/then/else statement
Browse files Browse the repository at this point in the history
Signed-off-by: reidspencer <reid.spencer@yoppworks.com>
  • Loading branch information
reid-spencer committed Sep 14, 2023
1 parent 11eeb12 commit 605fd72
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,19 @@ trait Statements {
do_.map(_.format).mkString("\n") + "end\n"
}

case class IfThenElseStatement(
loc: At,
cond: LiteralString,
thens: Seq[Statement],
elses: Seq[Statement]
) extends Statement {
override def kind: String = "IfThenElse Statement"
def format: String = s"if ${cond.format} then\n{\n${
thens.map(_.format).mkString(" ", "\n ", "\n}") +
(if elses.nonEmpty then
" else {\n" + elses.map(_.format).mkString(" ", "\n ", "\n}\n")
else
"\n"
)}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,31 @@ private[parsing] trait StatementParser {

private def forEachStatement[u: P](set: StatementsSet): P[ForEachStatement] = {
P(
location ~ Keywords.foreach ~ pathIdentifier ~ Keywords.do_ ~ setOfStatements(set) ~ Keywords.end_
location ~ Keywords.foreach ~ pathIdentifier ~ Keywords.do_ ~ pseudoCodeBlock(set) ~ Keywords.end_
)./.map { case (loc, pid, statements) =>
ForEachStatement(loc, pid, statements)
}
}

private def ifThenElseStatement[u: P](set: StatementsSet): P[IfThenElseStatement] = {
P(
location ~ Keywords.if_ ~ literalString ~ Keywords.then_ ~ pseudoCodeBlock(set) ~ (
Keywords.else_ ~ pseudoCodeBlock(set)
).?
)./.map { case (loc, cond, thens, maybeElses) =>
val elses = maybeElses.getOrElse(Seq.empty[Statement])
IfThenElseStatement(loc, cond, thens, elses)
}
}

private def callStatement[u: P]: P[CallStatement] = {
P(location ~ Keywords.call ~ functionRef)./.map { tpl => (CallStatement.apply _).tupled(tpl) }
}

private def anyDefStatements[u: P](set: StatementsSet): P[Statement] = {
P(
sendStatement | arbitraryStatement | errorStatement | setStatement | tellStatement | callStatement |
forEachStatement(set)
ifThenElseStatement(set) | forEachStatement(set)
)
}

Expand Down
4 changes: 2 additions & 2 deletions language/src/test/input/everything.riddl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ domain Everything by author Reid is {
state someState of Something.someData is {
handler foo is {
on command ACommand {
"if Something arrives and misc() then"
if "Something arrives" then {
send event Inebriated to outlet APlant.Source.Commands
"end"
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion language/src/test/input/full/context.riddl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ context context by author Reid is {
event event is { at: TimeStamp }
handler handler is {
on event event {
error "This is an error"
if "there is an error" then {
error "This is an error"
}
}
}
repository repository is { ??? }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.reactific.riddl.language.AST.*
import com.reactific.riddl.language.AST
import com.reactific.riddl.language.{ParsingTest, TestParser}

/** Unit Tests For ParserTest */
/** Unit Tests For Parsing */
class ParserTest extends ParsingTest {

"ParserContext" must {
Expand Down

0 comments on commit 605fd72

Please sign in to comment.