Example #1
0
        public override Template Visit(Class class_def)
        {
            Template template = null;
            if (class_def.GenericParameter.Count() == 0)
            {
                template = new Template("class <name><inherit> {\n<list; separator=\"\n\">\n};");
            }
            else
            {
                template = new Template("template \\<<generics; separator=\", \">>\nclass <name><inherit> {\n<list; separator=\"\n\">\n};");
                template.Add("generics", class_def.GenericParameter.Select(x => string.Format("typename {0}", x)));
            }
            template.Add("name", class_def.Name);
            if (class_def.Inherit.Count() > 0)
            {
                Template tmp = new Template(": <inherit; separator=\", \">");
                tmp.Add("inherit", class_def.Inherit.Select(x => string.Format("public {0}", x)));
                template.Add("inherit", tmp);
            }
            List<Template> list = new List<Template>();

            bool default_public = class_def.Attribute.Find(x => x.Name == "public") != null;

            string last = "private";

            bool last_flag = false;
            AstNode last_node = null;

            // friend class
            foreach (var attr in class_def.Attribute)
            {
                if (attr.Name == "friend")
                {
                    foreach (var name in attr.Args)
                    {
                        Template friend = new Template("    friend class <name>;");
                        friend.Add("name", name);
                        list.Add(friend);
                    }
                }
            }

            // Args
            if (class_def.Args.Count() > 0)
            {
                {
                    Template tmp = new Template("\npublic:\n    <nodes; separator=\"\n\">\n\n    <constructor>");
                    List<Template> nodes = new List<Template>();
                    foreach (var item in class_def.Args)
                    {
                        GlobalAlloc alloc = new GlobalAlloc(item.Type, item.Name, null, null, true);
                        nodes.Add(alloc.Accept(this));
                    }
                    tmp.Add("nodes", nodes);
                    list.Add(tmp);

                    FuncDef func = new FuncDef();
                    func.Type = null;
                    func.Name = class_def.Name;
                    func.Args = class_def.Args;
                    func.Body = new StmtBlock();
                    foreach (var item in class_def.Args)
                    {
                        string name = item.Name.First();
                        ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name),
                                                           new ExprConst(name, ConstType.Ident));
                        func.Body.StmtList.Add(new StmtExpr(assign));
                    }

                    tmp.Add("constructor", func.Accept(this));
                }

                {
                    Template tmp = new Template("\n    <unapply>");
                    List<Template> nodes = new List<Template>();
                    foreach (var item in class_def.Args)
                    {
                        GlobalAlloc alloc = new GlobalAlloc(item.Type, item.Name, null, null, true);
                        nodes.Add(alloc.Accept(this));
                    }
                    tmp.Add("nodes", nodes);
                    list.Add(tmp);

                    FuncDef func = new FuncDef();
                    Template type = new Template("tuple\\<<types; separator=\", \">>");
                    type.Add("types", class_def.Args.Select(x => x.Type));
                    func.Attribute = new List<Attr>{new Attr{Name = "inline"}};
                    func.Type = type.Render();
                    func.Name = "Unapply";
                    func.Body = new StmtBlock();
                    ExprTuple tuple = new ExprTuple();
                    foreach (var item in class_def.Args)
                    {
                        string name = item.Name.First();
                        tuple.ExprList.Add(new ExprConst(name, ConstType.Ident));
                    }
                    func.Body.StmtList.Add(new StmtReturn(tuple));
                    tmp.Add("unapply", func.Accept(this));
                }

                last = "public";
                last_flag = true;
            }

            if (class_def.Block != null)
            {
                foreach (var node in class_def.Block.List)
                {
                    bool current = node is FuncDef || node is Class || node is Enum || node is Import || node is GlobalUsing || node is Namespace;
                    string modifier = null;
                    if (!default_public)
                    {
                        modifier = node.Attribute.Find(x => x.Name == "public") != null ? "public" : "private";
                    }
                    else
                    {
                        modifier = node.Attribute.Find(x => x.Name == "private") != null ? "private" : "public";
                    }

                    if (modifier != last)
                    {
                        Template member = new Template("\n<modifier>:\n    <expr>");
                        member.Add("modifier", modifier);
                        member.Add("expr", node.Accept(this));
                        list.Add(member);
                    }
                    else
                    {
                        if ((last_flag || current) && !(last_node is Import && node is Import))
                        {
                            Template member = new Template("\n    <expr>");
                            member.Add("expr", node.Accept(this));
                            list.Add(member);
                        }
                        else
                        {
                            Template member = new Template("    <expr>");
                            member.Add("expr", node.Accept(this));
                            list.Add(member);
                        }

                    }

                    last = modifier;
                    last_flag = current;
                    last_node = node;
                }
            }

            template.Add("list", list);
            return template;
        }
Example #2
0
 public override Template Visit(ExprAssign expr)
 {
     Template template = new Template("<left> = <right>");
     template.Add("left", expr.Left.Accept(this));
     template.Add("right", expr.Right.Accept(this));
     return template;
 }
        public override Template Visit(Class class_def)
        {
            Template template = new Template("<list; separator=\"\n\n\">");

            List <Template> list = new List <Template>();

            EnterNameSpace(class_def.Name);
            this.class_stack.Push(class_def);

            if (class_def.Args.Count() > 0)
            {
                {
                    FuncDef func = new FuncDef();
                    func.Type = null;
                    func.Name = class_def.Name;
                    func.Args = class_def.Args;
                    func.Body = new StmtBlock();
                    foreach (var item in class_def.Args)
                    {
                        string     name   = item.Name.First();
                        ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name),
                                                           new ExprConst(name, ConstType.Ident));
                        func.Body.StmtList.Add(new StmtExpr(assign));
                    }

                    list.Add(func.Accept(this));
                }
            }

            if (class_def.Attribute.Exists(x => x.Name == "case"))
            {
                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = "GetType";
                func.Args = new List <ExprAlloc>();
                func.Body = new StmtBlock();
                StmtReturn stmt = new StmtReturn(new ExprConst("\"" + class_def.Name + "\"", ConstType.String));
                func.Body.StmtList.Add(stmt);
                list.Add(func.Accept(this));
            }

            if (class_def.Block != null)
            {
                foreach (var node in class_def.Block.List)
                {
                    if (node is FuncDef)
                    {
                        list.Add(node.Accept(this));
                    }

                    if (node is GlobalAlloc && node.Attribute.Exists(x => x.Name == "static") && !node.Attribute.Exists(x => x.Name == "const"))
                    {
                        list.Add(node.Accept(this));
                    }
                }
            }

            this.class_stack.Pop();
            PopNameSpace();

            template.Add("list", list);
            return(template);
        }
Example #4
0
 public abstract Template Visit(ExprAssign expr);
Example #5
0
 public abstract Template Visit(ExprAssign expr);
        public override Template Visit(Class class_def)
        {
            Template template = new Template("<list; separator=\"\n\n\">");

            List<Template> list = new List<Template>();

            EnterNameSpace(class_def.Name);
            this.class_stack.Push(class_def);

            if (class_def.Args.Count() > 0)
            {
                {
                    FuncDef func = new FuncDef();
                    func.Type = null;
                    func.Name = class_def.Name;
                    func.Args = class_def.Args;
                    func.Body = new StmtBlock();
                    foreach (var item in class_def.Args)
                    {
                        string name = item.Name.First();
                        ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name),
                                                           new ExprConst(name, ConstType.Ident));
                        func.Body.StmtList.Add(new StmtExpr(assign));
                    }

                    list.Add(func.Accept(this));
                }
            }

            if (class_def.Attribute.Exists(x => x.Name == "case"))
            {
                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = "GetType";
                func.Args = new List<ExprAlloc>();
                func.Body = new StmtBlock();
                StmtReturn stmt = new StmtReturn(new ExprConst("\"" + class_def.Name + "\"", ConstType.String));
                func.Body.StmtList.Add(stmt);
                list.Add(func.Accept(this));
            }

            if (class_def.Block != null)
            {
                foreach (var node in class_def.Block.List)
                {
                    if (node is FuncDef)
                    {
                        list.Add(node.Accept(this));
                    }

                    if (node is GlobalAlloc && node.Attribute.Exists(x => x.Name == "static") && !node.Attribute.Exists(x => x.Name == "const"))
                    {
                        list.Add(node.Accept(this));
                    }
                }
            }

            this.class_stack.Pop();
            PopNameSpace();

            template.Add("list", list);
            return template;
        }