DylanSp / wheel-lang

A small toy imperative language (with some OOP features) for demonstrating and practicing language design/implementation.
MIT License
14 stars 0 forks source link

Allow "else if" without nesting #25

Closed DylanSp closed 4 years ago

DylanSp commented 4 years ago

As of v0.1, multiple if statements in a row require nesting:

if (cond1)
{
}
else
{
  if (cond2)
  {
  }
  else
  {
  }
}

I'd like to allow the more normal "else if" construction:

if (cond1)
{
}
else if (cond2)
{
}
else
{
}

Right now, this is prohibited by the requirement that if/else bodies must be surrounded by braces, but it may be possible to change the grammar to avoid this.

DylanSp commented 4 years ago

Golang requires braces but allows else-if; the grammar is

IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .

I'd modify the grammar from:

IfStatement -> "if" "(" LogicalExpression ")" Block "else" Block

to

IfStatement -> "if" "(" LogicalExpression ")" Block "else" (Block | IfStatement)