예제 #1
0
        protected XCodeTypeReference BuildDataType(XSharpParser.DatatypeContext context)
        {
            //
            // Datatype is ptrDatatype
            // or arrayDatatype
            // or simpleDatatype
            // or nullableDatatype
            // they all have a TypeName
            XSharpParser.TypeNameContext tn = null;
            var sName = context.GetText();

            if (context is XSharpParser.PtrDatatypeContext)
            {
                XSharpParser.PtrDatatypeContext ptrData = (XSharpParser.PtrDatatypeContext)context;
                tn = ptrData.TypeName;
            }
            else if (context is XSharpParser.ArrayDatatypeContext)
            {
                XSharpParser.ArrayDatatypeContext aData = (XSharpParser.ArrayDatatypeContext)context;
                tn = aData.TypeName;
            }
            else if (context is XSharpParser.SimpleDatatypeContext)
            {
                XSharpParser.SimpleDatatypeContext sdt = (XSharpParser.SimpleDatatypeContext)context;
                tn = sdt.TypeName;
            }
            else if (context is XSharpParser.NullableDatatypeContext)
            {
                XSharpParser.NullableDatatypeContext ndc = context as XSharpParser.NullableDatatypeContext;
                tn = ndc.TypeName;
            }
            XCodeTypeReference expr = null;

            if (tn != null)
            {
                if (tn.NativeType != null)
                {
                    expr = BuildNativeType(tn.NativeType);
                }
                else if (tn.XType != null)
                {
                    expr = BuildXBaseType(tn.XType);
                }
                else if (tn.Name != null)
                {
                    expr = BuildName(tn.Name);
                }
            }
            else
            {
                expr = new XCodeTypeReference(typeof(void));
            }

            if (sName.Contains("[") || sName.Contains(">"))
            {
                // work around to fix type problems with generics and arrays
                expr.UserData[XSharpCodeConstants.USERDATA_CODE] = sName;
            }
            return(expr);
        }
예제 #2
0
        protected XCodeTypeReference BuildSimpleName(XSharpParser.SimpleNameContext simpleName)
        {
            XCodeTypeReference expr = null;
            //
            string name = simpleName.Id.GetText();
            string gen  = "";

            if (simpleName.GenericArgList != null)
            {
                string argList = "";
                int    i       = 0;
                foreach (var generic in simpleName.GenericArgList._GenericArgs)
                {
                    if (i > 0)
                    {
                        argList += ",";
                    }
                    var tmp = BuildDataType(generic);
                    argList += tmp.BaseType;
                    i++;
                }
                //
                gen = "`" + i.ToString() + "[" + argList + "]";
            }
            expr = BuildTypeReference(name + gen);
            //
            return(expr);
        }
예제 #3
0
 public override void EnterLocalvar([NotNull] XSharpParser.LocalvarContext context)
 {
     if (initComponent != null)
     {
         if (context.DataType != null)
         {
             CodeStatementCollection          locals    = new CodeStatementCollection();
             XCodeTypeReference               localType = BuildDataType(context.DataType);
             CodeVariableDeclarationStatement local;
             // Any previous Local ?
             while (LocalDecls.Count > 0)
             {
                 XSharpParser.LocalvarContext tmpContext = LocalDecls.Pop();
                 local = BuildLocalVar(tmpContext, localType);
                 locals.Add(local);
             }
             // Now, manage the current one
             local = BuildLocalVar(context, localType);
             locals.Add(local);
             //
             initComponent.Statements.AddRange(locals);
         }
         else
         {
             // We may have something like
             // LOCAL x,y as string
             // for x, we don't have a DataType, so save it
             LocalDecls.Push(context);
         }
     }
 }
        protected XCodeTypeReference BuildName(XSharpParser.NameContext context)
        {
            XCodeTypeReference expr = null;
            //
            var sName = context.GetText();

            //if (!sName.Contains(".") && !sName.Contains(":") && !sName.Contains(">"))
            //{
            //    return BuildTypeReference(sName);
            //}
            if (!sName.EndsWith(">"))
            {
                System.Type type = findType(sName);
                if (type != null)
                {
                    return(new XCodeTypeReference(type));
                }
            }
            if (context is XSharpParser.QualifiedNameContext)
            {
                XSharpParser.QualifiedNameContext qual = (XSharpParser.QualifiedNameContext)context;
                expr = BuildName(qual.Left);
                expr = BuildTypeReference(expr.BaseType + "." + BuildSimpleName(qual.Right).BaseType);
            }
            else if (context is XSharpParser.SimpleOrAliasedNameContext)
            {
                var alias = context as XSharpParser.SimpleOrAliasedNameContext;
                var name  = alias.Name as XSharpParser.AliasedNameContext;

                //
                if (name is XSharpParser.AliasQualifiedNameContext)
                {
                    XSharpParser.AliasQualifiedNameContext al = (XSharpParser.AliasQualifiedNameContext)name;
                    expr = BuildSimpleName(al.Right);
                    expr = BuildTypeReference(al.Alias.GetText() + "::" + expr.BaseType);
                }
                else if (name is XSharpParser.GlobalQualifiedNameContext)
                {
                    var gqn = name as XSharpParser.GlobalQualifiedNameContext;
                    expr = BuildSimpleName(gqn.Right);
                    expr = BuildTypeReference("global::" + expr.BaseType);
                }
                else if (name is XSharpParser.IdentifierOrGenericNameContext)
                {
                    var id = name as XSharpParser.IdentifierOrGenericNameContext;
                    expr = BuildSimpleName(id.Name);
                }
            }
            if (sName.Contains(">"))
            {
                // work around to fix type problems with generics
                expr.UserData[XSharpCodeConstants.USERDATA_CODE] = sName;
            }
            //
            return(expr);
        }
        protected XCodeTypeReference BuildDataType(XSharpParser.DatatypeContext context)
        {
            //
            // Datatype is ptrDatatype
            // or arrayDatatype
            // or simpleDatatype
            // or nullableDatatype
            // they all have a TypeName
            XSharpParser.TypeNameContext tn = null;
            if (context is XSharpParser.PtrDatatypeContext)
            {
                XSharpParser.PtrDatatypeContext ptrData = (XSharpParser.PtrDatatypeContext)context;
                tn = ptrData.TypeName;
            }
            else if (context is XSharpParser.ArrayDatatypeContext)
            {
                XSharpParser.ArrayDatatypeContext aData = (XSharpParser.ArrayDatatypeContext)context;
                tn = aData.TypeName;
            }
            else if (context is XSharpParser.SimpleDatatypeContext)
            {
                XSharpParser.SimpleDatatypeContext sdt = (XSharpParser.SimpleDatatypeContext)context;
                tn = sdt.TypeName;
            }
            else if (context is XSharpParser.NullableDatatypeContext)
            {
                XSharpParser.NullableDatatypeContext ndc = context as XSharpParser.NullableDatatypeContext;
                tn = ndc.TypeName;
            }
            XCodeTypeReference expr = null;

            if (tn != null)
            {
                if (tn.NativeType != null)
                {
                    expr = BuildNativeType(tn.NativeType);
                }
                else if (tn.XType != null)
                {
                    expr = BuildXBaseType(tn.XType);
                }
                else if (tn.Name != null)
                {
                    expr = BuildName(tn.Name);
                }
            }
            else
            {
                expr = new XCodeTypeReference(typeof(void));
            }
            //
            return(expr);
        }
예제 #6
0
        private CodeVariableDeclarationStatement BuildLocalVar(XSharpParser.LocalvarContext context, XCodeTypeReference localType)
        {
            CodeVariableDeclarationStatement local = new CodeVariableDeclarationStatement();

            local.Name = context.Id.GetCleanText();
            local.Type = localType;
            if (context.Expression != null)
            {
                local.InitExpression = BuildExpression(context.Expression, false);
            }
            var name = local.Name.ToLower();

            if (!_locals.ContainsKey(name))
            {
                _locals.Add(name, findType(localType.BaseType));
            }
            return(local);
        }