# If then

if a then b

==>

Program(IfExpr(if, Identifier, ThenClause(then, Identifier)))

# If then else

if a then b else c

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, Identifier),
    ElseClause(else, Identifier)))

# Dangling else attaches to inner if

if a then if b then c else d

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(
      then,
      IfExpr(
        if,
        Identifier,
        ThenClause(then, Identifier),
        ElseClause(else, Identifier)))))

# An enclosing else bounds an applied loop

if p then sum for i list x else y

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(
      then,
      Application(BuiltinFunction, ForExpr(for, Identifier, ListClause(list, Identifier)))),
    ElseClause(else, Identifier)))

# An enclosing else bounds a while loop

if a then while p do x else y

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, WhileExpr(while, Identifier, DoClause(do, Identifier))),
    ElseClause(else, Identifier)))

# An enclosing else bounds new

if p then new T from d else e

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, NewExpr(new, Identifier, FromClause(from, Identifier))),
    ElseClause(else, Identifier)))

# An else inside a loop body stays with its if

sum for i list if p then x else continue

==>

Program(
  Application(
    BuiltinFunction,
    ForExpr(
      for,
      Identifier,
      ListClause(
        list,
        IfExpr(
          if,
          Identifier,
          ThenClause(then, Identifier),
          ElseClause(else, DebugExpr(continue)))))))

# Control construct as a binary right operand (no parens needed)

1 + if p then a else b

==>

Program(
  BinaryExpr(
    Integer,
    ArithmeticOp,
    IfExpr(
      if,
      Identifier,
      ThenClause(then, Identifier),
      ElseClause(else, Identifier))))

# Clause bodies bind looser than operators (extend right)

if x then y else z + w

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, Identifier),
    ElseClause(else, BinaryExpr(Identifier, ArithmeticOp, Identifier))))

# A parenthesized control construct can be a left operand

(if x then y else z) + w

==>

Program(
  BinaryExpr(
    ParenExpr(
      IfExpr(
        if,
        Identifier,
        ThenClause(then, Identifier),
        ElseClause(else, Identifier))),
    ArithmeticOp,
    Identifier))

# Control construct as a lambda body

x -> if c then a else b

==>

Program(
  BinaryExpr(
    Definition,
    FunctionOp,
    IfExpr(
      if,
      Identifier,
      ThenClause(then, Identifier),
      ElseClause(else, Identifier))))

# While do

while a do b

==>

Program(WhileExpr(while, Identifier, DoClause(do, Identifier)))

# While list do

while a list b do c

==>

Program(
  WhileExpr(
    while,
    Identifier,
    ListClause(list, Identifier),
    DoClause(do, Identifier)))

# For from to do

for i from 1 to 10 do f

==>

Program(
  ForExpr(
    for,
    Identifier,
    FromClause(from, Integer),
    ToClause(to, Integer),
    DoClause(do, Identifier)))

# For in list

for i in L list f

==>

Program(
  ForExpr(
    for,
    Identifier,
    InClause(in, Identifier),
    ListClause(list, Identifier)))

# Try then else

try f then g else h

==>

Program(
  TryExpr(
    try,
    Identifier,
    ThenClause(then, Identifier),
    ElseClause(else, Identifier)))

# Try except do

try f except e do g

==>

Program(
  TryExpr(
    try,
    Identifier,
    ExceptClause(except, Identifier),
    DoClause(do, Identifier)))

# New of from

new T of A from B

==>

Program(
  NewExpr(
    new,
    Identifier,
    OfClause(of, Identifier),
    FromClause(from, Identifier)))

# Quote symbol

symbol x

==>

Program(QuoteExpr(symbol, Identifier))

# Bare return

return

==>

Program(DebugExpr(return))

# Debug keyword operand extends right

return f x

==>

Program(DebugExpr(return, Application(Identifier, Identifier)))

# A block comment does not end a debug keyword's operand

return -* comment
continues *- i

==>

Program(DebugExpr(return, BlockComment, Identifier))

# A block comment before a debug keyword's operand that extends right

f return -* comment *- i + j

==>

Program(
  Application(
    Identifier,
    DebugExpr(return, BlockComment, BinaryExpr(Identifier, ArithmeticOp, Identifier))))

# A line comment leaves the newline after a debug keyword significant

return -- comment
i

==>

Program(DebugExpr(return), LineComment, Identifier)

# Bare return in a sequence

(return; x)

==>

Program(ParenExpr(DebugExpr(return), Identifier))

# Clause combinations are not validated: any clause may follow any construct

if a do b

==>

Program(IfExpr(if, Identifier, DoClause(do, Identifier)))

# A construct with no clauses at all

for i

==>

Program(ForExpr(for, Identifier))

# Repeated clauses parse

if a then b then c

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, Identifier),
    ThenClause(then, Identifier)))

# Clause bodies still extend as far right as possible

if p then a + b else c * d

==>

Program(
  IfExpr(
    if,
    Identifier,
    ThenClause(then, BinaryExpr(Identifier, ArithmeticOp, Identifier)),
    ElseClause(else, BinaryExpr(Identifier, ArithmeticOp, Identifier))))

# Control constructs still bind loosest

1 + if x then 2 else 3

==>

Program(
  BinaryExpr(
    Integer,
    ArithmeticOp,
    IfExpr(
      if,
      Identifier,
      ThenClause(then, Integer),
      ElseClause(else, Integer))))
