4 ms·
Now that's interesting. The examples are a bit cherry-picked, but there are some interesting results. Seeing C++ int allocation[n]; memset(allocation,
by Animats 6y ago
Now that's interesting. The examples are a bit cherry-picked, but there are some interesting results.
Seeing C++
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for(int i = 0; i < n; i ++)
translated into Python:
allocation = [-1] * n
for i in range(n):
is impressive. The translator picked up on two idioms of C++ and translated them to more concise forms in Python. Most "transpilers" just compile into the target language as if compiling to something like machine code, generating worst case wordy output. There's a C to Rust translator which translated C pointer indexing into Rust unsafe pointer arithmetic, which is not a gain in safety.
It's also claimed that this system is good at guessing types from untyped languages.
So a good way to use this technology might be to have something that looks at source code and tries to translate all the function signatures and type definitions. This includes looking at function calls and bodies to help guess ("infer" is a stretch for this approach) the types of ambiguous variables.
Example is
int foo(char* s);
Is "s" a single character or a pointer to an array? You can't tell without context.
This system might be able to do that. Or
int getdata(int *buf, size_t bufl);
A system like this should be able to recognize the intent there.
Then try to translate the executable code with that information available. Bad guesses about types
will usually result in translation failures or code that compiles with type errors, so someone will notice.
So this is promising for modernizing code.
I want to see one able to turn C pointer arithmetic into slice syntax. Now that would be a big step forward.
- userbinator 6y agoThe translator picked up on two idioms of C++ and translated them to more concise forms in Python. It also either knew about the machine integer representation, or else made a combination of two mistakes that turned out to cancel each other out. This int allocation[n]; memset(allocation, 42, sizeof(allocation)); for(int i = 0; i < n; i ++) would NOT be equivalent to this: allocation = [42] * n for i in range(n):
- Animats 6y agoPython 3.6.9 (default, Apr 18 2020, 01:56:04) [GCC 8.4.0] on linux >>> n = 10 >>> allocation = [42] * n >>> allocation [42, 42, 42, 42, 42, 42, 42, 42, 42, 42] That's what "*" does on lists in Python. The idea is that "abc" + def" is "abcdef", and then they overgeneralized.
- lunixbochs 6y agomemset() is bytewise, the only reason it works with -1 is all the bits are set in every bit width. 0xff repeated still ends up being -1 at int size (0xffffffff). But the byte 0x2a repeated will be something more like 0x2a2a2a2a, which is not equivalent to 0x2a.
- Hello71 6y ago$ cat test.c #include <stdio.h> #include <string.h> int main() { int n = 10; int allocation[n]; memset(allocation, 42, sizeof(allocation)); for(int i = 0; i < n; i ++) printf("%d\n", allocation[i]); } $ gcc test.c $ ./a.out 707406378 707406378 707406378 707406378 707406378 707406378 707406378 707406378 707406378 707406378
- raxxorrax 6y agoIt would be interesting to test this and if they really don't rely on rules, which I would suspect for memset as well as sizeof.