示例#1
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);
    }
示例#2
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();
    }
示例#3
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));
    }