Esempio n. 1
0
        public string Visit(Class_Def node)
        {
            current_type = Types_Cool[node.type.s];

            // Formando el metodo init
            method = new Current_Method();
            string father = Types_Cool[node.type.s].Father.Name;

            if (father != "IO" && father != "Object")
            {
                string f = method.Add_local("father", true);
                method.Add_Instruction(new CIL_Allocate(f, father));
                method.Add_Instruction(new CIL_VCall(f, father, "__init", new List <string> {
                    f
                }));
                foreach (string attr in Types[father].Attributes)
                {
                    string a = method.Add_local("attr", true);
                    method.Add_Instruction(new CIL_GetAttr(a, new CIL_MyVar(f, father), attr));
                    method.Add_Instruction(new CIL_SetAttr(new CIL_MyVar("this", node.type.s), attr, a));
                }
            }
            foreach (var attr in node.attr.list_Node)
            {
                attr.Visit(this);
            }

            method.Add_Instruction(new CIL_Return("this"));
            string mtdName = "__init_" + node.type.s;

            Code.Add(mtdName, new CIL_Function(mtdName, new List <string>(), new List <string>(method.locals.Values), method.body));

            foreach (Method_Def item in node.method.list_Node)
            {
                Visit(item);
            }
            return("");
        }
Esempio n. 2
0
        public string Visit(Str node)
        {
            if (!Data.ContainsKey(node.s))
            {
                string v = take_data.take(method.current_scope.id);
                Data.Add(node.s, v);
            }
            var exp = method.Add_local("exp", true);

            method.Add_Instruction(new CIL_Load(exp, Data[node.s]));
            return(exp);
        }
Esempio n. 3
0
        public string Visit(Attr_Def node)
        {
            var exp = "";

            if (node.exp == null)
            {
                if (node.type.s == "Int")
                {
                    exp = new Const("0").Visit(this);
                }
                else if (node.type.s == "String")
                {
                    exp = method.Add_local("expr", true);
                    method.Add_Instruction(new CIL_Load(exp, Data[""]));
                }
                else if (node.type.s == "Bool")
                {
                    exp = new Const("false").Visit(this);
                }
                else
                {
                    exp = "void";
                }
            }
            else
            {
                exp = node.exp.Visit(this);
            }
            method.Add_Instruction(new CIL_SetAttr(new CIL_MyVar("this", current_type.Name), node.name.name, exp));
            return("");
        }