PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/FormatVerbs.cpp
00001 /*
00002  *  PredicateVerbs.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 17 Aug 2011
00006  *
00007  */
00008 
00009 #include "Verbs.h"
00010 
00011 #include "Thing.h"
00012 #include "World.h"
00013 #include "TokenStream.h"
00014 
00015 #include <sstream>
00016 #include <boost/format.hpp>
00017 #include <boost/lexical_cast.hpp>
00018 
00019 using namespace std;
00020 using namespace boost;
00021 
00022 namespace phlogo {
00023     
00024 void Verbs::form(TokenStream *ts) {
00025 
00026     pThing t = getThing(ts);
00027     long width = getLong(ts);
00028     
00029     stringstream ss;
00030     if (width >= 0) {
00031         long precision = getLong(ts);
00032         stringstream fss;
00033         fss << format("%%%s.%sf") % width % precision;
00034         ss << format(fss.str()) % t->asDouble();
00035     }
00036     else {
00037         string f = getWord(ts);
00038         
00039         if (t->isWord()) {
00040             string s = t->getWord();
00041             try {
00042                 ss << format(f) % lexical_cast<long>(s);
00043             }
00044             catch (bad_cast &x) {
00045                 try {
00046                     ss << format(f) % lexical_cast<double>(s);
00047                 }
00048                 catch (bad_cast &x) {
00049                     ss << format(f) % s;
00050                 }
00051             }
00052         }
00053         else if (t->isLong())
00054             ss << format(f) % t->getLong();
00055         else if (t->isDouble())
00056             ss << format(f) % t->getDouble();
00057         else
00058             ss << format(f) % t->str();
00059     }
00060     
00061     setStringResult(ss.str());
00062 }    
00063 
00064 }
 All Classes Functions