Concatenative-dsl as rust macros?

#1

EDIT: changed “Forth” to “ConcatenativeLangauge”

  1. I am a big fan of Rust’s type system (struct, enum traits, borrow checker)

  2. There are times I also like the succintness of ConcatenativeLanguage’s stack based system.

  3. Has anyone managed to implement a ConcatenativeLanguage-dsl as Rust macros?/////

Note: I am not asking for ConcatenativeLanguage interpreter in Rust or ConcatenativeLanguage VM in Rust. I want the “ConcatenativeLanguage” to be operating directly on Rust primitives and to go through Rust’s type checker.

I an mentally thinking that we can abuse Rust’s tuple support as a “typed stack” (since most ConcatenativeLanguage words tend to not use too many stack elements anyway).

Ideas / thoughts? :slight_smile:

Please no comments of “why the $&@( would you want to do that” – those are not particularly useful. What I am interested in is – if we tried to do this, what can we achieve by abusing Rust’s macros and what interesting DSLs can we get?

0 Likes

#2

You can get a compile time heterogeneous type safe stack like so,
(lots of buzz words)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=77448e0ecd9d7c2b65f2335348df0c13

If you want more control or privacy than what tuples offer, you can swap over to using your own types instead of tuples.

2 Likes

#3

Where can I learn more about this? Is this code from something larger, or did you just write it from scratch?

It reminds me of the “Add” from typenum: https://docs.rs/typenum/1.10.0/typenum/int/struct.PInt.html#impl-Add<PInt<Ur>>

The way it defines Push/Pop as traits and then allows typesafe heterogeneous stacks is quite clever.

0 Likes

#4

From scratch, I played around with stuff like this while trying to understand the madness that is type-num

1 Like