Previous Episodes: • Music Visualizer References: - Music by / @nu11_ft - X macro: en.wikipedia.org/wiki/X_macro
Пікірлер: 120
@julkiewicz9 ай бұрын
In my compiler class at the uni we were supposed to implement a simple cut-down Java-like curly braces compiler. We were allowed to extend it with extra features to pass the class with a higher grade. Somehow I got it into my head that I'll extend the language with C-style pointer syntax, including function pointers etc. I ended up spending 90% of my time trying to get the front-end to work. It's just about barely implementable with the classic parser-generator tools like antlr etc. You literally can be half way through parsing a line in C and still be unsure if: A) it's going to be a function declaration, B) pointer type declaration, C) function pointer declaration, D) variable declaration or something else. It's insane how a single asterisk can just completely change the meaning of a line. By the time I got the front-end to finally work I had time to only implement some basic backend that produced unoptimized bytecode and ran it in a simple bytecode machine. The whole thing was like this horse drawing meme
@dokchampa93249 ай бұрын
You can't just say all that and then not share your grade with us
@julkiewicz9 ай бұрын
@@dokchampa9324 Well I got the max grade possible but I submitted it after the deadline, so it was an automatic -1 deduction. Sad face :) I guess a way of my uni trying to teach me that "done is better than perfect (?)"
@mastershooter648 ай бұрын
@@julkiewicz Share the compiler source code!
@marcs94517 ай бұрын
I much rather hand write my parser than use ANTLR or Bison
@KeplerEmeritus6 ай бұрын
Compiler class…? Yeah I should have gone to school. Compilers are the types of things I dream of building. Instead, I make shitty web api which isn’t really programming at all…
@cramble9 ай бұрын
TSoding being amazing as always.
@poijmc6069 ай бұрын
Tsoding casually writing C language black magic curses
@Vank4o9 ай бұрын
Even after 50 years C is just amazing!
@DVRC9 ай бұрын
Speaking of compile time generated code, I've stumbled upon the source code of a weird preprocessor for C called Ace, which was written by James Gosling for the X/NeWS merge. There is a paper where he explains how it works (basically it operates on the syntax tree instead of doing näive macro substitutions), and how he could generate routines for different framebuffer depths/models using the same code.
@salman85629 ай бұрын
Can't find the paper, whats the title?
@DVRC9 ай бұрын
@@salman8562 "Ace: a syntax driven C preprocessor", James Gosling, 1989. Likely YT removed the comment where I linked it
@julians.25972 ай бұрын
Pretty sure Zig kinda does that. Having a preprocessor act on source code is just a recipe for desaster
@yaksher9 ай бұрын
I think in the header file, what you could do is add another layer. Like, have, #define LIST_OF_PLUGS \ BASE_PLUG(plug_hello, void) \ BASE_PLUG(plug_init, Plug *plug, const char *file_path) \ BASE_PLUG(plug_pre_reload, Plug *plug) \ BASE_PLUG(plug_post_reload, Plug *plug) \ BASE_PLUG(plug_update, Plug *plug) \ which includes the type signature information. Then, you would do // this generates the type defs #define BASE_PLUG(name, args...) typedef void (*name##_t)(args); LIST_OF_PLUGS #undef BASE_PLUG // and then, once you're done using the form which has the type signature, you can just do this to discard it and use the plug name #define BASE_PLUG(name, ...) PLUG(name) And better yet, if you ever need the type signature information again, you just redefine BASE_PLUG and use that, instead of PLUG.
@ChristopherDrum9 ай бұрын
I came into comments to note this technique, so I'll just append to yours. The core idea is that variadic arguments are valid in X macros. We can take this even a little further by including function return types in the initial #define. So each element of LIST_OF_PLUGS is defined as BASE_PLUG(, , , , ...) You can then do #define BASE_PLUG(name, return_type, ...)\ typedef return_type (* name##_t)(__VA_ARGS__); LIST_OF_PLUGS #undef BASE_PLUG Then you don't even have to update BASE_PLUG; *everything* is contained within the LIST_OF_PLUGS. This has been working great for me.
@michalbotor9 ай бұрын
(4:26) for the end user it needs to be a double click and it works. ;) you tell them to compile from source and link they will have a heart attack.
@portersky9 ай бұрын
Didn't know the macro technique had a name, I used it when I was defining enums in my C++ program to generate a list of names as strings from the enum values.
@phantomwizard58929 ай бұрын
The amount of knowldege ive gain from you these past couple of months is just insane Thanks a lot sir!
@diegorocha21869 ай бұрын
Pretty good!!! As usual! I've been following you for the last 3 years or more, and I keep learning from you in every session, thanks a lot Mr Zozin!
@alecs20569 ай бұрын
Why not make PLUG with 2 parameters and store in the second one the arguments of the function then you can generate automatically also the type definitions from the list
@TsodingDaily9 ай бұрын
Oh, that's a cool idea! Thanks!
@Mikayex9 ай бұрын
Also you can make a macro #define COMMA , to allow using commas in the second argument
@alecs20569 ай бұрын
@@Mikayex i think that using variadic argoments in the macro is cleaner
@SteveRowe4 ай бұрын
I've been programming in C since 1983. I've never seen the typedef (fn)(void) as a way to do function declaration. Not sure I'd ever do that, as I try to avoid obscure things. But I loved learning about it, so thank you!
@pedrorondon26179 ай бұрын
The day I understood how Apache http server modules work I was amazed that programming in C could do that, I didn't understood how It was implemented but I knew that could be somthing with shared libraries but didn't know how it could be implemented in code, thanks for the video!
@WilderPoo9 ай бұрын
Watching you keeps me motivated, thank you! 😁
@anon_y_mousse9 ай бұрын
It's kind of funny that so many people don't know about all of these old tricks and it's cool that you're bringing them to the forefront again. Something you might consider is using an X-macro to define the namespace that you use for variables and functions. #define PLUG(n) plug_##n##_t and perhaps more arguments to handle defining functions signatures versus defining the type of a variable and assigning a value to it. It looks weird, but works to have things like: PLUG(init,void,(int ac,char**av)) { ... } or PLUG(freq,int,20);
@CoderDBF9 ай бұрын
Great stuff, I saved this video to a playlist so that I can watch it again later. I just want to say, I wouldn't be opposed to seeing you do some short form content as well. You have some interesting stuff to share, but your videos take up a large part of a day to watch. I was thinking there might perhaps be a way to do TLDR; versions of your longer streams? Tsoding for those in a hurry, sort of speak. Anyway, keep up the good work.
@ruffianeo34189 ай бұрын
"If a code base gets bigger and bigger, eventually it will have a worse alternative of Common Lisp in its code." ~~~ Famous Klingon proverb Changing function implementations on the fly is another 0.5% of Common Lisp :)
@francklenormand26809 ай бұрын
The plugin should export only 1 symbol: the table of functions implemented. There are other fun stuff to do with macro like logging utility or boomen logic at compile time like checking size of data and such
@NeZversSounds9 ай бұрын
Hands down my favorite video! Learned a lot from it.
@pascualsu9 ай бұрын
Top notch programming channel for sure
@octagonal89059 ай бұрын
Lol, love the SCP-087 thumbnail
@arthurgrillo34629 ай бұрын
I learned about x-macros a week ago, now tsoding post a video about that. This is amazing but now I'm scared abou tsoding reading my mind
@screamyboy47229 ай бұрын
This man brain can simulate the universe
@TheRailroad999 ай бұрын
Wow, the "function variable" was very interesting! However to any dev: please only use such techniques when REALLY necessary and comment them well. Remember that in theory, you could build your own language with the preprocessor, its very powerful. But noone will understand your code anymore
@karmavil40349 ай бұрын
Wow I'm usually wordless at the end of your videos (and exhausted because they're veeeery long) but this one has been the most exiting video I've seen in a long long time on youtube. I've started today actually to "formally" learn C from manuals and really take a deep understanding because it is becoming an issue for me and I think not changing this will limit and devaluate any progress to become better with .cc This is very impressive and the same time the reason of my problems.. Not taking advantage of preprocessor statements is not just a matter of performance or readable code, it is necessary in some scenarios and you just game me a priceless lesson about that. And the example is so f amazing man congrats! you're a genius
@paklenizmaj9 ай бұрын
37:48 you can just compile both versions like as musialiser and musialiser-dev (or musialiser-hot) so user can just start what he want.
@MisererePart9 ай бұрын
I think you can take a look at inotify to implement auto compile after change, which would be cool. You can also add a pipe to your main to trigger reload from outside of the process, so the script which handle auto-compile would be able to reload completely the app without pressing r.
@mrmaniac99059 ай бұрын
I've been trying to catch one of your streams for half a year now. I always remember to check at the wrong times. Any chance you can post a schedule, even if it's only a day in advance of when you're going to stream?
@donkeyy83319 ай бұрын
this is the type of thing you don't see or hear about in your day to day, thank you for the knowledge.
@FireDragon912459 ай бұрын
man i watch 1 of your videos and instantly get the uncontrolable urge to code a shit ton of C
@einsjannis9 ай бұрын
Very cool thank you very much for sharing
@OM-yn8pt9 ай бұрын
Would you consider investigating the comparison of sound files from the ground up, i.e. checking the similarity between two audio signals
@skope20559 ай бұрын
Useful tech, it saved me a lot of time. I'm glad you demonstrated it here. Edit: You could also put function signature with definition in a macro where you defined your plug functions to avoid some duplication, if it makes sense.
@Hardcore_Remixer9 ай бұрын
#define def_func(func_name, // insert function here to use helper function , helper_function_name) \ new_function_here Something like this?
@rotbjorn9 ай бұрын
For each of the PLUG in ``` #define LIST_OF_PLUGS \ PLUG(plug_int_changer) \ ``` There can be more "metadata" added with each PLUG macro: ``` #define LIST_OF_PLUGS \ PLUG(plug_int_changer, "1.0.0", int*) ``` Where for example "1.0.0" is the version and int* is the type signature of the parameters You can then consume them with ``` #define PLUG(name, _unused_version, type) \ typedef void (*name##_t)(type) LIST_OF_PLUGS #undef PLUG ```
@Hardcore_Remixer9 ай бұрын
@@rotbjorn Wow. I didn't think about that. It reminds me of how C++ renames functions (so functions with the same name, but different paramerer types or number can be declared).
@JekasObps9 ай бұрын
pros are using -Wl,-rpath= linker option to specify search path for the .so in the executable itself and leave env vars untouchable
@JekasObps9 ай бұрын
great thing to typedef a function signature, neat stuff I wonder if I did noticed that behavior past life or maybe in a dream, so ridiculous... possibly that got rejected in a code review as missleading construct
@KafeinBE9 ай бұрын
Don't get me wrong, it's clever and I have seen X macros used to good effect in a few C projects. But it's really one of these huge reasons to use C++ rather than C. Anything that lets you remove a macro and use a type-safe construct instead is an immense improvement for maintainability.
@emperorpalpatine60809 ай бұрын
That's an SCP. But I prefer the SC++P
@DarioSterzi8 ай бұрын
Damn this was surprisingly fun
@yjk_ch9 ай бұрын
8:03 I saw something similar to X-macro in SerenityOS project, though sometimes it accepts X as macro argument instead of using whatever current definition of X is. It seemed like very clever idea, but I didn't know it had name!
@FredericoKlein9 ай бұрын
I admire your memory. If I did this to my code in like 1 week i would no longer remember how it worked.
@drdca82639 ай бұрын
Wow! I hadn’t seen X macros, and that seems very nice! Edit: I am also quite impressed by how quickly you made the change to make whether hot-reloading is set up or whether everything linked as one file, be determined by compiler options. I know you must get this often, but wow you are much better at programming than I am. I mean, I feel generally competent at it (I can generally get the things I want to work, working), but, I’m always both using higher-than-C level languages (usually python) and taking longer to get anything written. I suppose that’s partially to be expected because I’ve put more of my skill points into math than I’ve put into programming (and intend to continue to do so), but I also think that there’s a major difference beyond that. Edit 2: I hope I’m not being weird with excessive praise.. I’m just impressed, is all.
@jaygatward74509 ай бұрын
I’m glad you say that, I think everyone feels incompetent watching tsoding sessions😂
@OneWingedShark9 ай бұрын
The simplicity of C is a lie: it just tricks you into thinking it is simple. That is why making a standards-compliant C compiler is actually quite difficult, despite it's false-branding as "high level assembler", and why a verified C compiler is *much* harder than that.
@goawqebt69319 ай бұрын
I assume the problem is some combination of: mixed levels of abstraction, confusing syntax and historical artifacts ?
@OneWingedShark9 ай бұрын
@@goawqebt6931 - Yes to all of that, David Chisnall's paper "C is not a low level language" describes where a lot of the mismatch is; look up the term "sequence point" before reading the paper and you'll see EXACTLY what he's referring to. / The other thing that makes it only seem simple is how features interact; consider "if (user = admin)" -- this error is possible because (a) assignment returns a value, (b) C doesn't have a boolean (and thus treats "not zero" as false), and (c) the combination of 'a' & 'b' means there's no way for this to be prohibited and still be a compliant C compiler because then you're rejecting valid C.
@MichaelPohoreski9 ай бұрын
@@OneWingedSharkOne could extend C so that := is used inside an if clause to tell the compiler that “yes, I REALY do meant assignment here” but sadly C has remained stagnant instead relying on compiler warning spam. :-/
@OneWingedShark9 ай бұрын
@@MichaelPohoreski - Honestly, you'd be better off just using Ada. The FSF GNAT Ada compiler is part of GCC, and thus has that whole suite of tools available, and the actual standard is available for free from the standards committee (the ARG) on their site.
@FelipeMarkson9 ай бұрын
This feature to disable hot reloading was even more amazing of add hot reloading... God dammit C consistency
@luserdroog8 ай бұрын
Reiterating my SO post: make the convention explicit by passing in the inner macro by name. Then you give your inner handlers readable names , and don't have to undef anything.
@tbkih29 күн бұрын
You could have used X-macros a little bit more around 18:30 if you defined PLUG to take two arguments (and ignored the last one in two of its instantiations). In LIST_OF_PLUGS the second argument would be the argument list of the signature. You could also solve all this by using Common Lisp, but you're brave to hotreload in C
@SingleTheShot9 ай бұрын
Hello Mr Tsoding, this might be a good idea a little while later if you have a more developed interface, but what if you could switch between different plugins?
@ksco33099 ай бұрын
I read and wrote these macros a lot, now I finally know its name!
@alib55039 ай бұрын
As some say: dark corners of c
@business5489 ай бұрын
What font you use for terminal?
@tomaspecl10829 ай бұрын
Cool stuff!
@mithrandirek18139 ай бұрын
Xmacros seem like a long stretch for the C preprocessor, good luck with debugging, I just use autogen to solve this issue in any text format (you can mangle the list elements in GNU Guile). I remember you had custom solution in C for generating code for linalg, why not use that?
@darioabbece39489 ай бұрын
This is forbidden C
@bbq14239 ай бұрын
You could probably use varargs to put the function parameters in the macro
@lolcat699 ай бұрын
I love when TypeScript-oding does C coding :)
@anilgr11119 ай бұрын
Love from India ❣
@luserdroog8 ай бұрын
X-macros! Hurray!
@tcurdt5 ай бұрын
what are you using to create the window and draw?
@zoltankurti9 ай бұрын
This is exactly how I deal with loading vulkan functions.
@zoltankurti9 ай бұрын
Not exactly. My list of functions is in a different file and I just include that file after defining the macro and before undef.
@wrfsh4 ай бұрын
I've seen x macros a number of times in production, in super large C code bases (mil+ loc). They make finding a definition of a thing a real PITA. Every time i came across them, I would much rather prefer people to do a tiny bit of code duplication instead but keep things explicit and obvious.
@weakspirit_7 ай бұрын
macros in C! what we would we do without em
@rolandinnamorato19537 ай бұрын
Legcoded X Macros when?
@Asia_Bangladesh9 ай бұрын
Why you use Emac not VIM? Make a compare video about Vim Emac And VS code
@doce36099 ай бұрын
wow this is very cool
@OsamaHasan0009 ай бұрын
please can you make some beginner level project from scratch????????????????/
@alurma9 ай бұрын
Cool. Thanks
@IrtaMan2289 ай бұрын
Those are my favorite parts of C!
@vee.m3 ай бұрын
My mind is blown
@truectl9 ай бұрын
Right.
@oj00249 ай бұрын
So how about you try to solve the first 3 days of advent of code at compile time with only the preprocessor.
@xravenx24fe9 ай бұрын
You can do that with Zig fairly easily if the problem doesn't have a lot of branching :)
@thespiralofpowah9 ай бұрын
In the start, the bottom of the screen is SUS
@FunkschyIsWatchingYou9 ай бұрын
Will the government come after me now for knowing stuff I'm not supposed to? monkaS
@pajeetsingh9 ай бұрын
Is the background transparent?
@BennoKushnir9 ай бұрын
29:30 it's not "weirdness": int i = (int) double_var; is the usual method to cast a variable
@desertfish749 ай бұрын
Recreazional zozing zession.
@BboyKeny9 ай бұрын
29:00 That seems to me like it's practically function hoisting. This means the Uncle Bob's Clean Code is possible in C 😲
@goawqebt69319 ай бұрын
It's just a fun definition
@adr4209 ай бұрын
Hello 👋 Everyone...
@glados_creator9 ай бұрын
you think you can use it with nobuild so like with interpreted scripts language except you fully compile everytime ; gentoo style
@glados_creator9 ай бұрын
gotta to love C
@aaronspeedy77809 ай бұрын
Why couldn't you just have a simple function to restart the application at the current timestep?
@Njinx_9 ай бұрын
Not as fun as messing with shared libraries
@TsodingDaily9 ай бұрын
Because this limits the potential state of the whole application to a single timestamp, which is probably not the limitation you want to have if you plan to create anything interesting.
@aaronspeedy77809 ай бұрын
@@TsodingDaily If you want more state, couldn't you just add more arguments to the function that restarts the program? From what I see, the only limitation is that you need the entire state of your program to be viewable by a single function, which I guess could be hard for really large programs.
@aaronspeedy77809 ай бұрын
Wait, never mind. Isn't it actually literally the same except that you're restarting the program one function at time instead of just restarting the program all at once, or am I missing something?
@user-ge2vc3rl1n9 ай бұрын
why ifndef instead of pragma once ?
@garrysingh44849 ай бұрын
👋 Need a Video to demonstrate Buffer overflow ??? How to create a buffer overflow and possible fix's !!!
@energy-tunes9 ай бұрын
The only non grifter eceleb that can actually teach you a thing or two
@UnknownSENKu9 ай бұрын
Thanks A lot. For me Hard to follow CASEY code but I follow your code . Thanks.....
@SlinkyD9 ай бұрын
13:21 alternative method: gcc -E foo.c Do yo thang