tekknolagi / scrapscript

A functional, content-addressable programming language.
https://scrapscript.fly.dev/repl
Other
376 stars 10 forks source link

Add guard expressions to pattern matching #98

Open tekknolagi opened 10 months ago

tekknolagi commented 10 months ago

Adds guards to pattern matching:

id 2
. id =
  | x guard x==1 -> "one"
  | x -> "idk"

Where guards run in a merged version of the match case env and the env created by the pattern matching.

tekknolagi commented 9 months ago

TODO: add some EvalTests

surprisetalk commented 9 months ago

Would the question mark operator work here?

id 2
. id =
  | x ? x==1 -> "one"
  | x -> "idk"
tekknolagi commented 9 months ago

Yes, although I think it is already being used for what I originally understood as an assert. We can repurpose as guard (which won't raise/crash). Or we can rename to if.

I'm a liiiiittle wary of having a bunch of symbols

surprisetalk commented 9 months ago

Yeah, ? is an assert, that's why I think it fits kind of naturally there.

Regarding symbols, one of my design goals of scrapscript is to actually have no alphabetic keywords in the whole language! I don't know why this was a goal of mine, but I think it's a constraint that's encouraged "small" thinking.

tekknolagi commented 9 months ago

I think if we drop its meaning as assert and instead use it to mean guard (and fall-through on guard failure, instead of crash), sure, why not

tekknolagi commented 9 months ago

Then it's possible to write assert = | # true -> () which will raise/fail due to lack of matches

surprisetalk commented 9 months ago

Love it! Great idea