public string Visit(Method_Def node) { method = new Current_Method(); string solution = Visit(node.exp); method.Add_Instruction(new CIL_Return(solution)); Code.Add(node.name.name, new CIL_Function(node.name.name, new List <string>(node.args.list_Node.Select(x => x.name.name)), new List <string>(method.locals.Values), method.body)); return(""); }
public string Visit(Method_Def node) { method = new Current_Method(); method.args = new List <string>(node.args.list_Node.Select(m => m.name.name)); string solution = node.exp.Visit(this); method.Add_Instruction(new CIL_Return(solution)); string mtdName = node.name.name + "_" + current_type.Name; Code.Add(mtdName, new CIL_Function(mtdName, new List <string>(node.args.list_Node.Select(x => x.name.name)), new List <string>(method.locals.Values), method.body)); return(""); }
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(""); }
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(""); }