Ejemplo n.º 1
0
        public override void Evaluate()
        {
            var name = new Term(RegexMatch.Groups[2].Value);
            var type = new Term(RegexMatch.Groups[1].Value);
            var list = new ListValue();

            switch (type.Text.ToUpper())
            {
                case "BODIES":
                    foreach (var body in FlightGlobals.fetch.bodies)
                    {
                        list.Add(new BodyTarget(body, Vessel));
                    }
                    break;
                case "TARGETS":
                    foreach (var vessel in FlightGlobals.Vessels)
                    {
                        if (vessel == Vessel) continue;
                        list.Add(new VesselTarget(vessel, ParentContext));
                    }
                    break;
            }

            if (list.Empty())
            {
                list = Vessel.PartList(type.Text);
            }

            FindOrCreateVariable(name.Text).Value = list;

            State = ExecutionState.DONE;
        }
Ejemplo n.º 2
0
        public override void Evaluate()
        {
            var target = new Expression.Expression(RegexMatch.Groups[2].Value, ParentContext).GetValue();
            var name = new Term(RegexMatch.Groups[3].Value);
            var type = new Term(RegexMatch.Groups[1].Value);
            ListValue list;

            if (target is VesselTarget)
            {
                list = Vessel.PartList(type.Text);
            }
            else
            {
                var targetVessel = VesselUtils.GetVesselByName(target.ToString());
                if (targetVessel != null)
                {
                    if (targetVessel.loaded)
                    {
                        list = targetVessel.PartList(type.Text);
                    }
                    else
                    {
                        throw new KOSException("Vessel: " + target + " Is Unloaded, Cannot continue.");
                    }
                }
                else
                {
                    throw new KOSException("Could not get list: " + type.Text + " for Vessel: " + target);
                }
            }

            FindOrCreateVariable(name.Text).Value = list;
            State = ExecutionState.DONE;
        }
Ejemplo n.º 3
0
        public override void Evaluate()
        {
            var targetTerm = new Term(RegexMatch.Groups[1].Value);
            var e = new Expression.Expression(RegexMatch.Groups[2].Value, ParentContext);

            if (targetTerm.Type == Term.TermTypes.STRUCTURE)
            {
                var baseObj = new Expression.Expression(targetTerm.SubTerms[0], ParentContext).GetValue();

                var obj = baseObj as ISuffixed;
                if (obj != null)
                {
                    if (obj.SetSuffix(targetTerm.SubTerms[1].Text.ToUpper(), e.GetValue()))
                    {
                        State = ExecutionState.DONE;
                        return;
                    }
                    throw new KOSException(
                        "Suffix '" + targetTerm.SubTerms[1].Text + "' doesn't exist or is read only", this);
                }
                throw new KOSException(
                    "Can't set subvalues on a " + Expression.Expression.GetFriendlyNameOfItem(baseObj), this);
            }
            var v = FindOrCreateVariable(targetTerm.Text);

            if (v == null) return;
            v.Value = e.GetValue();
            State = ExecutionState.DONE;
        }
Ejemplo n.º 4
0
        public override void Evaluate()
        {
            var name = new Term(RegexMatch.Groups[2].Value);
            var type = new Term(RegexMatch.Groups[1].Value);
            var list = new ListValue();

            var partList = Vessel.Parts.ToList();

            switch (type.Text.ToUpper())
            {
                case "BODIES":
                    foreach (var body in FlightGlobals.fetch.bodies)
                    {
                        list.Add(new BodyTarget(body, Vessel));
                    }
                    break;
                case "TARGETS":
                    foreach (var vessel in FlightGlobals.Vessels)
                    {
                        if (vessel == Vessel) continue;
                        list.Add(new VesselTarget(vessel, ParentContext));
                    }
                    break;
                case "RESOURCES":
                    list = ResourceValue.PartsToList(partList);
                    break;
                case "PARTS":
                    list = PartValue.PartsToList(partList);
                    break;
                case "ENGINES":
                    list = EngineValue.PartsToList(partList);
                    break;
                case "SENSORS":
                    list = SensorValue.PartsToList(partList);
                    break;
                case "ELEMENTS":
                    list = ElementValue.PartsToList(partList);
                    break;
            }

            FindOrCreateVariable(name.Text).Value = list;

            State = ExecutionState.DONE;
        }
Ejemplo n.º 5
0
Archivo: Term.cs Proyecto: QuakeIV/KOS
        private void ProcessSymbols()
        {
            // Is the input empty?
            if (string.IsNullOrEmpty(Text)) return;

            // HEADING.. BY is now deprecated in favor of HEADING(x,y), but here it is if you're using it still
            Text = Regex.Replace(Text, "HEADING ([ :@A-Za-z0-9\\.\\-\\+\\*/]+) BY ([ :@A-Za-z0-9\\.\\-\\+\\*/]+)",
                                 "HEADING($2,$1)", RegexOptions.IgnoreCase);

            //enables scientific notation eg 6.6E-11 -> 6.6*10^-11
            Text = Regex.Replace(Text, "(\\d)E([-+]{1}[0-9]+)", "$1*10^$2");

            // Resource tags are now deprecated in favor of SHIP:ResourceName
            Text = Regex.Replace(Text, "(\\s|^)<([a-zA-Z]+)>(\\s|$)", " SHIP:$2 ", RegexOptions.IgnoreCase);

            // Is this JUST a matched symbol?
            var s = MatchAt(ref Text, 0, allSymbols);
            if (s != null && Text.Length == s.Length)
            {
                if (mathSymbols.Contains(s)) Type = TermTypes.MATH_OPERATOR;
                else if (comparisonSymbols.Contains(s)) Type = TermTypes.COMPARISON_OPERATOR;
                else if (booleanSymbols.Contains(s)) Type = TermTypes.BOOLEAN_OPERATOR;

                return;
            }

            SubTerms = new List<Term>();

            // If this is a parameter list, grab the parameters
            if (Type == TermTypes.PARAMETER_LIST)
            {
                var parameterList = ParseParameters(Text);
                if (parameterList != null)
                {
                    foreach (var param in parameterList)
                    {
                        SubTerms.Add(new Term(param));
                    }
                }

                return;
            }

            // Does this thing contain a boolean operation?
            var booleanElements = SplitByListIgnoreBracket(Text, ref booleanSymbols);
            if (booleanElements != null)
            {
                Type = TermTypes.BOOLEAN;

                foreach (var element in booleanElements)
                {
                    if (booleanSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.BOOLEAN_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }

            // Does this thing contain a comparison?
            var comparisonElements = SplitByListIgnoreBracket(Text, ref comparisonSymbols);
            if (comparisonElements != null)
            {
                Type = TermTypes.COMPARISON;

                foreach (var element in comparisonElements)
                {
                    SubTerms.Add(new Term(element));
                }

                return;
            }

            // Does this thing contain an Index?
            var listElements = SplitByListIgnoreBracket(Text, ref listaccessSymbols);
            if (listElements != null)
            {
                Type = TermTypes.INDEX;

                foreach (var element in listElements)
                {
                    if (listaccessSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.INDEX_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }

            // Parse this as a normal term
            var buffer = "";
            for (var i = 0; i < Text.Length; i++)
            {
                s = MatchAt(ref Text, i, allSymbols);

                if (s == null)
                {
                    buffer += Text[i];
                }
                else if (s == "(")
                {
                    var startI = i;
                    Utils.Balance(ref Text, ref i, ')');

                    if (buffer.Trim() != "")
                    {
                        var functionName = buffer.Trim();
                        buffer = "";

                        var bracketTerm = new Term(Text.Substring(startI + 1, i - startI - 1), TermTypes.PARAMETER_LIST);
                        var functionTerm = Merge(new Term(functionName), bracketTerm);
                        functionTerm.Type = TermTypes.FUNCTION;

                        SubTerms.Add(functionTerm);
                    }
                    else
                    {
                        SubTerms.Add(new Term(Text.Substring(startI + 1, i - startI - 1)));
                    }
                }
                else if (s == "\"")
                {
                    var startI = i;
                    i = Utils.FindEndOfstring(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (s == ":")
                {
                    var end = FindEndOfSuffix(Text, i + 1);
                    var suffixName = Text.Substring(i + 1, end - i);
                    i += end - i;

                    if (buffer.Trim() != "")
                    {
                        SubTerms.Add(new Term(buffer.Trim()));
                        buffer = "";
                    }

                    if (SubTerms.Count > 0)
                    {
                        var last = SubTerms.Last();
                        SubTerms.Remove(last);

                        var structureTerm = Merge(last, new Term(suffixName, TermTypes.SUFFIX));
                        structureTerm.Type = TermTypes.STRUCTURE;
                        SubTerms.Add(structureTerm);
                    }
                }
                else if (s == "-")
                {
                    if (buffer.Trim() != "" ||
                        (SubTerms.Count > 0 && SubTerms.Last().Type != TermTypes.MATH_OPERATOR
                         && SubTerms.Last().Type != TermTypes.COMPARISON_OPERATOR))
                    {
                        // Not a sign, treat as operator
                        if (buffer.Trim() != "") SubTerms.Add(new Term(buffer.Trim()));
                        SubTerms.Add(new Term(s));

                        buffer = "";
                        i += s.Length - 1;
                    }
                    else
                    {
                        buffer += Text[i];
                    }
                }
                else
                {
                    if (buffer.Trim() != "") SubTerms.Add(new Term(buffer.Trim()));
                    SubTerms.Add(new Term(s));

                    buffer = "";
                    i += s.Length - 1;
                }
            }

            // If there's only one term, we're done!
            if (SubTerms.Count == 0)
            {
                Type = TermTypes.FINAL;
                return;
            }

            if (buffer.Trim() != "") SubTerms.Add(new Term(buffer));

            // If I end up with exactly one subTerm, then I AM that subterm. Exception: If I already have a special type
            if (SubTerms.Count == 1 && Type == TermTypes.REGULAR)
            {
                var child = SubTerms[0];
                SubTerms.Clear();

                CopyFrom(ref child);
            }
        }
Ejemplo n.º 6
0
Archivo: Term.cs Proyecto: QuakeIV/KOS
        public Term Merge(params Term[] terms)
        {
            var output = new Term("");

            foreach (var t in terms)
            {
                switch (t.Type)
                {
                    case TermTypes.PARAMETER_LIST:
                        output.Text += "(" + t.Text + ")";
                        break;
                    case TermTypes.SUFFIX:
                        output.Text += ":" + t.Text;
                        break;
                    case TermTypes.INDEX:
                        output.Text += "#" + t.Text;
                        break;
                    default:
                        output.Text += t.Text;
                        break;
                }

                output.SubTerms.Add(t);
            }

            return output;
        }
Ejemplo n.º 7
0
Archivo: Term.cs Proyecto: QuakeIV/KOS
 public void CopyFrom(ref Term from)
 {
     Text = from.Text;
     SubTerms = from.SubTerms;
     Type = from.Type;
 }