Skip to content

Commit

Permalink
change format priority
Browse files Browse the repository at this point in the history
  • Loading branch information
shapoco committed May 14, 2023
1 parent d3b4525 commit 65d1382
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 85 deletions.
1 change: 1 addition & 0 deletions Calctus.csproj
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Calctus\Model\ScriptFilter.cs" />
<Compile Include="Calctus\Model\Syntax\BinaryPrefixFormatter.cs" />
<Compile Include="Calctus\Model\Syntax\SiPrefixFormatter.cs" />
<Compile Include="Calctus\Model\Syntax\Type.cs" />
<Compile Include="Calctus\Model\ufixed113.cs" />
<Compile Include="Calctus\Model\frac.cs" />
<Compile Include="Calctus\Model\FracVal.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/BinaryPrefixFormatter.cs
Expand Up @@ -10,7 +10,7 @@ namespace Shapoco.Calctus.Model.Syntax {
class BinaryPrefixFormatter : NumberFormatter {
private static readonly string Prefixes = "_kMGTPEZYR";

public BinaryPrefixFormatter() : base(new Regex(@"(([1-9][0-9]*|0)(\.[0-9]+)?)([" + Prefixes + "])i")) { }
public BinaryPrefixFormatter() : base(new Regex(@"(([1-9][0-9]*|0)(\.[0-9]+)?)([" + Prefixes + "])i"), FormatPriority.NextPriority) { }

public override Val Parse(Match m) {
var frac = decimal.Parse(m.Groups[1].Value, CultureInfo.InvariantCulture);
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/CharFormatter.cs
Expand Up @@ -7,7 +7,7 @@

namespace Shapoco.Calctus.Model.Syntax {
class CharFormatter : NumberFormatter {
public CharFormatter() : base(new Regex("'([^'\\\\]|\\\\[abfnrtv\\\\\'0]|\\\\o[0-7]{3}|\\\\x[0-9a-fA-F]{2}|\\\\u[0-9a-fA-F]{4})'")) { }
public CharFormatter() : base(new Regex("'([^'\\\\]|\\\\[abfnrtv\\\\\'0]|\\\\o[0-7]{3}|\\\\x[0-9a-fA-F]{2}|\\\\u[0-9a-fA-F]{4})'"), FormatPriority.LeftPriority) { }

public override Val Parse(Match m) {
var tok = m.Groups[1].Value;
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/DateTimeFormatter.cs
Expand Up @@ -9,7 +9,7 @@

namespace Shapoco.Calctus.Model.Syntax {
class DateTimeFormatter : NumberFormatter {
public DateTimeFormatter() : base(new Regex(@"#(\d+/\d+/\d+|\d+:\d+:\d+(\.\d+)?|\d+/\d+/\d+ \d+:\d+:\d+(\.\d+)?)#")) { }
public DateTimeFormatter() : base(new Regex(@"#(\d+/\d+/\d+|\d+:\d+:\d+(\.\d+)?|\d+/\d+/\d+ \d+:\d+:\d+(\.\d+)?)#"), FormatPriority.LeftPriority) { }

public override Val Parse(Match m) {
var tok = m.Groups[1].Value;
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/IntFormatter.cs
Expand Up @@ -11,7 +11,7 @@ class IntFormatter : NumberFormatter {
public string Prefix;
private int _captureGroupIndex;

public IntFormatter(int radix, string prefix, Regex regex, int groupIndex) : base(regex) {
public IntFormatter(int radix, string prefix, Regex regex, int groupIndex, FormatPriority priority) : base(regex, priority) {
this.Radix = radix;
this.Prefix = prefix;
this._captureGroupIndex = groupIndex;
Expand Down
14 changes: 8 additions & 6 deletions Calctus/Model/Syntax/NumberFormatter.cs
Expand Up @@ -8,9 +8,11 @@
namespace Shapoco.Calctus.Model.Syntax {
abstract class NumberFormatter {
public readonly Regex Pattern;
public readonly FormatPriority Priority;

public NumberFormatter(Regex ptn) {
this.Pattern = ptn;
public NumberFormatter(Regex ptn, FormatPriority priority) {
Pattern = ptn;
Priority = priority;
}

public abstract Val Parse(Match m);
Expand All @@ -25,11 +27,11 @@ abstract class NumberFormatter {
}
}

public static readonly IntFormatter CStyleInt = new IntFormatter(10, "", new Regex(@"([1-9][0-9]*|0)([eE][+-]?[0-9]+)?"), 0 );
public static readonly IntFormatter CStyleInt = new IntFormatter(10, "", new Regex(@"([1-9][0-9]*|0)([eE][+-]?[0-9]+)?"), 0, FormatPriority.Neutral);
public static readonly RealFormatter CStyleReal = new RealFormatter();
public static readonly IntFormatter CStyleHex = new IntFormatter(16, "0x", new Regex(@"0[xX]([0-9a-fA-F]+)"), 1);
public static readonly IntFormatter CStyleOct = new IntFormatter(8, "0", new Regex(@"0([0-7]+)"), 1);
public static readonly IntFormatter CStyleBin = new IntFormatter(2, "0b", new Regex(@"0[bB]([01]+)"), 1);
public static readonly IntFormatter CStyleHex = new IntFormatter(16, "0x", new Regex(@"0[xX]([0-9a-fA-F]+)"), 1, FormatPriority.LeftPriority);
public static readonly IntFormatter CStyleOct = new IntFormatter(8, "0", new Regex(@"0([0-7]+)"), 1, FormatPriority.LeftPriority);
public static readonly IntFormatter CStyleBin = new IntFormatter(2, "0b", new Regex(@"0[bB]([01]+)"), 1, FormatPriority.LeftPriority);
public static readonly CharFormatter CStyleChar = new CharFormatter();
public static readonly SiPrefixFormatter SiPrefixed = new SiPrefixFormatter();
public static readonly BinaryPrefixFormatter BinaryPrefixed = new BinaryPrefixFormatter();
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/RealFormatter.cs
Expand Up @@ -7,7 +7,7 @@

namespace Shapoco.Calctus.Model.Syntax {
class RealFormatter : NumberFormatter {
public RealFormatter() : base(new Regex(@"([1-9][0-9]*|0)\.[0-9]+([eE][+-]?[0-9]+)?")) { }
public RealFormatter() : base(new Regex(@"([1-9][0-9]*|0)\.[0-9]+([eE][+-]?[0-9]+)?"), FormatPriority.Neutral) { }

public override Val Parse(Match m) {
return new RealVal(real.Parse(m.Value), new ValFormatHint(this));
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/SiPrefixFormatter.cs
Expand Up @@ -11,7 +11,7 @@ class SiPrefixFormatter : NumberFormatter {
private static readonly string Prefixes = "ryzafpnum_kMGTPEZYR";
private const int PrefixOffset = 9;

public SiPrefixFormatter() : base(new Regex(@"(([1-9][0-9]*|0)(\.[0-9]+)?)([" + Prefixes + "])")) { }
public SiPrefixFormatter() : base(new Regex(@"(([1-9][0-9]*|0)(\.[0-9]+)?)([" + Prefixes + "])"), FormatPriority.NextPriority) { }

public override Val Parse(Match m) {
var frac = decimal.Parse(m.Groups[1].Value, CultureInfo.InvariantCulture);
Expand Down
13 changes: 13 additions & 0 deletions Calctus/Model/Syntax/Type.cs
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shapoco.Calctus.Model.Syntax {
enum FormatPriority {
Neutral,
NextPriority,
LeftPriority,
}
}
57 changes: 7 additions & 50 deletions Calctus/Model/Syntax/ValFormatHint.cs
Expand Up @@ -15,56 +15,13 @@ class ValFormatHint {
this.Formatter = f;
}

// todo: delete
/*
public string Format(object val) {
if (Formatter.Type == NumberFormatType.Time)
return FormatTime((double)val);
else if (val is double || val is float)
return Format((double)val);
else if (val is char || val is byte || val is int || val is uint || val is short || val is ushort || val is long || val is ulong)
return Format((double)val);
else
return val.ToString();
public ValFormatHint Select(ValFormatHint b) {
if (Formatter.Priority == FormatPriority.Neutral && b.Formatter.Priority == FormatPriority.NextPriority) {
// 左辺が標準の整数または実数で右辺が接頭語付きの場合は接頭語を優先する
return b;
}
// それ以外の場合は左辺を優先する
return this;
}
public string Format(double val) {
if (Formatter.Type == NumberFormatType.Time)
return FormatTime((double)val);
else if ((long)val == val)
return Format((long)val);
else
return val.ToString();
}
public string Format(long val) {
if (Formatter.Type == NumberFormatType.Time)
return FormatTime((double)val);
else
switch (Formatter.Type) {
case NumberFormatType.Real: return val.ToString();
case NumberFormatType.Integer: return val.ToString();
case NumberFormatType.CStyleHex: return "0x" + Convert.ToString(val & 0xffffffffL, 16);
case NumberFormatType.CStyleBin: return "0b" + Convert.ToString(val & 0xffffffffL, 2);
case NumberFormatType.CStyleOct: return "0" + Convert.ToString(val & 0xffffffffL, 8);
default: throw new NotImplementedException();
}
}
private static string FormatTime(double val) {
int sign = val >= 0 ? 1 : -1;
val *= sign;
long h = (long)Math.Truncate(val / 3600); val -= h * 3600;
long m = (long)Math.Truncate(val / 60); val -= m * 60;
double s = val;
var sb = new StringBuilder();
if (sign < 0) sb.Append('-');
if (h > 0) sb.Append(h).Append('h');
if (m > 0) sb.Append(m).Append('m');
// m だけだとメートルと区別がつかないので h == 0 の場合は s も付ける
if (s > 0 || h == 0) sb.Append(s).Append('s');
return sb.ToString();
}
*/
}
}
2 changes: 1 addition & 1 deletion Calctus/Model/Syntax/WebColorFormatter.cs
Expand Up @@ -8,7 +8,7 @@

namespace Shapoco.Calctus.Model.Syntax {
class WebColorFormatter : NumberFormatter {
public WebColorFormatter() : base(new Regex(@"#([0-9a-fA-F]+)")) { }
public WebColorFormatter() : base(new Regex(@"#([0-9a-fA-F]+)"), FormatPriority.LeftPriority) { }

public override Val Parse(Match m) {
var tok = m.Groups[1].Value;
Expand Down
30 changes: 15 additions & 15 deletions Calctus/Model/Val.cs
Expand Up @@ -80,12 +80,12 @@ abstract class Val {

// 算術演算
// 右項に精度を合わせるため UpConvert する
public Val Add(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnAdd(ctx, b).Format(FormatHint);
public Val Sub(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnSub(ctx, b).Format(FormatHint);
public Val Mul(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnMul(ctx, b).Format(FormatHint);
public Val Div(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnDiv(ctx, b).Format(FormatHint);
public Val IDiv(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnIDiv(ctx, b).Format(FormatHint);
public Val Mod(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnMod(ctx, b).Format(FormatHint);
public Val Add(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnAdd(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val Sub(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnSub(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val Mul(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnMul(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val Div(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnDiv(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val IDiv(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnIDiv(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val Mod(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnMod(ctx, b).Format(FormatHint.Select(b.FormatHint));
protected abstract Val OnAdd(EvalContext ctx, Val b);
protected abstract Val OnSub(EvalContext ctx, Val b);
protected abstract Val OnMul(EvalContext ctx, Val b);
Expand All @@ -105,25 +105,25 @@ abstract class Val {
protected abstract Val OnArithShiftR(EvalContext ctx, Val b);

// 比較演算
public Val Grater(EvalContext ctx, Val b) => UpConvert(ctx, b).OnGrater(ctx, b).Format(FormatHint);
public Val Less(EvalContext ctx, Val b) => b.UpConvert(ctx, this).OnGrater(ctx, this).Format(FormatHint);
public Val Equal(EvalContext ctx, Val b) => UpConvert(ctx, b).OnEqual(ctx, b).Format(FormatHint);
public Val Grater(EvalContext ctx, Val b) => UpConvert(ctx, b).OnGrater(ctx, b);
public Val Less(EvalContext ctx, Val b) => b.UpConvert(ctx, this).OnGrater(ctx, this);
public Val Equal(EvalContext ctx, Val b) => UpConvert(ctx, b).OnEqual(ctx, b);
public Val GraterEqual(EvalContext ctx, Val b) {
var a = UpConvert(ctx, b);
return a.OnGrater(ctx, b).OnLogicOr(ctx, a.OnEqual(ctx, b)).Format(FormatHint);
return a.OnGrater(ctx, b).OnLogicOr(ctx, a.OnEqual(ctx, b));
}
public Val LessEqual(EvalContext ctx, Val b) {
b = b.UpConvert(ctx, this);
return b.OnGrater(ctx, this).OnLogicOr(ctx, b.OnEqual(ctx, this)).Format(FormatHint);
return b.OnGrater(ctx, this).OnLogicOr(ctx, b.OnEqual(ctx, this));
}
public Val NotEqual(EvalContext ctx, Val b) => UpConvert(ctx, b).OnEqual(ctx, b).OnLogicNot(ctx).Format(FormatHint);
public Val NotEqual(EvalContext ctx, Val b) => UpConvert(ctx, b).OnEqual(ctx, b).OnLogicNot(ctx);
protected abstract Val OnGrater(EvalContext ctx, Val b);
protected abstract Val OnEqual(EvalContext ctx, Val b);

// ビット演算
public Val BitAnd(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitAnd(ctx, b).Format(FormatHint);
public Val BitXor(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitXor(ctx, b).Format(FormatHint);
public Val BitOr(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitOr(ctx, b).Format(FormatHint);
public Val BitAnd(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitAnd(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val BitXor(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitXor(ctx, b).Format(FormatHint.Select(b.FormatHint));
public Val BitOr(EvalContext ctx, Val b) => this.UpConvert(ctx, b).OnBitOr(ctx, b).Format(FormatHint.Select(b.FormatHint));
protected abstract Val OnBitAnd(EvalContext ctx, Val b);
protected abstract Val OnBitXor(EvalContext ctx, Val b);
protected abstract Val OnBitOr(EvalContext ctx, Val b);
Expand Down
21 changes: 15 additions & 6 deletions Calctus/UI/ExpressionBox.cs
Expand Up @@ -450,7 +450,12 @@ public class LOGFONT {
var id = item.Id;
setSelection(_candStart, _candEnd);
if (item.IsFunction) {
SelectedText = id + "()";
if (Settings.Instance.Input_AutoCloseBrackets) {
SelectedText = id + "()";
}
else {
SelectedText = id + "(";
}
SelectionStart = _candStart + id.Length + 1;
}
else {
Expand Down Expand Up @@ -643,10 +648,14 @@ public class LOGFONT {
}
text = text.Insert(selStart, e.KeyChar.ToString());

if (e.KeyChar == '(') {
if (Settings.Instance.Input_AutoCloseBrackets) {
// 閉じ括弧の補完
text = text.Insert(selStart + 1, ")");
if (Settings.Instance.Input_AutoCloseBrackets) {
// 閉じ括弧の補完
switch (e.KeyChar) {
case '(': text = text.Insert(selStart + 1, ")"); break;
case '[': text = text.Insert(selStart + 1, "]"); break;
case '{': text = text.Insert(selStart + 1, "}"); break;
case '\'': text = text.Insert(selStart + 1, "'"); break;
case '\"': text = text.Insert(selStart + 1, "\""); break;
}
}

Expand All @@ -655,7 +664,7 @@ public class LOGFONT {

selStart = this.SelectionStart;
var prevChar = selStart >= 2 ? text[selStart - 2] : '\0';
if (!isIdChar(prevChar) && isFirstIdChar(e.KeyChar)) {
if (!isIdChar(prevChar) && prevChar != '\'' && prevChar != '\"' && prevChar != '#' && prevChar != '\\' && isFirstIdChar(e.KeyChar)) {
if (Settings.Instance.Input_IdAutoCompletion) {
// 識別子の先頭文字が入力されたら補完候補を表示する
showCandidates();
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Expand Up @@ -32,5 +32,5 @@
// すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.*")]
[assembly: AssemblyVersion("0.3.*")]
//[assembly: AssemblyFileVersion("0.0.0.0")]

0 comments on commit 65d1382

Please sign in to comment.