/** * ***************************************************************************** * file name : cmd.cpp * author : Hung Ngo * description : definitions of simple toy commands * ***************************************************************************** */ #include #include // for exit() #include "cmd.h" #include "Lexer.h" #include "term_control.h" #include "error_handling.h" using namespace std; /** * ----------------------------------------------------------------------------- * expects two Tokens: IDENT and STRING * prints the STRING using the color specified by the IDENT token * ----------------------------------------------------------------------------- */ void print_color(Lexer lexer) { Token t1, t2; // the following is very clumsy; need a "tokenizer" member function if (lexer.has_more_token()) { t1 = lexer.next_token(); if (lexer.has_more_token()) { t2 = lexer.next_token(); if (!lexer.has_more_token()) { // this means we're probably ok if (t1.type == IDENT && t2.type == STRING) { if (t1.value == "red") { cout << term_cc(RED) << t2.value << endl; return; } else if (t1.value == "green") { cout << term_cc(GREEN) << t2.value << endl; return; } else if (t1.value == "blue") { cout << term_cc(BLUE) << t2.value << endl; return; } } } } } error_return("Syntax error, use \"Foreground [red|green|blue ] \"str\""); } /** * ----------------------------------------------------------------------------- * terminates the program, ignores all parameters * ----------------------------------------------------------------------------- */ void bye(Lexer lexer) { if (lexer.has_more_token()) { error_return("Syntax error: use bye/exit/quit\n"); } else { exit(0); } }