Compare commits
3 Commits
d3e21cad15
...
main
Author | SHA1 | Date | |
---|---|---|---|
58f3d1067c | |||
7b48066aaf | |||
dde9e6b82d |
@@ -1,4 +1,5 @@
|
|||||||
using Convention.RScript.Parser;
|
using Convention.RScript.Parser;
|
||||||
|
using System;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
namespace Convention.RScript.Runner
|
namespace Convention.RScript.Runner
|
||||||
@@ -7,54 +8,51 @@ namespace Convention.RScript.Runner
|
|||||||
{
|
{
|
||||||
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
|
protected static void DoJumpRuntimePointer(ExpressionParser parser, int target, RScriptContext context)
|
||||||
{
|
{
|
||||||
|
int currentPointer = context.CurrentRuntimePointer;
|
||||||
bool isForwardMove = target > context.CurrentRuntimePointer;
|
bool isForwardMove = target > context.CurrentRuntimePointer;
|
||||||
int step = isForwardMove ? 1 : -1;
|
int step = isForwardMove ? 1 : -1;
|
||||||
int insLayer = 0;
|
int depth = 0;
|
||||||
|
int lastLayer = 0;
|
||||||
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
|
for (; context.CurrentRuntimePointer != target; context.CurrentRuntimePointer += step)
|
||||||
{
|
{
|
||||||
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
|
if (context.CurrentSentence.mode == RScriptSentence.Mode.ExitNamespace)
|
||||||
{
|
{
|
||||||
if (isForwardMove)
|
if (isForwardMove)
|
||||||
{
|
lastLayer--;
|
||||||
if (insLayer > 0)
|
|
||||||
insLayer--;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
|
||||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
insLayer++;
|
lastLayer++;
|
||||||
}
|
}
|
||||||
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
else if (context.CurrentSentence.mode == RScriptSentence.Mode.EnterNamespace)
|
||||||
{
|
{
|
||||||
if (isForwardMove)
|
if (isForwardMove)
|
||||||
insLayer++;
|
lastLayer++;
|
||||||
else
|
else
|
||||||
{
|
lastLayer--;
|
||||||
if (insLayer > 0)
|
|
||||||
insLayer--;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (int disLayer = -insLayer; disLayer > 0; disLayer--)
|
|
||||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (insLayer > 0)
|
depth = lastLayer < depth ? lastLayer : depth;
|
||||||
|
}
|
||||||
|
// 对上层的最深影响
|
||||||
|
for (; depth < 0; depth++)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
for (; insLayer > 0; insLayer--)
|
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
{
|
|
||||||
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (insLayer < 0)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
for (; insLayer < 0; insLayer++)
|
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
|
||||||
{
|
}
|
||||||
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
|
}
|
||||||
}
|
// 恢复正确的层数
|
||||||
|
for (int i = depth, e = lastLayer; i < e; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new RScriptRuntimeException($"Jump pointer with error", currentPointer, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -56,16 +56,8 @@ namespace Convention.RScript
|
|||||||
// Skip single-line comment
|
// Skip single-line comment
|
||||||
if (line[i + 1] == '/')
|
if (line[i + 1] == '/')
|
||||||
{
|
{
|
||||||
while (i < line.Length && line[i] != '\n')
|
PushBuilder();
|
||||||
i++;
|
break;
|
||||||
}
|
|
||||||
// Skip multi-line comment
|
|
||||||
else if (line[i + 1] == '*')
|
|
||||||
{
|
|
||||||
i += 2;
|
|
||||||
while (i + 1 < line.Length && !(line[i] == '*' && line[i + 1] == '/'))
|
|
||||||
i++;
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -74,9 +66,8 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
else if (c == '#')
|
else if (c == '#')
|
||||||
{
|
{
|
||||||
// Skip single-line comment
|
PushBuilder();
|
||||||
while (i < line.Length && line[i] != '\n')
|
break;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
else if (c == '\"')
|
else if (c == '\"')
|
||||||
{
|
{
|
||||||
@@ -126,10 +117,7 @@ namespace Convention.RScript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (builder.Length > 0)
|
PushBuilder();
|
||||||
{
|
|
||||||
PushBuilder();
|
|
||||||
}
|
|
||||||
|
|
||||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||||
}
|
}
|
||||||
|
151
RScriptSerializer.cs
Normal file
151
RScriptSerializer.cs
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using static Convention.RScript.RScriptContext;
|
||||||
|
|
||||||
|
namespace Convention.RScript
|
||||||
|
{
|
||||||
|
public static class RScriptSerializer
|
||||||
|
{
|
||||||
|
public static byte[] SerializeClass(SerializableClass data)
|
||||||
|
{
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
using (var writer = new BinaryWriter(stream))
|
||||||
|
{
|
||||||
|
// 序列化 Sentences 数组
|
||||||
|
writer.Write(data.Sentences?.Length ?? 0);
|
||||||
|
if (data.Sentences != null)
|
||||||
|
{
|
||||||
|
foreach (var sentence in data.Sentences)
|
||||||
|
{
|
||||||
|
writer.Write(sentence.content ?? "");
|
||||||
|
writer.Write(sentence.info?.Length ?? 0);
|
||||||
|
if (sentence.info != null)
|
||||||
|
{
|
||||||
|
foreach (var info in sentence.info)
|
||||||
|
{
|
||||||
|
writer.Write(info ?? "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.Write((int)sentence.mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 Labels 数组
|
||||||
|
writer.Write(data.Labels?.Length ?? 0);
|
||||||
|
if (data.Labels != null)
|
||||||
|
{
|
||||||
|
foreach (var label in data.Labels)
|
||||||
|
{
|
||||||
|
writer.Write(label.Item1 ?? "");
|
||||||
|
writer.Write(label.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 NamespaceLayer 数组
|
||||||
|
writer.Write(data.NamespaceLayer?.Length ?? 0);
|
||||||
|
if (data.NamespaceLayer != null)
|
||||||
|
{
|
||||||
|
foreach (var layer in data.NamespaceLayer)
|
||||||
|
{
|
||||||
|
writer.Write(layer.Item1);
|
||||||
|
writer.Write(layer.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 序列化 NamespaceLabels 数组
|
||||||
|
writer.Write(data.NamespaceLabels?.Length ?? 0);
|
||||||
|
if (data.NamespaceLabels != null)
|
||||||
|
{
|
||||||
|
foreach (var nsLabel in data.NamespaceLabels)
|
||||||
|
{
|
||||||
|
writer.Write(nsLabel.Item1 ?? "");
|
||||||
|
writer.Write(nsLabel.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 这里需要根据 ExpressionParser.SerializableParser 的结构来序列化
|
||||||
|
// writer.Write(...); // CompileParser 的序列化
|
||||||
|
|
||||||
|
return stream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SerializableClass DeserializeClass(byte[] data)
|
||||||
|
{
|
||||||
|
using (var stream = new MemoryStream(data))
|
||||||
|
using (var reader = new BinaryReader(stream))
|
||||||
|
{
|
||||||
|
var result = new SerializableClass();
|
||||||
|
|
||||||
|
// 反序列化 Sentences 数组
|
||||||
|
int sentencesLength = reader.ReadInt32();
|
||||||
|
if (sentencesLength > 0)
|
||||||
|
{
|
||||||
|
result.Sentences = new RScriptSentence[sentencesLength];
|
||||||
|
for (int i = 0; i < sentencesLength; i++)
|
||||||
|
{
|
||||||
|
var sentence = new RScriptSentence();
|
||||||
|
sentence.content = reader.ReadString();
|
||||||
|
|
||||||
|
int infoLength = reader.ReadInt32();
|
||||||
|
if (infoLength > 0)
|
||||||
|
{
|
||||||
|
sentence.info = new string[infoLength];
|
||||||
|
for (int j = 0; j < infoLength; j++)
|
||||||
|
{
|
||||||
|
sentence.info[j] = reader.ReadString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sentence.mode = (RScriptSentence.Mode)reader.ReadInt32();
|
||||||
|
result.Sentences[i] = sentence;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 Labels 数组
|
||||||
|
int labelsLength = reader.ReadInt32();
|
||||||
|
if (labelsLength > 0)
|
||||||
|
{
|
||||||
|
result.Labels = new Tuple<string, int>[labelsLength];
|
||||||
|
for (int i = 0; i < labelsLength; i++)
|
||||||
|
{
|
||||||
|
string item1 = reader.ReadString();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.Labels[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 NamespaceLayer 数组
|
||||||
|
int namespaceLayerLength = reader.ReadInt32();
|
||||||
|
if (namespaceLayerLength > 0)
|
||||||
|
{
|
||||||
|
result.NamespaceLayer = new Tuple<int, int>[namespaceLayerLength];
|
||||||
|
for (int i = 0; i < namespaceLayerLength; i++)
|
||||||
|
{
|
||||||
|
int item1 = reader.ReadInt32();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.NamespaceLayer[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 NamespaceLabels 数组
|
||||||
|
int namespaceLabelsLength = reader.ReadInt32();
|
||||||
|
if (namespaceLabelsLength > 0)
|
||||||
|
{
|
||||||
|
result.NamespaceLabels = new Tuple<string, int>[namespaceLabelsLength];
|
||||||
|
for (int i = 0; i < namespaceLabelsLength; i++)
|
||||||
|
{
|
||||||
|
string item1 = reader.ReadString();
|
||||||
|
int item2 = reader.ReadInt32();
|
||||||
|
result.NamespaceLabels[i] = Tuple.Create(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 反序列化 CompileParser
|
||||||
|
// result.CompileParser = ...; // 根据具体结构实现
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user