// cmd_test.cpp : test simple command handling with map #include #include #include "Lexer.h" #include "term_control.h" #include "error_handling.h" #include "cmd.h" using namespace std; /** * ---------------------------------------------------------------------------------- * just print a prompt. Note the variable 'flush' which forces the prompt to be * printed right away * ---------------------------------------------------------------------------------- */ void prompt() { cout << term_cc(BLUE) << "> " << term_cc() << flush; } /** * ---------------------------------------------------------------------------------- * the body * ---------------------------------------------------------------------------------- */ int main() { map cmd_map; // simply add all commands to the map cmd_map["foreground"] = &print_color; cmd_map["exit"] = &bye; cmd_map["bye"] = &bye; cmd_map["quit"] = &bye; /* cmd_map["print"] = &print; cmd_map["assign"] = &assign; */ // read inputs, call appropriate function from the map Lexer lex; string line; Token tok; while (cin) { prompt(); getline(cin, line); lex.set_input(line); if (!lex.has_more_token()) continue; tok = lex.next_token(); if (tok.type != IDENT) { error_return("Syntax error\n"); continue; } if (cmd_map.find(tok.value) != cmd_map.end()) { cmd_map[tok.value](lex); } else { error_return("Unknown command"); } } return 0; }