Example #1
0
    /// <summary>Searches for constructions of the form "the TYPE which", as these indicate the beginning of a relative clause.
    /// Use only when parsing invocations, not definitions. </summary>
    public static theTypeWhich IsNameOfTypeWhich(List <string> words)
    {
        int          savedIndex = index;
        theTypeWhich theType    = new theTypeWhich {
            preposition = ""
        };

        theType.type = InheritanceTree.SingleOrDefault(itype => Match(words, itype.name));
        if (theType.type == null || index >= words.Count)
        {
            index = savedIndex;
            return(null);
        }
        if (Contains <RelativePronouns>(words[index]))
        {
            index++;
            return(theType);
        }
        if (stringnums.prepositions.Contains(words[index]) && Contains <RelativePronouns>(words[index + 1]))
        {
            theType.preposition = words[index];
            index += 2;
            return(theType);
        }
        index = savedIndex;
        return(null);
    }
Example #2
0
    /// <summary>Calculates if the child is the same type as parent (zero), or how many levels of inheritance it's
    /// removed from.  Returns -1 if the child isn't a subclass of (or equal to) the parent.</summary>
    public static int DistanceFrom(string child, string parent = "anything")// StandardType st, StandardType from = StandardType.anything)
    {
        int distance;

        for (distance = 0; child != null && child != parent; distance++)
        {
            child = InheritanceTree.Find(item => item.name == child).parent;
        }
        if (child != parent)
        {
            distance = -1;
        }
        return(distance); // Distance of 0 means same exact type. Distance of 1 means one's a direct parent, etc.
    }
Example #3
0
    /// <summary>Creates a new type defined by the user, as a subclass of another user-defined type.</summary>
    public static InheritedType CreateNewType(string ident, string parentClassName)
    {
        if (string.IsNullOrWhiteSpace(parentClassName))
        {
            return(CreateNewType(ident));
        }
        var newType = new InheritedType()
        {
            name = ident, parent = parentClassName, typeid = (StandardType)(InheritanceTree.Count + 2)
        };

        InheritanceTree.Add(newType);
        just_declared_new_type = true;
        return(newType);
    }
Example #4
0
        public void InheritanceTree_Test()
        {
            ModelRoot modelRoot = new ModelRoot();

            Interface I00 = new Interface(modelRoot, "I00");
            Interface I01 = new Interface(modelRoot, "I01");
            Interface I02 = new Interface(modelRoot, "I02");
            Interface I03 = new Interface(modelRoot, "I03");
            Interface I04 = new Interface(modelRoot, "I04");
            Interface I05 = new Interface(modelRoot, "I05");
            Entity    C01 = new Entity(modelRoot, "C01");
            Entity    C02 = new Entity(modelRoot, "C02");
            Entity    C03 = new Entity(modelRoot, "C03");

            // assign inheritance
            I02.SetInheritance(I01);

            I04.SetInheritance(I01);

            I03.SetInheritance(I02, I04);

            I05.SetInheritance(I04, I02);

            C01.SetInheritance(I03, I05, I00);

            C02.SetInheritance(C01);

            //C03.SetInheritance(I00);

            // test case #1
            var inheritanceTrees = InheritanceTree.Create(C01, C02, C03);

            InheritanceTree.RebuildTree(true, inheritanceTrees.ToArray());
            InheritanceTree C01_inheritanceTree = inheritanceTrees[0];
            //C01_inheritanceTree.RebuildTree(true);

            ReadOnlyCollection <InheritanceNode> flatList       = C01_inheritanceTree.GetFlatList(InheritanceListMode.CurrentLevel);
            IEnumerable <IInterface>             flatInterfaces = flatList.Select(node => node.Interface);

            Assert.IsTrue(flatInterfaces.Contains(I03));
            Assert.IsTrue(flatInterfaces.Contains(I05));
            Assert.IsTrue(flatInterfaces.Contains(I00));

            // test case #2
            //inheritanceTrees[1].RebuildTree(true);
            var mergedPaths = InheritanceTree.MergePaths(inheritanceTrees).ToArray();
        }
Example #5
0
    /// <summary> Creates the lookup table for what types inherit from what. This would be hard-coded if C# would allow. </summary>
    public static void InitInheritanceTree()
    {
        for (StandardType st = StandardType.anything; st < StandardType.Count; st++)
        {
            var it = new InheritedType()
            {
                name = st.ToString(), subtypes = null, typeid = st
            };
            switch (st)
            {
            case StandardType.anything: it.parent = null; break;

            case StandardType.something:
            case StandardType.nothing: it.parent = StandardType.anything.ToString(); break;

            case StandardType.valueType:
            case StandardType.referenceType: it.parent = StandardType.something.ToString(); break;

            case StandardType.number:
            case StandardType.discrete:
            case StandardType.reference:
            case StandardType.structure: it.parent = StandardType.valueType.ToString(); break;

            case StandardType.homogene:
            case StandardType.heterogene:
            case StandardType.value: it.parent = StandardType.referenceType.ToString(); break;

            case StandardType.percent:
            case StandardType.money: it.parent = StandardType.number.ToString(); break;

            case StandardType.position:
            case StandardType.boole: it.parent = StandardType.discrete.ToString(); break;

            case StandardType.time: it.parent = StandardType.structure.ToString(); break;

            case StandardType.text:
            case StandardType.list: it.parent = StandardType.homogene.ToString(); break;

            case StandardType.sequence:
            case StandardType.Object: it.parent = StandardType.heterogene.ToString(); break;

            default: Console.WriteLine("ERROR IN COMPILER: unknown base type {0}", st); break;
            }
            InheritanceTree.Add(it);
        }
        //TestInheritanceTree();
    }
Example #6
0
    /// <summary>Returns the parent of the passed-in type. Returns Anything for unrecognized types as well as Anything
    /// itself.  Returns Object (which is assumed) for types which claim to have no parent.  (Those were implicitly
    /// created by a "list of" construction, and will presumably be filled in later.) </summary>
    public static StandardType ParentOf(StandardType st)
    {
        if (st == StandardType.anything)
        {
            return(StandardType.anything);                             // anything is anything, the root type
        }
        InheritedType it = InheritanceTree.Find(item => item.typeid == st);

        if (it == null)
        {
            return(StandardType.anything);
        }
        if (it.parent == null)
        {
            return(StandardType.Object);                   // only happens with used but undefined types
        }
        return(InheritanceTree.Find(item => item.name == it.parent).typeid);
    }
Example #7
0
    /*public static void TestInheritanceTree()
     * {
     *  IsA(StandardType.number, StandardType.number);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.number, StandardType.number, (distance > -1));
     *  IsA(StandardType.number, StandardType.valueType);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.number, StandardType.valueType, (distance > -1));
     *  IsA(StandardType.Object, StandardType.anything);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.Object, StandardType.anything, (distance > -1));
     *  IsA(StandardType.anything, StandardType.anything);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.anything, StandardType.anything, (distance > -1));
     *  IsA(StandardType.text, StandardType.valueType);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.text, StandardType.valueType, (distance > -1));
     *  IsA(StandardType.text, StandardType.reference);
     *  Console.WriteLine("Is {0} a {1}? {2}.", StandardType.text, StandardType.reference, (distance > -1));
     *  IsA(StandardType.number, StandardType.percent);
     *  Console.WriteLine("Is {0} a {1}? {2}.", "number", "percent", (distance > -1));
     *  IsA(StandardType.percent, StandardType.number);
     *  Console.WriteLine("Is {0} a {1}? {2}.", "percent", "number", (distance > -1));
     *  IsA(StandardType.number, StandardType.anything);
     *  Console.WriteLine("Is {0} a {1}? {2}.", "number", "anything", (distance > -1));
     *  IsA(StandardType.number, StandardType.something);
     *  Console.WriteLine("Is {0} a {1}? {2}.", "number", "something", (distance > -1));
     *  /*IsA(StandardType.money, StandardType.car);
     *  Console.WriteLine("Is {0} a {1}? {2}.", "money", "car", (distance > -1));
     *  IsA("car", "object");
     *  Console.WriteLine("Is {0} a {1}? {2}.", "car", "object", (distance > -1));
     *  IsA("car", "something");
     *  Console.WriteLine("Is {0} a {1}? {2}.", "car", "something", (distance > -1));
     * }*/

    /// <summary>Returns the name of the given StandardType, including newly-defined types in the source. </summary>
    public static string NameOfType(StandardType?s)
    {
        if (s == null)
        {
            return("whatever");
        }
        var st = s.Value;

        if (st <= StandardType.anything)
        {
            return(StandardType.anything.ToString());
        }
        if (st < StandardType.Count)
        {
            return(st.ToString());
        }
        InheritedType it = InheritanceTree.Find(item => item.typeid == st);

        return(it.name);
    }
Example #8
0
        public static void Compile(string code, string outFileName)
        {
            //System.IO.DirectoryInfo d = new DirectoryInfo("E:\\Universidad\\4to\\compilacion 2\\testing");
            //var files=d.GetFiles("*.cl");
            //foreach (var file in files)
            //{



            var t = Parse(code);

            if (t == null)
            {
                Console.ReadLine();
                return;
            }

            var ast1            = t.GetAST1();
            var semanticResults = SemantickCheck((COOLLenguage.SemanticCheck.AST1.Program)ast1);

            if (semanticResults.Item1 > 0)
            {
                Console.WriteLine("Compilation terminated because of semantic errors");
                Console.ReadLine();
                return;
            }

            var Context = semanticResults.Item2;
            //build ast to generate
            var table        = MIPSCodeGenerator.SymbolUtils.IdTable;
            var astgenerator = (MIPSCodeGenerator.Program)ast1.GetAstCodeGenerator(table);
            var inheritance  = new InheritanceTree(astgenerator);

            var Cg = new CodeGen(astgenerator, inheritance, new StreamWriter(outFileName));

            Cg.Generate();
            Console.WriteLine("Compilation terminated sucessfully");
            Console.ReadLine();
        }
Example #9
0
    /// <summary>Part of CreateParameterObject, which does the work for ParseNounPhraseForParameter.
    /// When declaring the parameters of a function's signature, this helper digests the "many..." parameter, which isn't
    /// a trivial operation, considering that lists have subtypes, some of which may not be known yet.</summary>
    static parameter CreateListParameterObject(ref StandardType?type, string ident, bool adjnounswap)
    {
        if (ident == null && type == null)
        {
            Console.WriteLine("  ERROR: many whats?");
            method_being_constructed.problems++;
            return(new parameter("", ident, Article.many, (StandardType)0, adjnounswap));
        }

        //either ident has value, type has value, or both have value
        // if basetype has value, then ident must not be a type. i.e., warn of "numeric car" or "many numeric cars" when car is of type object
        if (ident.HasValue() && type.HasValue)
        {
            InheritedType it = InheritanceTree.Find(item => item.name == ident);
            if (it != null && it.typeid != type.Value)
            {
                Console.WriteLine("  WARNING: '{0}' is declared elsewhere as a '{1}', not a '{2}'", it.name, NameOfType(it.typeid), NameOfType(type.Value));
            }
        }

        // for the term "several/many/multiple numbers", set ident to the whole term "many numbers" -- a generated name
        if (string.IsNullOrEmpty(ident))
        {
            ident = string.Format("many {0}", type);
        }

        // for the term "many gadgets", set type to "gadgets" by finding the type by name, or making a new one
        if (type == null)
        {
            InheritedType subtype = InheritanceTree.Find(item => item.name == ident);
            if (subtype == null)
            {   // make a new type, "gadgets", whose parent (and subtype, if applicable) to be filled out later?
                subtype = new InheritedType()
                {
                    name = ident, typeid = (StandardType)(InheritanceTree.Count + 2)
                };
                InheritanceTree.Add(subtype);
            }
            type = subtype.typeid;
        }

        // now all three are populated:  mode, type, ident
        // now, does our composite type already exist? Else make a new typeid
        string        typeAsString = type.Value.ToString();
        InheritedType aggregate    = InheritanceTree.Find(item => item.name == ident && item.parent == StandardType.list.ToString() && item.subtypes == typeAsString);

        if (aggregate != null)
        {
            Console.WriteLine("  another '{0}' (a list of {1})", ident, NameOfType(type.Value));
            return(new parameter("", ident, Article.many, type.Value, adjnounswap));
        }
        aggregate = new InheritedType()
        {
            name = ident, parent = StandardType.list.ToString(), subtypes = type.Value.ToString(), typeid = (StandardType)(InheritanceTree.Count + 2)
        };
        InheritanceTree.Add(aggregate);
        just_declared_new_type = true;

        string typename = NameOfType(type.Value);

        if (ident != typename)
        {
            Console.WriteLine("  a list of {0}, called {1}", typename, ident);
        }
        else
        {
            Console.WriteLine("  a list of {0}", typename);
        }

        return(new parameter("", ident, Article.many, type.Value, adjnounswap));
    }
Example #10
0
    /// <summary>When defining a parameter of a new function's signature, and once parsing of that parameter is done, this converts
    /// the information into a parameter object. Uses CreateListParameterObject since that is a tome in itself.</summary>
    static parameter CreateParameterObject(string[] words, Article?mode, StandardType?type, StandardTypeAdjective?adjtype, string ident, bool identIsAdjectiveTypeIsNoun = false, bool isSubject = false)
    {
        // something/nothing/anything aren't preceded by an article. Treat as "a thing"
        if (type.HasValue && type <= StandardType.something)
        {
            Console.WriteLine("  declaring a parameter which {0}", type == StandardType.nothing ? "must be nothing" : type == StandardType.anything ? "could be anything" : "mustn't be nothing");
            return(new parameter("", type.Value.ToString(), Article.a, type.Value, identIsAdjectiveTypeIsNoun));
        }

        switch (mode)
        {
        case Article.the:
            if (type.HasValue)
            {
                if (adjtype == null)
                {
                    Console.WriteLine("ERROR: we say 'the {0}' to use it later in the sentence. Here we should say 'a {0}'.", NameOfType(type.Value));
                }
                else
                {
                    Console.WriteLine("ERROR: we say 'the {0}' to use it later in the sentence. Here we should say 'a {0} {1}'.", adjtype, ident == "" ? "name-of-it" : ident);
                }
                method_being_constructed.problems++;
                return(null);
            }
            if (isSubject || isQuestion)
            {
                just_declared_new_type = isSubject;     // and a global instance, as well
                return(new parameter("", ident, mode ?? Article.the, type ?? StandardType.number, identIsAdjectiveTypeIsNoun));
            }
            Console.WriteLine("ERROR: we should use 'a' not 'the' in this part of the sentence.");
            method_being_constructed.problems++;
            return(null);

        case Article.many:
            return(CreateListParameterObject(ref type, ident, identIsAdjectiveTypeIsNoun));

        case Article.a:
            if (type.HasValue)     // also, should we disallow it when the ident is also a type? "numeric car"
            {
                Console.WriteLine("  declaring a parameter of type {0} called '{1}'", NameOfType(type.Value), ident);
                InheritedType it = InheritanceTree.Find(item => item.name == ident);
                if (it != null && it.typeid != type.Value)
                {
                    Console.WriteLine("  WARNING: '{0}' is declared elsewhere as a '{1}', not a '{2}'", it.name, NameOfType(it.typeid), NameOfType(type.Value));
                }
            }
            else if (isQuestion && incarnations == TheVerbIs)
            {
                for (IdentEndsAt = index; IdentEndsAt < words.Length && words[IdentEndsAt] != "?"; IdentEndsAt++)
                {
                    ;
                }
                if (IdentEndsAt >= words.Length)
                {
                    Console.WriteLine("  ERROR: I thought #{0} was a question because it began with '{1}', but I couldn't find a question mark afterward.", method_being_constructed.prompt, method_being_constructed.question);
                    method_being_constructed.problems++;
                    return(null);
                }
                Console.WriteLine("  defining a {0} by Q&A.", ident);
                ident = ParseNewIdentifier(words, ref type);
            }
            else if (!isSubject)
            {
                if (ident == "")     // then we have no type info whatsoever. Just get a new ident
                {
                    ident = ParseNewIdentifier(words, ref type);
                }
                Console.WriteLine("  TODO: is '{0}' textual? Numeric? Or something more complex?", ident);
                method_being_constructed.todo++;
            }
            // defining a new type
            else if (InheritanceTree.Find(item => item.name == ident) != null)
            {
                Console.WriteLine("  ERROR: re-defining type '{0}'.", ident);
                method_being_constructed.problems++;
                //return null; // let it parse for now
                return(new parameter("", ident, mode ?? Article.a, type ?? (StandardType)0, identIsAdjectiveTypeIsNoun));
            }
            else
            {
                // object until proven otherwise. heck, it may not even be a type, it might be an instance or variable.
                var newType = CreateNewType(ident);
                Console.WriteLine("  assuming '{0}' is a new type", newType.name, newType.typeid);
            }
            break;

        case null:
            if (type == null)
            {
                return(null);
            }
            if (ident == null)
            {
                Console.WriteLine("  plural(?) parameter of type {0}", type);
            }
            else
            {
                Console.WriteLine("  plural(?) parameter of type {1} called the {0}", ident, NameOfType(type.Value));
            }
            break;
        }
        return(new parameter("", ident, mode ?? Article.a, type ?? (StandardType)0, identIsAdjectiveTypeIsNoun));
    }
Example #11
0
 public InheritanceTree GetInheritanceTree()
 {
     return(InheritanceTree.Create(this));
 }
Example #12
0
    public string[] InheritanceList(IPersistentType persistentType)
    {
        List <string> result = null;

        if (persistentType is ITypedEntitySet)
        {
            ITypedEntitySet typedEntitySet = (ITypedEntitySet)persistentType;
            result = new List <string> {
                BuildXtensiveType(OrmType.EntitySet, EscapeNameWithNamespace(typedEntitySet.ItemType, persistentType))
            };
            return(result.ToArray());
        }

        IInterface      @interface            = persistentType as IInterface;
        string          entityBaseBaseTypeStr = null;
        InheritanceTree inheritanceTree       = null;

        if (@interface != null)
        {
            inheritanceTree = InheritanceTreeCache.Get(@interface);
            if (!inheritanceTree.TreeRebuilded)
            {
                inheritanceTree.RebuildTree(false);
            }

            IInterface entityBaseBaseType = null;
            if (@interface is IEntityBase)
            {
                IEntityBase entityBase = (IEntityBase)@interface;
                entityBaseBaseType    = entityBase.BaseType;
                entityBaseBaseTypeStr = entityBase.BaseType == null
                                            ? null
                                            : EscapeNameWithNamespace(entityBase.BaseType, persistentType);
            }

            ReadOnlyCollection <InheritanceNode> flatList           = inheritanceTree.GetFlatList(InheritanceListMode.CurrentLevel);
            IEnumerable <IInterface>             allInheritanceList = flatList.Select(node => node.Interface).Where(item => item != entityBaseBaseType);

            result = allInheritanceList.Select(item => EscapeNameWithNamespace(item, persistentType)).ToList();
        }

        string commonBaseType = persistentType.TypeKind == PersistentTypeKind.Structure
                                    ? BuildXtensiveType(OrmType.Structure)
                                    : BuildXtensiveType(OrmType.Entity);

        bool noInheritance = (result == null || result.Count == 0 && persistentType.TypeKind != PersistentTypeKind.Interface) && string.IsNullOrEmpty(entityBaseBaseTypeStr);

        if (noInheritance)
        {
            result = new List <string>();
            if (!this.ActiveDTOStage)
            {
                result.Add(commonBaseType);
            }
        }
        else
        {
            if (persistentType.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Structure))
            {
                if (@interface.InheritingByInterfaces.Count == 0 && string.IsNullOrEmpty(entityBaseBaseTypeStr) && !this.ActiveDTOStage)
                {
                    result.Insert(0, commonBaseType);
                }
            }
            else if (persistentType.TypeKind == PersistentTypeKind.Interface)
            {
                bool inheritesCommonIEntity = @interface.InheritsIEntity == InheritsIEntityMode.AlwaysInherit;
                if (!inheritesCommonIEntity)
                {
                    inheritesCommonIEntity = @interface.InheritedInterfaces.Count == 0;
                }

                if (inheritesCommonIEntity && !this.ActiveDTOStage)
                {
                    string commonIEntityType = BuildXtensiveType(OrmType.IEntity);
                    result.Insert(0, commonIEntityType);
                }
            }
        }

        if (!string.IsNullOrEmpty(entityBaseBaseTypeStr))
        {
            result.Insert(0, entityBaseBaseTypeStr);
        }

        return(result.ToArray());
    }