public ForNode(string name, LanguageNode asig, LanguageNode to, LanguageNode body) { Name = name; Asig = asig; To = to; if (body == null) { Body = new ExpSeqNode(new List <LanguageNode>()); } else { Body = body; } }
public IfThenElseNode(LanguageNode cond, LanguageNode body, LanguageNode body_else) : base(cond, body) { Body_else = body_else; }
public AccessNode(LanguageNode value, List <Access> access_list) { Access_list = access_list; Value = value; }
public BinaryOperationNode(LanguageNode left, LanguageNode right) { Left = left; Right = right; }
public LessThanNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public UnaryOperationNode(LanguageNode value) { Value = value; }
public NegativeOperationNode(LanguageNode value) : base(value) { }
/// <summary> /// Genera el codigo de un programa semanticamente correcto. Este metodo es solo de uso interno /// </summary> /// <param name="root"></param> /// <param name="folder"></param> /// <param name="filename"></param> private static void GenCode(LanguageNode root, string folder, string filename) { AssemblyName name = new AssemblyName("TigerPM"); #region INICIALIZACION // Creando el ensamblado. AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave, folder); // Creando el módulo. Utils.module = assembly.DefineDynamicModule(filename + ".exe"); // Creando la clase Program. TypeBuilder program = Utils.module.DefineType("Program", TypeAttributes.Public | TypeAttributes.Class); ConstructorBuilder programCrt = program.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]); FieldBuilder field = program.DefineField("Break", typeof (bool), FieldAttributes.Public); ILGenerator ilG = programCrt.GetILGenerator(); ilG.Emit(OpCodes.Ldarg_0); //meto el this ilG.Emit(OpCodes.Ldc_I4_0); //meto un cero en la pila ilG.Emit(OpCodes.Stfld, field); ilG.Emit(OpCodes.Ret); // Creando el método Run. MethodBuilder run = program.DefineMethod("Run", MethodAttributes.Public, CallingConventions.Standard, typeof (void), new Type[0]); ILGenerator runIL = run.GetILGenerator(); #region CODIGO DEL PROGRAMA StandarLibrary.stanLib = null; StandarLibrary.meths = new Dictionary<string, MethodBuilder>(); Type t = root.GenCode(runIL, program, field, new List<FieldBuilder>(), default(Label)); if (t != null && t != typeof (void)) runIL.Emit(OpCodes.Call, typeof (Console).GetMethod("Write", new Type[] {t})); //para ver lo que devuelve if (StandarLibrary.stanLib != null) StandarLibrary.stanLib.CreateType(); #endregion runIL.Emit(OpCodes.Ret); // Creando el método Main. MethodBuilder main = program.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type[0]); main.InitLocals = true; // Haciendo el método Main el punto de entrada // de la aplicación. assembly.SetEntryPoint(main, PEFileKinds.ConsoleApplication); main.InitLocals = true; ILGenerator mainGen = main.GetILGenerator(); mainGen.Emit(OpCodes.Newobj, programCrt); mainGen.Emit(OpCodes.Callvirt, run); #endregion #region FINALIZACION // Hacer que el método Main retorne. mainGen.Emit(OpCodes.Ret); // Crear la clase Program. program.CreateType(); // Guardar el ensamblado. assembly.Save(Utils.module.ToString()); #endregion }
public IfThenNode(LanguageNode cond, LanguageNode body) { Cond = cond; Body = body; }
public GreaterThanNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public ArrayCreate(string name, LanguageNode size, LanguageNode initial) { Name = name; Size = size; Initial = initial; }
public VarDeclarationNode(string name, string var_type, LanguageNode asig) { Name = name; Asig = asig; VarType = var_type; }
public VarDeclarationNode(string name, LanguageNode asig) { Name = name; Asig = asig; }
public EqualsNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public RelationalNode(LanguageNode left, LanguageNode right) { Left = left; Right = right; }
public PlusNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public AsignNode(LanguageNode lvalue, LanguageNode asign) { LValue = lvalue; Asign = asign; }
/// <summary> /// Compila un string que constituye un programa escrito en tiger. /// </summary> /// <param name="programPath">Ruta al fichero de entrada</param> /// <param name="outputFolder">Fichero de salida</param> /// <returns>Lista de errores que tiene el programa</returns> public static List<Error> Compile(string programPath, string outputFolder, string outputFilename, out LanguageNode root) { root = null; stream = new ANTLRFileStream(programPath); lexer = new TigerPM_ASTLexer(stream); Errors.Clear(); tokens = new CommonTokenStream(lexer); parser = new TigerPM_ASTParser(tokens); Utils.autonumeric = 0; InstructionNode.countOfClicles = 0; var result = parser.prog(); if (Errors.Count == 0) { root = result; if (Errors.Count == 0) { //No hubo errores sintacticos //Crear el Scope Scope scope = new Scope(); scope.Types.Add("int", new ReturnInt()); scope.Types.Add("string", new ReturnString()); scope.Funcs.Add("print", new Fun_Scope("print", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("s", new ReturnString())}, new ReturnNotValue(), true)); scope.Funcs.Add("printi", new Fun_Scope("printi", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("i", new ReturnInt())}, new ReturnNotValue(), true)); scope.Funcs.Add("flush", new Fun_Scope("flush", new List<Tuple<string, TypeReturn>>(), new ReturnNotValue(), true)); scope.Funcs.Add("getchar", new Fun_Scope("getchar", new List<Tuple<string, TypeReturn>>(), new ReturnString(), true)); scope.Funcs.Add("ord", new Fun_Scope("ord", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("s", new ReturnString())}, new ReturnInt(), true)); scope.Funcs.Add("chr", new Fun_Scope("chr", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("i", new ReturnInt())}, new ReturnString(), true)); scope.Funcs.Add("size", new Fun_Scope("size", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("s", new ReturnString())}, new ReturnInt(), true)); scope.Funcs.Add("substring", new Fun_Scope("substring", new List<Tuple<string, TypeReturn>>() { new Tuple<string, TypeReturn>("s", new ReturnString()), new Tuple<string, TypeReturn>("f", new ReturnInt()), new Tuple<string, TypeReturn>("n", new ReturnInt()) }, new ReturnString(), true)); scope.Funcs.Add("concat", new Fun_Scope("concat", new List<Tuple<string, TypeReturn>>() { new Tuple<string, TypeReturn>("s1", new ReturnString()), new Tuple<string, TypeReturn>("s2", new ReturnString()) }, new ReturnString(), true)); scope.Funcs.Add("not", new Fun_Scope("not", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("i", new ReturnInt())}, new ReturnInt(), true)); scope.Funcs.Add("exit", new Fun_Scope("exit", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("i", new ReturnInt())}, new ReturnNotValue(), true)); scope.Funcs.Add("printline", new Fun_Scope("printline", new List<Tuple<string, TypeReturn>>() {new Tuple<string, TypeReturn>("s", new ReturnString())}, new ReturnNotValue(), true)); scope.Funcs.Add("getline", new Fun_Scope("getline", new List<Tuple<string, TypeReturn>>(), new ReturnString(), true)); List<Scope> s = new List<Scope>(); s.Add(scope); root.CheckSemantics(s, Errors); if (Errors.Count == 0 && outputFolder != null) { GenCode(root, outputFolder, outputFilename); } } //No hubo errores sintacticos } return Errors; }
public MultNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public AccessIndex(LanguageNode arg, int line, int column) { Arg = arg; Column = column; Line = line; }
public DivNode(LanguageNode left, LanguageNode right) : base(left, right) { }
public LogicalNode(LanguageNode left, LanguageNode right) { Left = left; Right = right; }
public WhileNode(LanguageNode condi, LanguageNode body) { Condi = condi; Body = body; }