From c8363709d3fb9569f45e67eaf07734b86a9f4566 Mon Sep 17 00:00:00 2001 From: Piotr Fusik Date: Mon, 24 Jan 2022 13:28:58 +0100 Subject: [cleanup] Don't box literals. --- CiResolver.cs | 67 ++++++++++++++++++++++--------------- CiTree.cs | 105 +++++++++++++++++++++------------------------------------- GenBase.cs | 62 +++++----------------------------- GenC.cs | 14 ++++---- GenCpp.cs | 4 +-- GenCs.cs | 4 +-- GenJava.cs | 14 ++++---- GenJs.cs | 8 ++--- GenPy.cs | 6 ++-- GenSwift.cs | 2 +- GenTyped.cs | 12 +++---- 11 files changed, 119 insertions(+), 179 deletions(-) diff --git a/CiResolver.cs b/CiResolver.cs index e94d3fa..eda933e 100644 --- a/CiResolver.cs +++ b/CiResolver.cs @@ -19,7 +19,6 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Text; @@ -357,9 +356,9 @@ public class CiResolver : CiVisitor int width = 0; if (part.WidthExpr != null) width = FoldConstInt(part.WidthExpr); - if (arg is CiLiteral literalArg && !(arg.Type is CiFloatingType)) { // float formatting is runtime-locale-specific - string stringArg = part.Format == ' ' ? literalArg.Value.ToString() - : ((long) literalArg.Value).ToString(part.Format + (part.Precision < 0 ? "" : part.Precision.ToString())); + if (arg is CiLiteral && !(arg.Type is CiFloatingType)) { // float formatting is runtime-locale-specific + string stringArg = part.Format == ' ' ? arg.ToString() + : ((CiLiteralLong) arg).Value.ToString(part.Format + (part.Precision < 0 ? "" : part.Precision.ToString())); if (part.WidthExpr != null) stringArg = width >= 0 ? stringArg.PadLeft(width) : stringArg.PadRight(-width); sb.Append(stringArg); @@ -418,8 +417,8 @@ public class CiResolver : CiVisitor return expr.ToLiteralLong(array.Length); throw new NotImplementedException(scope.GetType().Name); } - if (expr.Symbol == CiSystem.StringLength && left is CiLiteral leftLiteral) { - string s = (string) leftLiteral.Value; + if (expr.Symbol == CiSystem.StringLength && left is CiLiteralString leftLiteral) { + string s = leftLiteral.Value; if (IsAscii(s)) return expr.ToLiteralLong(s.Length); } @@ -491,8 +490,10 @@ public class CiResolver : CiVisitor range = inner.Type as CiRangeType; if (range != null) type = range = new CiRangeType(SaturatedNeg(range.Max), SaturatedNeg(range.Min)); - else if (inner is CiLiteral literal) - return literal.Value is double d ? expr.ToLiteralDouble(-d) : expr.ToLiteralLong(-(long) literal.Value); + else if (inner is CiLiteralDouble d) + return expr.ToLiteralDouble(-d.Value); + else if (inner is CiLiteralLong l) + return expr.ToLiteralLong(-l.Value); else type = inner.Type; break; @@ -531,9 +532,10 @@ public class CiResolver : CiVisitor throw StatementException(expr, "Invalid argument to new"); } case CiToken.Resource: - if (!(FoldConst(expr.Inner) is CiLiteral resourceName) || !(resourceName.Value is string name)) + if (!(FoldConst(expr.Inner) is CiLiteralString resourceName)) throw StatementException(expr, "Resource name must be string"); inner = resourceName; + string name = resourceName.Value; if (!this.Program.Resources.TryGetValue(name, out byte[] content)) { content = File.ReadAllBytes(FindFile(name, expr)); this.Program.Resources.Add(name, content); @@ -572,9 +574,8 @@ public class CiResolver : CiVisitor { if (expr is CiInterpolatedString interpolated) return interpolated; - if (expr is CiLiteral literal) - return new CiInterpolatedString(new CiInterpolatedPart[0], - Convert.ToString(literal.Value, CultureInfo.InvariantCulture)); + if (expr is CiLiteral) + return new CiInterpolatedString(new CiInterpolatedPart[0], expr.ToString()); return new CiInterpolatedString(new CiInterpolatedPart[1] { new CiInterpolatedPart("", expr) }, ""); } @@ -599,8 +600,22 @@ public class CiResolver : CiVisitor return expr.ToLiteralBool(expr.Op == CiToken.NotEqual); } else if (left.Type == right.Type) { - if (left is CiLiteral leftLiteral && right is CiLiteral rightLiteral) - return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ object.Equals(leftLiteral.Value, rightLiteral.Value)); + switch (left) { + case CiLiteralLong leftLong when right is CiLiteralLong rightLong: + return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (leftLong.Value == rightLong.Value)); + case CiLiteralDouble leftDouble when right is CiLiteralDouble rightDouble: + return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (leftDouble.Value == rightDouble.Value)); + case CiLiteralString leftString when right is CiLiteralString rightString: + return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (leftString.Value == rightString.Value)); + case CiLiteralNull _: + return expr.ToLiteralBool(expr.Op == CiToken.Equal); + case CiLiteralFalse _: + return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (right is CiLiteralFalse)); + case CiLiteralTrue _: + return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (right is CiLiteralTrue)); + default: + break; + } if (left.IsConstEnum && right.IsConstEnum) return expr.ToLiteralBool((expr.Op == CiToken.NotEqual) ^ (left.IntValue == right.IntValue)); } @@ -639,10 +654,10 @@ public class CiResolver : CiVisitor break; case CiStringType _: type = CiSystem.CharType; - if (left is CiLiteral leftLiteral && right is CiLiteral rightLiteral) { - string s = (string) leftLiteral.Value; + if (left is CiLiteralString stringLiteral && right is CiLiteralLong indexLiteral) { + string s = stringLiteral.Value; if (IsAscii(s)) { - long i = (long) rightLiteral.Value; + long i = indexLiteral.Value; if (i >= 0 && i < s.Length) return expr.ToLiteralLong(s[(int) i]); } @@ -663,9 +678,8 @@ public class CiResolver : CiVisitor else if (left.Type is CiStringType || right.Type is CiStringType) { Coerce(left, CiSystem.PrintableType); Coerce(right, CiSystem.PrintableType); - if (left is CiLiteral leftLiteral && right is CiLiteral rightLiteral) - return expr.ToLiteralString(Convert.ToString(leftLiteral.Value, CultureInfo.InvariantCulture) - + Convert.ToString(rightLiteral.Value, CultureInfo.InvariantCulture)); + if (left is CiLiteral && right is CiLiteral) + return expr.ToLiteralString(left.ToString() + right.ToString()); if (left is CiInterpolatedString || right is CiInterpolatedString) return Concatenate(ToInterpolatedString(left), ToInterpolatedString(right)); NotSupported(expr, "String concatenation", "c", "cl"); @@ -1121,13 +1135,13 @@ public class CiResolver : CiVisitor break; } break; - case CiBinaryExpr binary when binary.Left.IsReferenceTo(iter) && binary.Right is CiLiteral literalStep: + case CiBinaryExpr binary when binary.Left.IsReferenceTo(iter) && binary.Right is CiLiteralLong literalStep: switch (binary.Op) { case CiToken.AddAssign: - step = (long) literalStep.Value; + step = literalStep.Value; break; case CiToken.SubAssign: - step = -(long) literalStep.Value; + step = -literalStep.Value; break; default: break; @@ -1307,7 +1321,8 @@ public class CiResolver : CiVisitor int FoldConstInt(CiExpr expr) { - if (FoldConst(expr) is CiLiteral literal && literal.Value is long l) { + if (FoldConst(expr) is CiLiteralLong literal) { + long l = literal.Value; if (l < int.MinValue || l > int.MaxValue) throw StatementException(expr, "Only 32-bit ranges supported"); return (int) l; @@ -1401,9 +1416,9 @@ public class CiResolver : CiVisitor Coerce(lengthExpr, CiSystem.IntType); CiArrayStorageType arrayStorage = new CiArrayStorageType { LengthExpr = lengthExpr, ElementType = outerArray }; if (!dynamic || (binary.Left.IsIndexing)) { - if (!(lengthExpr is CiLiteral literal)) + if (!(lengthExpr is CiLiteralLong literal)) throw StatementException(lengthExpr, "Expected constant value"); - long length = (long) literal.Value; + long length = literal.Value; if (length < 0) throw StatementException(expr, "Expected non-negative integer"); if (length > int.MaxValue) diff --git a/CiTree.cs b/CiTree.cs index dbec8d9..5b9db18 100644 --- a/CiTree.cs +++ b/CiTree.cs @@ -22,6 +22,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; +using System.Globalization; using System.Linq; using System.Text; @@ -313,105 +314,74 @@ public class CiConst : CiMember public abstract class CiLiteral : CiExpr { - public readonly object Value; - - protected CiLiteral(object value) - { - switch (value) { - case null: - this.Type = CiSystem.NullType; - break; - case long l: - if (l >= int.MinValue && l <= int.MaxValue) - this.Type = new CiRangeType((int) l, (int) l); - else - this.Type = CiSystem.LongType; - break; - case bool _: - this.Type = CiSystem.BoolType; - break; - case double _: - this.Type = CiSystem.DoubleType; - break; - case string _: - this.Type = CiSystem.StringPtrType; - break; - default: - throw new NotImplementedException(value.GetType().Name); - } - this.Value = value; - } - - public bool IsDefaultValue - { - get - { - switch (this.Value) { - case null: - return true; - case long l: - return l == 0; - case bool b: - return b == false; - case double d: - return BitConverter.DoubleToInt64Bits(d) == 0; // rule out -0.0 - case string _: - return false; - default: - throw new NotImplementedException(this.Value.GetType().Name); - } - } - } - + public abstract bool IsDefaultValue { get; } public static readonly CiLiteralFalse False = new CiLiteralFalse(); public static readonly CiLiteralTrue True = new CiLiteralTrue(); - public override string ToString() => this.Value.ToString(); } public class CiLiteralLong : CiLiteral { - public override bool IsLiteralZero => (long) this.Value == 0; - public override int IntValue => (int) (long) this.Value; - - public CiLiteralLong(long value) : base(value) + public readonly long Value; + public CiLiteralLong(long value) { + this.Value = value; + if (value >= int.MinValue && value <= int.MaxValue) + this.Type = new CiRangeType((int) value, (int) value); + else + this.Type = CiSystem.LongType; } + public override bool IsLiteralZero => this.Value == 0; + public override int IntValue => (int) this.Value; + public override bool IsDefaultValue => this.Value == 0; public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { - visitor.VisitLiteralLong((long) this.Value); + visitor.VisitLiteralLong(this.Value); return this; } + public override string ToString() => this.Value.ToString(); } public class CiLiteralDouble : CiLiteral { - public CiLiteralDouble(double value) : base(value) + public readonly double Value; + public CiLiteralDouble(double value) { + this.Value = value; + this.Type = CiSystem.DoubleType; } + public override bool IsDefaultValue => BitConverter.DoubleToInt64Bits(this.Value) == 0; // rule out -0.0 public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { - visitor.VisitLiteralDouble((double) this.Value); + visitor.VisitLiteralDouble(this.Value); return this; } + public override string ToString() => this.Value.ToString(CultureInfo.InvariantCulture); } public class CiLiteralString : CiLiteral { - public CiLiteralString(string value) : base(value) + public readonly string Value; + public CiLiteralString(string value) { + this.Value = value; + this.Type = CiSystem.StringPtrType; } + public override bool IsDefaultValue => false; public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { - visitor.VisitLiteralString((string) this.Value); + visitor.VisitLiteralString(this.Value); return this; } + public override string ToString() => this.Value; } public class CiLiteralNull : CiLiteral { - public CiLiteralNull() : base(null) + public CiLiteralNull() { + this.Type = CiSystem.NullType; } + public override bool IsDefaultValue => true; public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { visitor.VisitLiteralNull(); @@ -422,33 +392,32 @@ public class CiLiteralNull : CiLiteral public abstract class CiLiteralBool : CiLiteral { - protected CiLiteralBool(bool value) : base(value) + protected CiLiteralBool() { + this.Type = CiSystem.BoolType; } } public class CiLiteralFalse : CiLiteralBool { - public CiLiteralFalse() : base(false) - { - } + public override bool IsDefaultValue => true; public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { visitor.VisitLiteralFalse(); return this; } + public override string ToString() => "false"; } public class CiLiteralTrue : CiLiteralBool { - public CiLiteralTrue() : base(true) - { - } + public override bool IsDefaultValue => false; public override CiExpr Accept(CiVisitor visitor, CiPriority parent) { visitor.VisitLiteralTrue(); return this; } + public override string ToString() => "true"; } public class CiImplicitEnumValue : CiExpr diff --git a/GenBase.cs b/GenBase.cs index c831773..4796726 100644 --- a/GenBase.cs +++ b/GenBase.cs @@ -427,32 +427,6 @@ public abstract class GenBase : CiVisitor Write('"'); } - protected void WriteLiteral(object value) - { - switch (value) { - case null: - VisitLiteralNull(); - break; - case long l: - VisitLiteralLong(l); - break; - case bool b: - if (b) - VisitLiteralTrue(); - else - VisitLiteralFalse(); - break; - case string s: - VisitLiteralString(s); - break; - case double d: - VisitLiteralDouble(d); - break; - default: - throw new NotImplementedException(value.GetType().Name); - } - } - protected abstract void WriteName(CiSymbol symbol); protected virtual TypeCode GetIntegerTypeCode(CiIntegerType integer, bool promote) @@ -655,16 +629,16 @@ public abstract class GenBase : CiVisitor WriteCoercedInternal(type, expr, parent); } - protected virtual void WriteCoercedLiteral(CiType type, CiLiteral literal) + protected virtual void WriteCoercedLiteral(CiType type, CiExpr literal) { - WriteLiteral(literal.Value); + literal.Accept(this, CiPriority.Argument); } protected void WriteCoercedLiterals(CiType type, CiExpr[] exprs) { for (int i = 0; i < exprs.Length; i++) { WriteComma(i); - WriteCoercedLiteral(type, (CiLiteral) exprs[i]); + WriteCoercedLiteral(type, exprs[i]); } } @@ -862,7 +836,7 @@ public abstract class GenBase : CiVisitor WriteNewArray(((CiArrayType) expr.Type).ElementType, expr.Inner, parent); return expr; case CiToken.Resource: - WriteResource((string) ((CiLiteral) expr.Inner).Value, ((CiArrayStorageType) expr.Type).Length); + WriteResource(((CiLiteralString) expr.Inner).Value, ((CiArrayStorageType) expr.Type).Length); return expr; default: throw new ArgumentException(expr.Op.ToString()); @@ -902,14 +876,14 @@ public abstract class GenBase : CiVisitor protected void WriteAdd(CiExpr left, CiExpr right) { - if (left is CiLiteral leftLiteral) { - long leftValue = (long) leftLiteral.Value; + if (left is CiLiteralLong leftLiteral) { + long leftValue = leftLiteral.Value; if (leftValue == 0) { right.Accept(this, CiPriority.Argument); return; } - if (right is CiLiteral rightLiteral) { - VisitLiteralLong(leftValue + (long) rightLiteral.Value); + if (right is CiLiteralLong rightLiteral) { + VisitLiteralLong(leftValue + rightLiteral.Value); return; } } @@ -1043,30 +1017,12 @@ public abstract class GenBase : CiVisitor return false; } - static RegexOptions GetRegexOptions(CiExpr expr) - { - switch (expr) { - case CiSymbolReference symbol: - return (RegexOptions) (long) ((CiLiteral) ((CiConst) symbol.Symbol).Value).Value; - case CiPrefixExpr unary when unary.Op == CiToken.Tilde: - return ~GetRegexOptions(unary.Inner); - case CiBinaryExpr binary when binary.Op == CiToken.And: - return GetRegexOptions(binary.Left) & GetRegexOptions(binary.Right); - case CiBinaryExpr binary when binary.Op == CiToken.Or: - return GetRegexOptions(binary.Left) | GetRegexOptions(binary.Right); - case CiBinaryExpr binary when binary.Op == CiToken.Xor: - return GetRegexOptions(binary.Left) ^ GetRegexOptions(binary.Right); - default: - throw new NotImplementedException(expr.ToString()); - } - } - protected bool WriteRegexOptions(CiExpr[] args, string prefix, string separator, string suffix, string i, string m, string s) { CiExpr expr = args[args.Length - 1]; if (expr.Type != CiSystem.RegexOptionsEnum) return false; - RegexOptions options = GetRegexOptions(expr); + RegexOptions options = (RegexOptions) expr.IntValue; if (options == RegexOptions.None) return false; Write(prefix); diff --git a/GenC.cs b/GenC.cs index 1d7821f..43a1bf9 100644 --- a/GenC.cs +++ b/GenC.cs @@ -544,7 +544,7 @@ public class GenC : GenCCpp break; case CiLiteral literal when literal.IsDefaultValue: Write(" = { "); - WriteLiteral(literal.Value); + literal.Accept(this, CiPriority.Argument); Write(" }"); break; default: @@ -1096,10 +1096,10 @@ public class GenC : GenCCpp protected override void WriteEqualString(CiExpr left, CiExpr right, CiPriority parent, bool not) { if (IsStringSubstring(left, out bool cast, out CiExpr ptr, out CiExpr offset, out CiExpr lengthExpr) - && right is CiLiteral literal) { - string rightValue = (string) literal.Value; - if (lengthExpr is CiLiteral leftLength) { - if ((long) leftLength.Value != rightValue.Length) + && right is CiLiteralString literal) { + string rightValue = literal.Value; + if (lengthExpr is CiLiteralLong leftLength) { + if (leftLength.Value != rightValue.Length) throw new NotImplementedException(); // TODO: evaluate compile-time WriteSubstringEqual(cast, ptr, offset, rightValue, parent, not); } @@ -1261,9 +1261,9 @@ public class GenC : GenCCpp Write(error ? ", stderr)" : ", stdout)"); } else if (error) { - if (args[0] is CiLiteral literal) { + if (args[0] is CiLiteralString literal) { Write("fputs("); - WriteStringLiteralWithNewLine((string) literal.Value); + WriteStringLiteralWithNewLine(literal.Value); Write(", stderr)"); } else { diff --git a/GenCpp.cs b/GenCpp.cs index b412ecb..5797bdd 100644 --- a/GenCpp.cs +++ b/GenCpp.cs @@ -545,8 +545,8 @@ public class GenCpp : GenCCpp } else { Write(" << "); - if (newLine && args[0] is CiLiteral literal) { - WriteStringLiteralWithNewLine((string) literal.Value); + if (newLine && args[0] is CiLiteralString literal) { + WriteStringLiteralWithNewLine(literal.Value); return; } args[0].Accept(this, CiPriority.Mul); diff --git a/GenCs.cs b/GenCs.cs index 841cf64..85eb833 100644 --- a/GenCs.cs +++ b/GenCs.cs @@ -270,9 +270,9 @@ public class GenCs : GenTyped } } - protected override void WriteCoercedLiteral(CiType type, CiLiteral literal) + protected override void WriteCoercedLiteral(CiType type, CiExpr literal) { - WriteLiteral(literal.Value); + literal.Accept(this, CiPriority.Argument); if (type == CiSystem.FloatType) Write('f'); } diff --git a/GenJava.cs b/GenJava.cs index 299c5fb..3050ac8 100644 --- a/GenJava.cs +++ b/GenJava.cs @@ -430,12 +430,12 @@ public class GenJava : GenTyped WriteCall(expr.Left, "equals", expr.Right); } else if (expr.Left is CiBinaryExpr leftBinary && leftBinary.Op == CiToken.LeftBracket && IsUnsignedByte(leftBinary.Type) - && expr.Right is CiLiteral rightLiteral && rightLiteral.Value is long l && l >= 0 && l <= byte.MaxValue) { + && expr.Right is CiLiteralLong rightLiteral && rightLiteral.Value >= 0 && rightLiteral.Value <= byte.MaxValue) { if (parent > CiPriority.Equality) Write('('); WriteIndexingInternal(leftBinary); // omit "& 0xff" Write(GetEqOp(not)); - VisitLiteralLong((sbyte) l); + VisitLiteralLong((sbyte) rightLiteral.Value); if (parent > CiPriority.Equality) Write(')'); } @@ -448,12 +448,12 @@ public class GenJava : GenTyped return type is CiRangeType range && range.Min >= 0 && range.Max > sbyte.MaxValue && range.Max <= byte.MaxValue; } - protected override void WriteCoercedLiteral(CiType type, CiLiteral literal) + protected override void WriteCoercedLiteral(CiType type, CiExpr literal) { if (IsUnsignedByte(type)) - VisitLiteralLong((sbyte) (long) literal.Value); + VisitLiteralLong((sbyte) ((CiLiteralLong) literal).Value); else { - WriteLiteral(literal.Value); + literal.Accept(this, CiPriority.Argument); if (type == CiSystem.FloatType) Write('f'); } @@ -462,12 +462,12 @@ public class GenJava : GenTyped protected override void WriteAnd(CiBinaryExpr expr, CiPriority parent) { if (expr.Left is CiBinaryExpr leftBinary && leftBinary.Op == CiToken.LeftBracket && IsUnsignedByte(leftBinary.Type) - && expr.Right is CiLiteral rightLiteral) { + && expr.Right is CiLiteralLong rightLiteral) { if (parent > CiPriority.CondAnd && parent != CiPriority.And) Write('('); base.WriteIndexing(leftBinary, CiPriority.And); Write(" & "); - VisitLiteralLong(0xff & (long) rightLiteral.Value); + VisitLiteralLong(0xff & rightLiteral.Value); if (parent > CiPriority.CondAnd && parent != CiPriority.And) Write(')'); } diff --git a/GenJs.cs b/GenJs.cs index 2e328b6..99fb28b 100644 --- a/GenJs.cs +++ b/GenJs.cs @@ -380,9 +380,9 @@ public class GenJs : GenBase CiExpr pattern = args[argIndex]; if (pattern.Type.IsClass(CiSystem.RegexClass)) pattern.Accept(this, CiPriority.Primary); - else if (pattern is CiLiteral literal) { + else if (pattern is CiLiteralString literal) { Write('/'); - foreach (char c in (string) literal.Value) { + foreach (char c in literal.Value) { if (c == '/') Write('\\'); WriteEscapedChar(c, false); @@ -527,9 +527,9 @@ public class GenJs : GenBase Write("))"); } else if (method == CiSystem.EnvironmentGetEnvironmentVariable) { - if (args[0] is CiLiteral literal && literal.Value is string name && IsIdentifier(name)) { + if (args[0] is CiLiteralString literal && IsIdentifier(literal.Value)) { Write("process.env."); - Write(name); + Write(literal.Value); } else { Write("process.env["); diff --git a/GenPy.cs b/GenPy.cs index eee9ec8..3bafa2e 100644 --- a/GenPy.cs +++ b/GenPy.cs @@ -854,8 +854,8 @@ public class GenPy : GenPySwift void WriteInclusiveLimit(CiExpr limit, int increment, string incrementString) { - if (limit is CiLiteral literal) - VisitLiteralLong((long) literal.Value + increment); + if (limit is CiLiteralLong literal) + VisitLiteralLong(literal.Value + increment); else { limit.Accept(this, CiPriority.Add); Write(incrementString); @@ -865,7 +865,7 @@ public class GenPy : GenPySwift protected override void WriteForRange(CiVar iter, CiBinaryExpr cond, long rangeStep) { Write("range("); - if (rangeStep != 1 || !(iter.Value is CiLiteral start) || (long) start.Value != 0) { + if (rangeStep != 1 || !iter.Value.IsLiteralZero) { iter.Value.Accept(this, CiPriority.Argument); Write(", "); } diff --git a/GenSwift.cs b/GenSwift.cs index 5adf5de..f21fc6f 100644 --- a/GenSwift.cs +++ b/GenSwift.cs @@ -890,7 +890,7 @@ public class GenSwift : GenPySwift Write(' '); Write(expr.OpString); Write(' '); - if (right is CiLiteral literal && literal.Value == null + if (right is CiLiteralNull && expr.Left is CiBinaryExpr leftBinary && leftBinary.Op == CiToken.LeftBracket && leftBinary.Left.Type is CiDictionaryType dict) { Write(dict.ValueType); Write(".none"); diff --git a/GenTyped.cs b/GenTyped.cs index d4e802a..a7e9b7a 100644 --- a/GenTyped.cs +++ b/GenTyped.cs @@ -82,8 +82,8 @@ public abstract class GenTyped : GenBase protected bool IsOneAsciiString(CiExpr expr, out char c) { - if (expr is CiLiteral literal && literal.Value is string s && s.Length == 1 && IsAscii(s[0])) { - c = s[0]; + if (expr is CiLiteralString literal && literal.Value.Length == 1 && IsAscii(literal.Value[0])) { + c = literal.Value[0]; return true; } c = '\0'; @@ -102,12 +102,12 @@ public abstract class GenTyped : GenBase protected override void WriteComparison(CiBinaryExpr expr, CiPriority parent, CiPriority child, string op) { if (expr.Left.IsIndexing && expr.Left is CiBinaryExpr indexing && indexing.Left.Type is CiStringType - && expr.Right is CiLiteral literal && literal.Value is long c && IsAscii(c)) { + && expr.Right is CiLiteralLong literal && IsAscii(literal.Value)) { if (parent > child) Write('('); expr.Left.Accept(this, child); Write(op); - WriteCharLiteral((char) c); + WriteCharLiteral((char) literal.Value); if (parent > child) Write(')'); } @@ -167,7 +167,7 @@ public abstract class GenTyped : GenBase protected CiExpr GetStaticCastInner(CiType type, CiExpr expr) { - if (expr is CiBinaryExpr binary && binary.Op == CiToken.And && binary.Right is CiLiteral rightMask + if (expr is CiBinaryExpr binary && binary.Op == CiToken.And && binary.Right is CiLiteralLong rightMask && type is CiIntegerType integer) { long mask; switch (GetIntegerTypeCode(integer, false)) { @@ -186,7 +186,7 @@ public abstract class GenTyped : GenBase default: return expr; } - if (((long) rightMask.Value & mask) == mask) + if ((rightMask.Value & mask) == mask) return binary.Left; } return expr; -- cgit v1.2.3