예제 #1
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));
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
0
        public override bool Install(LuatScript script)
        {
            if (false == base.Install(script))
            {
                return(false);
            }

            if (false == IsLocal && Values.Count == 0)
            {
                // Illegal assignment statement, assumes parser has already emitted error.
                return(false);
            }

            int variableCount = Variables.Count;

            for (int variableIndex = 0; variableIndex < variableCount; ++variableIndex)
            {
                Expression lhs = Variables[variableIndex] as Expression;
                Expression rhs;

                if (variableIndex < Values.Count)
                {
                    rhs = Values[variableIndex] as Expression;
                }
                else
                {
                    rhs = new NilExpression(lhs.TextRange);
                    rhs.Resolve(script);
                    this.ChildNodes.Add(rhs);
                }

                LuatVariable variable = lhs.Resolve(script) as LuatVariable;
                bool         bValid   = false;

                do
                {
                    if (null == variable)
                    {
                        break;
                    }

                    if (variable.IsReadOnly)
                    {
                        this.AddError(string.Format("{0} is read-only", lhs.DisplayText));
                        break;
                    }

                    bValid = true;
                }while (false);

                if (false == bValid)
                {
                    // Failed to resolve or create the name.
                    // Undo all the assignments we've done and return incomplete.
                    Uninstall(script);
                    return(false);
                }

                string displayText             = string.Format("{0} = {1}", lhs.DisplayText, rhs.DisplayText);
                LuatValue.IReference reference = new Parser.AST.Expression.Reference(script, rhs, displayText);
                variable.AddAssignment(reference);
                AddUninstallAction(script, delegate() { variable.RemoveAssignment(reference); });
            }

            return(true);
        }