view tty-main.cpp @ 12:894be158b32d

define parser as a class and eliminate some global variables
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 17:57:20 -0400
parents 69f5d5e42d2b
children
line wrap: on
line source

#include <iostream>
#include <string>

#include <cstdio>
#include <cstdlib>

#include <readline/readline.h>
#include <readline/history.h>

#include "tty-main.h"

#include "interpreter.h"
#include "parser.h"

namespace calc
{
  int tty_main (int, char **)
  {
    std::cout
      << "Example Calculator.\n"
      << "Available operations: + - * / ^ ()\n"
      << "Semicolon terminates statement.\n"
      << "GNU Readline available for history editing.\n" << std::endl;

    calc::interpreter interp;

    calc::parser& parser = interp.get_parser ();

    for (;;)
      {
        char *tmp = readline (parser.beg_of_stmt () ? ">> " : "");

        if (! tmp)
          {
            std::cout << "\n";
            break;
          }

        if (*tmp)
          {
            add_history (tmp);
            using_history ();
          }

        std::string line = tmp;

        free (tmp);

        int status = interp.parse_and_execute (line);

        if (status < 0)
          break;
      }

    return 0;
  }
}