脚本节点新增对Char字面量的支持

This commit is contained in:
fengjiayi
2025-05-31 00:20:29 +08:00
parent 8c6cb0a9c6
commit cc0b084c84
8 changed files with 159 additions and 76 deletions

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Script.Node
{
internal class CharNode : ASTNode
{
public char Value { get; }
public CharNode(string value)
{
Value = char.Parse(value);
}
}
}

View File

@@ -8,10 +8,13 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="bin\**" />
<Compile Remove="TestExpression\**" />
<Compile Remove="Tool\**" />
<EmbeddedResource Remove="bin\**" />
<EmbeddedResource Remove="TestExpression\**" />
<EmbeddedResource Remove="Tool\**" />
<None Remove="bin\**" />
<None Remove="TestExpression\**" />
<None Remove="Tool\**" />
</ItemGroup>

View File

@@ -442,6 +442,8 @@ namespace Serein.Script
return numberNode.Value; // 返回数值
case StringNode stringNode:
return stringNode.Value; // 返回字符串值
case CharNode charNode:
return charNode.Value; // 返回Char
case IdentifierNode identifierNode:
return context.GetVarValue(identifierNode.Name);
//throw new SereinSciptException(identifierNode, "尝试使用值为null的变量");

View File

@@ -11,7 +11,7 @@ namespace Serein.Script
/// </summary>
Null,
/// <summary>
/// 标识符
/// 标识符(变量)
/// </summary>
Identifier,
/// <summary>
@@ -27,6 +27,10 @@ namespace Serein.Script
/// </summary>
String,
/// <summary>
/// Char字符
/// </summary>
Char,
/// <summary>
/// 插值字符串
/// </summary>
InterpolatedString,
@@ -163,6 +167,20 @@ namespace Serein.Script
return ReadString();
}
if (currentChar == '\'')
{
if (_input[_index + 2] == '\'')
{
return ReadChar();
}
else
{
throw new Exception($"not is char: {currentChar},in Line.{_row}.");
}
}
// 跳过注释
if (_input[_index] == '/' && _input[_index + 1] == '/')
{
@@ -332,6 +350,23 @@ namespace Serein.Script
//return new Token(TokenType.String, value.ToString());
}
/// <summary>
/// 读取硬编码的Char字符
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private Token ReadChar()
{
_index++; // 跳过开头的引号
var start = _index;
var cahrValue = _input.Slice(start, 1).ToString();
_index++; // 跳过Char字符串后的引号
return CreateToken(TokenType.Char, cahrValue);
// _index++; // 跳过结束的引号
//return new Token(TokenType.String, value.ToString());
}
/// <summary>
/// 获取对应行的代码文本
/// </summary>

View File

@@ -210,7 +210,7 @@ namespace Serein.Script
private ASTNode ParseLetAssignment()
{
_currentToken = _lexer.NextToken(); // Consume "let"
string variable = _currentToken.Value.ToString();
string variable = _currentToken.Value.ToString(); // 变量名称
_currentToken = _lexer.NextToken(); // Consume identifier
ASTNode value;
if (_currentToken.Type == TokenType.Semicolon)
@@ -419,6 +419,7 @@ namespace Serein.Script
_currentToken = _lexer.NextToken(); // consume "("
var arguments = new List<ASTNode>();
bool isBreak = false;
while (_currentToken.Type != TokenType.ParenthesisRight)
{
var arg = Expression();
@@ -430,11 +431,12 @@ namespace Serein.Script
}
if (_currentToken.Type == TokenType.Semicolon)
{
isBreak = true;
break; // consume ";"
}
}
_currentToken = _lexer.NextToken(); // consume ")"
if(!isBreak)
_currentToken = _lexer.NextToken(); // consume ")"
//var node = Statements[^1];
@@ -586,8 +588,14 @@ namespace Serein.Script
if (_currentToken.Type == TokenType.String)
{
var text = _currentToken.Value;
_currentToken = _lexer.NextToken(); // 消耗
return new StringNode(text.ToString()).SetTokenInfo(_currentToken);
_currentToken = _lexer.NextToken(); // 消耗字符串
return new StringNode(text).SetTokenInfo(_currentToken);
}
if (_currentToken.Type == TokenType.Char)
{
var text = _currentToken.Value;
_currentToken = _lexer.NextToken(); // 消耗Char
return new CharNode(text).SetTokenInfo(_currentToken);
}
if( _currentToken.Type == TokenType.InterpolatedString)
{

View File

@@ -16,72 +16,7 @@ using System.Windows.Threading;
namespace Serein.Workbench
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// 注册ViewModel
/// </summary>
/// <param name="collection"></param>
public static void AddViewModelServices(this IServiceCollection collection)
{
collection.AddSingleton<Locator>(); // 主窗体
collection.AddSingleton<MainViewModel>();
collection.AddSingleton<MainMenuBarViewModel>();
collection.AddSingleton<FlowWorkbenchViewModel>();
collection.AddSingleton<BaseNodesViewModel>();
collection.AddSingleton<FlowLibrarysViewModel>();
collection.AddSingleton<FlowEditViewModel>();
collection.AddSingleton<ViewNodeInfoViewModel>();
collection.AddSingleton<ViewNodeMethodInfoViewModel>();
collection.AddTransient<FlowCanvasViewModel>(); // 画布
collection.AddTransient<ViewCanvasInfoViewModel>(); // 画布节点树视图
}
public static void AddWorkbenchServices(this IServiceCollection collection)
{
collection.AddSingleton<IFlowEEForwardingService, FlowEEForwardingService>(); // 流程事件管理
collection.AddSingleton<IKeyEventService, KeyEventService>();// 按键事件管理
collection.AddSingleton<IWorkbenchEventService, WorkbenchEventService>(); // 流程事件管理
collection.AddSingleton<FlowProjectService>(); // 项目管理
collection.AddSingleton<FlowNodeService>(); // 节点操作管理
}
/// <summary>
/// 注册流程接口相关实例
/// </summary>
/// <param name="collection"></param>
public static void AddFlowServices(this IServiceCollection collection)
{
#region
Func<SynchronizationContext> getSyncContext = null;
Dispatcher.CurrentDispatcher.Invoke(() =>
{
var uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
if (uiContext is not null)
{
getSyncContext = () => uiContext;
}
});
UIContextOperation? uIContextOperation = null;
uIContextOperation = new UIContextOperation(getSyncContext); // 封装一个调用UI线程的工具类
var flowEnvironmentDecorator = new FlowEnvironmentDecorator();
flowEnvironmentDecorator.SetUIContextOperation(uIContextOperation);
collection.AddSingleton<UIContextOperation>(uIContextOperation); // 注册UI线程操作上下文
collection.AddSingleton<IFlowEnvironment>(flowEnvironmentDecorator); // 注册运行环境
collection.AddSingleton<IFlowEnvironmentEvent>(flowEnvironmentDecorator); // 注册运行环境事件
#endregion
}
}
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
@@ -117,10 +52,10 @@ namespace Serein.Workbench
// 这里是测试代码,可以删除
private async Task LoadLocalProjectAsync()
{
var projectService = App.GetService<FlowProjectService>();
await Task.Delay(500);
if (1 == 1)
if (1 == 11)
{
var projectService = App.GetService<FlowProjectService>();
await Task.Delay(500);
string filePath;
filePath = @"F:\TempFile\flow\temp2\project.dnf";
projectService.LoadLocalProject(filePath);

View File

@@ -0,0 +1,83 @@
using Microsoft.Extensions.DependencyInjection;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.NodeFlow.Env;
using Serein.Workbench.Api;
using Serein.Workbench.Services;
using Serein.Workbench.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace Serein.Workbench
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// 注册ViewModel
/// </summary>
/// <param name="collection"></param>
public static void AddViewModelServices(this IServiceCollection collection)
{
collection.AddSingleton<Locator>(); // 主窗体
collection.AddSingleton<MainViewModel>();
collection.AddSingleton<MainMenuBarViewModel>();
collection.AddSingleton<FlowWorkbenchViewModel>();
collection.AddSingleton<BaseNodesViewModel>();
collection.AddSingleton<FlowLibrarysViewModel>();
collection.AddSingleton<FlowEditViewModel>();
collection.AddSingleton<ViewNodeInfoViewModel>();
collection.AddSingleton<ViewNodeMethodInfoViewModel>();
collection.AddTransient<FlowCanvasViewModel>(); // 画布
collection.AddTransient<ViewCanvasInfoViewModel>(); // 画布节点树视图
}
public static void AddWorkbenchServices(this IServiceCollection collection)
{
collection.AddSingleton<IFlowEEForwardingService, FlowEEForwardingService>(); // 流程事件管理
collection.AddSingleton<IKeyEventService, KeyEventService>();// 按键事件管理
collection.AddSingleton<IWorkbenchEventService, WorkbenchEventService>(); // 流程事件管理
collection.AddSingleton<FlowProjectService>(); // 项目管理
collection.AddSingleton<FlowNodeService>(); // 节点操作管理
}
/// <summary>
/// 注册流程接口相关实例
/// </summary>
/// <param name="collection"></param>
public static void AddFlowServices(this IServiceCollection collection)
{
#region
Func<SynchronizationContext> getSyncContext = null;
Dispatcher.CurrentDispatcher.Invoke(() =>
{
var uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
if (uiContext is not null)
{
getSyncContext = () => uiContext;
}
});
UIContextOperation? uIContextOperation = null;
uIContextOperation = new UIContextOperation(getSyncContext); // 封装一个调用UI线程的工具类
var flowEnvironmentDecorator = new FlowEnvironmentDecorator();
flowEnvironmentDecorator.SetUIContextOperation(uIContextOperation);
collection.AddSingleton<UIContextOperation>(uIContextOperation); // 注册UI线程操作上下文
collection.AddSingleton<IFlowEnvironment>(flowEnvironmentDecorator); // 注册运行环境
collection.AddSingleton<IFlowEnvironmentEvent>(flowEnvironmentDecorator); // 注册运行环境事件
#endregion
}
}
}

View File

@@ -13,7 +13,7 @@
<Menu DockPanel.Dock="Top" Grid.Row="0" Grid.ColumnSpan="5" Height="20">
<MenuItem Header="项目">
<MenuItem Header="保存项目" Command="{Binding SaveProjectCommand}"></MenuItem>
<MenuItem Header="加载本地项目" ></MenuItem>
<MenuItem Header="加载本地项目" Command="{Binding LoadLocalProjectCommand}" ></MenuItem>
<MenuItem Header="加载远程项目"></MenuItem>
</MenuItem>