From 67b898b44fd1e3d4234cbcbada55ef8fd9ec0378 Mon Sep 17 00:00:00 2001 From: Piotr Fusik Date: Mon, 24 Jan 2022 10:38:12 +0100 Subject: [cleanup] Don't unbox CiLiteralBool. --- CiResolver.cs | 20 +++++++++++--------- CiTree.cs | 2 +- GenCCpp.cs | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CiResolver.cs b/CiResolver.cs index 226078a..fc4c8f4 100644 --- a/CiResolver.cs +++ b/CiResolver.cs @@ -787,9 +787,9 @@ public class CiResolver : CiVisitor case CiToken.CondAnd: { Coerce(left, CiSystem.BoolType); Coerce(right, CiSystem.BoolType); - if (left is CiLiteral leftLiteral) - return (bool) leftLiteral.Value ? right : CiLiteral.False; - if (right is CiLiteral rightLiteral && (bool) rightLiteral.Value) + if (left is CiLiteralTrue) + return right; + if (left is CiLiteralFalse || right is CiLiteralTrue) return left; type = CiSystem.BoolType; break; @@ -797,10 +797,10 @@ public class CiResolver : CiVisitor case CiToken.CondOr: { Coerce(left, CiSystem.BoolType); Coerce(right, CiSystem.BoolType); - if (left is CiLiteral leftLiteral) - return (bool) leftLiteral.Value ? CiLiteral.True : right; - if (right is CiLiteral rightLiteral && !(bool) rightLiteral.Value) + if (left is CiLiteralTrue || right is CiLiteralFalse) return left; + if (left is CiLiteralFalse) + return right; type = CiSystem.BoolType; break; } @@ -908,8 +908,10 @@ public class CiResolver : CiVisitor CiType type = GetCommonType(onTrue, onFalse); Coerce(onTrue, type); Coerce(onFalse, type); - if (cond is CiLiteral literalCond) - return (bool) literalCond.Value ? onTrue : onFalse; + if (cond is CiLiteralTrue) + return onTrue; + if (cond is CiLiteralFalse) + return onFalse; return new CiSelectExpr { Line = expr.Line, Cond = cond, OnTrue = onTrue, OnFalse = onFalse, Type = type }; } @@ -1060,7 +1062,7 @@ public class CiResolver : CiVisitor { if (statement.Cond != null) { statement.Cond = ResolveBool(statement.Cond); - statement.SetCompletesNormally(!(statement.Cond is CiLiteral literal) || !(bool) literal.Value); + statement.SetCompletesNormally(!(statement.Cond is CiLiteralTrue)); } else statement.SetCompletesNormally(false); diff --git a/CiTree.cs b/CiTree.cs index 8201152..5048dd6 100644 --- a/CiTree.cs +++ b/CiTree.cs @@ -715,7 +715,7 @@ public class CiAssert : CiStatement { public CiExpr Cond; public CiExpr Message = null; - public override bool CompletesNormally => !(this.Cond is CiLiteral literal) || (bool) literal.Value; + public override bool CompletesNormally => !(this.Cond is CiLiteralFalse); public override void Accept(CiVisitor visitor) { visitor.Visit(this); } } diff --git a/GenCCpp.cs b/GenCCpp.cs index 484050a..6aa29d4 100644 --- a/GenCCpp.cs +++ b/GenCCpp.cs @@ -224,7 +224,7 @@ public abstract class GenCCpp : GenTyped Write("assert("); if (statement.Message == null) statement.Cond.Accept(this, CiPriority.Argument); - else if (statement.Cond is CiLiteral literal && !(bool) literal.Value) { + else if (statement.Cond is CiLiteralFalse) { Write('!'); statement.Message.Accept(this, CiPriority.Primary); } -- cgit v1.2.3