using System; using System.Collections.Generic; using System.Linq; using System.Text; using MoonSharp.Interpreter.Execution; using MoonSharp.Interpreter.Grammar; namespace MoonSharp.Interpreter.Tree.Expressions { class TableConstructor : Expression { List m_PositionalValues = new List(); List> m_CtorArgs = new List>(); public TableConstructor(LuaParser.TableconstructorContext context, ScriptLoadingContext lcontext) : base(context, lcontext) { var fieldlist = context.fieldlist(); if (fieldlist != null) { foreach (var field in fieldlist.field()) { var keyval = field.keyexp; var name = field.NAME(); if (keyval != null) { Expression exp = NodeFactory.CreateExpression(keyval, lcontext); m_CtorArgs.Add(new KeyValuePair( exp, NodeFactory.CreateExpression(field.keyedexp, lcontext))); } else if (name != null) { m_CtorArgs.Add(new KeyValuePair( new LiteralExpression(field, lcontext, new RValue(name.GetText())), NodeFactory.CreateExpression(field.namedexp, lcontext))); } else { m_PositionalValues.Add(NodeFactory.CreateExpression(field.positionalexp, lcontext)); } } } } public override RValue Eval(RuntimeScope scope) { var dic = m_CtorArgs.ToDictionary( kvp => kvp.Key.Eval(scope), kvp => kvp.Value.Eval(scope).ToSimplestValue().CloneAsWritable()); Table t = new Table(dic, m_PositionalValues.Select(e => e.Eval(scope))); 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(); } } } }