module Beautifier(beautify) where
import Datatypes


beautify :: Program -> Program
beautify (Program statements) =
  Program (map beautifyStatement statements)


beautifyStatement :: Statement -> Statement
beautifyStatement (AssignmentStatement (Assignment lefthand assignmentOperator oldExpression)) =
  let expression = beautifyExpression oldExpression in
  case assignmentOperator of
    AssignmentStraightUp ->
      AssignmentStatement
        (Assignment
          lefthand
          assignmentOperator
          expression)

    AssignmentBinaryOp binaryOperation -> -- Handle all other operators
      AssignmentStatement
        (Assignment
          lefthand
          AssignmentStraightUp
          (BinaryExpression
            binaryOperation
            (ExpressionItem lefthand)
            expression))

beautifyStatement other = other


beautifyExpression :: Expression -> Expression
beautifyExpression (IsInList lefthand [expression]) =
  (BinaryExpression
    EqualTo
    lefthand
    expression)


beautifyExpression (IsInList lefthand (expression:expressions)) =
  BinaryExpression
    Or
    (beautifyExpression (IsInList lefthand expressions))
    (BinaryExpression
      EqualTo
      lefthand
      expression)

-- Ensure recursion over nested expressions
beautifyExpression (BinaryExpression op e1 e2) =
  BinaryExpression
    op
    (beautifyExpression e1)
    (beautifyExpression e2)

beautifyExpression (TernaryExpression e1 e2 e3) =
  TernaryExpression
    (beautifyExpression e1)
    (beautifyExpression e2)
    (beautifyExpression e3)

beautifyExpression others = others

module Beautifier(beautify) where
import Datatypes


beautify :: Program -> Program
beautify (Program statements) =
  Program (map beautifyStatement statements)


beautifyStatement :: Statement -> Statement
beautifyStatement statement@(AssignmentStatement (Assignment lefthand assignmentOperator oldExpression)) =
  let expression = beautifyExpression oldExpression in
  case assignmentOperator of
    AssignmentStraightUp ->
      statement

    AssignmentBinaryOp binaryOperation -> -- Handle all other operators
      AssignmentStatement
        (Assignment
          lefthand
          AssignmentStraightUp
          (BinaryExpression
            binaryOperation
            (ExpressionItem lefthand)
            expression))

beautifyStatement other = other


beautifyExpression :: Expression -> Expression
beautifyExpression (IsInList lefthand [expression]) =
  (BinaryExpression
    EqualTo
    lefthand
    expression)


beautifyExpression (IsInList lefthand (expression:expressions)) =
  BinaryExpression
    Or
    (beautifyExpression (IsInList lefthand expressions))
    (BinaryExpression
      EqualTo
      lefthand
      expression)

-- Ensure recursion over nested expressions
beautifyExpression (BinaryExpression op e1 e2) =
  BinaryExpression
    op
    (beautifyExpression e1)
    (beautifyExpression e2)

beautifyExpression (TernaryExpression e1 e2 e3) =
  TernaryExpression
    (beautifyExpression e1)
    (beautifyExpression e2)
    (beautifyExpression e3)

beautifyExpression others = others

Use handy @ syntax for labeled destructuring
Use handy @ syntax for labeled destructuring
