Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SyntacticAnalyzer"/> class with the specified <see cref="LexicalAnalyzer"/>.
 /// </summary>
 /// <param name="lexical">The lexical analyzer.</param>
 public SyntacticAnalyzer(LexicalAnalyzer lexical)
 {
     if (lexical == null)
     {
         throw new ArgumentNullException(nameof(lexical));
     }
     
     _lexical = lexical;
     _symbolTable = new VectorSymbolTable();
     _codeGenerator = new CodeGenerator();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Compiles the source code.
        /// </summary>
        /// <returns><see cref="Task"/></returns>
        private async Task<bool> CompileAsync()
        {
            if (string.IsNullOrEmpty(Editor.Text))
            {
                return false;
            }

            if (_modificationsCount > 0)
            {
                if (string.IsNullOrEmpty(_selectedFile))
                {
                    await SaveAsFileAsync();
                }
                else
                {
                    await SaveFileAsync();
                }
            }

            TokensList.ItemsSource = null;
            ErrorListView.ItemsSource = null;

            var success = false;

            using (LexicalAnalyzer lexical = new LexicalAnalyzer(_selectedFile))
            {
                string outputFilePath = FileHelper.GetOutputFilePath(Path.GetFileNameWithoutExtension(_selectedFile) + OutputFileExtension);
                SyntacticAnalyzer syntactic = new SyntacticAnalyzer(lexical);
                CompilationResult compilationResult = await syntactic.DoAnalysisAsync(outputFilePath);

                if (compilationResult.Error != null)
                {
                    ErrorListView.ItemsSource = new List<CompilationResultViewModel>
                    {
                        new CompilationResultViewModel()
                        {
                            Message = compilationResult.Error.Message,
                            Position = compilationResult.Error.Position
                        }
                    };
                }
                else
                {
                    _currentProgramName = compilationResult.ProgramName;
                    _outputFile = outputFilePath;
                    success = true;

                    ErrorListView.ItemsSource = new List<CompilationResultViewModel>
                    {
                        new CompilationResultViewModel()
                        {
                            Message = "Compilação sem erros.",
                            Position = new CodePosition { Line = 0, Column = 0, Index = 0 }
                        }
                    };
                }

                TokensList.ItemsSource = lexical.ReadTokens;
            }

            int tokensCount = TokensList.Items.Count;

            if (tokensCount > 0)
            {
                TokenGroupBox.Header = tokensCount > 1 ? tokensCount + " tokens" : "1 token";
            }
            else
            {
                TokenGroupBox.Header = "Nenhum token";
            }

            return success;
        }