|
| 1 | +#include <lbd/intp/builtins.h> |
| 2 | + |
| 3 | +namespace intp::interp { |
| 4 | + // TODO: If Arity is -1 that means it can consume all the arguments |
| 5 | + // Prints Argument to stdout and returns the same Argument as Value |
| 6 | + static NativeFunction make_print() { |
| 7 | + const std::string name = "print"; |
| 8 | + return { |
| 9 | + 1, name, [](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 10 | + const Value &value = args[0]->force(); |
| 11 | + std::cout << value << std::endl; |
| 12 | + return value; |
| 13 | + } |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + static NativeFunction make_add() { |
| 18 | + const std::string name = "add"; |
| 19 | + return { |
| 20 | + 2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 21 | + const Value &value1 = args[0]->force(); |
| 22 | + const Value &value2 = args[1]->force(); |
| 23 | + if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) { |
| 24 | + std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl |
| 25 | + << name << " signature: Float -> Float -> Float" << std::endl; |
| 26 | + exit(EXIT_FAILURE); |
| 27 | + } |
| 28 | + const double result = std::get<double>(value1) + std::get<double>(value2); |
| 29 | + return Value{result}; |
| 30 | + } |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + static NativeFunction make_sub() { |
| 35 | + const std::string name = "sub"; |
| 36 | + return { |
| 37 | + 2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 38 | + const Value &value1 = args[0]->force(); |
| 39 | + const Value &value2 = args[1]->force(); |
| 40 | + if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) { |
| 41 | + std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl |
| 42 | + << name << " signature: Float -> Float -> Float" << std::endl; |
| 43 | + exit(EXIT_FAILURE); |
| 44 | + } |
| 45 | + const double result = std::get<double>(value1) - std::get<double>(value2); |
| 46 | + return Value{result}; |
| 47 | + } |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + static NativeFunction make_mul() { |
| 52 | + const std::string name = "mul"; |
| 53 | + return { |
| 54 | + 2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 55 | + const Value &value1 = args[0]->force(); |
| 56 | + const Value &value2 = args[1]->force(); |
| 57 | + if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) { |
| 58 | + std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl |
| 59 | + << name << " signature: Float -> Float -> Float" << std::endl; |
| 60 | + exit(EXIT_FAILURE); |
| 61 | + } |
| 62 | + const double result = std::get<double>(value1) * std::get<double>(value2); |
| 63 | + return Value{result}; |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + static NativeFunction make_cmp() { |
| 69 | + const std::string name = "cmp"; |
| 70 | + return { |
| 71 | + 2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 72 | + const Value &value1 = args[0]->force(); |
| 73 | + const Value &value2 = args[1]->force(); |
| 74 | + if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) { |
| 75 | + std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl |
| 76 | + << name << " signature: Float -> Float -> Float" << std::endl; |
| 77 | + exit(EXIT_FAILURE); |
| 78 | + } |
| 79 | + const double num1 = std::get<double>(value1); |
| 80 | + const double num2 = std::get<double>(value2); |
| 81 | + const int result = (num1 < num2) ? -1 : (num1 > num2 ? 1 : 0); |
| 82 | + return Value{static_cast<double>(result)}; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + static NativeFunction make_if_zero() { |
| 88 | + const std::string name = "if_zero"; |
| 89 | + return { |
| 90 | + 3, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value { |
| 91 | + const Value &cond_value = args[0]->force(); |
| 92 | + if (!std::holds_alternative<double>(cond_value)) { |
| 93 | + std::cerr << "runtime error: wrong argument type to native function " << name << "\n" |
| 94 | + << name << " signature: Float -> Any -> Any -> Any\n" |
| 95 | + << "runtime error: expected <double> got " << cond_value << std::endl; |
| 96 | + exit(EXIT_FAILURE); |
| 97 | + } |
| 98 | + // Lazy branching: only force the chosen clause |
| 99 | + if (const double cond = std::get<double>(cond_value); cond == 0.0) { |
| 100 | + return args[1]->force(); |
| 101 | + } |
| 102 | + return args[2]->force(); |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + std::vector<NativeFunction> get_builtins() { |
| 109 | + return { |
| 110 | + {make_print()}, |
| 111 | + {make_add()}, |
| 112 | + {make_sub()}, |
| 113 | + {make_mul()}, |
| 114 | + {make_cmp()}, |
| 115 | + {make_if_zero()} |
| 116 | + }; |
| 117 | + } |
| 118 | +} |
0 commit comments