Advertisement
Guest User

Untitled

a guest
Jun 7th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <lean/lean.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. typedef struct mathVec {
  8.     uint32_t length;
  9.     float*   data;
  10. } mathVec;
  11.  
  12. static lean_external_class* g_mathVec_external_class = NULL;
  13.  
  14.  lean_obj_res make_error(const char* err_msg) {
  15.     return lean_mk_io_user_error(lean_mk_io_user_error(lean_mk_string(err_msg)));
  16. }
  17.  
  18. void mathVec_finalizer(void* v_) {
  19.     mathVec* v = (mathVec*) v_;
  20.     if (v->data) {
  21.         free(v->data);
  22.     }
  23.     free(v);
  24. }
  25.  
  26. void noop_foreach(void* mod, b_lean_obj_arg fn) {}
  27.  
  28. lean_obj_res mathVec_initialize() {
  29.     g_mathVec_external_class = lean_register_external_class(
  30.         mathVec_finalizer,
  31.         noop_foreach
  32.     );
  33.     return lean_io_result_mk_ok(lean_box(0));
  34. }
  35.  
  36. mathVec* mathVec_alloc(uint32_t length){
  37.     mathVec* out = (mathVec*) malloc(sizeof(mathVec));
  38.     if (out == NULL) {
  39.         printf("idk im just not gonna deal with this lol\n");
  40.     }
  41.  
  42.     out->data = (float*)(malloc(sizeof(float)*length));
  43.     if (out->data == NULL) {
  44.         printf("meh\n");
  45.     }
  46.  
  47.     out->length = length;
  48.     return out;
  49. }
  50.  
  51. lean_object* mathVec_boxer(mathVec* v) {
  52.     return lean_alloc_external(g_mathVec_external_class, v);
  53. }
  54.  
  55. mathVec* mathVec_unboxer(lean_object* o) {
  56.     return (mathVec*) lean_get_external_data(o);
  57. }
  58.  
  59. lean_obj_res mathVec_new(uint32_t length, float x) {
  60.     if (length < 0) {
  61.         return make_error("invalid length");
  62.     }
  63.  
  64.     mathVec* v = mathVec_alloc(length);
  65.     if (!v) {
  66.         return make_error("ERROR_INSUF_MEM");
  67.     }
  68.     for (uint32_t i = 0; i < v->length; i++) {
  69.         v->data[i] = x;
  70.     }
  71.     return lean_io_result_mk_ok(mathVec_boxer(v));
  72. }
  73.  
  74. lean_obj_res mathVec_get_val(b_lean_obj_arg _v, uint32_t i) {
  75.     mathVec* v = mathVec_unboxer(_v);
  76.     if (i > v->length){
  77.         return make_error("sad");
  78.     }
  79.     return lean_io_result_mk_ok(lean_box_float(v->data[i]));
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement