Example #1
0
 public virtual string UnionAlternativeName(UnionAlternative alt, string userGivenName)
 {
     if (!string.IsNullOrEmpty(userGivenName))
     {
         return(userGivenName);
     }
     return($"u{alt.Index}");
 }
Example #2
0
        private static List <UnionDefinition> Parse(TextReader inputReader)
        {
            List <UnionDefinition> result = new List <UnionDefinition>();
            string allText = inputReader.ReadToEnd();

            // Remove comments and newlines
            allText = new Regex(@"/\*.*?\*/", RegexOptions.Multiline).Replace(allText, "");
            allText = new Regex(@"//.*$", RegexOptions.Multiline).Replace(allText, "");
            allText = new Regex(@"[\r\n]+").Replace(allText, "");

            string identifier = @"[A-Za-z][A-Za-z0-9]*";

            foreach (Match matchDef in new Regex(@"datatype\s+(" + identifier + @")\s*(:\s*([^ ]+)\s*)?{([^}%]*)(%%(.*?)%%)?}").Matches(allText))
            {
                UnionDefinition definition = new UnionDefinition();
                definition.Name         = matchDef.Groups[1].Value;
                definition.Alternatives = new List <UnionAlternative>();

                definition.BaseClass = matchDef.Groups[3].Success ? matchDef.Groups[3].Value : "";
                definition.RawText   = matchDef.Groups[5].Success ? matchDef.Groups[6].Value : "";

                string alternatives = matchDef.Groups[4].Value;
                foreach (Match matchAlt in new Regex(@"\b(" + identifier + @")\s*(\((.*?)\))?").Matches(alternatives))
                {
                    UnionAlternative alternative = new UnionAlternative();
                    alternative.Name     = matchAlt.Groups[1].Value;
                    alternative.Contents = new List <UnionAlternativeMember>();

                    string[] contents = matchAlt.Groups[3].Value.Split(new char[] { ' ', ',' },
                                                                       StringSplitOptions.RemoveEmptyEntries);
                    if ((contents.Length % 2) != 0)
                    {
                        throw new Exception("Must specify type and name for each alternative data field. Types cannot contain spaces.");
                    }
                    for (int i = 0; i < contents.Length; i += 2)
                    {
                        UnionAlternativeMember member = new UnionAlternativeMember();
                        member.Type = contents[i];
                        member.Name = contents[i + 1];
                        alternative.Contents.Add(member);
                    }

                    definition.Alternatives.Add(alternative);
                }

                result.Add(definition);
            }

            return(result);
        }
Example #3
0
        public Expression VisitUnion(UnionType ut)
        {
            // A constant can't have a union value, so we coerce it to the appropriate type.
            UnionAlternative a = ut.FindAlternative(pOrig);

            if (a == null)
            {
                // This is encountered when the original type is a [[word]] but
                // the alternatives are, say, [[int32]] and [[uint32]]. In this case
                // we pick an arbitrary type and go with it.
                //$REVIEW: should emit a warning.
                a = ut.Alternatives.Values[0];
            }
            c.TypeVariable.DataType = a.DataType;
            return(c);
        }
Example #4
0
        public Expression VisitUnion(UnionType ut)
        {
            UnionAlternative alt = ut.FindAlternative(dtOriginal);

            if (alt == null)
            {
                throw new TypeInferenceException("Unable to find {0} in {1} (offset {2}).", dtOriginal, ut, offset);
            }

            dt         = alt.DataType;
            dtOriginal = alt.DataType;
            if (ut.PreferredType != null)
            {
                complexExp = new Cast(ut.PreferredType, complexExp);
            }
            else
            {
                complexExp = new FieldAccess(alt.DataType, complexExp, alt.Name);
            }
            return(dt.Accept(this));
        }
        public Expression VisitUnion(UnionType ut)
        {
            UnionAlternative alt = ut.FindAlternative(dtComplexOrig);

            if (alt == null)
            {
                Debug.Print("Unable to find {0} in {1} (offset {2}).", dtComplexOrig, ut, offset);          //$diagnostic service
                return(expComplex);
            }

            dtComplex     = alt.DataType;
            dtComplexOrig = alt.DataType;
            if (ut.PreferredType != null)
            {
                expComplex = new Cast(ut.PreferredType, expComplex);
            }
            else
            {
                expComplex = CreateFieldAccess(ut, alt.DataType, expComplex, alt);
            }
            return(dtComplex.Accept(this));
        }