Beispiel #1
0
        public LuatVariable AddLocal(LuatScript script, Identifier Name, string type)
        {
            string name = Name.Text;

            LuatVariable variable = Locals[script].Index(name, false) as LuatVariable;

            if (null == variable)
            {
                LuatTable localTable = Locals[script];

                if (null != type)
                {
                    variable = Database.Instance.CreateReflectedVariable(type, localTable);
                }

                if (null == variable)
                {
                    variable = new LuatVariable(localTable);
                }

                localTable.AddChild(name, variable);
            }
            else
            {
                Name.AddWarning(script, WarningType.DuplicateLocal, "Warning: Local '" + name + "' already defined");
            }

            return(variable);
        }
Beispiel #2
0
        public override LuatValue Resolve(LuatScript script)
        {
            LuatValue value = base.Resolve(script);

            if (null != value)
            {
                return(value);
            }

            int index = 1;

            foreach (Identifier identifier in this.Parameters)
            {
                LuatVariable local = this.Block.AddLocal(script, identifier, null);
                local.Description = string.Format("parameter {0}", index++);
            }

            List <string> args = new List <string>();

            foreach (Identifier param in this.Parameters)
            {
                args.Add(param.Text);
            }

            LuatValue    returnValue = this.Block.ReturnValues[script];
            LuatFunction function    = new LuatFunction(returnValue, args.ToArray());

            function.Description = this.Description;
            function.ExpectsSelf = this.ExpectsSelf;
            value = function;

            this.ResolvedValues[script] = value;

            return(value);
        }
Beispiel #3
0
        public override IEnumerable <AutoCompleteItem> GetAutoCompleteList(LuatScript script, int offset)
        {
            Dictionary <string, LuatValue> scopedValues = new Dictionary <string, LuatValue>();

            GetScopedValues(script, ref scopedValues);
            foreach (KeyValuePair <string, LuatValue> v in scopedValues)
            {
                yield return(new AutoCompleteItem(v.Key, v.Value));
            }

            yield return(new AutoCompleteItem("function", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("local", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("end", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("for", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("if", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("do", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("while", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("repeat", null, LuaIntellisenseIconType.Keyword));

            yield return(new AutoCompleteItem("return", null, LuaIntellisenseIconType.Keyword));
        }
Beispiel #4
0
        public override bool Install(LuatScript script)
        {
            if (false == base.Install(script))
            {
                return(false);
            }

            if (null == this.Body)
            {
                return(false);
            }

            if (null == this.Start)
            {
                return(false);
            }

            LuatVariable iterator = Body.AddLocal(script, Iterator.Name, null);

            LuatValue.IReference reference = new Parser.AST.Expression.Reference(script, this.Start);
            iterator.AddAssignment(reference);
            AddUninstallAction(script, delegate() { iterator.RemoveAssignment(reference); });

            return(base.Install(script));
        }
Beispiel #5
0
 public T this[LuatScript script]
 {
     get
     {
         return(Add(script));
     }
 }
Beispiel #6
0
 public override void Invalidate(LuatScript script)
 {
     if (Database.Instance.AddUnresolvedStatement(script, this))
     {
         Uninstall(script);
     }
 }
Beispiel #7
0
 public void Invalidate(LuatScript script)
 {
     if (m_entries.ContainsKey(script))
     {
         m_entries[script].RemoveReference(new Parser.AST.Expression.Reference(script, m_owner));
         m_entries.Remove(script);
     }
 }
Beispiel #8
0
        public virtual void GetScopedValues(LuatScript script, ref Dictionary <string, LuatValue> scopedValues)
        {
            LuatAstNodeBase parent = this.ParentNode as LuatAstNodeBase;

            if (null != parent)
            {
                parent.GetScopedValues(script, ref scopedValues);
            }
        }
Beispiel #9
0
        public override LuatValue Resolve(LuatScript script)
        {
            LuatValue value = base.Resolve(script);

            if (null != value)
            {
                return(value);
            }

            LuatTable table      = new LuatTable(null);
            int       fieldIndex = 1;

            foreach (Field field in Fields)
            {
                string key;
                if (null == field.Key)
                {
                    key = (fieldIndex++).ToString();
                }
                else if (field.Key is StringExpression)
                {
                    key = (field.Key as StringExpression).String;
                }
                else if (field.Key is NumberExpression)
                {
                    key = (field.Key as NumberExpression).Number.ToString();
                }
                else if (field.Key is Identifier)
                {
                    key = (field.Key as Identifier).Text;
                }
                else
                {
                    continue;
                }

                if (null != table.Index(key, false))
                {
                    field.AddWarning(script, WarningType.DuplicateTableKey, string.Format("Table already contains key '{0}'", key));
                    continue;
                }

                LuatVariable entry = table.AddChild(key, new LuatVariable(table));
                entry.Description = Description;

                if (null != field.Value)
                {
                    entry.AddAssignment(new Parser.AST.Expression.Reference(script, field.Value, this.DisplayText));
                }
            }

            this.ResolvedValues[script] = table;
            return(table);
        }
Beispiel #10
0
        public virtual bool Uninstall(LuatScript script)
        {
            foreach (UninstallAction uninstallAction in m_uninstallScriptActions[script])
            {
                uninstallAction();
            }

            m_uninstallScriptActions.Remove(script);

            return(true);
        }
Beispiel #11
0
            public T Add(LuatScript script)
            {
                T value;

                if (false == m_entries.TryGetValue(script, out value))
                {
                    value = m_valueConstructor();
                    m_entries.Add(script, value);
                }
                return(value);
            }
Beispiel #12
0
        public override void GetScopedValues(LuatScript script, ref Dictionary <string, LuatValue> scopedValues)
        {
            foreach (var v in script.Table.Children)
            {
                if (scopedValues.ContainsKey(v.Key))
                {
                    continue;
                }

                scopedValues.Add(v.Key, v.Value);
            }
        }
Beispiel #13
0
        public override LuatValue Resolve(LuatScript script)
        {
            LuatValue value = base.Resolve(script);

            if (null == value)
            {
                value                       = new LuatLiteral(Type);
                value.Description           = this.Description;
                this.ResolvedValues[script] = value;
            }

            return(value);
        }
Beispiel #14
0
        private static LuatScript CreateLuatScript(ILuaIntellisenseDocument document)
        {
            var table = new LuatTable(null)
            {
                Description = Helpers.Decorate(document.Name, DecorationType.Code)
            };

            var luatScript = LuatScript.Create();

            luatScript.Table     = table;
            luatScript.Path      = document.Uri.LocalPath;
            luatScript.Reference = document;
            luatScript.Name      = document.Name;

            return(luatScript);
        }
Beispiel #15
0
        public override LuatValue Resolve(LuatScript script)
        {
            LuatValue value = base.Resolve(script);

            if (null != value)
            {
                return(value);
            }

            string name = Name.Text;

            BlockStatement block = this.FindAncestor(typeof(BlockStatement)) as BlockStatement;

            if (IsLocal)
            {
                value = block.AddLocal(script, Name, Type);
            }
            else
            {
                // Start by searching locals of the ancestor blocks
                while (block != null)
                {
                    value = block.Locals[script].Index(name);
                    if (null != value)
                    {
                        ResolvedValues[script] = value;
                        return(value);
                    }

                    block = block.FindAncestor(typeof(BlockStatement)) as BlockStatement;
                }

                // Now try globals
                value = script.Table.Index(name, IsLHSOfAssignment);
            }

            if (null != value)
            {
                this.ResolvedValues[script] = value;
            }

            return(value);
        }
Beispiel #16
0
        public override bool Install(LuatScript script)
        {
            if (false == base.Install(script))
            {
                return(false);
            }

            if (null == this.Body)
            {
                return(false);
            }

            foreach (Identifier iterator in this.Iterators)
            {
                Body.AddLocal(script, iterator, null);
            }

            return(base.Install(script));
        }
Beispiel #17
0
        public override bool Install(LuatScript script)
        {
            if (false == base.Install(script))
            {
                return(false);
            }

            BlockStatement block;

            Function func = this.FindAncestor <Function>();

            if (null != func)
            {
                block = func.Block;
            }
            else
            {
                block = this.FindAncestor <BlockStatement>();
            }

            if (null == block)
            {
                throw new Exception("ReturnStatement requires a BlockStatement as ancestor");
            }

            List <LuatValue> values = new List <LuatValue>();

            foreach (Expression expression in Values)
            {
                // TODO: not correct for multiple return values
                LuatVariable         returnValue = block.ReturnValues[script];
                LuatValue.IReference reference   = new Parser.AST.Expression.Reference(script, expression, "return " + expression.DisplayText);
                returnValue.AddAssignment(reference);
                AddUninstallAction(script, delegate() { returnValue.RemoveAssignment(reference); });
            }

            if (IsMultiline)
            {
                AddWarning(script, WarningType.MultilineReturn, string.Format("Returning value from next line"));
            }

            return(true);
        }
Beispiel #18
0
        public override void Invalidate(LuatScript script)
        {
            if (null == this.ResolvedValues[script])
            {
                return;
            }

            // Add this expression to the needs-resolving list
            Database.Instance.AddUnresolvedExpression(script, this);

            // Drop the value
            this.ResolvedValues.Invalidate(script);

            // Propagate invalidation up the syntax tree
            LuatAstNodeBase parent = this.ParentNode as LuatAstNodeBase;

            if (null != parent)
            {
                parent.Invalidate(script);
            }
        }
Beispiel #19
0
 public override IEnumerable <AutoCompleteItem> GetAutoCompleteList(LuatScript script, int offset)
 {
     if (offset > this.LHS.EndOffset)
     {
         LuatValue value = LHS.ResolvedValues[script];
         if (null != value)
         {
             foreach (KeyValuePair <string, LuatValue> child in value.Children)
             {
                 yield return(new AutoCompleteItem(child.Key, child.Value));
             }
         }
     }
     else
     {
         foreach (AutoCompleteItem item in base.GetAutoCompleteList(script, offset))
         {
             yield return(item);
         }
     }
 }
Beispiel #20
0
        public override LuatValue Resolve(LuatScript script)
        {
            LuatValue value = base.Resolve(script);

            if (null != value)
            {
                return(value);
            }

            LuatValue lhsValue = LHS.Resolve(script);

            if (null == lhsValue)
            {
                return(null);
            }

            if (null == RHS)
            {
                return(null);
            }

            string index;

            if (false == GetString(RHS, out index))
            {
                return(null);
            }

            value = lhsValue.Index(index, IsLHSOfAssignment);
            if (null == value)
            {
                return(null);
            }

            this.ResolvedValues[script] = value;
            return(value);
        }
Beispiel #21
0
            // Indexer
            public LuatValue this[LuatScript script]
            {
                get
                {
                    LuatValue value;
                    m_entries.TryGetValue(script, out value);
                    return(value);
                }
                set
                {
                    if (null == value)
                    {
                        throw new Exception("Do not assign null to Value. Instead call Invalidate()");
                    }

                    if (m_entries.ContainsKey(script))
                    {
                        throw new Exception("Value can only be assigned once after creation or invalidation");
                    }

                    this.m_entries.Add(script, value);
                    value.AddReference(new Parser.AST.Expression.Reference(script, m_owner, m_owner.DisplayText));
                }
            }
Beispiel #22
0
 public bool Contains(LuatScript script)
 {
     return(m_entries.ContainsKey(script));
 }
Beispiel #23
0
 public UnresolvedExpression(LuatScript script, Expression expression)
 {
     Script = script;
     Expression = expression;
 }
Beispiel #24
0
        /// <summary>
        /// Remove the LuatScript from the database
        /// </summary>
        /// <param name="script"></param>
        private void Drop(LuatScript script)
        {
            // Invalidate all nodes
            foreach (LuatAstNodeBase node in Helpers.Traversal<IAstNode>(script.CU, n => n.ChildNodes))
            {
                node.Invalidate(script);
            }

            script.UnresolvedExpressions.Clear();
            script.UnresolvedStatements.Clear();
        }
Beispiel #25
0
 public virtual bool Install(LuatScript script)
 {
     return(true);
 }
Beispiel #26
0
 public virtual void Invalidate(LuatScript script)
 {
     CU.ClearWarnings(script);
 }
Beispiel #27
0
        public override void Invalidate(LuatScript script)
        {
            this.Locals.Remove(script);

            base.Invalidate(script);
        }
Beispiel #28
0
 protected void AddUninstallAction(LuatScript script, UninstallAction action)
 {
     m_uninstallScriptActions[script].Add(action);
 }
Beispiel #29
0
		/// <summary>
		/// Resolves the expression.  Since the type of the LHS of a binary expression 
		/// must match the type of the RHS, resolve the RHS and return that type.
		/// </summary>
		/// <param name="script"></param>
		/// <returns></returns>
		public override LuatValue Resolve(LuatScript script)
		{
			LuatValue value = base.Resolve(script);
			if (null != value)
			{
				return value;
			}

			if (null != RightExpression)
			{
				value = RightExpression.Resolve(script);
				if (null != value)
				{
					this.ResolvedValues[script] = value;
				}
			}
			return value;
		}
Beispiel #30
0
 public void Remove(LuatScript script)
 {
     m_entries.Remove(script);
 }
Beispiel #31
0
        /// <summary>
        /// Adds an expression to the unresolved list
        /// </summary>
        /// <param name="script"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool AddUnresolvedExpression(LuatScript script, Expression expression)
        {
            LinkedListNode<Expression> node = expression.ListNode[script];
            if (node.List == script.UnresolvedExpressions)
            {
                return false;
            }

            if (null != node.List)
            {
                if (node.List != script.ResolvedExpressions)
                {
                    throw new Exception("Expression is part of an unknown list");
                }

                node.List.Remove(node);
            }

            script.UnresolvedExpressions.AddFirst(node);
            return true;
        }
Beispiel #32
0
        public void AddWarning(LuatScript script, WarningType type, string message)
        {
            LuatWarning warning = new LuatWarning(script, type, this.TextRange, message);

            CU.AddWarning(warning);
        }
Beispiel #33
0
        /// <summary>
        /// Adds a statement to the unresolved list
        /// </summary>
        /// <param name="script"></param>
        /// <param name="statement"></param>
        /// <returns></returns>
        public bool AddUnresolvedStatement(LuatScript script, Statement statement)
        {
            LinkedListNode<Statement> node = statement.ListNode[script];
            if (node.List == script.UnresolvedStatements)
            {
                return false;
            }

            if (null != node.List)
            {
                if (node.List != script.ResolvedStatements)
                {
                    throw new Exception("Statement is part of an unknown list");
                }

                node.List.Remove(node);
            }

            script.UnresolvedStatements.AddFirst(node);
            return true;
        }
Beispiel #34
0
        public virtual IEnumerable <AutoCompleteItem> GetAutoCompleteList(LuatScript script, int offset)
        {
            LuatAstNodeBase parent = this.ParentNode as LuatAstNodeBase;

            return((null != parent) ? parent.GetAutoCompleteList(script, offset) : new AutoCompleteItem[0]);
        }
Beispiel #35
0
 public override IEnumerable <AutoCompleteItem> GetAutoCompleteList(LuatScript script, int offset)
 {
     return(new AutoCompleteItem[0]);
 }