Ejemplo n.º 1
0
 // Makes a copy of another term
 public void CopyFrom(ref Term from)
 {
     this.Text = from.Text;
     this.SubTerms = from.SubTerms;
     this.Type = from.Type;
 }
Ejemplo n.º 2
0
        // This function pretty much does all the work
        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);

            // 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?
            String s = matchAt(ref Text, 0, ref 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 (String 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 (String 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 (String element in comparisonElements)
                {
                    SubTerms.Add(new Term(element));
                }

                return;
            }

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

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

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

                        Term bracketTerm = new Term(Text.Substring(startI + 1, i - startI - 1), TermTypes.PARAMETER_LIST);
                        Term 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 == "\"")
                {
                    int startI = i;
                    i = Utils.FindEndOfString(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (s == ":")
                {
                    int end = findEndOfSuffix(Text, i + 1);
                    String suffixName = Text.Substring(i + 1, end - i);
                    i += end - i;

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

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

                        Term 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 && this.Type == TermTypes.REGULAR)
            {
                Term child = SubTerms[0];
                SubTerms.Clear();

                CopyFrom(ref child);
            }
        }
Ejemplo n.º 3
0
        public override void Evaluate()
        {
            Term targetTerm = new Term(RegexMatch.Groups[1].Value);
            Expression e = new Expression(RegexMatch.Groups[2].Value, ParentContext);

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

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

                if (v != null)
                {
                    v.Value = e.GetValue();
                    State = ExecutionState.DONE;
                    return;
                }
            }
        }
Ejemplo n.º 4
0
        public Term Merge(params Term[] terms)
        {
            Term output = new Term("");

            foreach (Term t in terms)
            {
                output.Text += t.Type == TermTypes.PARAMETER_LIST ? "(" + t.Text + ")" :
                                t.Type == TermTypes.SUFFIX ? ":" + t.Text :
                                t.Text;

                output.SubTerms.Add(t);
            }

            return output;
        }
Ejemplo n.º 5
0
        public Term Merge(params Term[] terms)
        {
            Term output = new Term("");

            foreach (Term 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;
        }