public void OnField(IParseItem item) { TreeNode n = new TreeNode(""); m_curNode.Nodes.Add(n); n.Text = string.IsNullOrEmpty(item.FieldName) ? string.Format("[{0}]", item.Value) : string.Format("{0} = {1}", item.FieldName, item.Value); }
public void OnBeginContainer(IParseItem item) { m_stack.Push(m_curNode); TreeNode n = new TreeNode(""); m_curNode.Nodes.Add(n); m_curNode = n; m_curNode.Text = string.Format("{0} ({1})", item.FieldName, item.TypeName); }
public void OnField(IParseItem item) { var leaf = new Leaf(""); m_curLeaf.AddLeaf(leaf); leaf.Text = string.IsNullOrEmpty(item.FieldName) ? string.Format("[{0}]", item.Value) : string.Format("{0} = {1}", item.FieldName, item.Value); }
public void OnBeginContainer(IParseItem item) { m_leafs.Push(m_curLeaf); var newLeaf = new Leaf(""); m_curLeaf.AddLeaf(newLeaf); m_curLeaf = newLeaf; m_curLeaf.Text = string.Format("{0} ({1})", item.FieldName, item.TypeName); }
/// <summary> /// Resolves all the labels and updates the information in the given /// IParseItem tree. /// </summary> /// <param name="target">The IParseItem tree to traverse.</param> /// <exception cref="System.ArgumentNullException">If target is null.</exception> public void Resolve(IParseItem target) { if (target == null) throw new ArgumentNullException("target"); tree = new GetInfoTree(); target.Accept(this); tree.Resolve(); var info = tree.EndFunc(); GlobalCaptures = info.CapturedLocals; GlobalNested = info.HasNested; }
/// <summary> /// Resolves all the labels and updates the information in the given IParseItem tree. /// </summary> /// <param name="target">The IParseItem tree to traverse.</param> /// <exception cref="System.ArgumentNullException">If target is null.</exception> public void Resolve(IParseItem target) { if (target == null) { throw new ArgumentNullException(nameof(target)); } _tree = new GetInfoTree(); target.Accept(this); _tree.Resolve(); var info = _tree.EndFunc(); _globalCaptures = info.CapturedLocals; _globalNested = info.HasNested; }
public IParseItem Parse(Stream input, Encoding encoding, string name) { var lexer = new Lexer(input, encoding, name); if (input == null) { throw new ArgumentNullException(nameof(input)); } // parse the chunk IParseItem read = _readBlock(lexer); if (!lexer.PeekType(TokenType.None)) { throw lexer.SyntaxError("Expecting EOF"); } return(read); }
/// <summary> /// Compiles an IParseItem tree indo an IModule object so that it can /// be executed. /// </summary> /// <param name="name">The name to given the module, can be null to /// auto-generate.</param> /// <param name="E">The current environment.</param> /// <param name="item">The item to compile.</param> /// <returns>A compiled version of the object.</returns> /// <exception cref="System.ArgumentNullException">If E or item is null.</exception> /// <exception cref="ModMaker.Lua.Parser.SyntaxException">If there is /// syntax errors in the item tree.</exception> public ILuaValue Compile(ILuaEnvironment E, IParseItem item, string name) { if (E == null) { throw new ArgumentNullException(nameof(E)); } if (item == null) { throw new ArgumentNullException(nameof(item)); } // get the name name = name ?? "<>_func_" + (_tid++); if (_types.Contains(name)) { int i = 0; while (_types.Contains(name + i)) { i++; } name += i; } // resolve labels GetInfoVisitor lVisitor = new GetInfoVisitor(); lVisitor.Resolve(item); // create the type TypeBuilder tb = _mb.DefineType(name, TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed, typeof(LuaValueBase), Type.EmptyTypes); ChunkBuilder cb = new ChunkBuilder(tb, lVisitor.GlobalCaptures, lVisitor.GlobalNested); // compile the code CompilerVisitor cVisitor = new CompilerVisitor(cb); item.Accept(cVisitor); var ret = cb.CreateChunk(E); return(ret); }
/// <summary> /// Assigns the values of the parse item to the given value. /// </summary> /// <param name="target">The item to assign the value to (e.g. NameItem).</param> /// <param name="local">Whether this is a local definition.</param> /// <param name="getIndex">A function to get the index of the object, /// pass null to use the default.</param> /// <param name="getValue">A function to get the value to set to.</param> void AssignValue(IParseItem target, bool local, Action getIndex, Action getValue) { ILGenerator gen = compiler.CurrentGenerator; ChunkBuilder.VarDefinition field; if (local) { field = compiler.DefineLocal((NameItem)target); } else if (target is IndexerItem) { IndexerItem name = (IndexerItem)target; // {name.Prefix}.SetIndex({name.Expression}, value); name.Prefix.Accept(this); if (getIndex != null) { getIndex(); } else { name.Expression.Accept(this); } getValue(); gen.Emit(OpCodes.Callvirt, typeof(ILuaValue).GetMethod(nameof(ILuaValue.SetIndex))); return; } else // names[i] is NameItem { NameItem item = (NameItem)target; field = compiler.FindVariable(item); } // envField = value; field.StartSet(); getValue(); field.EndSet(); }
public void OnContainer(IParseItem item) { }
/// <summary> /// Assigns the values of the parse item to the given value. /// </summary> /// <param name="target">The item to assign the value to (e.g. NameItem).</param> /// <param name="local">Whether this is a local definition.</param> /// <param name="getIndex">A function to get the index of the object, /// pass null to use the default.</param> /// <param name="getValue">A function to get the value to set to.</param> void AssignValue(IParseItem target, bool local, Action getIndex, Action getValue) { ILGenerator gen = compiler.CurrentGenerator; ChunkBuilder.VarDefinition field; if (local) { field = compiler.DefineLocal((NameItem)target); } else if (target is IndexerItem) { IndexerItem name = (IndexerItem)target; // {name.Prefix}.SetIndex({name.Expression}, value); name.Prefix.Accept(this); if (getIndex != null) getIndex(); else name.Expression.Accept(this); getValue(); gen.Emit(OpCodes.Callvirt, typeof(ILuaValue).GetMethod("SetIndex")); return; } else // names[i] is NameItem { NameItem item = (NameItem)target; field = compiler.FindVariable(item); } // envField = value; field.StartSet(); getValue(); field.EndSet(); }
/// <summary> /// Compiles an IParseItem tree indo an IModule object so that it can /// be executed. /// </summary> /// <param name="name">The name to given the module, can be null to /// auto-generate.</param> /// <param name="E">The current environment.</param> /// <param name="item">The item to compile.</param> /// <returns>A compiled version of the object.</returns> /// <exception cref="System.ArgumentNullException">If E or item is null.</exception> /// <exception cref="ModMaker.Lua.Parser.SyntaxException">If there is /// syntax errors in the item tree.</exception> public ILuaValue Compile(ILuaEnvironment E, IParseItem item, string name) { if (E == null) throw new ArgumentNullException("E"); if (item == null) throw new ArgumentNullException("item"); // get the name name = name ?? "<>_func_" + (_tid++); if (_types.Contains(name)) { int i = 0; while (_types.Contains(name + i)) i++; name += i; } // resolve labels GetInfoVisitor lVisitor = new GetInfoVisitor(); lVisitor.Resolve(item); // create the type TypeBuilder tb = _mb.DefineType(name, TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed, typeof(LuaValueBase), Type.EmptyTypes); ChunkBuilder cb = new ChunkBuilder(tb, lVisitor.GlobalCaptures, lVisitor.GlobalNested); // compile the code CompilerVisitor cVisitor = new CompilerVisitor(cb); item.Accept(cVisitor); var ret = cb.CreateChunk(E); return ret; }