orangeduck / BuildYourOwnLisp

Learn C and build your own programming language in under 1000 lines of code!
http://www.buildyourownlisp.com/
Other
2.84k stars 392 forks source link

What is the best way to include stdlib into binary? #173

Closed KazakovDenis closed 1 year ago

KazakovDenis commented 1 year ago

Hello, Dan! Your book is great, thank you so much!

I've just finished Chapter 15 "Standard Library" and have a question. There is a bonus mark: "Incorporate your standard library directly into the language. Make it load at start-up." I implemented, but I don't like the way I did it.

The first try was to implement a function like load_std(lenv* e, char* string), where to pass a string representation of stdlib item. But with many items the code looks ugly.

The next idea was to create stdlib directory with *.lispy modules and load each one via load_stdlib(lenv* e, char* content). But they are loaded at runtime, not at compile-time, it's useless for users.

I found the way to include stdlib using the macro:

static char *stdlib = 
#include "../stdlib/stdlib.lispy"
;

But I don't like this method because you need to wrap your lispy module with R"( <lispy code> )".

Is there a way to load lispy stdlib at compile-time and keep *.lispy modules clean?

orangeduck commented 1 year ago

Hi Denis,

Great question. If there is a better solution I'm not sure I know what it is! I quite like your #include based solution to be honest :)

Dan

KazakovDenis commented 1 year ago

Thank you for the quick reply, got it)