PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/FileProcessor.cpp
00001 /*
00002  *  FileProcessor.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 11 Aug 2011
00006  *
00007  */
00008 #include "FileProcessor.h"
00009         
00010 #include "Exceptions.h"
00011 #include "Interpreter.h"
00012 
00013 #include <boost/filesystem/operations.hpp>
00014 #include <boost/algorithm/string.hpp>
00015 
00016 using namespace std;
00017 using namespace boost;
00018 using namespace boost::filesystem;
00019 
00020 namespace phlogo {
00021 
00022 path FileProcessor::locateFile(path p, path fn) {
00023     
00024     path f = p / fn;
00025     if (!exists(f)) {
00026         f = path("../../..") / p / fn;
00027         if (!exists(f)) {
00028             f = path("..") / p / fn;
00029             if (!exists(f)) {
00030                 BOOST_THROW_EXCEPTION( bad_testsuite_exception() << errinfo_name(f.c_str()) );
00031             }
00032         }
00033     }
00034     
00035     return f;
00036 }
00037 
00038 void FileProcessor::parseLine(const string &s, Interpreter *itp, World *world) {
00039     
00040     string line = s;
00041     trim(line);
00042     if (line.length() > 0 && line[0] != '#') {
00043         if (line.find("defun") == 0) {
00044             string remain = line.substr(6);
00045             size_t i = remain.find_first_of("\t ");
00046             if (i != string::npos) {
00047                 string funcname = remain.substr(0, i);
00048                 remain = remain.substr(i);
00049                 i = remain.find_first_not_of("\t ");
00050                 string args = "";
00051                 if (i != string::npos)
00052                     args = remain.substr(i);
00053                 itp->defun(funcname, args);
00054             }
00055             else {
00056                 itp->defun(remain, "");
00057             }
00058         }
00059         else if (line.find("end") == 0) {
00060             itp->end();
00061         }
00062         else
00063             itp->run(world, line);
00064     }
00065 }
00066     
00067 void FileProcessor::parseFile(boost::filesystem::path f, LineProcessor *processor) {
00068     
00069         ifstream is(f.c_str());
00070     char buf[10 * 1024];
00071     while (!is.eof()) {
00072         is.getline(buf, sizeof(buf));
00073         string line(buf);
00074         processor->process(line);
00075     }
00076 }
00077     
00078 void LogoLineProcessor::process(const std::string &line) {
00079     FileProcessor::parseLine(line, _itp, _world);
00080 }
00081     
00082 }
 All Classes Functions