javascript.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "examples/support.hh"
00039 #include "gecode/serialization.hh"
00040 #include <fstream>
00041 #include <string>
00042
00047 class FileNameOptions : public Options {
00048 protected:
00049 const char* _file;
00050 public:
00052 FileNameOptions(const char* n);
00054 virtual void help(void);
00056 void parse(int& argc, char* argv[]);
00057
00059 void file(const char* f);
00061 const char* file(void) const;
00062 };
00063
00064 inline void
00065 FileNameOptions::file(const char* f) { _file = f; }
00066
00067 inline const char*
00068 FileNameOptions::file(void) const {
00069 return _file;
00070 }
00071
00072 FileNameOptions::FileNameOptions(const char* n)
00073 : Options(n), _file(NULL) {}
00074
00075 void
00076 FileNameOptions::help(void) {
00077 Options::help();
00078 std::cerr << "\t(string)" << std::endl
00079 << "\t\tJavaScript source file to parse" << std::endl;
00080 }
00081
00082 void
00083 FileNameOptions::parse(int& argc, char* argv[]) {
00084 Options::parse(argc,argv);
00085 if (argc < 2)
00086 return;
00087 file(argv[1]);
00088 }
00089
00099 class JavaScript : public Example {
00100 protected:
00102 VarArray<Reflection::Var> x;
00103 public:
00105 JavaScript(const FileNameOptions& opt) : x(this, 0) {
00106 if (opt.file() == NULL) {
00107 throw Exception("JavaScript", "no file given");
00108 }
00109 std::ifstream programFile(opt.file());
00110 std::string program;
00111 if (programFile.fail()) {
00112 throw Exception("JavaScript", "error reading file");
00113 }
00114 while (!programFile.eof()) {
00115 std::string line;
00116 std::getline(programFile, line);
00117 program += line+"\n";
00118 }
00119 programFile.close();
00120 fromJavaScript(this, program);
00121 Reflection::VarMap vm;
00122 for (Reflection::ActorSpecIter si(this, vm); si(); ++si) {
00123 (void) si.actor();
00124 }
00125 for (Reflection::VarMapIter vmi(vm); vmi(); ++vmi) {
00126 x.add(this, Reflection::Var(vmi.varImpBase(), vmi.spec().vti()));
00127 }
00128 }
00129
00131 JavaScript(bool share, JavaScript& s) : Example(share,s) {
00132 x.update(this, share, s.x);
00133 }
00134
00136 virtual Space*
00137 copy(bool share) {
00138 return new JavaScript(share,*this);
00139 }
00140
00142 virtual void
00143 print(std::ostream& os) {
00144 for (int i=0; i<x.size(); i++) {
00145 os << x[i] << (i < x.size() - 1 ? "," : "");
00146 }
00147 os << std::endl;
00148 }
00149 };
00150
00154 int
00155 main(int argc, char* argv[]) {
00156 FileNameOptions opt("JavaScript");
00157 opt.parse(argc,argv);
00158 Gecode::Serialization::initRegistry();
00159 Example::run<JavaScript,DFS,FileNameOptions>(opt);
00160 return 0;
00161 }
00162
00163