Example #1
0
 public bool Parse(string code, bool peformSemanticFixes=true)
 {
     var resultTokens = _lexer.BuildTextTokens(code);
     FileIncludeLogic.ScanAndImportIncludes(resultTokens);
     AstTree = _parser.Parse(resultTokens);
     if (peformSemanticFixes)
     {
         var semantic = new SemanticAnalysis(AstTree);
         semantic.Perform();
     }
     return true;
 }
Example #2
0
        public static void GenerateCode()
        {
            var lexer = new Mq4Lexer();
            var resultTokens = lexer.BuildTextTokens(ScriptUtils.ComplexCode);

            var parser = new Mq4Parser();
            var astTree = parser.Parse(resultTokens);
            var semantic = new SemanticAnalysis(astTree);
            semantic.Perform();

            var codeGenerator = new CsCodeGenerator();
            codeGenerator.GenerateCodeForNode(astTree.Children[0]);
        }
Example #3
0
        public void DoSecondSemanticAnalysisPass()
        {
            if (State != ModuleState.SemanticAnalysisFirstPassDone)
            {
                throw new InvalidOperationException("Invalid state");
            }

            Debug.Assert(Ast != null);
            Debug.Assert(SymbolTable != null);

            SemanticAnalysis.DoSecondPass(Ast, SymbolTable, Diagnostics);
            State = ModuleState.SemanticAnalysisSecondPassDone;
        }
Example #4
0
        public void DoFirstSemanticAnalysisPass(IUsingModuleResolver?usingResolver)
        {
            if (State != ModuleState.Parsed)
            {
                throw new InvalidOperationException("Invalid state");
            }

            Debug.Assert(Ast != null);
            SymbolTable = new SymbolTable(Ast);

            SemanticAnalysis.DoFirstPass(Ast, SymbolTable, usingResolver, Diagnostics);
            State = ModuleState.SemanticAnalysisFirstPassDone;
        }
Example #5
0
        public void DoBinding()
        {
            if (State != ModuleState.SemanticAnalysisSecondPassDone)
            {
                throw new InvalidOperationException("Invalid state");
            }

            Debug.Assert(Ast != null);
            Debug.Assert(SymbolTable != null);

            BoundModule = SemanticAnalysis.DoBinding(Ast, SymbolTable, Diagnostics);
            State       = ModuleState.Bound;
        }
Example #6
0
        public void Compile(string source)
        {
            var syntacticAnalysis = new SyntacticAnalysis();
            var ast = syntacticAnalysis.Run(source);

            var semanticAnalysis = new SemanticAnalysis();

            semanticAnalysis.Run(ast);

            // Generate IL program

            /*var ilGenerator = new ILGenerator();
             * return ilGenerator.Build(ast);*/
        }
Example #7
0
        private void recorrerAST(ParseTreeNode node)
        {
            var semanticAnalysis = new SemanticAnalysis(rtxt_code);
            var arbolFuntions    = new ArbolFunctions();
            var nodos            = arbolFuntions.Recorrer(node);

            for (int i = 0; i < nodos.Count(); i++)
            {
                var nodo = nodos[i];
                // verifica la tabla de simbolos
                if (nodo.Term.Name == grammarCS.NoTerminales.DeclaracionVariable)
                {
                    var tipo          = nodo.FindTokenAndGetText();
                    var identificador = nodos[i + 3].FindTokenAndGetText();
                    var operador      = nodos[i + 6].FindTokenAndGetText();
                    var dato          = nodos[i + 9].FindTokenAndGetText();
                    if (dato == grammarCS.ExpresionesRegulares.StringRegex)
                    {
                        Console.WriteLine(dato);
                    }
                    else
                    {
                        tablaSimbolos.AgregarSimbolo(new Symbols(tipo, identificador, operador, dato));
                        Console.WriteLine(tablaSimbolos.ToString());
                    }
                }
                if (nodo.Term.Name == grammarCS.NoTerminales.DeclaracionArreglo)
                {
                    rtxt_out.Text += semanticAnalysis.declaracionDeArreglo(nodo, nodos, i);
                }
                if (nodo.Term.Name == grammarCS.NoTerminales.EntradaDatos)
                {
                    //TODO: QUEDA PENDIENTE
                    var nombre      = nodo.FindTokenAndGetText();
                    var punto       = nodos[i + 3].FindTokenAndGetText();
                    var tipoEntrada = nodos[i + 4].FindTokenAndGetText();
                    Console.WriteLine(nombre + " " + punto + " " + tipoEntrada);
                }
                // Verifica el uso de palabras reservadas en los nombres de las clases

                if (nodo.Term.Name == grammarCS.NoTerminales.DeclaracionClase)
                {
                    rtxt_out.Text += semanticAnalysis.declaracionDeClase(nodo, nodos, i);
                }
            }
            rtxt_out.Text += "\n" + semanticAnalysis.verificarIdentificadores(tablaSimbolos);
            rtxt_out.Text += "\n" + semanticAnalysis.verificarTipoDeDato(tablaSimbolos);
            rtxt_out.Text += "\n" + semanticAnalysis.verificarIdentificadoresReservados(tablaSimbolos);
        }
Example #8
0
 private void Recompile()
 {
     AbstractSyntaxTree   = null;
     IntermediateLanguage = null;
     CompilerErrors       = string.Empty;
     try
     {
         var program = Parser.parse(_sourceCode);
         AbstractSyntaxTree = program.Select(x => new DeclarationItem(x));
         var semanticAnalysisResult = SemanticAnalysis.analyze(program);
         IntermediateLanguage = new ILBuilder(semanticAnalysisResult).BuildClass(program);
     }
     catch (CompilerException ex)
     {
         CompilerErrors = ex.Message;
     }
 }
Example #9
0
        public static void Main(string[] args)
        {
            var program = CreateFibonacciProgram();

            // semantic analysis
            var semanticAnalysisErrors = SemanticAnalysis.Validate(program);

            if (semanticAnalysisErrors.Any())
            {
                Console.WriteLine($"Semantic analysis failed with {semanticAnalysisErrors.Count} errors.");

                foreach (var error in semanticAnalysisErrors)
                {
                    Console.WriteLine(error);
                }

                return;
            }

            // code gen
            Console.WriteLine("Generating C# program...");
            var(generatedCode, codeGenErrors) = CSharpCodeGenerator.GenerateCode(program);

            if (codeGenErrors.Any())
            {
                Console.WriteLine($"Code generation failed with {codeGenErrors.Count} errors.");

                foreach (var error in codeGenErrors)
                {
                    Console.WriteLine(error);
                }

                return;
            }

            // C# compilation
            Console.WriteLine("Compiling C# program...");
            CSharpCompiler.CompileCSharpProgram(generatedCode);

            // run executable
            Console.WriteLine("Starting compiled program...");
            var process = Process.Start("bin/PrestoProgram.exe");

            process.WaitForExit();
        }
Example #10
0
 public TestCompiler()
 {
     this.syntacticAnalysis = new SyntacticAnalysis();
     this.semanticAnalysis  = new SemanticAnalysis();
 }