mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-20 08:16:34 +08:00
暂时实现了简陋的脚本AST分析解释,后面再绑定到控件上
This commit is contained in:
26
Serein.Script/Node/ASTNode.cs
Normal file
26
Serein.Script/Node/ASTNode.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
public abstract class ASTNode
|
||||
{
|
||||
public string Code { get; private set; }
|
||||
public int Row { get; private set; }
|
||||
public int StartIndex { get; private set; }
|
||||
public int Length { get; private set; }
|
||||
|
||||
internal ASTNode SetTokenInfo(Token token)
|
||||
{
|
||||
Row = token.Row;
|
||||
StartIndex = token.StartIndex;
|
||||
Length = token.Length;
|
||||
Code = token.Code;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
21
Serein.Script/Node/AssignmentNode.cs
Normal file
21
Serein.Script/Node/AssignmentNode.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 变量节点
|
||||
/// </summary>
|
||||
public class AssignmentNode : ASTNode
|
||||
{
|
||||
public string Variable { get; }
|
||||
public ASTNode Value { get; }
|
||||
public AssignmentNode(string variable, ASTNode value) => (Variable, Value) = (variable, value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
26
Serein.Script/Node/BinaryOperationNode.cs
Normal file
26
Serein.Script/Node/BinaryOperationNode.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 二元表达式节点
|
||||
/// </summary>
|
||||
|
||||
public class BinaryOperationNode : ASTNode
|
||||
{
|
||||
public ASTNode Left { get; }
|
||||
public string Operator { get; }
|
||||
public ASTNode Right { get; }
|
||||
|
||||
public BinaryOperationNode(ASTNode left, string op, ASTNode right)
|
||||
{
|
||||
Left = left;
|
||||
Operator = op;
|
||||
Right = right;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Serein.Script/Node/BooleanNode.cs
Normal file
17
Serein.Script/Node/BooleanNode.cs
Normal 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
|
||||
{
|
||||
/// <summary>
|
||||
/// 布尔字面量
|
||||
/// </summary>
|
||||
public class BooleanNode : ASTNode
|
||||
{
|
||||
public bool Value { get; }
|
||||
public BooleanNode(bool value) => Value = value;
|
||||
}
|
||||
}
|
||||
24
Serein.Script/Node/ClassTypeDefinitionNode.cs
Normal file
24
Serein.Script/Node/ClassTypeDefinitionNode.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 动态类型定义
|
||||
/// </summary>
|
||||
public class ClassTypeDefinitionNode : ASTNode
|
||||
{
|
||||
public string ClassName { get; }
|
||||
public Dictionary<string, Type> Fields { get; }
|
||||
|
||||
public ClassTypeDefinitionNode(Dictionary<string, Type> fields, string className)
|
||||
{
|
||||
this.Fields = fields;
|
||||
this.ClassName = className;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
20
Serein.Script/Node/CollectionIndexNode.cs
Normal file
20
Serein.Script/Node/CollectionIndexNode.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 集合索引获取
|
||||
/// </summary>
|
||||
public class CollectionIndexNode : ASTNode
|
||||
{
|
||||
public ASTNode IndexValue { get; }
|
||||
public CollectionIndexNode(ASTNode indexValue)
|
||||
{
|
||||
this.IndexValue = indexValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Serein.Script/Node/FunctionCallNode.cs
Normal file
24
Serein.Script/Node/FunctionCallNode.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 挂载函数调用
|
||||
/// </summary>
|
||||
public class FunctionCallNode : ASTNode
|
||||
{
|
||||
public string FunctionName { get; }
|
||||
public List<ASTNode> Arguments { get; }
|
||||
|
||||
public FunctionCallNode(string functionName, List<ASTNode> arguments)
|
||||
{
|
||||
FunctionName = functionName;
|
||||
Arguments = arguments;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
17
Serein.Script/Node/IdentifierNode.cs
Normal file
17
Serein.Script/Node/IdentifierNode.cs
Normal 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
|
||||
{
|
||||
/// <summary>
|
||||
/// 标识符(变量)
|
||||
/// </summary>
|
||||
public class IdentifierNode : ASTNode
|
||||
{
|
||||
public string Name { get; }
|
||||
public IdentifierNode(string name) => Name = name;
|
||||
}
|
||||
}
|
||||
21
Serein.Script/Node/IfNode.cs
Normal file
21
Serein.Script/Node/IfNode.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 条件节点
|
||||
/// </summary>
|
||||
public class IfNode : ASTNode
|
||||
{
|
||||
public ASTNode Condition { get; }
|
||||
public List<ASTNode> TrueBranch { get; }
|
||||
public List<ASTNode> FalseBranch { get; }
|
||||
public IfNode(ASTNode condition, List<ASTNode> trueBranch, List<ASTNode> falseBranch)
|
||||
=> (Condition, TrueBranch, FalseBranch) = (condition, trueBranch, falseBranch);
|
||||
}
|
||||
|
||||
}
|
||||
23
Serein.Script/Node/MemberAccessNode.cs
Normal file
23
Serein.Script/Node/MemberAccessNode.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示对象的成员访问
|
||||
/// </summary>
|
||||
public class MemberAccessNode : ASTNode
|
||||
{
|
||||
public ASTNode Object { get; }
|
||||
public string MemberName { get; }
|
||||
|
||||
public MemberAccessNode(ASTNode obj, string memberName)
|
||||
{
|
||||
Object = obj;
|
||||
MemberName = memberName;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Serein.Script/Node/MemberAssignmentNode.cs
Normal file
25
Serein.Script/Node/MemberAssignmentNode.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示对对象成员的赋值
|
||||
/// </summary>
|
||||
public class MemberAssignmentNode : ASTNode
|
||||
{
|
||||
public ASTNode Object { get; }
|
||||
public string MemberName { get; }
|
||||
public ASTNode Value { get; }
|
||||
|
||||
public MemberAssignmentNode(ASTNode obj, string memberName, ASTNode value)
|
||||
{
|
||||
Object = obj;
|
||||
MemberName = memberName;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Serein.Script/Node/MemberFunctionCallNode.cs
Normal file
25
Serein.Script/Node/MemberFunctionCallNode.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象成员方法调用
|
||||
/// </summary>
|
||||
public class MemberFunctionCallNode : ASTNode
|
||||
{
|
||||
public ASTNode Object { get; }
|
||||
public string FunctionName { get; }
|
||||
public List<ASTNode> Arguments { get; }
|
||||
|
||||
public MemberFunctionCallNode(ASTNode @object, string functionName, List<ASTNode> arguments)
|
||||
{
|
||||
Object = @object;
|
||||
FunctionName = functionName;
|
||||
Arguments = arguments;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Serein.Script/Node/NullNode.cs
Normal file
15
Serein.Script/Node/NullNode.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// Null节点
|
||||
/// </summary>
|
||||
public class NullNode : ASTNode
|
||||
{
|
||||
}
|
||||
}
|
||||
21
Serein.Script/Node/NumberNode.cs
Normal file
21
Serein.Script/Node/NumberNode.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 整数型字面量
|
||||
/// </summary>
|
||||
public class NumberNode : ASTNode
|
||||
{
|
||||
public int Value { get; }
|
||||
public NumberNode(int value) => Value = value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
23
Serein.Script/Node/ObjectInstantiationNode.cs
Normal file
23
Serein.Script/Node/ObjectInstantiationNode.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 类型创建
|
||||
/// </summary>
|
||||
public class ObjectInstantiationNode : ASTNode
|
||||
{
|
||||
public string TypeName { get; }
|
||||
public List<ASTNode> Arguments { get; }
|
||||
public ObjectInstantiationNode(string typeName, List<ASTNode> arguments)
|
||||
{
|
||||
this.TypeName = typeName;
|
||||
this.Arguments = arguments;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
22
Serein.Script/Node/ProgramNode.cs
Normal file
22
Serein.Script/Node/ProgramNode.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 程序入口
|
||||
/// </summary>
|
||||
public class ProgramNode : ASTNode
|
||||
{
|
||||
public List<ASTNode> Statements { get; }
|
||||
|
||||
public ProgramNode(List<ASTNode> statements)
|
||||
{
|
||||
Statements = statements;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
Serein.Script/Node/ReturnNode.cs
Normal file
25
Serein.Script/Node/ReturnNode.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回值
|
||||
/// </summary>
|
||||
public class ReturnNode : ASTNode
|
||||
{
|
||||
public ASTNode Value { get; }
|
||||
|
||||
public ReturnNode(ASTNode returnNode)
|
||||
{
|
||||
Value = returnNode;
|
||||
}
|
||||
public ReturnNode()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
61
Serein.Script/Node/StringNode.cs
Normal file
61
Serein.Script/Node/StringNode.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串字面量节点
|
||||
/// </summary>
|
||||
public class StringNode : ASTNode
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public StringNode(string input)
|
||||
{
|
||||
// 使用 StringBuilder 来构建输出
|
||||
StringBuilder output = new StringBuilder(input.Length);
|
||||
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
if (i < input.Length - 1 && input[i] == '\\') // 找到反斜杠
|
||||
{
|
||||
char nextChar = input[i + 1];
|
||||
|
||||
// 处理转义符
|
||||
switch (nextChar)
|
||||
{
|
||||
case 'r':
|
||||
output.Append('\r');
|
||||
i++; // 跳过 'r'
|
||||
break;
|
||||
case 'n':
|
||||
output.Append('\n');
|
||||
i++; // 跳过 'n'
|
||||
break;
|
||||
case 't':
|
||||
output.Append('\t');
|
||||
i++; // 跳过 't'
|
||||
break;
|
||||
case '\\': // 字面量反斜杠
|
||||
output.Append('\\');
|
||||
i++; // 跳过第二个 '\\'
|
||||
break;
|
||||
default:
|
||||
output.Append(input[i]); // 不是转义符,保留反斜杠
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Append(input[i]); // 其他字符直接添加
|
||||
}
|
||||
}
|
||||
Value = output.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
19
Serein.Script/Node/WhileNode.cs
Normal file
19
Serein.Script/Node/WhileNode.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Script.Node
|
||||
{
|
||||
/// <summary>
|
||||
/// 循环条件节点
|
||||
/// </summary>
|
||||
public class WhileNode : ASTNode
|
||||
{
|
||||
public ASTNode Condition { get; }
|
||||
public List<ASTNode> Body { get; }
|
||||
public WhileNode(ASTNode condition, List<ASTNode> body) => (Condition, Body) = (condition, body);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user