Note that most of what makes python great isn't the language, it is the library. I believe that large parts of the python library are also written in C (for speed), and thus you won't be able to use my 500 line python compiler for anything useful because you won't have any useful libraries.
In my first stage Onramp linker [1], converting linear search to an open hash table adds a grand total of 24 bytecode instructions, including the FNV-1a hash function. There's no reason to ever linear search a symbol table.
[1]: https://github.com/ludocode/onramp/blob/develop/core/ld/0-gl...
// Hashtable definition
#include <string.h>
#define N 1024
int* map_ptr(const char **keys, int *values, const char *key){
size_t h = 0, c = 0, i;
for (const char *c = key; *c; c++) h = h * 33 + *(unsigned char*)c;
for (i = h % N; c < N && keys[i] && 0 != strcmp(keys[i], key); i = (i + 1) % N, c++);
if (!keys[i]) keys[i] = key;
return 0 == strcmp(keys[i], key) ? &values[i] : NULL;
}
// Example usage
const char *keys[N];
int values[N];
#include <stdio.h>
int main(){
// Set some values
*map_ptr(keys, values, "one") = 1;
*map_ptr(keys, values, "two") = 2;
*map_ptr(keys, values, "three") = 3;
// Retrieve values
printf("one: %i\n", *map_ptr(keys, values, "one"));
printf("two: %i\n", *map_ptr(keys, values, "two"));
printf("three: %i\n", *map_ptr(keys, values, "three"));
return 0;
}This:
entry = list_search(key, list);
becomes: entry = list_search(key, lists[hash(key) % (sizeof(lists)/sizeof(lists[0]))]);
This: list_add(key, entry, list);
becomes: list_add(key, entry, lists[hash(key) % (sizeof(lists)/sizeof(lists[0]))]);
etc.A simple hash(), not high quality but good enough for non-adversarial inputs, can be a one-liner.
A good dictionary does more than this of course, especially dynamic sizing, but this simple change can radically speed up simple linked list code when the lists are long, you don't have a hash table implemention you can use, and you want to keep the code change very small.
The same principle can be used with other things than lists too.
https://github.com/python/cpython/blob/main/Python/ceval.c
It's 3619 lines. It's explained in this 515 line file:
https://github.com/python/cpython/blob/main/InternalDocs/
For comparison, there is a pure Python bytecode interpreter, its VM is here:
https://github.com/nedbat/byterun/blob/master/byterun/pyvm2....
It's 1043 lines.
That said, I guess technically you could make something that compiles python to an executable? This is hacker news after all
Some languages are more or less easy to compile efficiently and without embedding a JIT compiler, but any language can be compiled.
For Python in particular, there are already compilers.
If you want a nightmarish language to compile, look at Ruby. There are compilers even for Ruby.