Skip to content
Snippets Groups Projects
Select Git revision
  • 620be0d6523bf3f2430d6453bad84e89f69bad45
  • develop default protected
  • feature/gmsh-reader
  • origin/stage/bouguettaia
  • feature/kinetic-schemes
  • feature/reconstruction
  • feature/local-dt-fsi
  • feature/composite-scheme-sources
  • feature/composite-scheme-other-fluxes
  • feature/serraille
  • feature/variational-hydro
  • feature/composite-scheme
  • hyperplastic
  • feature/polynomials
  • feature/gks
  • feature/implicit-solver-o2
  • feature/coupling_module
  • feature/implicit-solver
  • feature/merge-local-dt-fsi
  • master protected
  • feature/escobar-smoother
  • v0.5.0 protected
  • v0.4.1 protected
  • v0.4.0 protected
  • v0.3.0 protected
  • v0.2.0 protected
  • v0.1.0 protected
  • Kidder
  • v0.0.4 protected
  • v0.0.3 protected
  • v0.0.2 protected
  • v0 protected
  • v0.0.1 protected
33 results

PugsParser.cpp

Blame
    • Stéphane Del Pino's avatar
      620be0d6
      Remove ASTNode::m_value member · 620be0d6
      Stéphane Del Pino authored
      Values are no more stored into AST nodes. The reason for that is that it was not
      appropriate for multi-threaded evaluation of functions, since multiple
      evaluations could not occur at the same time.
      
      Now expression evaluation do not fill the node value but eventually return the
      computed value.
      
      Also, function are evaluated using a context array of values. Thus each function
      calls is done in a separated context (as it should have always been).
      
      This is a big change in the AST execution strategy. Nonetheless, it leads to a
      seemingly better code, and there will be no more undefined trailing values in
      the AST (for some node ASTNode::m_value would never be initialized/used).
      620be0d6
      History
      Remove ASTNode::m_value member
      Stéphane Del Pino authored
      Values are no more stored into AST nodes. The reason for that is that it was not
      appropriate for multi-threaded evaluation of functions, since multiple
      evaluations could not occur at the same time.
      
      Now expression evaluation do not fill the node value but eventually return the
      computed value.
      
      Also, function are evaluated using a context array of values. Thus each function
      calls is done in a separated context (as it should have always been).
      
      This is a big change in the AST execution strategy. Nonetheless, it leads to a
      seemingly better code, and there will be no more undefined trailing values in
      the AST (for some node ASTNode::m_value would never be initialized/used).
    PugsParser.cpp 2.85 KiB
    #include <PugsParser.hpp>
    
    #include <PugsAssert.hpp>
    
    #include <fstream>
    #include <iostream>
    #include <unordered_map>
    #include <variant>
    
    #include <rang.hpp>
    
    #include <pegtl/analyze.hpp>
    #include <pegtl/contrib/parse_tree.hpp>
    #include <pegtl/contrib/parse_tree_to_dot.hpp>
    
    #include <ASTNode.hpp>
    
    #include <ASTBuilder.hpp>
    #include <PEGGrammar.hpp>
    #include <SymbolTable.hpp>
    
    #include <ASTModulesImporter.hpp>
    
    #include <ASTNodeDataTypeBuilder.hpp>
    #include <ASTNodeDataTypeChecker.hpp>
    
    #include <ASTNodeJumpPlacementChecker.hpp>
    
    #include <ASTNodeExpressionBuilder.hpp>
    
    #include <ASTSymbolInitializationChecker.hpp>
    #include <ASTSymbolTableBuilder.hpp>
    
    #include <ASTNodeEmptyBlockCleaner.hpp>
    
    #include <ASTNodeDeclarationToAffectationConverter.hpp>
    #include <ASTNodeTypeCleaner.hpp>
    
    #include <ASTDotPrinter.hpp>
    #include <ASTPrinter.hpp>
    
    void
    parser(const std::string& filename)
    {
      const size_t grammar_issues = analyze<language::grammar>();
    
      std::cout << rang::fgB::yellow << "grammar_issues=" << rang::fg::reset << grammar_issues << '\n';
    
      std::cout << rang::style::bold << "Parsing file " << rang::style::reset << rang::style::underline << filename
                << rang::style::reset << " ...\n";
    
      std::unique_ptr<ASTNode> root_node;
      read_input input(filename);
      try {
        root_node = ASTBuilder::build(input);
    
        ASTModulesImporter{*root_node};
        ASTNodeTypeCleaner<language::import_instruction>{*root_node};
    
        ASTSymbolTableBuilder{*root_node};
    
        ASTSymbolInitializationChecker{*root_node};
    
        ASTNodeDataTypeBuilder{*root_node};
    
        {
          std::string dot_filename{"parse_tree.dot"};
          std::ofstream fout(dot_filename);
          ASTDotPrinter dot_printer{*root_node};
          fout << dot_printer;
          std::cout << "   AST dot file: " << dot_filename << '\n';
        }
    
        ASTNodeDataTypeChecker{*root_node};
    
        ASTNodeJumpPlacementChecker{*root_node};
    
        // optimizations
        ASTNodeDeclarationToAffectationConverter{*root_node};
    
        ASTNodeTypeCleaner<language::declaration>{*root_node};
        ASTNodeTypeCleaner<language::let_declaration>{*root_node};
    
        ASTNodeEmptyBlockCleaner{*root_node};
    
        ASTNodeExpressionBuilder{*root_node};
    
        std::cout << ASTPrinter{*root_node} << '\n';
    
        ExecutionPolicy exec_all;
        root_node->execute(exec_all);
        std::cout << *(root_node->m_symbol_table) << '\n';
      }
      catch (const parse_error& e) {
        const auto p = e.positions.front();
        std::cerr << rang::style::bold << p.source << ':' << p.line << ':' << p.byte_in_line << ": " << rang::style::reset
                  << rang::fgB::red << "error: " << rang::fg::reset << rang::style::bold << e.what() << rang::style::reset
                  << '\n'
                  << input.line_at(p) << '\n'
                  << std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' << rang::fg::reset << std::endl;
        std::exit(1);
      }
    
      std::cout << "Parsed: " << filename << '\n';
    }