Beispiel #1
0
        public static void AddFlushFunctionToScope(Inizializator <ILCode> init)
        {
            var funInfo = new FunctionInfo(new List <KeyValuePair <string, TigerType> >(), TigerType.GetType <NoType>())
            {
                FunctionName = "flush",
                IsPredifined = true
            };
            var exit = new FunctionPredifined <ILCode>(funInfo, FlushFunction);

            init.AddPredifinedFunction(exit);
        }
Beispiel #2
0
        public static void AddGetCharFunctionToScope(Inizializator <ILCode> init)
        {
            FunctionInfo funInfo;

            funInfo = new FunctionInfo(new List <KeyValuePair <string, TigerType> >(), TigerType.GetType <StringType>());
            funInfo.FunctionName = "getchar";
            funInfo.IsPredifined = true;
            var getChar = new FunctionPredifined <ILCode>(funInfo, GetCharFunction);

            init.AddPredifinedFunction(getChar);
        }
Beispiel #3
0
        public static void AddCharFunctionToScope(Inizializator <ILCode> init)
        {
            FunctionInfo funInfo;

            funInfo = new FunctionInfo(new List <KeyValuePair <string, TigerType> >(), TigerType.GetType <StringType>());
            funInfo.ParameterList.Add(GetKeyValue("i", TigerType.GetType <IntType>()));
            funInfo.FunctionName = "chr";
            funInfo.IsPredifined = true;
            var chr = new FunctionPredifined <ILCode>(funInfo, CharFunction);

            init.AddPredifinedFunction(chr);
        }
Beispiel #4
0
        /// <summary>
        /// Annade las funciones predifinidas
        /// </summary>
        /// <param name="init"></param>
        /// <remarks>Esta funcion asume que las funciones que ella llama contienen en el nombre el patron "FuncionToScope"</remarks>
        private static void AddFunctionsToScope(Inizializator <ILCode> init)
        {
            Type current = typeof(TigerIlFunctions);
            //se queda con todos los metodos publicos que contienes "FunctionToScope"
            IEnumerable <MethodInfo> methodsToCall = from m in current.GetMethods()
                                                     where m.Name.Contains("FunctionToScope")
                                                     select m;

            foreach (var met in methodsToCall)
            {
                met.Invoke(null, new object[] { init });
            }
        }
Beispiel #5
0
        public static void AddPrintIntFunctionToScope(Inizializator <ILCode> init)
        {
            FunctionInfo funInfo;

            funInfo = new FunctionInfo(new List <KeyValuePair <string, TigerType> >(), TigerType.GetType <NoType>());
            funInfo.ParameterList.Add(new KeyValuePair <string, TigerType>("i", TigerType.GetType <IntType>()));
            funInfo.FunctionName = "printi";
            funInfo.IsPredifined = true;

            var printi = new FunctionPredifined <ILCode>(funInfo, PrintIntFunction);

            init.AddPredifinedFunction(printi);
        }
Beispiel #6
0
        private void InitScope(Scope scope)
        {
            init = new Inizializator <ILCode>(scope);

            //anadir la funcion main al scope ,esta no genera codigo es solo para que toda instruccion en tiger este dentro de alguna
            //funcion.
            AddMainToScope(scope);
            //annadir las funcione predefinidas

            TigerIlFunctions.InitScope(init);


            init.InitializeScope();
        }
Beispiel #7
0
        /// <summary>
        /// Compila el codigo que se encuentra en el fichero filename
        /// </summary>
        /// <param name="filename">El fichero a compilar</param>
        /// <param name="typeName">El nombre del tipo que contendra las funciones que se definan</param>
        /// <returns>Retorna una lista con los errores que se produjeron</returns>
        private IEnumerable <AnalysisError> Compile(string filename, string typeName)
        {
            var s           = new StreamReader(filename);
            var stm         = new AntlrInputStream(s);;
            var lexer       = new TigerLexer(stm);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new BengalaParser(tokenStream);

            parser.ConfigErrorListeners();
            var expContext = parser.program();

            var contextVisitor = new BuildAstVisitor();

            var errorsWarning = parser.Errors;

            if (parser.NumberOfSyntaxErrors != 0 || parser.Errors.Any())
            {
                return(errorsWarning);
            }

            var astRoot = expContext.Accept(contextVisitor);

            var generalScope = new Scope(null);

            init = new Inizializator <ILCode>(generalScope);
            InitScope(generalScope);

            var errorListener         = new BengalaBaseErrorListener();
            var staticAnalysisVisitor = new StaticChecker(errorListener, generalScope);

            astRoot.Accept(staticAnalysisVisitor);

            if (GenerateCode && !errorListener.Errors.Any())
            {
                CreateCode(FileName(filename), typeName, astRoot);
            }

            return(errorListener.Errors);
        }
Beispiel #8
0
        /// <summary>
        /// Annade los tipos predefinidos
        /// </summary>
        /// <param name="init"></param>
        private static void AddTypes(Inizializator <ILCode> init)
        {
            //anadir los tipos
            //--> int
            var typeTemp = new PrefinedType <ILCode>(TigerType.GetType <IntType>(),
                                                     (name, code) =>
                                                     code.DefinedType.Add(name, typeof(int)));

            init.AddPredifinedTypes(typeTemp);
            //<-- end int
            //--> string
            typeTemp = new PrefinedType <ILCode>(TigerType.GetType <StringType>(),
                                                 (name, code) => code.DefinedType.Add(name, typeof(string)));

            init.AddPredifinedTypes(typeTemp);
            //<-- end string

            //--> errorType
            typeTemp = new PrefinedType <ILCode>(TigerType.GetType <ErrorType>(),
                                                 (name, code) => { });
            init.AddPredifinedTypes(typeTemp);
            //<-- end errorType
        }
Beispiel #9
0
        /// <summary>
        /// Metodo encargado de annadir al scope las funciones predefinidas ,asi como asociarlas con sus generadores
        /// de codigo
        /// </summary>
        /// <param name="init"></param>
        public static void InitScope(Inizializator <ILCode> init)
        {
            AddFunctionsToScope(init);

            AddTypes(init);
        }