r/stackoverflow • • Dec 02 '24

C Coding challenge: Convert JavaScript to C - without "artificial intelligence"

Convert JavaScript to C as demonstrated here https://www.codeconvert.ai/javascript-to-c-converter - without using "artificial intelligence".

Sample input

permutations.js

function array_nth_permutation(a = [0,1,2,3,4], n = 5) {
  let lex = n;
  let b = []; // copy of the set a.slice()
  for (let x = 0; x < a.length; x++) {
    b[x] = a[x];
  }
  let len = a.length; // length of the set
  const res = []; // return value, undefined
  let i = 1;
  let f = 1;

  // compute f = factorial(len)
  for (; i <= len; i++) {
    f *= i;
  }

  // if the permutation number is within range
  if (n >= 0 && n < f) {
    // start with the empty set, loop for len elements
    //let result_len = 0;
    for (; len > 0; len--) {     
      // determine the next element:
      // there are f/len subsets for each possible element,
      f /= len;
      // a simple division gives the leading element index
      i = (n - n % f) / f; // Math.floor(n / f);
      res.push(b.splice(i, 1)[0]);
      // reduce n for the remaining subset:
      // compute the remainder of the above division
      n %= f;
      // extract the i-th element from b and push it at the end of res
    }
    console.log(`[${lex}] ${JSON.stringify(res)}`);
  } else {
    console.log(`${n} >= 0 && ${n} < ${f}: ${n >= 0 && n < f}`);
  }
  // return the permutated set or undefined if n is out of range
  return 0;
}

Sample output

permutations.c

#include <stdio.h>
#include <stdlib.h>

void array_nth_permutation(int a[], int len, int n) {
    int lex = n;
    int *b = (int *)malloc(len * sizeof(int)); // copy of the set
    for (int x = 0; x < len; x++) {
        b[x] = a[x];
    }
    
    int res[len]; // return value
    int i = 1;
    int f = 1;

    // compute f = factorial(len)
    for (; i <= len; i++) {
        f *= i;
    }

    // if the permutation number is within range
    if (n >= 0 && n < f) {
        // start with the empty set, loop for len elements
        for (; len > 0; len--) {
            // determine the next element:
            f /= len;
            // a simple division gives the leading element index
            i = (n - n % f) / f;
            res[len - 1] = b[i]; // store the result
            // shift elements left
            for (int j = i; j < len; j++) {
                b[j] = b[j + 1];
            }
            // reduce n for the remaining subset:
            n %= f;
        }
        printf("[%d] [", lex);
        for (int j = 0; j < sizeof(res)/sizeof(res[0]); j++) {
            printf("%d", res[j]);
            if (j < sizeof(res)/sizeof(res[0]) - 1) {
                printf(", ");
            }
        }
        printf("]\n");
    } else {
        printf("%d >= 0 && %d < %d: %d\n", n, n, f, n >= 0 && n < f);
    }
    
    free(b);
}

int main() {
    int a[] = {0, 1, 2, 3, 4};
    int n = 5;
    array_nth_permutation(a, sizeof(a) / sizeof(a[0]), n);
    return 0;
}

0 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/guest271314 Dec 03 '24

Tried all of these (all on GitHub). So far. Don't work as expected.

  • jsxx
  • ts2c
  • TypeScriptCxx

QuickJS's qjsc works very well, at the cost of embedding the QuickJS engine into the executable, or WASM application.

What's close, from my perspective, is Facebook's Static Hermes -emit-c option. I'm pretty sure the JavaScript interpreter is not embedded in the resulting executable when compiled with gcc. That's not "idiomatic C", if I use that term loosely. Though compiles to a 39.8 KB executable using gcc, whereas the qjsc code compiled with gcc is 1.2 MB, accounting for QuickJS engine in there, too.


#include "hermes/VM/static_h.h"

#include <stdlib.h>


static uint32_t unit_index;
static inline SHSymbolID* get_symbols(SHUnit *);
static inline SHPropertyCacheEntry* get_prop_cache(SHUnit *);
static const SHSrcLoc s_source_locations[];
static SHNativeFuncInfo s_function_info_table[];
static SHLegacyValue _0_global(SHRuntime *shr);
static SHLegacyValue _1_array_nth_permutation(SHRuntime *shr);
// permutations.js:3:1
static SHLegacyValue _0_global(SHRuntime *shr) {
#line 3 "permutations.js"
  _SH_MODEL();
#line 3 "permutations.js"
  struct {
#line 3 "permutations.js"
    SHLocals head;
#line 3 "permutations.js"
    SHLegacyValue t0;
# ...
# ... CODE
# ...
static SHNativeFuncInfo s_function_info_table[] = {
  { .name_index = 14, .arg_count = 0, .prohibit_invoke = 2, .kind = 0 },
  { .name_index = 4, .arg_count = 2, .prohibit_invoke = 2, .kind = 0 },
};
static const char s_ascii_pool[] = {
  '\0',
  'p', 'e', 'r', 'm', 'u', 't', 'a', 't', 'i', 'o', 'n', 's', '.', 'j', 's', '\0',
  'i', 'n', 'p', 'u', 't', '\0',
  'l', 'e', 'x', '\0',
  'a', 'r', 'r', 'a', 'y', '_', 'n', 't', 'h', '_', 'p', 'e', 'r', 'm', 'u', 't', 'a', 't', 'i', 'o', 'n', '\0',
  'c', 'o', 'n', 's', 'o', 'l', 'e', '\0',
  'l', 'o', 'g', '\0',
  '[', '\0',
  'J', 'S', 'O', 'N', '\0',
  's', 't', 'r', 'i', 'n', 'g', 'i', 'f', 'y', '\0',
  ']', '\0',
  'l', 'e', 'n', 'g', 't', 'h', '\0',
  'p', 'u', 's', 'h', '\0',
  's', 'p', 'l', 'i', 'c', 'e', '\0',
  'g', 'l', 'o', 'b', 'a', 'l', '\0',
};
static const char16_t s_u16_pool[] = {
};
static const uint32_t s_strings[] = {0,0,0,1,15,2061427151,17,5,2653113407,23,3,544182755,27,21,3239187722,49,7,1654270973,57,3,473294856,61,1,92650,63,4,2535253447,68,9,2366981053,78,1,94604,80,6,363462486,87,4,1059174534,92,6,554150397,99,6,615793799,};
#define CREATE_THIS_UNIT sh_export_this_unit
struct UnitData {
  SHUnit unit;
  SHSymbolID symbol_data[15];
  SHPropertyCacheEntry prop_cache_data[16];
;  SHCompressedPointer object_literal_class_cache[0];
};
SHUnit *CREATE_THIS_UNIT(SHRuntime *shr) {
  struct UnitData *unit_data = calloc(sizeof(struct UnitData), 1);
  *unit_data = (struct UnitData){.unit = {.index = &unit_index,.num_symbols =15, .num_prop_cache_entries = 16, .ascii_pool = s_ascii_pool, .u16_pool = s_u16_pool,.strings = s_strings, .symbols = unit_data->symbol_data,.prop_cache = unit_data->prop_cache_data,.obj_key_buffer = s_obj_key_buffer, .obj_key_buffer_size = 0, .literal_val_buffer = s_literal_val_buffer, .literal_val_buffer_size = 21, .obj_shape_table = s_obj_shape_table, .obj_shape_table_count = 0, .object_literal_class_cache = unit_data->object_literal_class_cache, .source_locations = s_source_locations, .source_locations_size = 31, .unit_main = _0_global, .unit_main_info = &s_function_info_table[0], .unit_name = "sh_compiled" }};
  return (SHUnit *)unit_data;
}

SHSymbolID *get_symbols(SHUnit *unit) {
  return ((struct UnitData *)unit)->symbol_data;
}

SHPropertyCacheEntry *get_prop_cache(SHUnit *unit) {
  return ((struct UnitData *)unit)->prop_cache_data;
}

void init_console_bindings(SHRuntime *shr);

int main(int argc, char **argv) {
  SHRuntime *shr = _sh_init(argc, argv);
  init_console_bindings(shr);
  bool success = _sh_initialize_units(shr, 1, CREATE_THIS_UNIT);
  _sh_done(shr);
  return success ? 0 : 1;
}