Пример #1
0
        // assumes a valid syntactic element
        public ProgramFile Add(string line)
        {
            SyntacticElement se = LineParser.Parse(line);

            if (se is InvalidSyntacticElement)
            {
                throw new ArgumentException("Unable to add invalid syntactic element! " + se.Text);
            }
            if (se is PredicateSyntacticElement)
            {
                PredicateSyntacticElement p = (PredicateSyntacticElement)se;
                if (!programData.Add(p))
                {
                    throw new ArgumentException("Predicate {0} already exists", p.Name);
                }
            }
            if (se is PropositionExpression)
            {
                PropositionExpression p = (PropositionExpression)se;
                programData.Add(p);
            }
            if (se is PredicateSetterSyntacticElement)
            {
                programData.Add((PredicateSetterSyntacticElement)se);
            }
            if (se is ThereforeSyntacticElement)
            {
                ThereforeSyntacticElement t = (ThereforeSyntacticElement)se;
                programData.Add(t);
            }
            if (!(se is NullSyntacticElement))
            {
                Listing.Add(line);
            }
            return(this);
        }
Пример #2
0
        public static SyntacticElement Parse(List <string> lineElements)
        {
            // syntax is
            // <predicate> ::= predicate <name> > <predicate text> [= <value>]
            // <name>      ::= (<letter>|<number>|<symbol>)*
            // <symbol>    ::= _@&^*$£!
            // <value>     ::= true|false
            // <predicate text> may not contain a reserved word
            //string word = line.ContainsaReservedWord();
            InvalidSyntacticElement ise = null;

            ise = CheckForPredicateSymbol(lineElements);
            if (ise != null)
            {
                return(ise);
            }
            ise = CheckForReservedWordsUsage(lineElements);
            if (ise != null)
            {
                return(ise);
            }
            PredicateSyntacticElement pe = new PredicateSyntacticElement();

            pe.Name = lineElements[1];
            int equalitySymbolPosition = lineElements.IndexOf("=");

            if (equalitySymbolPosition < 0)
            {
                pe.Value     = null;
                pe.Predicate = string.Join(" ", lineElements.Skip(3).ToArray());
            }
            else if (equalitySymbolPosition == lineElements.Count() - 2) // 0 1 2 3 4 5 cnt=6
            {                                                            // p p > t = v
                pe.Predicate = string.Join(" ", lineElements.Skip(3).
                                           Take(lineElements.Count() - ((lineElements.Count() - equalitySymbolPosition) + 3)).ToArray());
                int valueElement = equalitySymbolPosition + 1;
                if ((lineElements.Count() - 1) == valueElement)
                {
                    bool result;
                    if (bool.TryParse(lineElements.Last(), out result))
                    {
                        pe.Value = result;
                    }
                    else
                    {
                        int iResult = 0;
                        if (int.TryParse(lineElements.Last(), out iResult))
                        {
                            if (iResult == 0)
                            {
                                pe.Value = false;
                            }
                            else if (iResult == 1)
                            {
                                pe.Value = true;
                            }
                            else
                            {
                                ise = new InvalidSyntacticElement("Invalid value", "value must be [true|false], numbers or letters are non truth values and aliases for true or false cannot be used");
                                return(ise);
                            }
                        }
                        else
                        {
                            ise = new InvalidSyntacticElement("Invalid value", "value must be [true|false], numbers or letters are non truth values and aliases for true or false cannot be used");
                            return(ise);
                        }
                    }
                }
            }
            else
            {
                ise = new InvalidSyntacticElement("Equals sign not in the expected place", "the value must be [true|false], numbers or letters are non truth values and aliases for true or false cannot be used and the value must be the last thing on the line");
                return(ise);
            }
            if (string.IsNullOrWhiteSpace(pe.Predicate))
            {
                ise = new InvalidSyntacticElement("No predicate text found", "a predicate with no words, you must be a buddha if you think that is sensical");
                return(ise);
            }
            return(pe);
        }