Mercurial > jwe > qt-gui-with-push-parser
diff tty-main.cpp @ 0:dff751fb985c
initial revision
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 13 May 2019 09:48:06 -0500 |
parents | |
children | 08df60a01bc1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tty-main.cpp Mon May 13 09:48:06 2019 -0500 @@ -0,0 +1,59 @@ +#include <iostream> +#include <string> + +#include <cstdio> +#include <cstdlib> + +#include <readline/readline.h> +#include <readline/history.h> + +#include "gui-main.h" +#include "tty-main.h" + +#include "parse.h" + +namespace tty +{ + int main (int, char **) + { + std::cout + << "Example Calculator.\n" + << "Available operations: + - * / ^ ()\n" + << "Semicolon terminates statement.\n" + << "GNU Readline available for history editing.\n" << std::endl; + + interpreter::parser_init (); + + for (;;) + { + char *tmp = readline (interpreter::beg_of_stmt ? ">> " : ""); + + if (! tmp) + break; + + if (*tmp) + { + add_history (tmp); + using_history (); + } + + std::string line = tmp; + + free (tmp); + + int status = interpreter::parse_and_execute (line); + + if (status < 0) + break; + } + + interpreter::parser_fini (); + + return 0; + } + + void emit_result (double value) + { + std::cout << "ans = " << value << std::endl; + } +}