Compare commits

...

13 Commits

Author SHA1 Message Date
b1c36c08fb EP RScript/内置辅助解释 2025-10-21 15:31:57 +08:00
d2bd58568a EP RScript/新增跳转缓存 2025-10-21 10:26:37 +08:00
15dd1c9aa4 EP RScript/优化跳转 2025-10-17 16:43:15 +08:00
21f3c580eb 修复了File中有关二进制的错误 2025-10-17 16:25:18 +08:00
09b334ca58 修复一些内容 2025-10-17 15:47:20 +08:00
4dc6691650 EP RScript 2025-10-16 20:15:59 +08:00
22b8c1838a EP RScript/修复遗漏 2025-10-16 17:24:06 +08:00
b9012674d6 EP RScript/解离出Runner 2025-10-16 15:21:33 +08:00
8a4edfcb79 EP RScript/允许变量声明时赋值 2025-10-16 11:58:47 +08:00
8d6f96b99a Save 2025-10-16 11:29:50 +08:00
a91c9741e4 加入自动类型转换 2025-10-16 10:25:31 +08:00
9d7fbf9786 Update RScript 2025-10-16 00:39:45 +08:00
8e8edd8724 Update RScript/新增NamedSpace为命名空间命名 2025-10-15 16:50:47 +08:00
6 changed files with 132 additions and 35 deletions

View File

@@ -5,6 +5,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<RootNamespace>Convention</RootNamespace> <RootNamespace>Convention</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -1,12 +1,13 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Runtime.Serialization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Xml;
namespace Convention namespace Convention
{ {
@@ -79,6 +80,39 @@ namespace Convention
} }
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] Serialize<T>(T obj)
{
ArgumentNullException.ThrowIfNull(obj);
using var memoryStream = new MemoryStream();
DataContractSerializer ser = new DataContractSerializer(typeof(T));
ser.WriteObject(memoryStream, obj);
var data = memoryStream.ToArray();
return data;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T Deserialize<T>(byte[] data)
{
ArgumentNullException.ThrowIfNull(data);
using var memoryStream = new MemoryStream(data);
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());
DataContractSerializer ser = new DataContractSerializer(typeof(T));
var result = (T)ser.ReadObject(reader, true);
return result;
}
public static object SeekValue(object obj, string name, BindingFlags flags, out bool isSucceed) public static object SeekValue(object obj, string name, BindingFlags flags, out bool isSucceed)
{ {
Type type = obj.GetType(); Type type = obj.GetType();
@@ -409,5 +443,25 @@ namespace Convention
{ {
return DateTime.Now.ToString(format); return DateTime.Now.ToString(format);
} }
public class EnumerableClass : IEnumerable
{
private readonly IEnumerator ir;
public EnumerableClass(IEnumerator ir)
{
this.ir = ir;
}
public IEnumerator GetEnumerator()
{
return ir;
}
}
public static IEnumerable AsEnumerable(this IEnumerator ir)
{
return new EnumerableClass(ir);
}
} }
} }

View File

@@ -161,18 +161,7 @@ namespace Convention
{ {
if (IsFile() == false) if (IsFile() == false)
throw new InvalidOperationException("Target is not a file"); throw new InvalidOperationException("Target is not a file");
var file = this.OriginInfo as FileInfo; return File.ReadAllBytes(FullPath);
const int BlockSize = 1024;
long FileSize = file.Length;
byte[] result = new byte[FileSize];
long offset = 0;
using (var fs = file.OpenRead())
{
fs.ReadAsync(result[(int)(offset)..(int)(offset + BlockSize)], 0, (int)(offset + BlockSize) - (int)(offset));
offset += BlockSize;
offset = System.Math.Min(offset, FileSize);
}
return result;
} }
public List<string[]> LoadAsCsv() public List<string[]> LoadAsCsv()
@@ -245,7 +234,7 @@ namespace Convention
public void SaveAsBinary(byte[] data) public void SaveAsBinary(byte[] data)
{ {
SaveDataAsBinary(FullPath, data, (OriginInfo as FileInfo).OpenWrite()); File.WriteAllBytes(FullPath, data);
} }
public void SaveAsCsv(List<string[]> csvData) public void SaveAsCsv(List<string[]> csvData)
@@ -1113,4 +1102,32 @@ namespace Convention
#endregion #endregion
} }
#if ENABLE_UNSAFE
public static class UnsafeBinarySerializer
{
public static unsafe byte[] StructToBytes<T>(T structure) where T : unmanaged
{
int size = sizeof(T);
byte[] bytes = new byte[size];
fixed (byte* ptr = bytes)
{
*(T*)ptr = structure;
}
return bytes;
}
public static unsafe T BytesToStruct<T>(byte[] bytes) where T : unmanaged
{
fixed (byte* ptr = bytes)
{
return *(T*)ptr;
}
}
}
#endif
} }

View File

@@ -1,37 +1,62 @@
using Convention.RScript; using Convention;
using Flee.PublicTypes; using Convention.EasySave;
using Convention.RScript;
using System; using System;
using System.IO;
public class Program public class Program
{ {
static class Test
{
public static object Func(object x)
{
Console.WriteLine(x);
return x;
}
}
static void Main(string[] args) static void Main(string[] args)
{ {
RScriptEngine engine = new(); RScriptEngine engine = new();
RScriptImportClass import = new() RScriptImportClass import = new()
{ {
typeof(Math) typeof(Math),
typeof(ExpressionMath),
typeof(Test)
}; };
var result = engine.Run(@"
double i;
i = 2.0; var result = engine.Compile(@"
int i= 2;
int count = 0;
label(test); label(test);
{ goto(true,func1);
goto(true,func1); Func(i);
goto(100>i,test); goto(100>i,test);
}
double result;
result = i;
goto(true,end); goto(context.ExistNamespace(""x""),end);
namespace(x)
label(func1);
{ {
i = Pow(i,2.0); Func(""xxx"");
}
namespace(func1)
{
i = Pow(i,2);
count = count + 1;
Func(count);
} }
back(true);
label(end); label(end);
", import); ", import);
Console.WriteLine($"Script executed successfully. Result: {result["result"].data}"); var data = RScriptSerializer.SerializeClass(result);
var file = new ToolFile("F:\\test_after_run.dat");
file.SaveAsBinary(data);
data = file.LoadAsBinary();
engine.Run(RScriptSerializer.DeserializeClass(data), import);
return;
var data2 = RScriptSerializer.SerializeClass(engine.GetCompileResultFromCurrent());
var file2 = new ToolFile("F:\\test_after_run.dat");
file2.SaveAsBinary(data2);
} }
} }