#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; } } |