Skip to content
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

Why do we need macros in the first place? #5

Open
xiaodaigh opened this issue Oct 7, 2024 · 2 comments
Open

Why do we need macros in the first place? #5

xiaodaigh opened this issue Oct 7, 2024 · 2 comments

Comments

@xiaodaigh
Copy link

Great write-up on macros but it doesn't answer a more fundamental question: why do we need macros in the first place?

It's because the syntax of the code doesn't support what you want or you want to repeat duplicated code that only changes a few things.

@NICUP14
Copy link
Owner

NICUP14 commented Oct 7, 2024

Great write-up on macros but it doesn't answer a more fundamental question: why do we need macros in the first place?

It's because the syntax of the code doesn't support what you want or you want to repeat duplicated code that only changes a few things.

That's a really good question! The macro system of MiniLang is completely optional (but hear me out). The language is complete to the point that it doesn't require macros to write programs. However, macros provide convenience and simplicity (through flexible and safe AST transformations), which cannot be expressed by the rigid structure of a MiniLang program. The ability to modify statement lists and argument lists greatly simplifies the interaction between function (simple or variadic) and abstracts code generation and transformation behind a macro. The user doesn't necesarily need to know how the macro is implemented to be able to use it effectively.

Take print as an example. It's a recursive and variadic macro which calls a _print helper function (argument type is determined via function overloading of _print), which inturn calls printf. You can definitely use other methods to output values to the console, but print is convenient and safe enough to serve ~90% of output-related tasks. Additionally, the developer isn't concerned about the inner-working of print. He passes a bunch of arguments to print and it outputs them to the console. That's all the developer needs to know to be able to use print effectively.

@NICUP14
Copy link
Owner

NICUP14 commented Oct 8, 2024

It's also important to mention that macros provide a way to force inlining, compared to languages like C++ where inline is more of a request and reuqires more attention as inline functions need to be defined within the header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants