public override Template Visit(Enum enum_def)
        {
            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc(new IdentType("const " + enum_def.Name + "&"), "_t_value", null, AllocType.Declare));
                List <StmtSwitchItem> switch_list = new List <StmtSwitchItem>();
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    switch_list.Add(new StmtSwitchItem(new List <Expr> {
                        new ExprConst(enum_def.Name + "::" + item, ConstType.Ident)
                    }, block));
                }

                StmtBlock default_block = new StmtBlock();
                {
                    default_block.StmtList.Add(new StmtExpr(new ExprCall(new ExprConst("throw", ConstType.Ident), null, new List <Expr> {
                        new ExprConst("\"Not Found\"", ConstType.String)
                    })));
                }

                StmtSwitch stmt_switch = new StmtSwitch(new ExprConst("_t_value", ConstType.Ident), switch_list, default_block);
                StmtBlock  body        = new StmtBlock();
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                return(func.Accept(this));
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Esempio n. 2
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;
        }
Esempio n. 3
0
        public override Template Visit(Enum enum_def)
        {
            Template template = new Template("enum <name> {\n    <list; separator=\",\n\">\n};<tostring>");
            template.Add("name", enum_def.Name);
            List<Template> list = new List<Template>();
            bool hasFlagAttribute = enum_def.Attribute.Find(x => x.Name == "FlagAttribute") != null;
            int i = 0;
            foreach (var item in enum_def.Values)
            {
                Template tp = new Template("<node><suffix>");
                tp.Add("node", item);
                if (i == 0 || hasFlagAttribute)
                {
                    tp.Add("suffix", string.Format(" = {0}", i));
                }
                else
                {
                    tp.Add("suffix", "");
                }
                list.Add(tp);
                if (hasFlagAttribute)
                {
                    i = i == 0 ? 1 : i * 2;
                }
                else
                {
                    i = i + 1;
                }
            }
            template.Add("list", list);

            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = "const char*";
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc("const " + enum_def.Name + "&", new List<string> { "a" }, null, true));
                StmtBlock body = new StmtBlock();
                StmtSwitch stmt_switch = new StmtSwitch();
                stmt_switch.Expr = new ExprConst("a", ConstType.Ident);
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    stmt_switch.List.Add(new StmtSwitchItem(new ExprConst(item, ConstType.Ident), block));
                }
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                Template node = new Template("\n\n<stmt>");
                node.Add("stmt", func.Accept(this));
                template.Add("tostring", node);
            }
            else
            {
                template.Add("tostring", "");
            }
            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);
        }
        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;
        }
        public override Template Visit(Enum enum_def)
        {
            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc(new IdentType("const " + enum_def.Name + "&"), "_t_value", null, AllocType.Declare));
                List<StmtSwitchItem> switch_list = new List<StmtSwitchItem>();
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    switch_list.Add(new StmtSwitchItem(new List<Expr> { new ExprConst(enum_def.Name + "::" + item, ConstType.Ident) }, block));
                }

                StmtBlock default_block = new StmtBlock();
                {
                    default_block.StmtList.Add(new StmtExpr(new ExprCall(new ExprConst("throw", ConstType.Ident), null, new List<Expr> { new ExprConst("\"Not Found\"", ConstType.String) })));
                }

                StmtSwitch stmt_switch = new StmtSwitch(new ExprConst("_t_value", ConstType.Ident), switch_list, default_block);
                StmtBlock body = new StmtBlock();
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                return func.Accept(this);
            }
            else
            {
                throw new NotSupportedException();
            }
        }