ソースを参照

table tests ok

Xanathar 11 年 前
コミット
7a8d2d5d87
25 ファイル変更524 行追加98 行削除
  1. 3 3
      src/MoonSharp.Interpreter.Tests/ClosureTests.cs
  2. 2 2
      src/MoonSharp.Interpreter.Tests/SimpleTests.cs
  3. 25 7
      src/MoonSharp.Interpreter.Tests/TableTests.cs
  4. 82 0
      src/MoonSharp.Interpreter/DataStructs/FastStack.cs
  5. 98 0
      src/MoonSharp.Interpreter/DataStructs/Slice.cs
  6. 1 0
      src/MoonSharp.Interpreter/Execution/DataTypes/DataType.cs
  7. 7 0
      src/MoonSharp.Interpreter/Execution/DataTypes/RValue.cs
  8. 9 0
      src/MoonSharp.Interpreter/Execution/Scopes/SymbolRef.cs
  9. 2 1
      src/MoonSharp.Interpreter/Execution/Scopes/SymbolRefType.cs
  10. 25 11
      src/MoonSharp.Interpreter/Execution/VM/Chunk.cs
  11. 4 3
      src/MoonSharp.Interpreter/Execution/VM/Instruction.cs
  12. 12 5
      src/MoonSharp.Interpreter/Execution/VM/OpCode.cs
  13. 179 52
      src/MoonSharp.Interpreter/Execution/VM/VmExecutor.cs
  14. 1 1
      src/MoonSharp.Interpreter/Helpers/List_ExtensionMethods.cs
  15. 3 1
      src/MoonSharp.Interpreter/MoonSharp.Interpreter.csproj
  16. 12 2
      src/MoonSharp.Interpreter/Tree/Expressions/IndexExpression.cs
  17. 1 1
      src/MoonSharp.Interpreter/Tree/Expressions/OperatorExpression.cs
  18. 23 1
      src/MoonSharp.Interpreter/Tree/Expressions/TableConstructor.cs
  19. 13 3
      src/MoonSharp.Interpreter/Tree/FunctionCall.cs
  20. 0 1
      src/MoonSharp.Interpreter/Tree/NodeFactory.cs
  21. 15 1
      src/MoonSharp.Interpreter/Tree/Statements/FunctionDefinitionStatement.cs
  22. 2 1
      src/MoonSharp/Program.cs
  23. 1 1
      src/MoonSharpTests/Program.cs
  24. 1 1
      src/PerformanceComparison/Program.cs
  25. 3 0
      src/moonsharp.sln

+ 3 - 3
src/MoonSharp.Interpreter.Tests/ClosureTests.cs

@@ -7,7 +7,7 @@ namespace MoonSharp.Interpreter.Tests
 	[TestFixture]
 	[TestFixture]
 	public class ClosureTests
 	public class ClosureTests
 	{
 	{
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void Closures()
 		public void Closures()
 		{
 		{
 			// expected : 201 2001 20001 200001 2000001
 			// expected : 201 2001 20001 200001 2000001
@@ -47,7 +47,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(2000001, res.Tuple[4].Number);
 			Assert.AreEqual(2000001, res.Tuple[4].Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void ClosuresNonAnonymousLocal()
 		public void ClosuresNonAnonymousLocal()
 		{
 		{
 			// expected : 201 2001 20001 200001 2000001
 			// expected : 201 2001 20001 200001 2000001
@@ -88,7 +88,7 @@ namespace MoonSharp.Interpreter.Tests
 		}
 		}
 
 
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void ClosuresNonAnonymous()
 		public void ClosuresNonAnonymous()
 		{
 		{
 			// expected : 201 2001 20001 200001 2000001
 			// expected : 201 2001 20001 200001 2000001

+ 2 - 2
src/MoonSharp.Interpreter.Tests/SimpleTests.cs

@@ -278,7 +278,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(42, res.Tuple[1].Number);
 			Assert.AreEqual(42, res.Tuple[1].Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void LengthOperator()
 		public void LengthOperator()
 		{
 		{
 			string script = @"    
 			string script = @"    
@@ -539,7 +539,7 @@ namespace MoonSharp.Interpreter.Tests
 
 
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void FunctionWithTableArg()
 		public void FunctionWithTableArg()
 		{
 		{
 			string script = @"    
 			string script = @"    

+ 25 - 7
src/MoonSharp.Interpreter.Tests/TableTests.cs

@@ -10,7 +10,25 @@ namespace MoonSharp.Interpreter.Tests
 	[TestFixture]
 	[TestFixture]
 	public class TableTests
 	public class TableTests
 	{
 	{
-		[Test][Ignore("VM Transition")]
+		[Test]
+		public void TableAccessAndEmptyCtor()
+		{
+			string script = @"
+						a = { }
+						
+						a[1] = 1;
+
+						return a[1]";
+
+			RValue res = MoonSharpInterpreter.LoadFromString(script, null).Execute();
+
+			Assert.AreEqual(DataType.Number, res.Type);
+			Assert.AreEqual(1, res.Number);
+		}
+
+
+
+		[Test]
 		public void TableAccessAndCtor()
 		public void TableAccessAndCtor()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -42,7 +60,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(7, res.Tuple[6].Number);
 			Assert.AreEqual(7, res.Tuple[6].Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod1()
 		public void TableMethod1()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -67,7 +85,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(1994, res.Number);
 			Assert.AreEqual(1994, res.Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod2()
 		public void TableMethod2()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -92,7 +110,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(1994, res.Number);
 			Assert.AreEqual(1994, res.Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod3()
 		public void TableMethod3()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -118,7 +136,7 @@ namespace MoonSharp.Interpreter.Tests
 		}
 		}
 
 
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod4()
 		public void TableMethod4()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -143,7 +161,7 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(1994, res.Number);
 			Assert.AreEqual(1994, res.Number);
 		}
 		}
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod5()
 		public void TableMethod5()
 		{
 		{
 			string script = @"
 			string script = @"
@@ -172,7 +190,7 @@ namespace MoonSharp.Interpreter.Tests
 		}
 		}
 
 
 
 
-		[Test][Ignore("VM Transition")]
+		[Test]
 		public void TableMethod6()
 		public void TableMethod6()
 		{
 		{
 			string script = @"
 			string script = @"

+ 82 - 0
src/MoonSharp.Interpreter/DataStructs/FastStack.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MoonSharp.Interpreter.Execution.VM
+{
+	public class FastStack<T> 
+	{
+		T[] m_Storage;
+		int m_HeadIdx = 0;
+
+		public FastStack(int maxCapacity)
+		{
+			m_Storage = new T[maxCapacity];
+		}
+
+		public T this[int index]
+		{
+			get { return m_Storage[index]; }
+			set { m_Storage[index] = value; }
+		}
+
+		public T Push(T item)
+		{
+			m_Storage[m_HeadIdx++] = item;
+			return item;
+		}
+
+		public void Zero(int from, int to)
+		{
+			Array.Clear(m_Storage, from, to - from + 1);
+		}
+
+		public void Zero(int index)
+		{
+			m_Storage[index] = default(T);
+		}
+
+		public T Peek(int idxofs = 0)
+		{
+			T item = m_Storage[m_HeadIdx - 1 - idxofs];
+			return item;
+		}
+
+		public void RemoveLast( int cnt = 1)
+		{
+			if (cnt == 1)
+			{
+				--m_HeadIdx;
+				m_Storage[m_HeadIdx] = default(T);
+			}
+			else
+			{
+				int oldhead = m_HeadIdx;
+				m_HeadIdx -= cnt;
+				Zero(m_HeadIdx + 1, oldhead);
+			}
+		}
+
+		public T Pop()
+		{
+			--m_HeadIdx;
+			T retval = m_Storage[m_HeadIdx];
+			m_Storage[m_HeadIdx] = default(T);
+			return retval;
+		}
+
+		public void Clear()
+		{
+			Array.Clear(m_Storage, 0, m_Storage.Length);
+			m_HeadIdx = 0;
+		}
+
+		public int Count
+		{
+			get { return m_HeadIdx; }
+		}
+
+
+	}
+}

+ 98 - 0
src/MoonSharp.Interpreter/DataStructs/Slice.cs

@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MoonSharp.Interpreter.DataStructs
+{
+	public class Slice<T> : IEnumerable<T>
+	{
+		IList<T> m_SourceList;
+		int m_From, m_Length;
+		bool m_Reversed;
+
+		public Slice(IList<T> list, int from, int length, bool reversed)
+		{
+			m_SourceList = list;
+			m_From = from;
+			m_Length = length;
+			m_Reversed = reversed;
+		}
+
+		public T this[int index]
+		{
+			get 
+			{
+				return m_SourceList[CalcRealIndex(index)];
+			}
+			set
+			{
+				m_SourceList[CalcRealIndex(index)] = value;
+			}
+		}
+
+		public int From
+		{
+			get { return m_From; }
+		}
+
+		public int Count
+		{
+			get { return m_Length; }
+		}
+
+		public bool Reversed
+		{
+			get { return m_Reversed; }
+		}
+
+		private int CalcRealIndex(int index)
+		{
+			if (index < 0 || index >= m_Length)
+				throw new ArgumentOutOfRangeException("index");
+
+			if (m_Reversed)
+			{
+				return m_From + m_Length - index - 1;
+			}
+			else
+			{
+				return m_From + index;
+			}
+		}
+
+		public IEnumerator<T> GetEnumerator()
+		{
+			for (int i = 0; i < m_Length; i++)
+				yield return m_SourceList[CalcRealIndex(i)];
+		}
+
+		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+		{
+			for (int i = 0; i < m_Length; i++)
+				yield return m_SourceList[CalcRealIndex(i)];
+		}
+
+		public T[] ToArray()
+		{
+			T[] array = new T[m_Length];
+
+			for (int i = 0; i < m_Length; i++)
+				array[i] = m_SourceList[CalcRealIndex(i)];
+
+			return array;
+		}
+
+		public List<T> ToList()
+		{
+			List<T> list = new List<T>(m_Length);
+
+			for (int i = 0; i < m_Length; i++)
+				list.Add(m_SourceList[CalcRealIndex(i)]);
+
+			return list;
+		}
+
+
+	}
+}

+ 1 - 0
src/MoonSharp.Interpreter/Execution/DataTypes/DataType.cs

@@ -17,6 +17,7 @@ namespace MoonSharp.Interpreter.Execution
 		Tuple,
 		Tuple,
 
 
 		Symbol,
 		Symbol,
+		TableRef,
 		ClrFunction,
 		ClrFunction,
 
 
 		UNSUPPORTED_UserData,
 		UNSUPPORTED_UserData,

+ 7 - 0
src/MoonSharp.Interpreter/Execution/DataTypes/RValue.cs

@@ -48,6 +48,7 @@ namespace MoonSharp.Interpreter.Execution
 			this.Type = DataType.Symbol;
 			this.Type = DataType.Symbol;
 		}
 		}
 
 
+
 		public RValue(string str)
 		public RValue(string str)
 		{
 		{
 			Assign(str);
 			Assign(str);
@@ -150,6 +151,9 @@ namespace MoonSharp.Interpreter.Execution
 
 
 		public RValue Clone()
 		public RValue Clone()
 		{
 		{
+			if (this.Type == DataType.Symbol)
+				throw new ArgumentException("Can't clone Symbol or TableRef values");
+
 			RValue v = new RValue();
 			RValue v = new RValue();
 			v.Boolean = this.Boolean;
 			v.Boolean = this.Boolean;
 			v.Callback = this.Callback;
 			v.Callback = this.Callback;
@@ -166,6 +170,9 @@ namespace MoonSharp.Interpreter.Execution
 
 
 		public RValue CloneAsWritable()
 		public RValue CloneAsWritable()
 		{
 		{
+			if (this.Type == DataType.Symbol || this.Type == DataType.TableRef)
+				throw new ArgumentException("Can't clone Symbol or TableRef values");
+
 			RValue v = new RValue();
 			RValue v = new RValue();
 			v.Boolean = this.Boolean;
 			v.Boolean = this.Boolean;
 			v.Function = this.Function;
 			v.Function = this.Function;

+ 9 - 0
src/MoonSharp.Interpreter/Execution/Scopes/SymbolRef.cs

@@ -14,6 +14,10 @@ namespace MoonSharp.Interpreter.Execution
 
 
 		public string Name { get; private set; }
 		public string Name { get; private set; }
 
 
+		public RValue TableRefObject { get; internal set; }
+		public RValue TableRefIndex { get; internal set; }
+
+
 		public static SymbolRef Global(string name, int index)
 		public static SymbolRef Global(string name, int index)
 		{
 		{
 			return new SymbolRef() { Index = index, Type = SymbolRefType.Global, Name = name };
 			return new SymbolRef() { Index = index, Type = SymbolRefType.Global, Name = name };
@@ -34,6 +38,11 @@ namespace MoonSharp.Interpreter.Execution
 			return new SymbolRef() { Index = -1, Type = SymbolRefType.Invalid, Name = "!INV!" };
 			return new SymbolRef() { Index = -1, Type = SymbolRefType.Invalid, Name = "!INV!" };
 		}
 		}
 
 
+		public static SymbolRef ObjIndex(RValue baseObject, RValue indexObject)
+		{
+			return new SymbolRef() { TableRefObject = baseObject, TableRefIndex = indexObject, Type = SymbolRefType.Index };
+		}
+
 		public bool IsValid()
 		public bool IsValid()
 		{
 		{
 			return Index >= 0 && Type !=  SymbolRefType.Invalid;
 			return Index >= 0 && Type !=  SymbolRefType.Invalid;

+ 2 - 1
src/MoonSharp.Interpreter/Execution/Scopes/SymbolRefType.cs

@@ -10,6 +10,7 @@ namespace MoonSharp.Interpreter.Execution
 		Invalid,
 		Invalid,
 		Global,
 		Global,
 		Local,
 		Local,
-		Upvalue
+		Upvalue,
+		Index
 	}
 	}
 }
 }

+ 25 - 11
src/MoonSharp.Interpreter/Execution/VM/Chunk.cs

@@ -76,7 +76,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			return Emit(new Instruction() { OpCode = OpCode.Pop, NumVal = num });
 			return Emit(new Instruction() { OpCode = OpCode.Pop, NumVal = num });
 		}
 		}
 
 
-		public Instruction Invoke(int argCount)
+		public Instruction Call(int argCount)
 		{
 		{
 			return Emit(new Instruction() { OpCode = OpCode.Call, NumVal = argCount });
 			return Emit(new Instruction() { OpCode = OpCode.Call, NumVal = argCount });
 		}
 		}
@@ -91,11 +91,6 @@ namespace MoonSharp.Interpreter.Execution.VM
 			return Emit(new Instruction() { OpCode = OpCode.Literal, Value = value });
 			return Emit(new Instruction() { OpCode = OpCode.Literal, Value = value });
 		}
 		}
 
 
-		public Instruction TableInvoke(int argCount, string name)
-		{
-			return Emit(new Instruction() { OpCode = OpCode.Method, Name = name, NumVal = argCount });
-		}
-
 		public Instruction Assign(int cntL, int cntR)
 		public Instruction Assign(int cntL, int cntR)
 		{
 		{
 			return Emit(new Instruction() { OpCode = OpCode.Assign, NumVal = cntL, NumVal2 = cntR });
 			return Emit(new Instruction() { OpCode = OpCode.Assign, NumVal = cntL, NumVal2 = cntR });
@@ -126,11 +121,6 @@ namespace MoonSharp.Interpreter.Execution.VM
 			return Emit(new Instruction() { OpCode = opcode });
 			return Emit(new Instruction() { OpCode = opcode });
 		}
 		}
 
 
-		public Instruction Reduce()
-		{
-			return Emit(new Instruction() { OpCode = OpCode.Reduce });
-		}
-
 		public Instruction Bool()
 		public Instruction Bool()
 		{
 		{
 			return Emit(new Instruction() { OpCode = OpCode.Bool });
 			return Emit(new Instruction() { OpCode = OpCode.Bool });
@@ -195,5 +185,29 @@ namespace MoonSharp.Interpreter.Execution.VM
 		{
 		{
 			return Emit(new Instruction() { OpCode = OpCode.Incr, NumVal = i });
 			return Emit(new Instruction() { OpCode = OpCode.Incr, NumVal = i });
 		}
 		}
+
+		public Instruction IndexGet()
+		{
+			return Emit(new Instruction() { OpCode = OpCode.IndexGet });
+		}
+
+		public Instruction IndexSet()
+		{
+			return Emit(new Instruction() { OpCode = OpCode.IndexSet });
+		}
+		public Instruction IndexSetN()
+		{
+			return Emit(new Instruction() { OpCode = OpCode.IndexSetN });
+		}
+
+		public Instruction NewTable()
+		{
+			return Emit(new Instruction() { OpCode = OpCode.NewTable });
+		}
+
+		public Instruction TempOp(OpCode opCode, int regNum)
+		{
+			return Emit(new Instruction() { OpCode = opCode, NumVal = regNum });
+		}
 	}
 	}
 }
 }

+ 4 - 3
src/MoonSharp.Interpreter/Execution/VM/Instruction.cs

@@ -40,6 +40,10 @@ namespace MoonSharp.Interpreter.Execution.VM
 				case OpCode.MkTuple:
 				case OpCode.MkTuple:
 				case OpCode.Incr:
 				case OpCode.Incr:
 				case OpCode.Pop:
 				case OpCode.Pop:
+				case OpCode.TmpClear:
+				case OpCode.TmpPush:
+				case OpCode.TmpPop:
+				case OpCode.TmpPeek:
 					append = string.Format("{0}{1}", GenSpaces(), NumVal);
 					append = string.Format("{0}{1}", GenSpaces(), NumVal);
 					break;
 					break;
 				case OpCode.JtOrPop:
 				case OpCode.JtOrPop:
@@ -49,9 +53,6 @@ namespace MoonSharp.Interpreter.Execution.VM
 				case OpCode.JFor:
 				case OpCode.JFor:
 					append = string.Format("{0}{1:X8}", GenSpaces(), NumVal);
 					append = string.Format("{0}{1:X8}", GenSpaces(), NumVal);
 					break;
 					break;
-				case OpCode.Method:
-					append = string.Format("{0}{1}:{2}", GenSpaces(), NumVal, Name ?? "(null)");
-					break;
 				case OpCode.Invalid:
 				case OpCode.Invalid:
 					append = string.Format("{0}{1}", GenSpaces(), Name ?? "(null)");
 					append = string.Format("{0}{1}", GenSpaces(), Name ?? "(null)");
 					break;
 					break;

+ 12 - 5
src/MoonSharp.Interpreter/Execution/VM/OpCode.cs

@@ -14,7 +14,6 @@ namespace MoonSharp.Interpreter.Execution.VM
 		Load,		// Loads a value from the specified symbol, and pushes it on the v-stack.
 		Load,		// Loads a value from the specified symbol, and pushes it on the v-stack.
 		Call,		// Calls the function specified on the specified element from the top of the v-stack. If the function is a Moon# function, it pushes its numeric value on the v-stack, then pushes the current PC onto the x-stack, enters the function closure and jumps to the function first instruction. If the function is a CLR function, it pops the function value from the v-stack, then invokes the function synchronously and finally pushes the result on the v-stack.
 		Call,		// Calls the function specified on the specified element from the top of the v-stack. If the function is a Moon# function, it pushes its numeric value on the v-stack, then pushes the current PC onto the x-stack, enters the function closure and jumps to the function first instruction. If the function is a CLR function, it pops the function value from the v-stack, then invokes the function synchronously and finally pushes the result on the v-stack.
 		Literal,	// Pushes a literal (constant value) on the stack. 
 		Literal,	// Pushes a literal (constant value) on the stack. 
-		Store,		// Performs a single value assignment
 		Symbol,		// Loads a symbol on the stack
 		Symbol,		// Loads a symbol on the stack
 		MkTuple,	// Creates a tuple from the topmost n values
 		MkTuple,	// Creates a tuple from the topmost n values
 		JtOrPop,	// Peeks at the topmost value of the v-stack as a boolean. If true, it performs a jump, otherwise it removes the topmost value from the v-stack.
 		JtOrPop,	// Peeks at the topmost value of the v-stack as a boolean. If true, it performs a jump, otherwise it removes the topmost value from the v-stack.
@@ -29,7 +28,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 		Div,		// Division of the two topmost operands on the v-stack
 		Div,		// Division of the two topmost operands on the v-stack
 		Mod,		// Modulus of the two topmost operands on the v-stack
 		Mod,		// Modulus of the two topmost operands on the v-stack
 		Not,		// Logical inversion of the topmost operand on the v-stack
 		Not,		// Logical inversion of the topmost operand on the v-stack
-		Size,		// Size operator of the topmost operand on the v-stack
+		Len,		// Size operator of the topmost operand on the v-stack
 		Neg,		// Negation (unary minus) operator of the topmost operand on the v-stack
 		Neg,		// Negation (unary minus) operator of the topmost operand on the v-stack
 		Power,		// Power of the two topmost operands on the v-stack
 		Power,		// Power of the two topmost operands on the v-stack
 		Bool,		// Converts the top of the v-stack to a boolean
 		Bool,		// Converts the top of the v-stack to a boolean
@@ -48,9 +47,17 @@ namespace MoonSharp.Interpreter.Execution.VM
 		JFor,		// Peeks at the top, top-1 and top-2 values of the v-stack which it assumes to be numbers. Then if top-1 is less than zero, checks if top is <= top-2, otherwise it checks that top is >= top-2. Then if the condition is false, it jumps.
 		JFor,		// Peeks at the top, top-1 and top-2 values of the v-stack which it assumes to be numbers. Then if top-1 is less than zero, checks if top is <= top-2, otherwise it checks that top is >= top-2. Then if the condition is false, it jumps.
 		ToNum,		// Converts the top of the stack to a number
 		ToNum,		// Converts the top of the stack to a number
 		NSymStor,	// Performs a store to a symbol, without needing the symbol on the v-stack and without extracting the operand from the v-stack.
 		NSymStor,	// Performs a store to a symbol, without needing the symbol on the v-stack and without extracting the operand from the v-stack.
+		Store,		// Performs a single value assignment [including table fields]
+		Assign,		// Performs complex assignment supporting tuples [including table fields]
+		IndexGet,	// Performs an index operation, pushing the indexed value on the stack.
+		IndexSet,	// Performs an index operation, pushing a writable indexded value on the stack.
+		IndexSetN,	// Performs an index operation, pushing a writable indexed value on the stack, does not pop the table.
+		NewTable,	// Creates a new table on the stack
+
+		TmpPeek,	// Peeks the top of the stack in a temporary reg. 
+		TmpPush,	// Pushes a temporary reg on the top of the stack. 
+		TmpPop,		// Pops the top of the stack in a temporary reg. 
+		TmpClear,	// Clears a temporary reg
 
 
-		Method,		// NOT IMPLEMENTED YET
-		Assign,		// NOT IMPLEMENTED YET
-		Reduce,		// NOT IMPLEMENTED YET
 	}
 	}
 }
 }

+ 179 - 52
src/MoonSharp.Interpreter/Execution/VM/VmExecutor.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
+using MoonSharp.Interpreter.DataStructs;
 
 
 namespace MoonSharp.Interpreter.Execution.VM
 namespace MoonSharp.Interpreter.Execution.VM
 {
 {
@@ -9,17 +10,21 @@ namespace MoonSharp.Interpreter.Execution.VM
 	{
 	{
 		Chunk m_RootChunk;
 		Chunk m_RootChunk;
 		Chunk m_CurChunk;
 		Chunk m_CurChunk;
-		int m_ProgramCounter;
+		int m_InstructionPtr;
 
 
 		List<RValue> m_ValueStack = new List<RValue>();
 		List<RValue> m_ValueStack = new List<RValue>();
 		List<int> m_ExecutionStack = new List<int>();
 		List<int> m_ExecutionStack = new List<int>();
+		bool m_Terminate = false;
 
 
 		RuntimeScope m_Scope;
 		RuntimeScope m_Scope;
 
 
+		RValue[] m_TempRegs = new RValue[8];
+
+
 		public VmExecutor(Chunk rootChunk, RuntimeScope scope)
 		public VmExecutor(Chunk rootChunk, RuntimeScope scope)
 		{
 		{
 			m_RootChunk = m_CurChunk = rootChunk;
 			m_RootChunk = m_CurChunk = rootChunk;
-			m_ProgramCounter = 0;
+			m_InstructionPtr = 0;
 			m_Scope = scope;
 			m_Scope = scope;
 		}
 		}
 
 
@@ -72,7 +77,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			int cnt = 6;
 			int cnt = 6;
 			List<RValue> values = new List<RValue>();
 			List<RValue> values = new List<RValue>();
 
 
-			for(int i = m_ValueStack.Count - 1; i >= 0 && cnt > 0; i--)
+			for (int i = m_ValueStack.Count - 1; i >= 0 && cnt > 0; i--)
 			{
 			{
 				values.Add(m_ValueStack[i]);
 				values.Add(m_ValueStack[i]);
 				cnt--;
 				cnt--;
@@ -85,9 +90,9 @@ namespace MoonSharp.Interpreter.Execution.VM
 		bool m_StepEnabled = true;
 		bool m_StepEnabled = true;
 		public RValue Execute()
 		public RValue Execute()
 		{
 		{
-			while (m_ProgramCounter < m_CurChunk.Code.Count)
+			while (m_InstructionPtr < m_CurChunk.Code.Count && !m_Terminate)
 			{
 			{
-				Instruction i = m_CurChunk.Code[m_ProgramCounter];
+				Instruction i = m_CurChunk.Code[m_InstructionPtr];
 
 
 
 
 				//if (m_DoDebug)
 				//if (m_DoDebug)
@@ -102,7 +107,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 				//		m_StepEnabled = false;
 				//		m_StepEnabled = false;
 				//}
 				//}
 
 
-				++m_ProgramCounter;
+				++m_InstructionPtr;
 
 
 				switch (i.OpCode)
 				switch (i.OpCode)
 				{
 				{
@@ -121,32 +126,29 @@ namespace MoonSharp.Interpreter.Execution.VM
 					case OpCode.Bool:
 					case OpCode.Bool:
 						Bool(i);
 						Bool(i);
 						break;
 						break;
-					case OpCode.Reduce:
-						Reduce(i);
-						break;
 					case OpCode.Add:
 					case OpCode.Add:
-						Add(i);
+						ExecAdd(i);
 						break;
 						break;
 					case OpCode.Neg:
 					case OpCode.Neg:
-						Neg(i);
+						ExecNeg(i);
 						break;
 						break;
 					case OpCode.Sub:
 					case OpCode.Sub:
-						Sub(i);
+						ExecSub(i);
 						break;
 						break;
 					case OpCode.Mul:
 					case OpCode.Mul:
-						Mul(i);
+						ExecMul(i);
 						break;
 						break;
 					case OpCode.Div:
 					case OpCode.Div:
-						Div(i);
+						ExecDiv(i);
 						break;
 						break;
 					case OpCode.Power:
 					case OpCode.Power:
-						Power(i);
+						ExecPower(i);
 						break;
 						break;
 					case OpCode.Eq:
 					case OpCode.Eq:
-						Eq(i);
+						ExecEq(i);
 						break;
 						break;
 					case OpCode.LessEq:
 					case OpCode.LessEq:
-						LessEq(i);
+						ExecLessEq(i);
 						break;
 						break;
 					case OpCode.Call:
 					case OpCode.Call:
 						ExecCall(i);
 						ExecCall(i);
@@ -167,8 +169,11 @@ namespace MoonSharp.Interpreter.Execution.VM
 					case OpCode.Symbol:
 					case OpCode.Symbol:
 						m_ValueStack.Push(new RValue(i.Symbol));
 						m_ValueStack.Push(new RValue(i.Symbol));
 						break;
 						break;
+					case OpCode.Assign:
+						ExecAssign(i);
+						break;
 					case OpCode.Jump:
 					case OpCode.Jump:
-						m_ProgramCounter = i.NumVal;
+						m_InstructionPtr = i.NumVal;
 						break;
 						break;
 					case OpCode.MkTuple:
 					case OpCode.MkTuple:
 						m_ValueStack.Push(RValue.FromPotentiallyNestedTuple(StackTopToArrayReverse(i.NumVal, true)));
 						m_ValueStack.Push(RValue.FromPotentiallyNestedTuple(StackTopToArrayReverse(i.NumVal, true)));
@@ -206,6 +211,33 @@ namespace MoonSharp.Interpreter.Execution.VM
 					case OpCode.JFor:
 					case OpCode.JFor:
 						ExecJFor(i);
 						ExecJFor(i);
 						break;
 						break;
+					case OpCode.IndexGet:
+						ExecIndexGet(i);
+						break;
+					case OpCode.IndexSet:
+						ExecIndexSet(i, false);
+						break;
+					case OpCode.IndexSetN:
+						ExecIndexSet(i, true);
+						break;
+					case OpCode.NewTable:
+						m_ValueStack.Push(new RValue(new Table()));
+						break;
+					case OpCode.TmpClear:
+						m_TempRegs[i.NumVal] = null;
+						break;
+					case OpCode.TmpPeek:
+						m_TempRegs[i.NumVal] = m_ValueStack.Peek();
+						break;
+					case OpCode.TmpPop:
+						m_TempRegs[i.NumVal] = m_ValueStack.Pop();
+						break;
+					case OpCode.Len:
+						ExecLen(i);
+						break;
+					case OpCode.TmpPush:
+						m_ValueStack.Push(m_TempRegs[i.NumVal]);
+						break;
 					case OpCode.Invalid:
 					case OpCode.Invalid:
 						throw new NotImplementedException(string.Format("Compilation for {0} not implented yet!", i.Name));
 						throw new NotImplementedException(string.Format("Compilation for {0} not implented yet!", i.Name));
 					default:
 					default:
@@ -223,6 +255,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 		}
 		}
 
 
 
 
+
 		private void ExecExit(Instruction i)
 		private void ExecExit(Instruction i)
 		{
 		{
 			if (i.Frame == null)
 			if (i.Frame == null)
@@ -242,7 +275,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			if (i.OpCode == OpCode.Debug)
 			if (i.OpCode == OpCode.Debug)
 				Console.Write("    {0}", i);
 				Console.Write("    {0}", i);
 			else
 			else
-				Console.Write("{0:X8}  {1}", m_ProgramCounter, i);
+				Console.Write("{0:X8}  {1}", m_InstructionPtr, i);
 
 
 			Console.SetCursorPosition(40, Console.CursorTop);
 			Console.SetCursorPosition(40, Console.CursorTop);
 			Console.WriteLine("|| VS={1:X4}  XS={2:X4} || {0}", DumpValueStack(), m_ValueStack.Count, m_ExecutionStack.Count);
 			Console.WriteLine("|| VS={1:X4}  XS={2:X4} || {0}", DumpValueStack(), m_ValueStack.Count, m_ExecutionStack.Count);
@@ -260,13 +293,10 @@ namespace MoonSharp.Interpreter.Execution.VM
 			bool whileCond = (step > 0) ? val <= stop : val >= stop;
 			bool whileCond = (step > 0) ? val <= stop : val >= stop;
 
 
 			if (!whileCond)
 			if (!whileCond)
-				m_ProgramCounter = i.NumVal;
+				m_InstructionPtr = i.NumVal;
 		}
 		}
 
 
-		private void ExecNSymStor(Instruction i)
-		{
-			m_Scope.Assign(i.Symbol, m_ValueStack.Peek());
-		}
+
 
 
 		private void ExecIncr(Instruction i)
 		private void ExecIncr(Instruction i)
 		{
 		{
@@ -291,17 +321,20 @@ namespace MoonSharp.Interpreter.Execution.VM
 			m_ValueStack.Push(new RValue(!(v.TestAsBoolean())));
 			m_ValueStack.Push(new RValue(!(v.TestAsBoolean())));
 		}
 		}
 
 
-		
+
 		private void ExecRet(Instruction i)
 		private void ExecRet(Instruction i)
 		{
 		{
 			if (m_ExecutionStack.Count == 0)
 			if (m_ExecutionStack.Count == 0)
+			{
+				m_Terminate = true;
 				return;
 				return;
+			}
 
 
 			if (i.NumVal == 0)
 			if (i.NumVal == 0)
 			{
 			{
 				var argscnt = (int)(m_ValueStack.Pop().Number);
 				var argscnt = (int)(m_ValueStack.Pop().Number);
 				m_ValueStack.RemoveLast(argscnt + 1);
 				m_ValueStack.RemoveLast(argscnt + 1);
-				m_ProgramCounter = m_ExecutionStack.Pop();
+				m_InstructionPtr = m_ExecutionStack.Pop();
 				m_ValueStack.Push(RValue.Nil);
 				m_ValueStack.Push(RValue.Nil);
 			}
 			}
 			else if (i.NumVal == 1)
 			else if (i.NumVal == 1)
@@ -310,7 +343,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 				var argscnt = (int)(m_ValueStack.Pop().Number);
 				var argscnt = (int)(m_ValueStack.Pop().Number);
 				m_ValueStack.RemoveLast(argscnt + 1);
 				m_ValueStack.RemoveLast(argscnt + 1);
 				m_ValueStack.Push(retval);
 				m_ValueStack.Push(retval);
-				m_ProgramCounter = m_ExecutionStack.Pop();
+				m_InstructionPtr = m_ExecutionStack.Pop();
 			}
 			}
 			else
 			else
 			{
 			{
@@ -340,8 +373,8 @@ namespace MoonSharp.Interpreter.Execution.VM
 			else if (fn.Type == DataType.Function)
 			else if (fn.Type == DataType.Function)
 			{
 			{
 				m_ValueStack.Push(new RValue(i.NumVal));
 				m_ValueStack.Push(new RValue(i.NumVal));
-				m_ExecutionStack.Push(m_ProgramCounter);
-				m_ProgramCounter = fn.Function.ByteCodeLocation;
+				m_ExecutionStack.Push(m_InstructionPtr);
+				m_InstructionPtr = fn.Function.ByteCodeLocation;
 				fn.Function.EnterClosureBeforeCall(m_Scope);
 				fn.Function.EnterClosureBeforeCall(m_Scope);
 			}
 			}
 			else
 			else
@@ -358,7 +391,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			RValue op = m_ValueStack.Pop();
 			RValue op = m_ValueStack.Pop();
 
 
 			if (op.TestAsBoolean() == expectedValueForJump)
 			if (op.TestAsBoolean() == expectedValueForJump)
-				m_ProgramCounter = i.NumVal;
+				m_InstructionPtr = i.NumVal;
 		}
 		}
 
 
 		private void ExecShortCircuitingOperator(Instruction i)
 		private void ExecShortCircuitingOperator(Instruction i)
@@ -368,7 +401,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			RValue op = m_ValueStack.Peek();
 			RValue op = m_ValueStack.Peek();
 
 
 			if (op.TestAsBoolean() == expectedValToShortCircuit)
 			if (op.TestAsBoolean() == expectedValToShortCircuit)
-				m_ProgramCounter = i.NumVal;
+				m_InstructionPtr = i.NumVal;
 			else
 			else
 				m_ValueStack.Pop();
 				m_ValueStack.Pop();
 		}
 		}
@@ -383,17 +416,18 @@ namespace MoonSharp.Interpreter.Execution.VM
 			}
 			}
 		}
 		}
 
 
-		private void Reduce(Instruction i)
+
+		private void ExecLen(Instruction i)
 		{
 		{
-			RValue v = m_ValueStack.Peek();
-			if (v.Type == DataType.Tuple)
-			{
-				m_ValueStack.Pop();
-				m_ValueStack.Push(v.ToSimplestValue());
-			}
+			RValue r = m_ValueStack.Pop();
+
+			if (r.Type == DataType.Table)
+				m_ValueStack.Push(new RValue(r.Table.Length));
+			else
+				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void Add(Instruction i)
+		private void ExecAdd(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -404,7 +438,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void Sub(Instruction i)
+		private void ExecSub(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -415,16 +449,16 @@ namespace MoonSharp.Interpreter.Execution.VM
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void Neg(Instruction i)
+		private void ExecNeg(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 
 
 			if (r.Type == DataType.Number)
 			if (r.Type == DataType.Number)
-				m_ValueStack.Push(new RValue( -r.Number));
+				m_ValueStack.Push(new RValue(-r.Number));
 			else
 			else
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
-		private void Power(Instruction i)
+		private void ExecPower(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -435,7 +469,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void Mul(Instruction i)
+		private void ExecMul(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -446,7 +480,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void Eq(Instruction i)
+		private void ExecEq(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -455,7 +489,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 		}
 		}
 
 
 
 
-		private void LessEq(Instruction i)
+		private void ExecLessEq(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -470,7 +504,7 @@ namespace MoonSharp.Interpreter.Execution.VM
 			}
 			}
 		}
 		}
 
 
-		private void Div(Instruction i)
+		private void ExecDiv(Instruction i)
 		{
 		{
 			RValue r = m_ValueStack.Pop();
 			RValue r = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
 			RValue l = m_ValueStack.Pop();
@@ -481,18 +515,111 @@ namespace MoonSharp.Interpreter.Execution.VM
 				throw new NotImplementedException("Meta operators");
 				throw new NotImplementedException("Meta operators");
 		}
 		}
 
 
-		private void ExecStore(Instruction i)
+
+		private void Internal_Assign(SymbolRef l, RValue r)
 		{
 		{
-			RValue r = m_ValueStack.Pop();
-			RValue l = m_ValueStack.Pop();
+			if (l.Type == SymbolRefType.Index)
+			{
+				l.TableRefObject.Table[l.TableRefIndex] = r;
+			}
+			else
+			{
+				m_Scope.Assign(l, r.ToSimplestValue());
+			}
+		}
 
 
+		private void Internal_Assign(RValue l, RValue r)
+		{
 			if (l.Type == DataType.Symbol)
 			if (l.Type == DataType.Symbol)
 			{
 			{
-				m_Scope.Assign(l.Symbol, r.ToSimplestValue());
+				Internal_Assign(l.Symbol, r);
+			}
+			else
+			{
+				throw new NotImplementedException("How should we manage this ?");
+			}
+		}
+
+		private void ExecIndexGet(Instruction i)
+		{
+			RValue indexValue = m_ValueStack.Pop();
+			RValue baseValue = m_ValueStack.Pop();
+
+			if (baseValue.Type != DataType.Table)
+			{
+				throw new NotImplementedException("META! : Can't index non-table yet");
 			}
 			}
 			else
 			else
 			{
 			{
-				throw new InternalErrorException("Assignment on type {0}", l.Type);
+				RValue v = baseValue.Table[indexValue];
+				m_ValueStack.Push(v.AsReadOnly());
+			}
+		}
+
+		private void ExecIndexSet(Instruction i, bool keepOnStack)
+		{
+			RValue indexValue = m_ValueStack.Pop();
+			RValue baseValue = keepOnStack ? m_ValueStack.Peek() : m_ValueStack.Pop();
+
+			if (baseValue.Type != DataType.Table)
+			{
+				throw new NotImplementedException("META! : Can't index non-table yet");
+			}
+			else
+			{
+				SymbolRef s = SymbolRef.ObjIndex(baseValue, indexValue);
+				m_ValueStack.Push(new RValue(s));
+			}
+		}
+
+		private void ExecStore(Instruction i)
+		{
+			RValue r = m_ValueStack.Pop();
+			RValue l = m_ValueStack.Pop();
+
+			Internal_Assign(l, r);
+		}
+
+
+		private void ExecNSymStor(Instruction i)
+		{
+			m_Scope.Assign(i.Symbol, m_ValueStack.Peek());
+		}
+
+
+		private void ExecAssign(Instruction i)
+		{
+			Slice<RValue> rvalues = new Slice<RValue>(m_ValueStack, m_ValueStack.Count - i.NumVal2, i.NumVal2, false);
+			Slice<RValue> lvalues = new Slice<RValue>(m_ValueStack, m_ValueStack.Count - i.NumVal2 - i.NumVal, i.NumVal, false);
+
+			Internal_MultiAssign(lvalues, rvalues);
+
+			m_ValueStack.CropAtCount(m_ValueStack.Count - i.NumVal - i.NumVal2);
+		}
+
+		public void Internal_MultiAssign(Slice<RValue> lValues, Slice<RValue> rValues)
+		{
+			int li = 0;
+			int rValues_Count = rValues.Count;
+			int lValues_Count = lValues.Count;
+
+			for (int ri = 0; ri < rValues_Count && li < lValues_Count; ri++, li++)
+			{
+				RValue vv = rValues[ri];
+
+				if ((ri != rValues_Count - 1) || (vv.Type != DataType.Tuple))
+				{
+					Internal_Assign(lValues[li], vv.ToSingleValue());
+					// Debug.WriteLine(string.Format("{0} <- {1}", li, vv.ToSingleValue()));
+				}
+				else
+				{
+					for (int rri = 0, len = vv.Tuple.Length; rri < len && li < lValues_Count; rri++, li++)
+					{
+						Internal_Assign(lValues[li], vv.Tuple[rri].ToSingleValue());
+						// Debug.WriteLine(string.Format("{0} <- {1}", li, vv.Tuple[rri].ToSingleValue()));
+					}
+				}
 			}
 			}
 		}
 		}
 
 

+ 1 - 1
src/MoonSharp.Interpreter/Helpers/List_ExtensionMethods.cs

@@ -10,7 +10,7 @@ namespace MoonSharp.Interpreter
 
 
 		public static void CropAtCount<T>(this List<T> list, int count)
 		public static void CropAtCount<T>(this List<T> list, int count)
 		{
 		{
-			list.RemoveRange(count - 1, list.Count - count);
+			list.RemoveRange(count, list.Count - count);
 		}
 		}
 
 
 		public static T Peek<T>(this List<T> list, int idxofs = 0)
 		public static T Peek<T>(this List<T> list, int idxofs = 0)

+ 3 - 1
src/MoonSharp.Interpreter/MoonSharp.Interpreter.csproj

@@ -55,6 +55,7 @@
   <ItemGroup>
   <ItemGroup>
     <Compile Include="CoreLib\BasicMethods.cs" />
     <Compile Include="CoreLib\BasicMethods.cs" />
     <Compile Include="CoreLib\MetaTableMethods.cs" />
     <Compile Include="CoreLib\MetaTableMethods.cs" />
+    <Compile Include="DataStructs\Slice.cs" />
     <Compile Include="Diagnostics\Debug.cs" />
     <Compile Include="Diagnostics\Debug.cs" />
     <Compile Include="Errors\InternalErrorException.cs" />
     <Compile Include="Errors\InternalErrorException.cs" />
     <Compile Include="Execution\Behaviours\Behaviour.cs" />
     <Compile Include="Execution\Behaviours\Behaviour.cs" />
@@ -82,6 +83,7 @@
     <Compile Include="Execution\Operator.cs" />
     <Compile Include="Execution\Operator.cs" />
     <Compile Include="Execution\DataTypes\Table.cs" />
     <Compile Include="Execution\DataTypes\Table.cs" />
     <Compile Include="Execution\VM\Chunk.cs" />
     <Compile Include="Execution\VM\Chunk.cs" />
+    <Compile Include="DataStructs\FastStack.cs" />
     <Compile Include="Execution\VM\LoopTracker.cs" />
     <Compile Include="Execution\VM\LoopTracker.cs" />
     <Compile Include="Execution\VM\Instruction.cs" />
     <Compile Include="Execution\VM\Instruction.cs" />
     <Compile Include="Execution\VM\OpCode.cs" />
     <Compile Include="Execution\VM\OpCode.cs" />
@@ -123,7 +125,7 @@
     <Compile Include="Tree\Statements\RepeatStatement.cs" />
     <Compile Include="Tree\Statements\RepeatStatement.cs" />
     <Compile Include="Tree\Statements\ReturnStatement.cs" />
     <Compile Include="Tree\Statements\ReturnStatement.cs" />
     <Compile Include="Helpers\LuaGrammar_ExtensionMethods.cs" />
     <Compile Include="Helpers\LuaGrammar_ExtensionMethods.cs" />
-    <Compile Include="Tree\Expressions\Tables\TableConstructor.cs" />
+    <Compile Include="Tree\Expressions\TableConstructor.cs" />
     <Compile Include="Tree\Statements\ScopeBlockStatement.cs" />
     <Compile Include="Tree\Statements\ScopeBlockStatement.cs" />
     <Compile Include="Tree\Statements\WhileStatement.cs" />
     <Compile Include="Tree\Statements\WhileStatement.cs" />
   </ItemGroup>
   </ItemGroup>

+ 12 - 2
src/MoonSharp.Interpreter/Tree/Expressions/IndexExpression.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Text;
 using Antlr4.Runtime.Tree;
 using Antlr4.Runtime.Tree;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Execution;
+using MoonSharp.Interpreter.Execution.VM;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Grammar;
 
 
 namespace MoonSharp.Interpreter.Tree.Expressions
 namespace MoonSharp.Interpreter.Tree.Expressions
@@ -43,11 +44,20 @@ namespace MoonSharp.Interpreter.Tree.Expressions
 			baseValue.Table[indexValue] = rValue;
 			baseValue.Table[indexValue] = rValue;
 		}
 		}
 
 
+		public override void Compile(Chunk bc)
+		{
+			m_BaseExp.Compile(bc);
+			m_IndexExp.Compile(bc);
+			bc.IndexGet();
+		}
+
 
 
 
 
-		public void CompileAssignment(Execution.VM.Chunk bc)
+		public void CompileAssignment(Chunk bc)
 		{
 		{
-			throw new NotImplementedException();
+			m_BaseExp.Compile(bc);
+			m_IndexExp.Compile(bc);
+			bc.IndexSet();
 		}
 		}
 	}
 	}
 }
 }

+ 1 - 1
src/MoonSharp.Interpreter/Tree/Expressions/OperatorExpression.cs

@@ -248,7 +248,7 @@ namespace MoonSharp.Interpreter.Tree.Expressions
 				case Operator.Not:
 				case Operator.Not:
 					return OpCode.Not;
 					return OpCode.Not;
 				case Operator.Size:
 				case Operator.Size:
-					return OpCode.Size;
+					return OpCode.Len;
 				case Operator.Neg:
 				case Operator.Neg:
 					return OpCode.Neg;
 					return OpCode.Neg;
 				case Operator.Power:
 				case Operator.Power:

+ 23 - 1
src/MoonSharp.Interpreter/Tree/Expressions/Tables/TableConstructor.cs → src/MoonSharp.Interpreter/Tree/Expressions/TableConstructor.cs

@@ -5,7 +5,7 @@ using System.Text;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Grammar;
 
 
-namespace MoonSharp.Interpreter.Tree.Expressions.Tables
+namespace MoonSharp.Interpreter.Tree.Expressions
 {
 {
 	class TableConstructor : Expression 
 	class TableConstructor : Expression 
 	{
 	{
@@ -58,5 +58,27 @@ namespace MoonSharp.Interpreter.Tree.Expressions.Tables
 
 
 			return new RValue(t);
 			return new RValue(t);
 		}
 		}
+
+		public override void Compile(Execution.VM.Chunk bc)
+		{
+			bc.NewTable();
+
+			foreach (var kvp in m_CtorArgs)
+			{
+				kvp.Key.Compile(bc);
+				bc.IndexSetN();
+				kvp.Value.Compile(bc);
+				bc.Store();
+			}
+
+			for (int i = 0; i < m_PositionalValues.Count; i++)
+			{
+				bc.Literal(new RValue(i+1));
+				bc.IndexSetN();
+				m_PositionalValues[i].Compile(bc);
+				bc.Store();
+			}
+		}
+
 	}
 	}
 }
 }

+ 13 - 3
src/MoonSharp.Interpreter/Tree/FunctionCall.cs

@@ -2,9 +2,9 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
-
 using Antlr4.Runtime.Tree;
 using Antlr4.Runtime.Tree;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Execution;
+using MoonSharp.Interpreter.Execution.VM;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Tree.Expressions;
 using MoonSharp.Interpreter.Tree.Expressions;
 
 
@@ -63,16 +63,26 @@ namespace MoonSharp.Interpreter.Tree
 
 
 		public override void Compile(Execution.VM.Chunk bc)
 		public override void Compile(Execution.VM.Chunk bc)
 		{
 		{
+			if (!string.IsNullOrEmpty(m_Name))
+			{
+				bc.TempOp(OpCode.TmpPeek, 0);
+				bc.Literal(new RValue(m_Name));
+				bc.IndexGet();
+			}
+
+
 			for (int i = m_Arguments.Length - 1; i >= 0; i--)
 			for (int i = m_Arguments.Length - 1; i >= 0; i--)
 				m_Arguments[i].Compile(bc);
 				m_Arguments[i].Compile(bc);
 
 
 			if (string.IsNullOrEmpty(m_Name))
 			if (string.IsNullOrEmpty(m_Name))
 			{
 			{
-				bc.Invoke(m_Arguments.Length);
+				bc.Call(m_Arguments.Length);
 			}
 			}
 			else
 			else
 			{
 			{
-				bc.TableInvoke(m_Arguments.Length, m_Name);
+				bc.TempOp(OpCode.TmpPush, 0);
+				bc.TempOp(OpCode.TmpClear, 0);
+				bc.Call(m_Arguments.Length + 1);
 			}
 			}
 		}
 		}
 	}
 	}

+ 0 - 1
src/MoonSharp.Interpreter/Tree/NodeFactory.cs

@@ -6,7 +6,6 @@ using Antlr4.Runtime.Tree;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Execution;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Grammar;
 using MoonSharp.Interpreter.Tree.Expressions;
 using MoonSharp.Interpreter.Tree.Expressions;
-using MoonSharp.Interpreter.Tree.Expressions.Tables;
 using MoonSharp.Interpreter.Tree.Statements;
 using MoonSharp.Interpreter.Tree.Statements;
 
 
 namespace MoonSharp.Interpreter.Tree
 namespace MoonSharp.Interpreter.Tree

+ 15 - 1
src/MoonSharp.Interpreter/Tree/Statements/FunctionDefinitionStatement.cs

@@ -105,7 +105,21 @@ namespace MoonSharp.Interpreter.Tree.Statements
 			}
 			}
 			else
 			else
 			{
 			{
-				throw new NotImplementedException("table deindex in funcdef");
+				bc.Load(m_FuncName);
+
+				foreach (string str in m_TableAccessors)
+				{
+					bc.Literal(new RValue(str));
+					bc.IndexGet();
+				}
+
+				bc.Literal(new RValue(m_MethodName));
+
+				bc.IndexSet();
+
+				m_FuncDef.Compile(bc);
+
+				bc.Store();
 			}
 			}
 		}
 		}
 
 

+ 2 - 1
src/MoonSharp/Program.cs

@@ -54,7 +54,8 @@ namespace MoonSharp
 			}
 			}
 			else
 			else
 			{
 			{
-				Console.WriteLine("Usage : MoonSharp [filename]");
+				Console.WriteLine("Sorry, at the moment, only file operations are supported:");
+				Console.WriteLine("\tUsage : MoonSharp [filename]");
 			}
 			}
 		}
 		}
 
 

+ 1 - 1
src/MoonSharpTests/Program.cs

@@ -12,7 +12,7 @@ namespace MoonSharpTests
 {
 {
 	class Program
 	class Program
 	{
 	{
-		public const string RESTRICT_TEST = "ClosureNoTable";
+		public const string RESTRICT_TEST = "TableMethod6";
 
 
 		static void Main(string[] args)
 		static void Main(string[] args)
 		{
 		{

+ 1 - 1
src/PerformanceComparison/Program.cs

@@ -13,7 +13,7 @@ namespace PerformanceComparison
 {
 {
 	class Program
 	class Program
 	{
 	{
-		const int ITERATIONS = 1000000;
+		const int ITERATIONS = 10000;
 
 
 		static  string scriptText = @"
 		static  string scriptText = @"
 			function move(n, src, dst, via)
 			function move(n, src, dst, via)

+ 3 - 0
src/moonsharp.sln

@@ -47,4 +47,7 @@ Global
 	GlobalSection(SolutionProperties) = preSolution
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 		HideSolutionNode = FALSE
 	EndGlobalSection
 	EndGlobalSection
+	GlobalSection(Performance) = preSolution
+		HasPerformanceSessions = true
+	EndGlobalSection
 EndGlobal
 EndGlobal