using System;
using Calculator;

// ====================================================================
//
//  Desk Calculator
//  ---------------
//
//  Author: S. Laakko
//
//  COMMANDS:
//
//      print           - print current values of variables
//      <var> = <expr>  - evaluate <expr> and assign its value to <var>
//      <expr>          - evaluate <expr> and print its value
//
//  SYNTAX:
//
//      <expr>          ::= <term> (('+' | '-') <term>)*
//      <term>          ::= <factor> (('*' | '/') <factor>)*
//      <factor>        ::= ('+' | '-')? <primary>
//      <primary>       ::= <number> | <var> | '(' <expr> ')'
//      <number>        ::= DIGIT+ ('.' DIGIT+)?
//      <var>           ::= LETTER+
//
// ====================================================================

int main()
{
    try
    {
        Console.WriteLine("desk calculator");
        string eof = "CTRL-D";
        if (GetPlatform() == Platform.windows)
        {
            eof = "CTRL-Z";
        }
        Console.WriteLine("enter command, or " + eof + " to end:");
        SymbolTable symbolTable = new SymbolTable();
        Console.Write("> ");
        string line = Console.ReadLine();
        while (line != null)
        {
            try
            {
                Parse(line, symbolTable);
            }
            catch (ParsingException ex)
            {
                Console.Error.WriteLine("error: " + ex.Message);
            }
            catch (DivisionByZeroException ex)
            {
                Console.Error.WriteLine("error: " + ex.Message);
            }
            catch (VariableNotFoundException ex)
            {
                Console.Error.WriteLine("error: " + ex.Message);
            }
            catch (ConversionException ex)
            {
                Console.Error.WriteLine("error: " + ex.Message);
            }
            Console.Write("> ");
            line = Console.ReadLine();
        }
        Console.WriteLine("bye!");
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine(ex.ToString());
        return 1;
    }
    return 0;
}