1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
#include <iostream>
#include "binary.hpp"
#include "lex.hpp"
#include "parser.hpp"
#include "runtime.hpp"
#include "symbol-table.hpp"
#include "value.hpp"

using namespace LambdaCalc;

int main() {
   Lex lex(std::cin, std::cout, "> ""| ");
   SymbolTable symtab;
   Parser parser(lex, symtab);

   /* Predefined functions */
   symtab.define("+", make_binary([](int a, int b){ return a + b; }));
   symtab.define("-", make_binary([](int a, int b){ return a - b; }));
   symtab.define("*", make_binary([](int a, int b){ return a * b; }));
   symtab.define("/", make_binary([](int a, int b){ return a / b; }));
   symtab.define("%", make_binary([](int a, int b){ return a % b; }));
   symtab.define("=", make_binary([](int a, int b){ return a == b; }));
   symtab.define("<", make_binary([](int a, int b){ return a < b; }));
   symtab.define("<=", make_binary([](int a, int b){ return a <= b; }));
   symtab.define(">", make_binary([](int a, int b){ return a > b; }));
   symtab.define(">=", make_binary([](int a, int b){ return a >= b; }));
   symtab.define("!=", make_binary([](int a, int b){ return a != b; }));

   /* Abort on syntax errors, i.e. no recovery takes place
      except by printing a final error message and exiting
      the program. */
   try {
      /* Run the outer loop as long as we receive syntactically
     correct expressions. */
      FunctionPtr f;
      while (f = parser.getFunction()) {
     ValuePtr value = (*f)(nullptr);
     if (value->get_type() == Value::INTEGER) {
        std::cout << value->get_integer() << std::endl;
     }
      }
   } catch (Parser::Exception e) {
      std::cout << std::endl << e.what() << std::endl;
   } catch (RuntimeException e) {
      std::cout << std::endl << "runtime exception: " << e.what() << std::endl;
   }
}