test-parser.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
00003  *
00004  * This file is part of vyp08 (compiler and interpreter of VYP08 language).
00005  *
00006  * vyp08 is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * any later version.
00010  *
00011  * vyp08 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with vyp08.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include "config.h"
00021 #include "parser.h"
00022 #include "vypIO.h"
00023 
00024 #ifndef BUILDING_DOX
00025 #   include <string>
00026 #   include <sstream>
00027 #endif
00028 
00029 using namespace StreamDecorator;
00030 using std::string;
00031 
00032 class TestBuilder: public IBuilder {
00033     public:
00034         TestBuilder() { }
00035         virtual bool hasError() const { return false; }
00036         virtual void errorDetected()                    { std::cerr << "\t" << Color(C_LIGHT_CYAN) << "ERROR_DETECTED" << Color(C_NO_COLOR) << std::endl; }
00037         virtual void glVar(EToken type, Token id)       { std::cerr << Color(C_LIGHT_BLUE)  << "GL_VAR" << Color(C_NO_COLOR) << ":\t" << type << "\t"<< id << std::endl; }
00038         virtual void fncDeclInit(EToken type, Token id) { std::cerr << Color(C_LIGHT_RED)   << "FNC_DECL_INIT" << Color(C_NO_COLOR) << ":\t" << type << "\t" << id << std::endl; }
00039         virtual void fncDeclArg(EToken type)            { std::cerr << "\t" << Color(C_LIGHT_PURPLE)<< "FNC_DECL_ARG" << Color(C_NO_COLOR) << ":\t" << type << std::endl; }
00040         virtual void fncDecl()                          { std::cerr << "\t" << Color(C_LIGHT_GREEN) << "FNC_DECL_OK" << Color(C_NO_COLOR) << std::endl << std::endl; }
00041         virtual void fncDefInit(EToken type, Token id)  { std::cerr << Color(C_LIGHT_RED)   << "FNC_DEF_INIT" << Color(C_NO_COLOR) << ":\t" << type << "\t" << id << std::endl; }
00042         virtual void fncDefArg(EToken type, Token id)   { std::cerr << "\t" << Color(C_LIGHT_PURPLE)<< "FNC_DEF_ARG" << Color(C_NO_COLOR) << ":\t" << type << "\t" << id << std::endl; }
00043         virtual void fncDefVar(EToken type, Token id)   { std::cerr << "\t" << Color(C_LIGHT_BLUE)  << "FNC_DEF_VAR" << Color(C_NO_COLOR) << ":\t" << type << "\t"<< id << std::endl; }
00044         virtual void fncDefBody()                       { std::cerr << "\t" << Color(C_LIGHT_CYAN) << "FNC_DEF_BODY" << Color(C_NO_COLOR) << std::endl; }
00045         virtual void fncDef()                           { std::cerr << "\t" << Color(C_LIGHT_GREEN) << "FNC_DEF_OK" << Color(C_NO_COLOR) << std::endl << std::endl; }
00046         virtual void assign(Token dest)                 { std::cerr << "\t" << dest << " := " << Color(C_YELLOW) << "POP" << Color(C_NO_COLOR) << std::endl; }
00047         virtual void ifEnter(Token)                     { std::cerr << "\t" << Color(C_LIGHT_RED)   << "if" << Color(C_NO_COLOR) << " (EXPR) " << Color(C_LIGHT_RED) << "{" << Color(C_NO_COLOR) << std::endl; }
00048         virtual void ifElse()                           { std::cerr << "\t" << Color(C_LIGHT_RED)   << "} else {" << Color(C_NO_COLOR) << std::endl; }
00049         virtual void ifLeave()                          { std::cerr << "\t" << Color(C_LIGHT_RED)   << "}" << Color(C_NO_COLOR) << std::endl; }
00050         virtual void whileInit(Token)                   { std::cerr << "\t" << Color(C_LIGHT_CYAN)   << "while (" << Color(C_NO_COLOR) << std::endl; }
00051         virtual void whileEnter()                       { std::cerr << "\t" << Color(C_LIGHT_CYAN)   << ") {" << Color(C_NO_COLOR) << std::endl; }
00052         virtual void whileLeave()                       { std::cerr << "\t" << Color(C_LIGHT_CYAN)   << "}" << Color(C_NO_COLOR) << std::endl; }
00053         virtual void pushToken(Token token)             { std::cerr << "\t" << Color(C_YELLOW) << "PUSH" << Color(C_NO_COLOR) << ": " << token << std::endl; }
00054         virtual void evalUnOp(Token token)              { std::cerr << "\t" << Color(C_LIGHT_CYAN) << "EVAL_UNARY" << Color(C_NO_COLOR) << ": " << token << std::endl; }
00055         virtual void evalBinOp(Token token)             { std::cerr << "\t" << Color(C_LIGHT_PURPLE) << "EVAL_BINARY" << Color(C_NO_COLOR) << ": " << token << std::endl; }
00056         virtual void fncCall(Token id, int argsToPop, bool pushResult) {
00057             std::cerr << "\t" << Color(C_LIGHT_BLUE) << "FNC_CALL" << Color(C_NO_COLOR) << ": " << id << std::endl;
00058             for(int i=0; i<argsToPop; i++)
00059                 std::cerr << "\t\t" << Color(C_LIGHT_PURPLE) << "POP" << Color(C_NO_COLOR) << " -> ARG" << std::endl;
00060             if (pushResult)
00061                 std::cerr << "\t\t" << Color(C_YELLOW) << "PUSH" << Color(C_NO_COLOR) << " <- RESULT" << std::endl;
00062         }
00063         virtual void fncCallPrint(Token id, EToken valType) {
00064             std::cerr << "\t" << Color(C_LIGHT_RED) << "FNC_CALL_PRINT" << Color(C_NO_COLOR) << ":\t" << id << ":\t" << valType << std::endl;
00065         }
00066 };
00067 
00068 static char const *testName = "test-parser";
00069 
00071 static char const *testContent = "var\n\
00072     int glInt,\n\
00073     double glDouble,\n\
00074     string glString\n\
00075 \n\
00076 void f();\n\
00077 double toDouble(string);\n\
00078 string strcat(string;string);\n\
00079 int main(int argc;string argv) {\n\
00080     f();\n\
00081     main := 1.2e-3 - argc - toDouble(argv);\n\
00082 }\n\
00083 \n\
00084 string strcat(string a; string b)var string c;\n\
00085 {\n\
00086     while a + b eq a+b {\n\
00087         c := a + b;\n\
00088         if \"\" <= c {\n\
00089             strcat := main(0,c);\n\
00090         } else {\n\
00091             strcat := strcat(c,a+b);\n\
00092         };\n\
00093     };\n\
00094 }\n";
00095 
00096 int main(int argc, char *argv[]) {
00097 #if CONSOLE_COLOR_OUTPUT
00098     StreamDecorator::Color::enable(true);
00099 #endif
00100 
00101     string fn(testName);
00102     IScanner *scanner = 0;
00103     std::istringstream *stream = 0;
00104 
00105     if (argc == 2 && string(argv[1]) == string("-")) {
00106         // read from std::cin if "-" is the only arg
00107         fn = "-";
00108         scanner = ScannerFactory::createScanner(std::cin, fn);
00109     } else {
00110         // use built-in test input otherwise
00111         stream = new std::istringstream(testContent);
00112         scanner = ScannerFactory::createScanner(*stream, fn);
00113     }
00114 
00115     // run parser
00116     IBuilder *builder = new TestBuilder;
00117     int status = Parser::parse(scanner, builder, fn);
00118 
00119     // cleanup
00120     delete builder;
00121     delete scanner;
00122     delete stream;
00123     return status;
00124 }

Generated on Sat Jul 4 18:32:59 2009 for vyp08 (compiler and interpreter of VYP08 language) by  doxygen 1.5.4