|
|
@@ -1,91 +1,52 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
-using MoonSharp.Interpreter.Diagnostics;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
+using MoonSharp.Interpreter.Execution.Scopes;
|
|
|
|
|
|
namespace MoonSharp.Interpreter.Execution
|
|
|
{
|
|
|
public class BuildTimeScope
|
|
|
{
|
|
|
- List<BuildTimeScopeFrame> m_Locals = new List<BuildTimeScopeFrame>();
|
|
|
+ List<BuildTimeScopeFrame> m_Frames = new List<BuildTimeScopeFrame>();
|
|
|
List<IClosureBuilder> m_ClosureBuilders = new List<IClosureBuilder>();
|
|
|
|
|
|
public BuildTimeScope()
|
|
|
{
|
|
|
- PushFunction();
|
|
|
+ //PushFunction();
|
|
|
}
|
|
|
|
|
|
- public void EnterClosure(IClosureBuilder closureBuilder)
|
|
|
- {
|
|
|
- m_ClosureBuilders.Add(closureBuilder);
|
|
|
- closureBuilder.UpvalueCreationTag = (m_Locals.Count - 1);
|
|
|
- }
|
|
|
-
|
|
|
- public void LeaveClosure()
|
|
|
- {
|
|
|
- m_ClosureBuilders.RemoveAt(m_ClosureBuilders.Count - 1);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- int GetStartIndexForPush()
|
|
|
- {
|
|
|
- return m_Locals[m_Locals.Count - 1].MaxIndex;
|
|
|
- }
|
|
|
- int GetBaseIndexForPush()
|
|
|
+ public void PushFunction()
|
|
|
{
|
|
|
- return m_Locals[m_Locals.Count - 1].BaseIndex;
|
|
|
+ m_Frames.Add(new BuildTimeScopeFrame());
|
|
|
}
|
|
|
|
|
|
public void PushBlock()
|
|
|
{
|
|
|
- // Debug.WriteLine("PushBlock");
|
|
|
- m_Locals.Add(new BuildTimeScopeFrame(GetBaseIndexForPush(), GetStartIndexForPush(), false));
|
|
|
+ m_Frames.Last().PushBlock();
|
|
|
}
|
|
|
|
|
|
- public void PushFunction()
|
|
|
+ public RuntimeScopeBlock PopBlock()
|
|
|
{
|
|
|
- // Debug.WriteLine("PushFunction");
|
|
|
- m_Locals.Add(new BuildTimeScopeFrame(0, 0, true));
|
|
|
+ return m_Frames.Last().PopBlock();
|
|
|
}
|
|
|
|
|
|
- RuntimeScopeFrame GetRuntimeFrameFromBuildFrame(BuildTimeScopeFrame frame, bool local)
|
|
|
+ public RuntimeScopeFrame PopFunction()
|
|
|
{
|
|
|
- List<LRef> symbols = new List<LRef>();
|
|
|
- for (int i = frame.StartIndex; i < frame.MaxIndex; i++)
|
|
|
- {
|
|
|
- LRef s;
|
|
|
- if (local)
|
|
|
- s = LRef.Local(frame.FindRev(i - frame.BaseIndex), i - frame.BaseIndex);
|
|
|
- else
|
|
|
- s = LRef.Global(frame.FindRev(i - frame.BaseIndex));
|
|
|
-
|
|
|
- symbols.Add(s);
|
|
|
- }
|
|
|
-
|
|
|
- return new RuntimeScopeFrame(symbols, frame.MaxIndex - frame.StartIndex, frame.Breaking);
|
|
|
+ var last = m_Frames.Last();
|
|
|
+ last.ResolveLRefs();
|
|
|
+ m_Frames.RemoveAt(m_Frames.Count - 1);
|
|
|
+
|
|
|
+ return last.GetRuntimeFrameData();
|
|
|
}
|
|
|
|
|
|
- public RuntimeScopeFrame Pop()
|
|
|
- {
|
|
|
- BuildTimeScopeFrame frame = m_Locals[m_Locals.Count - 1];
|
|
|
- m_Locals.RemoveAt(m_Locals.Count - 1);
|
|
|
- // Debug.WriteLine(string.Format("Pop : {0}", frame.MaxIndex - frame.StartIndex));
|
|
|
-
|
|
|
- return GetRuntimeFrameFromBuildFrame(frame, true);
|
|
|
- }
|
|
|
|
|
|
public LRef Find(string name)
|
|
|
{
|
|
|
- for (int i = m_Locals.Count - 1; i >= 0; i--)
|
|
|
- {
|
|
|
- int idx = m_Locals[i].Find(name);
|
|
|
- if (idx >= 0)
|
|
|
- return LRef.Local(name, idx);
|
|
|
+ LRef local = m_Frames.Last().Find(name);
|
|
|
|
|
|
- if (m_Locals[i].Breaking)
|
|
|
- break;
|
|
|
- }
|
|
|
+ if (local != null)
|
|
|
+ return local;
|
|
|
|
|
|
IClosureBuilder closure = m_ClosureBuilders.LastOrDefault();
|
|
|
|
|
|
@@ -97,9 +58,9 @@ namespace MoonSharp.Interpreter.Execution
|
|
|
{
|
|
|
for (int i = closureLocalBlockIdx; i >= 0; i--)
|
|
|
{
|
|
|
- int idx = m_Locals[i].Find(name);
|
|
|
- if (idx >= 0)
|
|
|
- return closure.CreateUpvalue(this, LRef.Local(name, idx));
|
|
|
+ LRef symb = m_Frames[i].Find(name);
|
|
|
+ if (symb != null)
|
|
|
+ return closure.CreateUpvalue(this, symb);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -109,20 +70,26 @@ namespace MoonSharp.Interpreter.Execution
|
|
|
|
|
|
public LRef DefineLocal(string name)
|
|
|
{
|
|
|
- return LRef.Local(name, m_Locals[m_Locals.Count - 1].Define(name));
|
|
|
+ return m_Frames.Last().DefineLocal(name);
|
|
|
}
|
|
|
|
|
|
public LRef TryDefineLocal(string name)
|
|
|
{
|
|
|
- int idx = m_Locals[m_Locals.Count - 1].Find(name);
|
|
|
+ return m_Frames.Last().TryDefineLocal(name);
|
|
|
+ }
|
|
|
|
|
|
- if (idx >= 0)
|
|
|
- return LRef.Local(name, idx);
|
|
|
|
|
|
- var s = LRef.Local(name, m_Locals[m_Locals.Count - 1].Define(name));
|
|
|
- // Debug.WriteLine(string.Format("Define local : {0}", s));
|
|
|
- return s;
|
|
|
+ public void EnterClosure(IClosureBuilder closureBuilder)
|
|
|
+ {
|
|
|
+ m_ClosureBuilders.Add(closureBuilder);
|
|
|
+ closureBuilder.UpvalueCreationTag = (m_Frames.Count - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void LeaveClosure()
|
|
|
+ {
|
|
|
+ m_ClosureBuilders.RemoveAt(m_ClosureBuilders.Count - 1);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|
|
|
}
|