fix errors in interpreter. add a enumerator for tree. working on tree optimizer
This commit is contained in:
@@ -1,30 +1,27 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="AbstractSyntaxTree.cs"
|
||||
// file="AbstractSyntaxTree"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -34,18 +31,18 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Xml.Schema;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.Abstraction
|
||||
{
|
||||
public sealed class AbstractSyntaxTree<TNode, TType> where TNode: ISyntaxNode<TNode, TType> where TType: unmanaged
|
||||
public sealed class AbstractSyntaxTree<TNode, TType> where TNode : ISyntaxNode<TNode, TType>
|
||||
where TType : unmanaged
|
||||
{
|
||||
|
||||
private HashSet<TNode> _allToken = new();
|
||||
|
||||
private TNode _root;
|
||||
|
||||
public HashSet<TNode> AllNodes
|
||||
{
|
||||
get { return _allToken; }
|
||||
@@ -68,12 +65,16 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AddNode(TNode node)
|
||||
public bool AddNode(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return _allToken.Add(node);
|
||||
}
|
||||
|
||||
public bool Contains(TNode node)
|
||||
public bool Contains(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return _allToken.Contains(node);
|
||||
}
|
||||
@@ -87,23 +88,26 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
{
|
||||
|
||||
private int _state,_target; // 0: not started, 1: on right, 2: ended
|
||||
private Stack<TNode> _stack, _listStack;
|
||||
private Stack<Record> _recycle;
|
||||
private Stack<Record> _stack;
|
||||
private TNode _lRoot;
|
||||
private TNode _root;
|
||||
|
||||
public Enumerator(AbstractSyntaxTree<TNode, TType> tree)
|
||||
public Enumerator(
|
||||
AbstractSyntaxTree<TNode, TType> tree
|
||||
)
|
||||
{
|
||||
_root = tree._root;
|
||||
_root = tree.Root;
|
||||
_stack = [];
|
||||
_listStack = [];
|
||||
_recycle = [];
|
||||
}
|
||||
|
||||
public TNode Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_stack.TryPeek(out TNode? node)) {
|
||||
return node;
|
||||
if (_stack.TryPeek(out Record? r)) {
|
||||
return r.Node;
|
||||
}
|
||||
return TNode.Empty;
|
||||
}
|
||||
@@ -116,8 +120,48 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_stack.TryPop(out TNode? cur)) { }
|
||||
_stack.Push(_root);
|
||||
while (_stack.TryPeek(out Record? cur))
|
||||
{
|
||||
switch (cur.State)
|
||||
{
|
||||
case 0:
|
||||
cur.State++;
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Left))
|
||||
{
|
||||
goto case 1;
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Left));
|
||||
return true;
|
||||
}
|
||||
case 1:
|
||||
cur.State++;
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Right))
|
||||
{
|
||||
goto case 2;
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Right));
|
||||
return true;
|
||||
}
|
||||
case 2:
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Next))
|
||||
{
|
||||
_recycle.Push(_stack.Pop());
|
||||
if (_stack.Count == 0) {
|
||||
return false;
|
||||
}
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Next));
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
_stack.Push(GetNewRecord(_root));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -127,8 +171,33 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
_state = 0;
|
||||
}
|
||||
|
||||
private Record GetNewRecord(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
if (_recycle.TryPop(out Record? r))
|
||||
{
|
||||
r.State = 0;
|
||||
r.Node = node;
|
||||
return r;
|
||||
}
|
||||
return new Record(node, 0);
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
private class Record(
|
||||
TNode node,
|
||||
int state
|
||||
)
|
||||
{
|
||||
|
||||
public int State { get; set; } = state;
|
||||
|
||||
public TNode Node { get; set; } = node;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
public TNode PopOperand(
|
||||
)
|
||||
{
|
||||
if (OperatorInDepthCount > 0) {
|
||||
if (OperandInDepthCount > 0) {
|
||||
return _operands.Pop();
|
||||
}
|
||||
return TNode.Empty;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="ISyntaxNode"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -31,11 +31,13 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.RegularAnalysis.Abstraction
|
||||
{
|
||||
public interface ISyntaxNode<TNode, TLabel> where TNode: ISyntaxNode<TNode, TLabel> where TLabel: unmanaged
|
||||
public interface ISyntaxNode<TNode, TLabel> where TNode : ISyntaxNode<TNode, TLabel>
|
||||
where TLabel : unmanaged
|
||||
{
|
||||
|
||||
long NodeFlag { get; }
|
||||
@@ -60,9 +62,21 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
|
||||
protected internal int EnumState { get; set; }
|
||||
|
||||
void AppendNode(TNode nextToken);
|
||||
void AppendNode(
|
||||
TNode nextToken
|
||||
);
|
||||
|
||||
void PrependNode(TNode previousNode);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static virtual bool IsNullOrEmpty(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return node is null || node.Equals(TNode.Empty);
|
||||
}
|
||||
|
||||
void PrependNode(
|
||||
TNode previousNode
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user