Compare commits

..

2 Commits

Author SHA1 Message Date
249a2f9ce3 内置更加友好的类型打印 2025-12-15 11:38:49 +08:00
0a7f6eb362 更新异步step 2025-12-11 18:03:28 +08:00
12 changed files with 290 additions and 36 deletions

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -27,5 +28,23 @@ namespace Convention.RScript.Runner
} }
return null; return null;
} }
[return: MaybeNull]
public override IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 检查并跳转到上次跳转的位置
if (parser.Evaluate<bool>(sentence.content))
{
if (context.GotoPointerStack.Count == 0)
{
throw new RScriptRuntimeException($"No position to back.", context.CurrentRuntimePointer);
}
else
{
DoJumpRuntimePointer(parser, context.GotoPointerStack.Pop(), context);
}
}
yield break;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript namespace Convention.RScript
@@ -23,7 +24,7 @@ namespace Convention.RScript
else if (context.NamespaceLayer.TryGetValue(context.RuntimePointerStack.Peek(), out var exitPointer)) else if (context.NamespaceLayer.TryGetValue(context.RuntimePointerStack.Peek(), out var exitPointer))
{ {
context.CurrentRuntimePointer = exitPointer; context.CurrentRuntimePointer = exitPointer;
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context); return context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
} }
else else
{ {
@@ -32,5 +33,27 @@ namespace Convention.RScript
} }
return null; return null;
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 检查并跳转到当前命名空间的结束位置
if (parser.Evaluate<bool>(sentence.content))
{
if (context.RuntimePointerStack.Count == 0)
{
context.CurrentRuntimePointer = context.Sentences.Length;
}
else if (context.NamespaceLayer.TryGetValue(context.RuntimePointerStack.Peek(), out var exitPointer))
{
context.CurrentRuntimePointer = exitPointer;
yield return context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].RunAsync(parser, context.CurrentSentence, context);
}
else
{
throw new RScriptRuntimeException($"No namespace to break.", context.CurrentRuntimePointer);
}
}
}
} }
} }

View File

@@ -1,5 +1,6 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System; using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -123,5 +124,63 @@ namespace Convention.RScript.Runner
} }
return null; return null;
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 定义变量
var varTypeName = sentence.info[0];
var varName = sentence.info[1];
var varInitExpression = sentence.info[2];
Type varType;
object varDefaultValue;
{
if (varTypeName == "string")
{
varType = typeof(string);
varDefaultValue = varInitExpression == null ? string.Empty : varInitExpression.Trim('\"');
}
else if (varTypeName == "int")
{
varType = typeof(int);
varDefaultValue = varInitExpression == null ? 0 : Convert.ChangeType(parser.Evaluate(varInitExpression), typeof(int));
}
else if (varTypeName == "double")
{
varType = typeof(double);
varDefaultValue = varInitExpression == null ? 0.0 : Convert.ChangeType(parser.Evaluate(varInitExpression), typeof(double));
}
else if (varTypeName == "float")
{
varType = typeof(float);
varDefaultValue = varInitExpression == null ? 0.0f : Convert.ChangeType(parser.Evaluate(varInitExpression), typeof(float));
}
else if (varTypeName == "bool")
{
varType = typeof(bool);
varDefaultValue = varInitExpression == null ? false : Convert.ChangeType(parser.Evaluate(varInitExpression), typeof(bool));
}
else if (varTypeName == "var")
{
varType = typeof(object);
varDefaultValue = varInitExpression == null ? new object() : parser.Evaluate(varInitExpression);
}
else
{
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", context.CurrentRuntimePointer);
}
}
if (context.CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
{
context.Variables.Add(varName, new(varType, varDefaultValue));
parser.context.Variables[varName] = varDefaultValue;
context.CurrentLocalSpaceVariableNames.Peek().Add(varName);
}
else
{
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", context.CurrentRuntimePointer);
}
yield break;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -16,5 +17,12 @@ namespace Convention.RScript.Runner
context.CurrentRuntimePointer = context.NamespaceLayer[context.NamespaceLabels[sentence.content]]; context.CurrentRuntimePointer = context.NamespaceLayer[context.NamespaceLabels[sentence.content]];
return null; return null;
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
context.CurrentRuntimePointer = context.NamespaceLayer[context.NamespaceLabels[sentence.content]];
yield break;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -24,5 +25,20 @@ namespace Convention.RScript.Runner
context.RuntimePointerStack.Push(context.CurrentRuntimePointer); context.RuntimePointerStack.Push(context.CurrentRuntimePointer);
return null; return null;
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 准备记录当前命名空间中定义的变量, 清空上层命名空间的变量
context.CurrentLocalSpaceVariableNames.Push(new());
// 更新变量值
foreach (var (varName, varValue) in parser.context.Variables)
{
context.Variables.SetValue(varName, varValue);
}
// 压栈
context.RuntimePointerStack.Push(context.CurrentRuntimePointer);
yield break;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -36,5 +37,32 @@ namespace Convention.RScript.Runner
context.RuntimePointerStack.Pop(); context.RuntimePointerStack.Pop();
return null; return null;
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 移除当前命名空间的变量
foreach (var local in context.CurrentLocalSpaceVariableNames.Peek())
{
context.Variables.Remove(local);
parser.context.Variables.Remove(local);
}
// 还原上层命名空间的变量
foreach (var local in context.CurrentLocalSpaceVariableNames.Peek())
{
if (context.Variables.ContainsKey(local))
{
parser.context.Variables[local] = context.Variables[local].data;
}
else
{
parser.context.Variables.Remove(local);
}
}
context.CurrentLocalSpaceVariableNames.Pop();
// 弹栈
context.RuntimePointerStack.Pop();
yield break;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -15,5 +16,14 @@ namespace Convention.RScript.Runner
{ {
return parser.Evaluate(sentence.content); return parser.Evaluate(sentence.content);
} }
[return: MaybeNull]
public IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
var result = parser.Evaluate(sentence.content);
if(result is IEnumerator ir)
yield return ir;
yield return null;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner namespace Convention.RScript.Runner
@@ -48,5 +49,43 @@ namespace Convention.RScript.Runner
} }
return null; return null;
} }
[return: MaybeNull]
public override IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context)
{
// 检查并跳转到指定标签
if (parser.Evaluate<bool>(sentence.info[0]))
{
if (context.Labels.TryGetValue(sentence.content, out var labelPointer))
{
context.GotoPointerStack.Push(context.CurrentRuntimePointer);
DoJumpRuntimePointer(parser, labelPointer, context);
}
else if (context.NamespaceLabels.TryGetValue(sentence.content, out labelPointer))
{
int current = context.CurrentRuntimePointer;
//DoEnterNamespace(parser);
context.SentenceRunners[RScriptSentence.Mode.EnterNamespace].Run(parser, context.CurrentSentence, context);
context.CurrentRuntimePointer = labelPointer;
for (int e = context.NamespaceLayer[context.NamespaceLabels[sentence.content]]; ;)
{
yield return context.RunNextStepAsync(parser);
if (context.CurrentRuntimePointer >= context.Sentences.Length)
break;
else if (context.CurrentRuntimePointer == e)
break;
else
context.CurrentRuntimePointer++;
}
//context.DoExitNamespace(parser);
context.SentenceRunners[RScriptSentence.Mode.ExitNamespace].Run(parser, context.CurrentSentence, context);
context.CurrentRuntimePointer = current;
}
else
{
throw new RScriptRuntimeException($"Label '{sentence.content}' not found.", context.CurrentRuntimePointer);
}
}
}
} }
} }

View File

@@ -1,5 +1,6 @@
using Convention.RScript.Parser; using Convention.RScript.Parser;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
@@ -72,5 +73,7 @@ namespace Convention.RScript.Runner
public abstract void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context); public abstract void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
[return: MaybeNull] [return: MaybeNull]
public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context); public abstract object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
[return: MaybeNull]
public abstract IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
} }
} }

View File

@@ -1,14 +1,79 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Convention.RScript.Variable.CStyle namespace Convention.RScript.Variable.CStyle
{ {
public class CScriptRScriptVariableGenerater : RScriptInjectVariableGenerater public class CScriptRScriptVariableGenerater : RScriptInjectVariableGenerater
{ {
/// <summary>
/// 获取类型的友好显示名称,支持泛型、数组等复杂类型
/// </summary>
public static string GetFriendlyName(Type type)
{
if (type == null) return null;
// 处理泛型类型
if (type.IsGenericType)
{
// 特殊处理可空类型Nullable<T>
if (type.IsNullable())
{
return $"{GetFriendlyName(type.GetGenericArguments()[0])}?";
}
var sb = new StringBuilder();
// 获取类型基础名称(移除 `1, `2 等后缀)
string baseName = type.Name.Contains('`') ? type.Name[..type.Name.IndexOf('`')] : type.Name;
sb.Append(baseName);
sb.Append('<');
// 递归处理泛型参数
Type[] args = type.GetGenericArguments();
for (int i = 0; i < args.Length; i++)
{
if (i > 0) sb.Append(", ");
sb.Append(GetFriendlyName(args[i]));
}
sb.Append('>');
return sb.ToString();
}
// 处理数组类型
if (type.IsArray)
{
string elementName = GetFriendlyName(type.GetElementType());
int rank = type.GetArrayRank();
return rank == 1 ? $"{elementName}[]" : $"{elementName}[{new string(',', rank - 1)}]";
}
// 处理指针类型
if (type.IsPointer)
{
return $"{GetFriendlyName(type.GetElementType())}*";
}
// 处理引用类型ref/out 参数)
if (type.IsByRef)
{
return $"{GetFriendlyName(type.GetElementType())}&";
}
// 普通类型直接返回名称
return type.Name;
}
/// <summary>
/// 判断是否为可空类型
/// </summary>
public static bool IsNullable(Type type)
{
return type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
public static string GetTypename(Type type) public static string GetTypename(Type type)
{ {
if (type == typeof(int)) if (type == typeof(int))
@@ -21,12 +86,7 @@ namespace Convention.RScript.Variable.CStyle
return "bool"; return "bool";
if (type == typeof(void)) if (type == typeof(void))
return "void"; return "void";
if (type.IsEnum) return GetFriendlyName(type).Replace('`', '_');
return type.FullName.Replace('`', '_');
if (type.IsClass)
return type.FullName.Replace('`', '_');
var name = type.Name.Replace('`', '_');
return name;
} }
private int m_layer = 0; private int m_layer = 0;
@@ -116,7 +176,9 @@ namespace Convention.RScript.Variable.CStyle
protected override string WritePageHead(Type currentType) protected override string WritePageHead(Type currentType)
{ {
if (currentType.BaseType != null)
return $"#include\"{GetFilename(currentType.BaseType)}\""; return $"#include\"{GetFilename(currentType.BaseType)}\"";
return string.Empty;
} }
public override string GetFilename(Type currentType) public override string GetFilename(Type currentType)

View File

@@ -77,6 +77,7 @@ namespace Convention.RScript
public interface IRSentenceRunner public interface IRSentenceRunner
{ {
[return: MaybeNull] object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context); [return: MaybeNull] object Run(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
[return: MaybeNull] IEnumerator RunAsync(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context); void Compile(ExpressionParser parser, RScriptSentence sentence, RScriptContext context);
} }
@@ -266,18 +267,8 @@ namespace Convention.RScript
public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer]; public RScriptSentence CurrentSentence => Sentences[CurrentRuntimePointer];
public int StepCount { get; private set; }
#if DEBUG
public List<RScriptSentence> StepStack = new();
#endif
internal object RunNextStep(ExpressionParser parser) internal object RunNextStep(ExpressionParser parser)
{ {
StepCount++;
#if DEBUG
StepStack.Add(CurrentSentence);
#endif
var sentence = CurrentSentence; var sentence = CurrentSentence;
try try
{ {
@@ -292,6 +283,12 @@ namespace Convention.RScript
throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex); throw new RScriptRuntimeException($"Runtime error: {ex.Message}", CurrentRuntimePointer, ex);
} }
} }
internal IEnumerator RunNextStepAsync(ExpressionParser parser)
{
var sentence = CurrentSentence;
if (SentenceRunners.TryGetValue(sentence.mode, out var runner))
yield return runner.RunAsync(parser, sentence, this);
}
internal readonly Stack<int> RuntimePointerStack = new(); internal readonly Stack<int> RuntimePointerStack = new();
internal readonly Stack<int> GotoPointerStack = new(); internal readonly Stack<int> GotoPointerStack = new();
@@ -311,7 +308,6 @@ namespace Convention.RScript
private void BeforeRun(ExpressionParser parser) private void BeforeRun(ExpressionParser parser)
{ {
StepCount = 0;
CurrentLocalSpaceVariableNames.Clear(); CurrentLocalSpaceVariableNames.Clear();
RuntimePointerStack.Clear(); RuntimePointerStack.Clear();
GotoPointerStack.Clear(); GotoPointerStack.Clear();
@@ -331,13 +327,13 @@ namespace Convention.RScript
private void ResetAndSureRunAgain(ExpressionParser parser) private void ResetAndSureRunAgain(ExpressionParser parser)
{ {
StepCount = 0;
CurrentLocalSpaceVariableNames.Clear(); CurrentLocalSpaceVariableNames.Clear();
RuntimePointerStack.Clear(); RuntimePointerStack.Clear();
GotoPointerStack.Clear(); GotoPointerStack.Clear();
CurrentLocalSpaceVariableNames.Clear(); CurrentLocalSpaceVariableNames.Clear();
CurrentLocalSpaceVariableNames.Push(new()); CurrentLocalSpaceVariableNames.Push(new());
JumpPointerCache.Clear(); JumpPointerCache.Clear();
parser.context.Variables.Clear();
foreach (var (name, varObject) in Variables) foreach (var (name, varObject) in Variables)
{ {
parser.context.Variables[name] = varObject.data; parser.context.Variables[name] = varObject.data;
@@ -356,6 +352,7 @@ namespace Convention.RScript
public void Run(ExpressionParser parser) public void Run(ExpressionParser parser)
{ {
BeforeRun(parser); BeforeRun(parser);
Stack<IEnumerator> buffer = new();
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++) for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
{ {
RunNextStep(parser); RunNextStep(parser);
@@ -368,12 +365,7 @@ namespace Convention.RScript
BeforeRun(parser); BeforeRun(parser);
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++) for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
{ {
var ret = RunNextStep(parser); yield return RunNextStepAsync(parser);
if (ret is IEnumerator ir)
{
yield return ir;
}
yield return null;
} }
AfterRun(parser); AfterRun(parser);
} }
@@ -393,12 +385,7 @@ namespace Convention.RScript
ResetAndSureRunAgain(parser); ResetAndSureRunAgain(parser);
for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++) for (CurrentRuntimePointer = 0; CurrentRuntimePointer < Sentences.Length; CurrentRuntimePointer++)
{ {
var ret = RunNextStep(parser); yield return RunNextStepAsync(parser);
if (ret is IEnumerator ir)
{
yield return ir;
}
yield return null;
} }
AfterRun(parser); AfterRun(parser);
} }

View File

@@ -143,7 +143,7 @@ namespace Convention.RScript
{ {
parser = new(new()); parser = new(new());
context = CreateContext(SplitScript(script).ToArray(), import, variables); context = CreateContext(SplitScript(script).ToArray(), import, variables);
return context.RunAsync(parser); yield return context.RunAsync(parser);
} }
public SerializableClass Compile(string script, RScriptImportClass import = null, RScriptVariables variables = null) public SerializableClass Compile(string script, RScriptImportClass import = null, RScriptVariables variables = null)
{ {
@@ -179,7 +179,7 @@ namespace Convention.RScript
} }
public IEnumerator ReRunAsync() public IEnumerator ReRunAsync()
{ {
return context.ReRunAsync(parser); yield return context.ReRunAsync(parser);
} }
} }