[vala/staging: 5/6] ccode: Replace if-else-tree with switch
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging: 5/6] ccode: Replace if-else-tree with switch
- Date: Sun, 30 Oct 2016 14:56:31 +0000 (UTC)
commit 0f65d306f5a2eca7ea798e5c835cf66b6e327c8e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sun Oct 30 15:09:53 2016 +0100
ccode: Replace if-else-tree with switch
ccode/valaccodebinaryexpression.vala | 59 +++++++++++----------------------
ccode/valaccodeunaryexpression.vala | 38 +++++++++------------
2 files changed, 36 insertions(+), 61 deletions(-)
---
diff --git a/ccode/valaccodebinaryexpression.vala b/ccode/valaccodebinaryexpression.vala
index 047b55c..651e2ca 100644
--- a/ccode/valaccodebinaryexpression.vala
+++ b/ccode/valaccodebinaryexpression.vala
@@ -50,47 +50,28 @@ public class Vala.CCodeBinaryExpression : CCodeExpression {
public override void write (CCodeWriter writer) {
left.write_inner (writer);
- writer.write_string (" ");
- if (operator == CCodeBinaryOperator.PLUS) {
- writer.write_string ("+");
- } else if (operator == CCodeBinaryOperator.MINUS) {
- writer.write_string ("-");
- } else if (operator == CCodeBinaryOperator.MUL) {
- writer.write_string ("*");
- } else if (operator == CCodeBinaryOperator.DIV) {
- writer.write_string ("/");
- } else if (operator == CCodeBinaryOperator.MOD) {
- writer.write_string ("%");
- } else if (operator == CCodeBinaryOperator.SHIFT_LEFT) {
- writer.write_string ("<<");
- } else if (operator == CCodeBinaryOperator.SHIFT_RIGHT) {
- writer.write_string (">>");
- } else if (operator == CCodeBinaryOperator.LESS_THAN) {
- writer.write_string ("<");
- } else if (operator == CCodeBinaryOperator.GREATER_THAN) {
- writer.write_string (">");
- } else if (operator == CCodeBinaryOperator.LESS_THAN_OR_EQUAL) {
- writer.write_string ("<=");
- } else if (operator == CCodeBinaryOperator.GREATER_THAN_OR_EQUAL) {
- writer.write_string (">=");
- } else if (operator == CCodeBinaryOperator.EQUALITY) {
- writer.write_string ("==");
- } else if (operator == CCodeBinaryOperator.INEQUALITY) {
- writer.write_string ("!=");
- } else if (operator == CCodeBinaryOperator.BITWISE_AND) {
- writer.write_string ("&");
- } else if (operator == CCodeBinaryOperator.BITWISE_OR) {
- writer.write_string ("|");
- } else if (operator == CCodeBinaryOperator.BITWISE_XOR) {
- writer.write_string ("^");
- } else if (operator == CCodeBinaryOperator.AND) {
- writer.write_string ("&&");
- } else if (operator == CCodeBinaryOperator.OR) {
- writer.write_string ("||");
+ switch (operator) {
+ case CCodeBinaryOperator.PLUS: writer.write_string (" + "); break;
+ case CCodeBinaryOperator.MINUS: writer.write_string (" - "); break;
+ case CCodeBinaryOperator.MUL: writer.write_string (" * "); break;
+ case CCodeBinaryOperator.DIV: writer.write_string (" / "); break;
+ case CCodeBinaryOperator.MOD: writer.write_string (" % "); break;
+ case CCodeBinaryOperator.SHIFT_LEFT: writer.write_string (" << "); break;
+ case CCodeBinaryOperator.SHIFT_RIGHT: writer.write_string (" >> "); break;
+ case CCodeBinaryOperator.LESS_THAN: writer.write_string (" < "); break;
+ case CCodeBinaryOperator.GREATER_THAN: writer.write_string (" > "); break;
+ case CCodeBinaryOperator.LESS_THAN_OR_EQUAL: writer.write_string (" <= "); break;
+ case CCodeBinaryOperator.GREATER_THAN_OR_EQUAL: writer.write_string (" >= "); break;
+ case CCodeBinaryOperator.EQUALITY: writer.write_string (" == "); break;
+ case CCodeBinaryOperator.INEQUALITY: writer.write_string (" != "); break;
+ case CCodeBinaryOperator.BITWISE_AND: writer.write_string (" & "); break;
+ case CCodeBinaryOperator.BITWISE_OR: writer.write_string (" | "); break;
+ case CCodeBinaryOperator.BITWISE_XOR: writer.write_string (" ^ "); break;
+ case CCodeBinaryOperator.AND: writer.write_string (" && "); break;
+ case CCodeBinaryOperator.OR: writer.write_string (" || "); break;
+ default: assert_not_reached ();
}
- writer.write_string (" ");
-
right.write_inner (writer);
}
diff --git a/ccode/valaccodeunaryexpression.vala b/ccode/valaccodeunaryexpression.vala
index 7f2b89b..afcb8c4 100644
--- a/ccode/valaccodeunaryexpression.vala
+++ b/ccode/valaccodeunaryexpression.vala
@@ -42,15 +42,12 @@ public class Vala.CCodeUnaryExpression : CCodeExpression {
}
public override void write (CCodeWriter writer) {
- if (operator == CCodeUnaryOperator.PLUS) {
- writer.write_string ("+");
- } else if (operator == CCodeUnaryOperator.MINUS) {
- writer.write_string ("-");
- } else if (operator == CCodeUnaryOperator.LOGICAL_NEGATION) {
- writer.write_string ("!");
- } else if (operator == CCodeUnaryOperator.BITWISE_COMPLEMENT) {
- writer.write_string ("~");
- } else if (operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
+ switch (operator) {
+ case CCodeUnaryOperator.PLUS: writer.write_string ("+"); inner.write_inner (writer); break;
+ case CCodeUnaryOperator.MINUS: writer.write_string ("-"); inner.write_inner (writer); break;
+ case CCodeUnaryOperator.LOGICAL_NEGATION: writer.write_string ("!"); inner.write_inner
(writer); break;
+ case CCodeUnaryOperator.BITWISE_COMPLEMENT: writer.write_string ("~"); inner.write_inner
(writer); break;
+ case CCodeUnaryOperator.POINTER_INDIRECTION:
var inner_unary = inner as CCodeUnaryExpression;
if (inner_unary != null && inner_unary.operator == CCodeUnaryOperator.ADDRESS_OF) {
// simplify expression
@@ -58,7 +55,9 @@ public class Vala.CCodeUnaryExpression : CCodeExpression {
return;
}
writer.write_string ("*");
- } else if (operator == CCodeUnaryOperator.ADDRESS_OF) {
+ inner.write_inner (writer);
+ break;
+ case CCodeUnaryOperator.ADDRESS_OF:
var inner_unary = inner as CCodeUnaryExpression;
if (inner_unary != null && inner_unary.operator ==
CCodeUnaryOperator.POINTER_INDIRECTION) {
// simplify expression
@@ -66,18 +65,13 @@ public class Vala.CCodeUnaryExpression : CCodeExpression {
return;
}
writer.write_string ("&");
- } else if (operator == CCodeUnaryOperator.PREFIX_INCREMENT) {
- writer.write_string ("++");
- } else if (operator == CCodeUnaryOperator.PREFIX_DECREMENT) {
- writer.write_string ("--");
- }
-
- inner.write_inner (writer);
-
- if (operator == CCodeUnaryOperator.POSTFIX_INCREMENT) {
- writer.write_string ("++");
- } else if (operator == CCodeUnaryOperator.POSTFIX_DECREMENT) {
- writer.write_string ("--");
+ inner.write_inner (writer);
+ break;
+ case CCodeUnaryOperator.PREFIX_INCREMENT: writer.write_string ("++"); break;
+ case CCodeUnaryOperator.PREFIX_DECREMENT: writer.write_string ("--"); break;
+ case CCodeUnaryOperator.POSTFIX_INCREMENT: inner.write_inner (writer); writer.write_string
("++"); break;
+ case CCodeUnaryOperator.POSTFIX_DECREMENT: inner.write_inner (writer); writer.write_string
("--"); break;
+ default: assert_not_reached ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]