tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

c_abi_append_test.c (2642B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <time.h>
      5 #include "../include/arboricx.h"
      6 
      7 static uint8_t *read_file(const char *path, size_t *out_len) {
      8     FILE *f = fopen(path, "rb");
      9     if (!f) return NULL;
     10     fseek(f, 0, SEEK_END);
     11     *out_len = ftell(f);
     12     fseek(f, 0, SEEK_SET);
     13     uint8_t *buf = malloc(*out_len);
     14     fread(buf, 1, *out_len, f);
     15     fclose(f);
     16     return buf;
     17 }
     18 
     19 int main() {
     20     clock_t t0 = clock();
     21     arb_ctx_t *ctx = arboricx_init();
     22     clock_t t1 = clock();
     23     if (!ctx) { printf("init failed\n"); return 1; }
     24     printf("ctx=%p\n", (void*)ctx);
     25     printf("arboricx_init (kernel load) took %.3f ms\n", (double)(t1 - t0) * 1000.0 / CLOCKS_PER_SEC);
     26 
     27     size_t bundle_len;
     28     uint8_t *bundle = read_file("../../test/fixtures/append.arboricx", &bundle_len);
     29     if (!bundle) { printf("bundle not found\n"); return 1; }
     30     printf("bundle size=%zu\n", bundle_len);
     31 
     32     uint32_t bundle_tree = arb_of_bytes(ctx, bundle, bundle_len);
     33     printf("bundle_tree=%u\n", bundle_tree);
     34 
     35     uint32_t tag = arb_of_number(ctx, 1);
     36     printf("tag=%u\n", tag);
     37 
     38     uint32_t arg1 = arb_of_string(ctx, "Hello, ");
     39     uint32_t arg2 = arb_of_string(ctx, "world!");
     40     printf("arg1=%u arg2=%u\n", arg1, arg2);
     41 
     42     uint32_t list_tail = arb_fork(ctx, arg2, arb_leaf(ctx));
     43     uint32_t args_list = arb_fork(ctx, arg1, list_tail);
     44     printf("args_list=%u\n", args_list);
     45 
     46     uint32_t app0 = arb_app(ctx, arb_kernel_root(ctx), tag);
     47     uint32_t app1 = arb_app(ctx, app0, bundle_tree);
     48     uint32_t app2 = arb_app(ctx, app1, args_list);
     49     printf("app2=%u\n", app2);
     50 
     51     printf("reducing...\n");
     52     clock_t t2 = clock();
     53     uint32_t result = arb_reduce(ctx, app2, 1000000000ULL);
     54     clock_t t3 = clock();
     55     printf("arb_reduce took %.3f ms, result=%u\n", (double)(t3 - t2) * 1000.0 / CLOCKS_PER_SEC, result);
     56 
     57     int ok;
     58     uint32_t value, rest;
     59     if (!arb_unwrap_result(ctx, result, &ok, &value, &rest)) {
     60         printf("unwrap_result failed\n");
     61         return 1;
     62     }
     63     printf("ok=%d value=%u\n", ok, value);
     64 
     65     uint64_t htag;
     66     uint32_t payload;
     67     if (!arb_unwrap_host_value(ctx, value, &htag, &payload)) {
     68         printf("unwrap_host_value failed\n");
     69         return 1;
     70     }
     71     printf("htag=%lu payload=%u\n", htag, payload);
     72 
     73     uint8_t *str_ptr;
     74     size_t str_len;
     75     if (!arb_to_string(ctx, payload, &str_ptr, &str_len)) {
     76         printf("to_string failed\n");
     77         return 1;
     78     }
     79     printf("RESULT: %.*s\n", (int)str_len, str_ptr);
     80     arboricx_free_buf(ctx, str_ptr, str_len);
     81 
     82     free(bundle);
     83     arboricx_free(ctx);
     84     printf("done\n");
     85     return 0;
     86 }