Skip to content
Snippets Groups Projects
Commit 987da1a7 authored by Marco Antognini's avatar Marco Antognini Committed by Etienne Kneuss
Browse files

Improve C Pretty Printer regarding semicolon

parent 13937ce4
Branches
Tags
No related merge requests found
...@@ -230,11 +230,29 @@ object CAST { // C Abstract Syntax Tree ...@@ -230,11 +230,29 @@ object CAST { // C Abstract Syntax Tree
/* ------------------------------------------------------------- DSL ----- */ /* ------------------------------------------------------------- DSL ----- */
// Operator ~~ appends and flattens nested compounds // Operator ~~ appends and flattens nested compounds
implicit class StmtOps(val stmt: Stmt) { implicit class StmtOps(val stmt: Stmt) {
def ~(other: Stmt) = (stmt, other) match { // In addition to combining statements together in a compound
case (Compound(stmts), Compound(others)) => Compound(stmts ++ others) // we remove the empty ones and if the resulting compound
case (stmt , Compound(others)) => Compound(stmt +: others) // has only one statement we return this one without being
case (Compound(stmts), other ) => Compound(stmts :+ other ) // wrapped into a Compound
case (stmt , other ) => Compound(stmt :: other :: Nil) def ~(other: Stmt) = {
val stmts = (stmt, other) match {
case (Compound(stmts), Compound(others)) => stmts ++ others
case (stmt , Compound(others)) => stmt +: others
case (Compound(stmts), other ) => stmts :+ other
case (stmt , other ) => stmt :: other :: Nil
}
def isNoStmt(s: Stmt) = s match {
case NoStmt => true
case _ => false
}
val compound = Compound(stmts filterNot isNoStmt)
compound match {
case Compound(stmts) if stmts.length == 0 => NoStmt
case Compound(stmts) if stmts.length == 1 => stmts.head
case compound => compound
}
} }
def ~~(others: Seq[Stmt]) = stmt ~ Compound(others) def ~~(others: Seq[Stmt]) = stmt ~ Compound(others)
......
...@@ -57,25 +57,16 @@ class CPrinter(val sb: StringBuffer = new StringBuffer) { ...@@ -57,25 +57,16 @@ class CPrinter(val sb: StringBuffer = new StringBuffer) {
/* --------------------------------------------------------- Stmts ----- */ /* --------------------------------------------------------- Stmts ----- */
case NoStmt => c"/* empty */" case NoStmt => c"/* empty */"
// Try to print new lines and semicolon somewhat correctly
case Compound(stmts) if stmts.isEmpty => // should not happen
case Compound(stmts) if stmts.length == 1 =>
stmts.head match {
case s: Call => c"$s;" // for function calls whose returned value is not saved
case s => c"$s"
}
case Compound(stmts) => case Compound(stmts) =>
val head = stmts.head val lastIdx = stmts.length - 1
val tail = Compound(stmts.tail)
for ((stmt, idx) <- stmts.zipWithIndex) {
if (stmt.isValue) c"$stmt;"
else c"$stmt"
head match { if (idx != lastIdx)
case s: Call => c"$s;" // for function calls whose returned value is not saved c"$NewLine"
case s => c"$s"
} }
c"$NewLine$tail"
case Assert(pred, Some(error)) => c"assert($pred); /* $error */" case Assert(pred, Some(error)) => c"assert($pred); /* $error */"
case Assert(pred, None) => c"assert($pred);" case Assert(pred, None) => c"assert($pred);"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment