Refactor representation of character category in PDT.

This commit is contained in:
2023-11-29 14:51:14 +08:00
parent 6d0975b0a1
commit 5ef6e2b4a3
3 changed files with 37 additions and 41 deletions

View File

@@ -140,24 +140,24 @@ namespace Cryville.Common.Pdt {
PdtExpToken GetToken() {
ws();
var result = new PdtExpToken {
Type = ct & 0x0fe0
Type = ct & (CharCategory)0x0fe0
};
switch (result.Type) {
case 0x0020: result.Value = GetIdentifier(); break;
case 0x0040: result.Value = GetNumber(); break;
case 0x0100: result.Value = GetString(); break;
case CharCategory.IdentifierBegin: result.Value = GetIdentifier(); break;
case CharCategory.Digit : result.Value = GetNumber(); break;
case CharCategory.StringDelimiter: result.Value = GetString(); break;
default: result.Value = cc.ToString(); Position++; break;
}
return result;
}
private struct PdtExpToken {
public int Type { get; set; }
public CharCategory Type { get; set; }
public string Value { get; set; }
public override string ToString() {
return string.Format("0x{0:x4}: {1}", Type, Value);
}
public static readonly PdtExpToken EmptyOperator = new PdtExpToken {
Type = 0x0080,
Type = CharCategory.Operator,
Value = "$",
};
}
@@ -192,7 +192,7 @@ namespace Cryville.Common.Pdt {
t2 = InterpretExpBlock(ins);
panic:
switch (t2.Type) {
case 0x0080:
case CharCategory.Operator:
if (OP_TYPE[t1.Value[0]] != -1) {
int p1 = OP_PRIORITY[t1.Value[0]];
int p2 = OP_PRIORITY[t2.Value[0]];
@@ -213,11 +213,11 @@ namespace Cryville.Common.Pdt {
}
t1 = t2;
break;
case 0x0400:
case CharCategory.ClosingBracket:
if (enc == -2) throw new FormatException("Expression not enclosed correctly: Too many closing brackets");
if (ins.Count == insc0) pc = 0;
goto exit;
case 0x0800:
case CharCategory.EndOfExpression:
goto exit;
}
}
@@ -240,7 +240,7 @@ namespace Cryville.Common.Pdt {
/// <returns>The expression token following this expression block.</returns>
PdtExpToken InterpretExpBlock(LinkedList<PdtInstruction> ins) {
var t = GetToken();
if (t.Type == 0x0080) {
if (t.Type == CharCategory.Operator) {
var r = InterpretExpBlock(ins);
ins.AddLast(new PdtInstruction.Operate(t.Value, 1));
return r;
@@ -248,7 +248,7 @@ namespace Cryville.Common.Pdt {
bool flag = false;
PdtExpToken? buf = null;
while (true) {
if (buf != null && t.Type != 0x0200) {
if (buf != null && t.Type != CharCategory.OpeningBracket) {
PdtExpression def;
if (defs.TryGetValue(buf.Value.Value, out def)) {
foreach (var i in def.Instructions) ins.AddLast(i);
@@ -262,14 +262,14 @@ namespace Cryville.Common.Pdt {
TryPushAdjMul(ins, ref flag);
}
switch (t.Type) {
case 0x0020:
case CharCategory.IdentifierBegin:
buf = t;
break;
case 0x0040:
case CharCategory.Digit:
float num = float.Parse(t.Value);
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.Number, BitConverter.GetBytes(num)));
break;
case 0x0100:
case CharCategory.StringDelimiter:
int strlen = t.Value.Length;
unsafe {
var strbuf = new byte[strlen * sizeof(char) + sizeof(int)];
@@ -280,7 +280,7 @@ namespace Cryville.Common.Pdt {
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.String, strbuf));
}
break;
case 0x0200:
case CharCategory.OpeningBracket:
int pc;
InterpretExp(ins, -1, out pc);
if (buf != null) {