Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <lean/lean.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct mathVec {
- uint32_t length;
- float* data;
- } mathVec;
- static lean_external_class* g_mathVec_external_class = NULL;
- lean_obj_res make_error(const char* err_msg) {
- return lean_mk_io_user_error(lean_mk_io_user_error(lean_mk_string(err_msg)));
- }
- void mathVec_finalizer(void* v_) {
- mathVec* v = (mathVec*) v_;
- if (v->data) {
- free(v->data);
- }
- free(v);
- }
- void noop_foreach(void* mod, b_lean_obj_arg fn) {}
- lean_obj_res mathVec_initialize() {
- g_mathVec_external_class = lean_register_external_class(
- mathVec_finalizer,
- noop_foreach
- );
- return lean_io_result_mk_ok(lean_box(0));
- }
- mathVec* mathVec_alloc(uint32_t length){
- mathVec* out = (mathVec*) malloc(sizeof(mathVec));
- if (out == NULL) {
- printf("idk im just not gonna deal with this lol\n");
- }
- out->data = (float*)(malloc(sizeof(float)*length));
- if (out->data == NULL) {
- printf("meh\n");
- }
- out->length = length;
- return out;
- }
- lean_object* mathVec_boxer(mathVec* v) {
- return lean_alloc_external(g_mathVec_external_class, v);
- }
- mathVec* mathVec_unboxer(lean_object* o) {
- return (mathVec*) lean_get_external_data(o);
- }
- lean_obj_res mathVec_new(uint32_t length, float x) {
- if (length < 0) {
- return make_error("invalid length");
- }
- mathVec* v = mathVec_alloc(length);
- if (!v) {
- return make_error("ERROR_INSUF_MEM");
- }
- for (uint32_t i = 0; i < v->length; i++) {
- v->data[i] = x;
- }
- return lean_io_result_mk_ok(mathVec_boxer(v));
- }
- lean_obj_res mathVec_get_val(b_lean_obj_arg _v, uint32_t i) {
- mathVec* v = mathVec_unboxer(_v);
- if (i > v->length){
- return make_error("sad");
- }
- return lean_io_result_mk_ok(lean_box_float(v->data[i]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement