更新异步step

This commit is contained in:
2025-12-11 18:03:28 +08:00
parent e4b7dc0f55
commit 0a7f6eb362
11 changed files with 219 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
using Convention.RScript.Parser;
using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace Convention.RScript.Runner
@@ -123,5 +124,63 @@ namespace Convention.RScript.Runner
}
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;
}
}
}