Esempio n. 1
0
        /// <summary> Used by "A car is.." and "The corvette is..." but can possibly be used to create a symbol from any aNP or NP. </summary>
        protected Symbol CreateSymbolFromNP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context)
        {
            string adj = "", noun = "", det = "a";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.The: det = forest.a[i]; break;

                case NonTerminalSymbol.a: det = forest.a[i]; break;

                case NonTerminalSymbol.Adj: adj += (adj == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.N: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;

                default: throw new Exception("CreateSymbolFromSubject() visitor found a " + nt + " instead of a N or Adj.");
                }
            });

            if (adj == "" && noun == "")
            {
                throw new Exception("both adj & noun are blank???  CreateSymbolFromSubject()");
            }

            if (adj != "" && noun != "")
            {
                Symbol nountype = Scope.Current.ResolveLoneSymbol(noun);
                if (nountype != null)
                {
                    throw new RedeclareException(noun);
                }
                Symbol adjtype = (adj != "") ? Scope.Current.ResolveLoneSymbol(t.AdjToNounTypes.OrKey(adj)) : null;
                if (adjtype != null)
                {
                    throw new RedeclareException(adj);
                }
                throw new RedefineException(noun, adj);
            }

            string fullname  = (det != "a") ? "the " + (adj + " ").TrimStart() + noun : (noun != "") ? noun : adj;
            var    newSymbol = new Symbol
            {
                Name         = fullname,
                Type         = t.Something,
                Category     = (det != "a") ? /* Global */ Categories.Variable : Categories.ClassObjectStructType,
                PartOfSpeech = (fullname == adj) ? NonTerminalSymbol.Adj : NonTerminalSymbol.N,
            };

            "  DEFINED:".Log(newSymbol.Name);
            return(Scope.Current.Define(newSymbol));
        }
Esempio n. 2
0
        /// <summary> Used by invocations. </summary>
        protected Symbol FillParameterWith_NP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context)
        {
            if (visitor.currentType != NonTerminalSymbol.The && visitor.currentType != NonTerminalSymbol.Adj && visitor.currentType != NonTerminalSymbol.N &&
                visitor.currentType != NonTerminalSymbol.AdN && visitor.currentType != NonTerminalSymbol.G && visitor.currentType != NonTerminalSymbol.NP)
            {
                throw new Exception(visitor.currentType + " was passed to FillParameterWith_NP; was expecting Adj, N, AdN, a, G");
            }
            string sym = "", det = "the";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.The:
                    det = forest.a[i];
                    break;

                case NonTerminalSymbol.N:
                case NonTerminalSymbol.NP:
                case NonTerminalSymbol.Adj:
                    sym += (sym == "") ? forest.a[i] : " " + forest.a[i];
                    break;

                case NonTerminalSymbol.Rp:
                    PrintAST(forest);
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, the/each, or NP.");

                default:
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, the/each, or NP.");
                }
            });
            if (sym == "")
            {
                throw new Exception("sym is blank??? FillParameterWith_NP()");
            }

            det = (det == "several" || det == "many" || det == "each" || det == "every") ? "each " : "the ";
            Symbol symbol = Scope.Current.ResolveLoneSymbol(det + sym) ?? t.AsNumber(sym);

            if (symbol == null)
            {
                throw new UndefinedException(det + sym);
            }
            context.InvocationBeingDefined.SuppliedArguments.Add(new Parameter(context.prepFound, symbol));
            return(symbol);
        }
Esempio n. 3
0
        /// <summary> Used for parameters of a function, struct, etc. For subject, used by struct.</summary>
        protected Symbol CreateParameterFrom_aNP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context, bool isSubject = false)
        {
            if (visitor.currentType != NonTerminalSymbol.a && visitor.currentType != NonTerminalSymbol.Adj && visitor.currentType != NonTerminalSymbol.N &&
                visitor.currentType != NonTerminalSymbol.AdN && visitor.currentType != NonTerminalSymbol.aGP && visitor.currentType != NonTerminalSymbol.G && visitor.currentType != NonTerminalSymbol.aNP)
            {
                throw new Exception(visitor.currentType + " was passed to CreateParameterFrom_aNP; was expecting Adj, N, AdN, a, G, aGP");
            }
            string adj = "", noun = "", det = "a";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.a: det = forest.a[i]; break;

                case NonTerminalSymbol.Adj: adj += (adj == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.N: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.aNP: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;                         // "nothing"

                default:
                    Scope.Abandon();
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, a/many, or aNP.");
                }
            });

            if (adj == "" && noun == "")
            {
                Scope.Abandon();
                throw new Exception("both adj & noun are blank??? CreateParameterFrom_aNP()");
            }

            Symbol nountype = Scope.Current.ResolveLoneSymbol(noun, Categories.ClassObjectStructType);
            Symbol adjtype  = (nountype == null && adj != "") ? Scope.Current.ResolveLoneSymbol(t.AdjToNounTypes.OrKey(adj), Categories.ClassObjectStructType) : null;

            if (nountype == null && adjtype == null)
            {
                Scope.Abandon();
                throw new Exception("neither '" + noun + "' nor '" + adj + "' are types in current scope. CreateParameterFrom_aNP()");
            }

            bool   dontFullyQualify = (adjtype == null && adj != "") || (adjtype != null && nountype == null) || (isSubject);
            string sym = (adjtype == null) ? (adj + " ").TrimStart() + noun : (nountype != null ? t.AdjToNounTypes.OrKey(adj) : noun);

            string fullName = dontFullyQualify ? sym : (sym + " " + (context.SignatureBeingDefined.verb == "" ? "" : "being " + context.SignatureBeingDefined.verb.FormOf(Inflections.past_participal)) + " " + context.prepFound).TrimEnd();

            det = (det == "several" || det == "many") ? "each " : "the ";

            string verb_s = context.SignatureBeingDefined.verb.FormOf(Inflections.plural);
            string verber = !isSubject ? "" : verb_s.EndsWith("e") ? det + verb_s + "r" : det + verb_s + "er";

            var newSymbol = new Symbol
            {
                Name         = det + fullName,
                Type         = (adjtype == null) ? nountype.Name : t.AdjToNounTypes.OrKey(adjtype.Name),
                Category     = Categories.Variable,             // local variable
                PartOfSpeech = NonTerminalSymbol.N,
                SecondName   = fullName.Contains(" being ") ? det + fullName.Substring(0, fullName.IndexOf(" being ")) : verber,
            };

            context.SignatureBeingDefined[context.prepFound] = newSymbol;
            context.SignatureBeingDefined.HasSubject        |= isSubject;
            "  PARAM:".Log(newSymbol.Name + "  (" + (det == "the " ? "a " : det) + newSymbol.Type + ") " + (newSymbol.SecondName == "" ? "" : "\"" + newSymbol.SecondName + "\""));
            return(Scope.Current.Define(newSymbol));
        }