Bubbler-4 / TryInBrowser

Online interpreter that works even if you go offline
https://try-in-browser.netlify.app/
MIT License
4 stars 0 forks source link

Make language addition easier #6

Closed Bubbler-4 closed 3 years ago

Bubbler-4 commented 3 years ago

Currently, adding a language requires editing four places in lang/mod.rs and adds five lines (mod declaration and adding at four places). It would be better (and less error-prone if we get more langs) if a macro is used to allow editing in only one place.

ysthakur commented 3 years ago

There used to be a Language trait, right? What's wrong with making instances of Language and putting them in a Vec just once to access everywhere?

Bubbler-4 commented 3 years ago

It's slightly harder than that because of interpret<T>. Some quick search gives that I'll need something like anymap to collect them (which means I won't need a macro after all).

ysthakur commented 3 years ago

Could using dynamic dispatch (something like static LANGS: Vec<dyn Language> = vec![Deafish{}, S10K{}, ...];) work?

Edit: Never mind, that doesn't work, but would using &dyn LangWriter work instead to get rid of T? Language could just be a struct that you make instances of for each language instead of a trait to be implemented.

Bubbler-4 commented 3 years ago

Implemented in https://github.com/Bubbler-4/TryInBrowser/commit/4b6d61f34faf8fb48950b70f0c63c5a2fb726682