void z::Compiler::compile() { for(z::Ast::Config::PathList::const_iterator it = _config.sourceFileList().begin(); it != _config.sourceFileList().end(); ++it) { const z::string& filename = *it; z::string ext = z::dir::getExtention(filename); if(_project.zppExt().find(ext) == z::string::npos) { continue; } std::cout << "Compiling " << filename << std::endl; z::Ast::Unit unit; initContext(unit, false); z::Ast::Module module(unit, filename, 0); if(!compileFile(module, filename, "Compiling")) throw z::Exception("Compiler", zfmt(Ast::Token(filename, 0, 0, ""), z::string("Cannot open source file: %{s}").arg("s", filename))); ZenlangGenerator zgenerator(_project, _config, module); zgenerator.run(); if(_config.olanguage() == "stlcpp") { StlcppGenerator generator(_project, _config, module); generator.run(); } else { throw z::Exception("Compiler", zfmt(Ast::Token(filename, 0, 0, ""), "Unknown code generator %{s}").arg("s", _config.olanguage())); } } }
bool z::Compiler::compileFile(z::Ast::Module& module, const z::string& filename, const z::string& msg) { if(_project.verbosity() >= z::Ast::Project::Verbosity::Normal) { z::string indent = " "; for(z::Ast::Module::Level_t i = 0; i < module.level(); ++i) { indent += " "; } std::cout << indent << msg << " " << filename << std::endl; } std::ifstream is; is.open(z::s2e(filename).c_str(), std::ifstream::in); if(is.is_open() == false) { throw z::Exception("Compiler", zfmt(z::Ast::Token(filename, 0, 0, ""), z::string("Error opening file %{s}").arg("s", filename))); } Parser parser; Lexer lexer(parser); z::Ast::Factory factory(module); ParserContext pctx(factory, z::ref(this)); while(!is.eof()) { char buf[1025]; memset(buf, 0, 1024); is.read(buf, 1024); std::streamsize got = is.gcount(); lexer.push(pctx, buf, got, is.eof()); } return true; }
inline z::string z::Compiler::findImport(const z::string& filename) { z::string cfile; // first check current dir cfile = "./" + filename; if(checkFile(cfile)) return cfile; // next check zen lib dir cfile = _project.zlibPath() + "/" + filename; if(checkFile(cfile)) return cfile; // then all other include paths for(z::Ast::Config::PathList::const_iterator it = _config.includePathList().begin(); it != _config.includePathList().end(); ++it) { const z::string& dir = *it; cfile = dir + "/" + filename; if(checkFile(cfile)) return cfile; } throw z::Exception("Compiler", zfmt(z::Ast::Token(filename, 0, 0, ""), z::string("Cannot open include file: %{s}").arg("s", filename))); }
inline void z::CmakeGenerator::Impl::generateProject(const z::Ast::Config& config, z::ofile& os) { os() << "CMAKE_MINIMUM_REQUIRED(VERSION 2.6)" << std::endl; os() << "PROJECT(" << _project.name() << ")\n"; os() << "SET(ZEN_ROOT \"" << _project.zlibPath() << "\")" << std::endl; if(config.gui()) { os() << "INCLUDE(FindPkgConfig)" << std::endl; os() << "pkg_check_modules(WEBKIT REQUIRED webkit-1.0)" << std::endl; os() << "INCLUDE_DIRECTORIES(${WEBKIT_INCLUDE_DIRS})" << std::endl; os() << "LINK_DIRECTORIES(${WEBKIT_LIBRARY_DIRS})" << std::endl; os() << "ADD_DEFINITIONS( \"-DGUI\" )" << std::endl; } os() << "INCLUDE(${ZEN_ROOT}/SetupZL.cmake)" << std::endl; os() << std::endl; os() << "INCLUDE_DIRECTORIES(\"${ZEN_ROOT}\")" << std::endl; os() << "LINK_DIRECTORIES(\"${ZEN_ROOT}\")" << std::endl; os() << "IF(CMAKE_COMPILER_IS_GNUCXX)" << std::endl; os() << " ADD_DEFINITIONS( \"-Wall\" )" << std::endl; os() << "ENDIF(CMAKE_COMPILER_IS_GNUCXX)" << std::endl; os() << std::endl; if(config.debug()) { os() << "ADD_DEFINITIONS( \"-DDEBUG\" )" << std::endl; } if(config.test()) { os() << "ADD_DEFINITIONS( \"-DUNIT_TEST\" )" << std::endl; } os() << "INCLUDE_DIRECTORIES(\"${CMAKE_CURRENT_SOURCE_DIR}\")" << std::endl; os() << "INCLUDE_DIRECTORIES(\"" << config.apidir() << "\")" << std::endl; os() << "INCLUDE_DIRECTORIES(\"" << config.srcdir() << "\")" << std::endl; for(z::Ast::Config::PathList::const_iterator it = config.includePathList().begin(); it != config.includePathList().end(); ++it) { const z::string& dir = *it; os() << "INCLUDE_DIRECTORIES(\"" << dir << "\")" << std::endl; } os() << std::endl; os() << "SET(project_SOURCES ${project_SOURCES} ${ZEN_ROOT}/zenlang.cpp)" << std::endl; z::string zexePath = _project.zexePath(); zexePath.replace("\\", "/"); for(z::Ast::Config::PathList::const_iterator it = config.sourceFileList().begin(); it != config.sourceFileList().end(); ++it) { const z::string& filename = *it; z::string basename = z::dir::getBaseName(filename); z::string ext = z::dir::getExtention(filename); if((_project.hppExt().find(ext) != z::string::npos) || (_project.cppExt().find(ext) != z::string::npos)) { os() << "SET(project_SOURCES ${project_SOURCES} " << filename << ")" << std::endl; } else if(_project.zppExt().find(ext) != z::string::npos) { z::string debugFlag = config.debug()?" --debug":""; z::string testFlag = config.test()?" ":" --test"; os() << "ADD_CUSTOM_COMMAND(" << std::endl; os() << " COMMAND \"" << zexePath << "\"" << debugFlag << testFlag << " -c \"" << filename << "\"" << std::endl; os() << " OUTPUT \"" << basename << ".cpp\"" << std::endl; os() << " DEPENDS \"" << filename << "\"" << std::endl; os() << ")" << std::endl; os() << "SET(project_SOURCES ${project_SOURCES} " << basename << ".cpp)" << std::endl; } else { throw z::Exception("CmakeGenerator", zfmt(z::Ast::Token(filename, 0, 0, ""), "Unknown file type for: %{s}").arg("s", filename)); } } os() << std::endl; switch(config.buildMode()) { case z::Ast::Config::BuildMode::Executable: os() << "ADD_DEFINITIONS( \"-DZ_EXE\" )" << std::endl; if(config.gui()) { os() << "IF(WIN32)" << std::endl; os() << " ADD_EXECUTABLE(" << _project.name() << " WIN32 ${project_SOURCES})" << std::endl; os() << "ELSE(WIN32)" << std::endl; os() << " ADD_EXECUTABLE(" << _project.name() << " ${project_SOURCES})" << std::endl; os() << "ENDIF(WIN32)" << std::endl; } else { os() << "ADD_EXECUTABLE(" << _project.name() << " ${project_SOURCES})" << std::endl; } break; case z::Ast::Config::BuildMode::Shared: os() << "ADD_DEFINITIONS( \"-DZ_DLL\" )" << std::endl; os() << "ADD_LIBRARY(" << _project.name() << " SHARED ${project_SOURCES})" << std::endl; break; case z::Ast::Config::BuildMode::Static: os() << "ADD_DEFINITIONS( \"-DZ_LIB\" )" << std::endl; os() << "ADD_LIBRARY(" << _project.name() << " STATIC ${project_SOURCES})" << std::endl; break; } for(z::Ast::Config::PathList::const_iterator it = config.linkFileList().begin(); it != config.linkFileList().end(); ++it) { const z::string& filename = *it; os() << "TARGET_LINK_LIBRARIES(" << _project.name() << " " << filename << ")" << std::endl; } if(config.gui()) { os() << "TARGET_LINK_LIBRARIES(" << _project.name() << " ${WEBKIT_LIBRARIES})" << std::endl; os() << "TARGET_LINK_LIBRARIES(" << _project.name() << " ${ZENLANG_LIBRARIES})" << std::endl; os() << std::endl; } }
// DataIO_Std::WriteSet3D() int DataIO_Std::WriteSet3D( DataSet const& setIn, CpptrajFile& file ) { if (setIn.Ndim() != 3) { mprinterr("Internal Error: DataSet %s in DataFile %s has %zu dimensions, expected 3.\n", setIn.legend(), file.Filename().full(), setIn.Ndim()); return 1; } DataSet_3D const& set = static_cast<DataSet_3D const&>( setIn ); Dimension const& Xdim = static_cast<Dimension const&>(set.Dim(0)); Dimension const& Ydim = static_cast<Dimension const&>(set.Dim(1)); Dimension const& Zdim = static_cast<Dimension const&>(set.Dim(2)); //if (Xdim.Step() == 1.0) xcol_precision = 0; if (sparse_) mprintf("\tOnly writing voxels with value > %g\n", cut_); // Print X Y Z Values // x y z val(x,y,z) DataSet::SizeArray pos(3); if (writeHeader_) { file.Printf("#counts %zu %zu %zu\n", set.NX(), set.NY(), set.NZ()); file.Printf("#origin %12.7f %12.7f %12.7f\n", set.Bin().GridOrigin()[0], set.Bin().GridOrigin()[1], set.Bin().GridOrigin()[2]); if (set.Bin().IsOrthoGrid()) { GridBin_Ortho const& b = static_cast<GridBin_Ortho const&>( set.Bin() ); file.Printf("#delta %12.7f %12.7f %12.7f\n", b.DX(), b.DY(), b.DZ()); } else { GridBin_Nonortho const& b = static_cast<GridBin_Nonortho const&>( set.Bin() ); file.Printf("#delta %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f %12.7f\n", b.Ucell()[0]/set.NX(), b.Ucell()[1]/set.NX(), b.Ucell()[2]/set.NX(), b.Ucell()[3]/set.NY(), b.Ucell()[4]/set.NY(), b.Ucell()[5]/set.NY(), b.Ucell()[6]/set.NZ(), b.Ucell()[7]/set.NZ(), b.Ucell()[8]/set.NZ()); } file.Printf("#%s %s %s %s\n", Xdim.Label().c_str(), Ydim.Label().c_str(), Zdim.Label().c_str(), set.legend()); } std::string xyz_fmt; if (XcolPrecSet()) { TextFormat nfmt( XcolFmt(), XcolWidth(), XcolPrec() ); xyz_fmt = nfmt.Fmt() + " " + nfmt.Fmt() + " " + nfmt.Fmt() + " "; } else { TextFormat xfmt( XcolFmt(), set.NX(), Xdim.Min(), Xdim.Step(), 8, 3 ); TextFormat yfmt( XcolFmt(), set.NY(), Ydim.Min(), Ydim.Step(), 8, 3 ); TextFormat zfmt( XcolFmt(), set.NZ(), Zdim.Min(), Zdim.Step(), 8, 3 ); xyz_fmt = xfmt.Fmt() + " " + yfmt.Fmt() + " " + zfmt.Fmt() + " "; } if (sparse_) { for (pos[2] = 0; pos[2] < set.NZ(); ++pos[2]) { for (pos[1] = 0; pos[1] < set.NY(); ++pos[1]) { for (pos[0] = 0; pos[0] < set.NX(); ++pos[0]) { double val = set.GetElement(pos[0], pos[1], pos[2]); if (val > cut_) { Vec3 xyz = set.Bin().Corner(pos[0], pos[1], pos[2]); file.Printf( xyz_fmt.c_str(), xyz[0], xyz[1], xyz[2] ); set.WriteBuffer( file, pos ); file.Printf("\n"); } } } } } else { for (pos[2] = 0; pos[2] < set.NZ(); ++pos[2]) { for (pos[1] = 0; pos[1] < set.NY(); ++pos[1]) { for (pos[0] = 0; pos[0] < set.NX(); ++pos[0]) { Vec3 xyz = set.Bin().Corner(pos[0], pos[1], pos[2]); file.Printf( xyz_fmt.c_str(), xyz[0], xyz[1], xyz[2] ); set.WriteBuffer( file, pos ); file.Printf("\n"); } } } } return 0; }