|
PH Logo
Logo intepreter modeled after UCB Logo.
|
00001 /* 00002 * RestProcessor.cpp 00003 * 00004 * Author: Paul Hamilton 00005 * Date: 8 Aug 2011 00006 * 00007 */ 00008 #include "RestProcessor.h" 00009 00010 #include "RestOutput.h" 00011 00012 #include <boost/regex.hpp> 00013 00014 using namespace std; 00015 00016 namespace phlogo { 00017 00018 boost::shared_ptr<RestProcessor> RestProcessor::instance; 00019 00020 RestProcessor *RestProcessor::getInstance() { 00021 if (!instance.get()) 00022 instance.reset(new RestProcessor()); 00023 return instance.get(); 00024 } 00025 00026 bool RestProcessor::process(const string &path, RestOutput *output) { 00027 00028 boost::smatch what; 00029 if (!boost::regex_match(path, what, boost::regex("^/(.*)/\\?(.*)$"))) 00030 return false; 00031 00032 if (what.size() < 2) 00033 return false; 00034 00035 string restpath = what[1]; 00036 string args = what[2]; 00037 00038 if (restpath == "logo") { 00039 if (boost::regex_match(args, what, boost::regex("^cmd=(.*)$"))) { 00040 startOutput(); 00041 _t.startGData(); 00042 00043 // process the args against out internal interpreter and world. 00044 _itp.run(&_w, what[1]); 00045 00046 // get the output. 00047 output->output(endOutput(_t.endGData())); 00048 } 00049 else 00050 return false; 00051 } 00052 else if (restpath == "defun") { 00053 if (boost::regex_match(args, what, boost::regex("^name=(.*)$"))) { 00054 _itp.defun(what[1], ""); 00055 startOutput(); 00056 outputLine("defining function " + what[1]); 00057 output->output(endOutput("[]")); 00058 } 00059 } 00060 else if (restpath == "end") { 00061 _itp.end(); 00062 startOutput(); 00063 outputLine("function defined"); 00064 output->output(endOutput("[]")); 00065 } 00066 else 00067 return false; 00068 return true; 00069 } 00070 00071 void RestProcessor::startOutput() { 00072 _ss.str(""); 00073 _ss << "[["; 00074 _error = false; 00075 _firstop = true; 00076 } 00077 00078 string RestProcessor::endOutput(const string &gdata) { 00079 _ss << "]," << gdata << "]"; 00080 return _ss.str(); 00081 } 00082 00083 void RestProcessor::reset() { 00084 if (_firstop) 00085 _firstop = false; 00086 else 00087 _ss << ","; 00088 _ss << "{c:0}"; 00089 } 00090 00091 void RestProcessor::outputLine(const string &s) { 00092 if (_firstop) 00093 _firstop = false; 00094 else 00095 _ss << ","; 00096 _ss << "{o:['" << s << "']}"; 00097 } 00098 00099 void RestProcessor::outputError(const string &s) { 00100 if (_firstop) 00101 _firstop = false; 00102 else 00103 _ss << ","; 00104 _ss << "{e:['" << s << "']}"; 00105 _error = true; 00106 } 00107 00108 bool RestProcessor::isError() { 00109 return _error; 00110 } 00111 00112 std::string RestProcessor::getString() { 00113 return _ss.str(); 00114 } 00115 00116 }
1.7.4