3 ms·
Yeah, you can write a compiler for a better language. This is disingenuous. Show me Pi types in C.
by jcora 7y ago
Yeah, you can write a compiler for a better language.
This is disingenuous. Show me Pi types in C.
- codr7 7y agoWord. Interpreter, but close enough :) https://github.com/codr7/g-fu/tree/master/v1 https://github.com/codr7/g-fu/tree/master/v1
- jcora 7y agoThis just isn't what people mean by "writing an abstraction in the language".
- codr7 7y agoI know quite a few who would consider that an option. Embedding a higher level language that's controlled from a more primitive host language allows solving many otherwise tricky problems without the struggle.
- MiroF 7y agoReimplemented generics are all over any C codebase you might lion at.. I mean just look at the Linux kernel
- Crinus 7y ago> Pi types in C. I took a quick look at what these are. Couldn't they implemented with macros like (this builds a type "Utf8String" which "depends" on the value of the "ENCODING" parameter to figure out what set of functions to call for the guts of the functions that handle the type, e.g. RawStringUtf8Length): enum Encoding { Ascii, Utf8, Utf16 }; #define TYPENAME Utf8String #define ENCODING Utf8 #define CHARTYPE char #include "stringdef.ph" Then in stringdef.ph (ph=parametric header, just came up with it): typedef struct { size_t len; CHARTYPE* text; } TYPENAME; #define MAKEFUNC(name) TYPENAME##name #define RAWSTRINGFUNC(name) RawString##ENCODING void MAKEFUNC(Create)(TYPENAME* str); void MAKEFUNC(Destroy)(TYPENAME* str); void MAKEFUNC(CreateFrom)(TYPENAME* str, const CHARTYPE* text); /* this goes in a single C file, single header library style */ #ifdef IMPLEMENTATION void MAKEFUNC(Create)(TYPENAME* str) { ... } void MAKEFUNC(Destroy)(TYPENAME* str) { ... } void MAKEFUNC(CreateFrom)(TYPENAME* str, const CHARTYPE* text) { str->len = RAWSTRINGFUNC(Length)(text); str->text = RAWSTRINGFUNC(Duplicate)(text); } #endif #undef MAKEFUNC /* cleanup input parameters so includers wont need to do it */ #undef CHARTYPE #undef ENCODING #undef TYPENAME Then you can use the above simply like Utf8String u8s; Utf8StringCreateFrom(&u8s, "foo..."); It isn't exactly pretty, but seems to do the work. The ENCODING parameter is only conceptually an Encoding enum, but this can be enforced with some dummy (unused) code that does use of the ENCODING value as Encoding (and if extra strength is needed, as enums are weakly typed, instead of native enums, you could define macros that create stronger "enums" out of structs).