I'm writing compiler from kind of functional language to JS. Compiler would run in browser. I need to implement pattern matching mechanics in JS, because original language have one. I've found Sparkler and Z. Sparkler can't be executed in browser as far as I know and Z doesn't have all possibilities I need.
So my language have semantics like this:
count x [] <- 0
count x [ x : xs ] <- 1 + count x xs
count x [ y : xs ] <- count x xs
This is what happens in this snippet:
First line is definition of a function, which takes two parameters: some variable x and empty list, and returns zero.
Second line is definition of a function, which also takes two parameters: some variable x and list, which starts with x, and returns 1 + count(x, xs)
Fot this example I want to generate code like this:
const count = (x, list) => {
match(x, list) => (
(x, []) => {...}
(x, [ x : xs ]) => {...}
(x, [ y : xs ]) => {...}
)
}
How properly unfold this kind of pattern matching into ifs and ors?
[x : xs]a linked list (that could be represented as an ADT) or an array (that'd presumably be represented as a JavaScript array)? If you support ADTs, do you already know how you'll represent them?switches and list/array patterns toifs that check the size of the list and then assign the elements.