1 {} 2 unit Parser; 3 {$mode objfpc}{$H+} 4 5 interface 6 uses 7 Classes, SysUtils, LCLType, Dialogs, lclProc, Graphics, SynEditHighlighter, 8 SynFacilBasic, 9 XpresTypes, XPresParser, Interprete; 10 11 type 12 13 { TCompiler } 14 15 TCompiler = class(TInterprete) 16 private 17 procedure CompilarArc; 18 public 19 procedure Compilar(NombArc: string; LinArc: Tstrings); 20 constructor Create; override; 21 destructor Destroy; override; 22 end; 23 24 //procedure Compilar(NombArc: string; LinArc: Tstrings); 25 var 26 cxp : TCompiler; 27 28 implementation 29 30 //Métodos OVERRIDE 31 procedure TCompiler.CompilarArc; 32 //Compila un programa en el contexto actual 33 begin 34 // CompilarAct; 35 Perr.Clear; 36 if cIn.Eof then begin 37 GenError('Se esperaba "begin", "var", "type" o "const".'); 38 exit; 39 end; 40 cIn.SkipWhites; 41 //empiezan las declaraciones 42 Cod_StartData; //debe definirse en el "Interprete.pas" 43 if cIn.tokL = 'var' then begin 44 cIn.Next; //lo toma 45 while (cIn.tokL <>'begin') and (cIn.tokL <>'const') and 46 (cIn.tokL <>'type') and (cIn.tokL <>'var') do begin 47 CompileVarDeclar; 48 if pErr.HayError then exit;; 49 end; 50 end; 51 if cIn.tokL = 'begin' then begin 52 Cod_StartProgram; 53 cIn.Next; //coge "begin" 54 //codifica el contenido 55 SkipWhites; 56 while not cIn.Eof and (cIn.tokL<>'end') do begin 57 //se espera una expresión o estructura 58 GetExpressionE(0); 59 if perr.HayError then exit; //aborta 60 //busca delimitador 61 SkipWhites; 62 if EOExpres then begin //encontró delimitador de expresión 63 cIn.Next; //lo toma 64 SkipWhites; //quita espacios 65 end; 66 end; 67 if Perr.HayError then exit; 68 if cIn.Eof then begin 69 GenError('Inesperado fin de archivo. Se esperaba "end".'); 70 exit; //sale 71 end; 72 if cIn.tokL <> 'end' then begin //verifica si termina el programa 73 GenError('Se esperaba "end".'); 74 exit; //sale 75 end; 76 cIn.Next; //coge "end" 77 end else begin 78 GenError('Se esperaba "begin", "var", "type" o "const".'); 79 exit; 80 end; 81 Cod_EndProgram; 82 end; 83 procedure TCompiler.Compilar(NombArc: string; LinArc: Tstrings); 84 //Compila el contenido de un archivo a ensamblador 85 begin 86 //se pone en un "try" para capturar errores y para tener un punto salida de salida 87 //único 88 try 89 ejecProg := true; //marca bandera 90 91 Perr.IniError; 92 ClearVars; //limpia las variables 93 ClearFuncs; //limpia las funciones 94 cIn.ClearAll; //elimina todos los Contextos de entrada 95 ExprLevel := 0; //inicia 96 //compila el archivo abierto 97 98 cIn.NewContextFromFile(NombArc,LinArc); //Crea nuevo contenido 99 if PErr.HayError then exit; 100 CompilarArc; //puede dar error 101 102 cIn.RemoveContext; //es necesario por dejar limpio 103 if PErr.HayError then exit; //sale 104 finally 105 ejecProg := false; 106 //tareas de finalización 107 //como actualizar estado 108 end; 109 end; 110 111 constructor TCompiler.Create; 112 begin 113 inherited Create; 114 //se puede definir la sintaxis aquí o dejarlo para StartSyntax() 115 StartSyntax; //Debe hacerse solo una vez al inicio 116 end; 117 118 destructor TCompiler.Destroy; 119 begin 120 inherited Destroy; 121 end; 122 123 initialization 124 //Es necesario crear solo una instancia del compilador. 125 cxp := TCompiler.Create; //Crea una instancia del compilador 126 127 finalization 128 cxp.Destroy; 129 end. 130 131