-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various "C+" improvements #5
Comments
I didn't try it, but https://github.com/red0124/sgc looks straightforward, and could be not-too-verbose with some helper syntax sugar. |
Some random scribblings on that idea (user syntax at bottom) #pragma luv
// #includes all of SGC.
// reserves: `vector, `str, `list, `map, `set, `hashmap, `hashset, ...
//
// at parse time `vector(int) v:
// 1. inserts SGC_INIT_HEADERS(VECTOR, int, _luv_vector__int_) at top level
// 2. adds SGC_INIT_DEFINITIONS(VECTOR, int, _luv_vector__int_) in a temporary ltcg.c
// 3. declares _luv_vector__int_ v
// 4. calls v..init();
//
// where .. syntax is also specially handled and does:
//
// x..something(a, b, c) <==> <static_type_of_x>_something(&x, a, b, c)
//
// e.g.
//
// v..push_back(1) ==> _luv_vector_int__push_back(&v, 1)
//
// .. is chosen because it's not too ugly, and works with clang-format.
//
// :: also mostly works, makes me think of static calls (sort of what it is, so maybe good?)
// doesn't format quite as reliably as ..
// :, @, #, ! don't work for format
// $ doesn't work because it's sorta an identifier character in C
// ^ and | maybe plausible but always get spaces which looks a bit weird
//
// Single . could maybe be made to work since we have to know the static type of the lhs anyway
// - ambiguous for member access, but prettier
// - possibly more confusing what's happening?
// - ? if rhs of . exists on structure, then it's just a field access, otherwise member invoke
//
// What about pointers? declaration seems fine, but then more magic required in
// ., .., -> etc.
#if 0
SGC_INIT(VECTOR, int, vec)
int main(void) {
vec v;
vec_init(&v);
vec_push_back(&v, 1);
vec_push_back(&v, 5);
*vec_at(&v, 0) = 2;
for_each(i IN v AS vec) { printf("%d ", *i); }
vec_free(&v);
return 0;
}
#endif // Would be written as vvv
int main() {
`vector(int) v;
v.push_back(1);
v.push_back(5);
v.at(0) = 2;
foreach (i, v) {
printf("%d ", *i);
}
v.free();
} |
No need for #pragma, or wacky backtick prefixes. Instead, just use I think the operator overloading for |
apparently reserving
One of |
I started by implementing an Then I thought the rest might just be a matter of having the preprocessor map And that was almost enough for it to work! One problem that comes up is where to insert the header when it's magically instantiated. Since the preprocessor is doing the work, the only two possible locations at the head and tail of the current file (because we have no lexical context to know where "global scope" is). The tail seemed wrong because we need (e.g.) the struct definition early. So the head seemed like it'd work, but the problem is that by the time we see I could force include some headers (but it'd probably have to be always, not only when containers are in use, which is no good). Perhaps rewrite the containers to need to include any files? A related problem that I haven't thought too much about is what to do when the |
Containers are actually working (?) in a branch now. This code 3c3ad8e doesn't feel too awful, though the implementation is maybe a bit unpleasant (and of course ignoring the lack of wisdom in extending C at the syntax level)! The extensions are:
|
(I think I'm probably going to remove this as it's too hacky even for me, and put this type of functionality into a more extensive alternate "front-end".) |
Some possibilities:
Obviously first would need way to opt in per file/per section, and a way of cleanly integrating/generating down to normal C.
The text was updated successfully, but these errors were encountered: