Skip to content

Instantly share code, notes, and snippets.

@CrowdHailer
Created May 6, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrowdHailer/2ff967e2003268455d130b3702e0513a to your computer and use it in GitHub Desktop.
Save CrowdHailer/2ff967e2003268455d130b3702e0513a to your computer and use it in GitHub Desktop.
Trying to make a type safe parser
import gleam/should
pub type Parser(r) {
Pop(Parser(fn(String) -> r))
End(r)
}
fn apply(parser: Parser(fn(String) -> r), value: String) -> Parser(r) {
// fn apply(parser) {
case parser {
End(func) -> End(func(value))
// Pop(inner) -> apply(inner)
Pop(End(func)) -> Pop(End(func(value)))
Pop(Pop(End(func))) -> Pop(Pop(End(func(value))))
}
}
fn decode(parser: Parser(r), input: List(String)) -> Result(r, Nil) {
case parser {
Pop(inner) -> {
// For now just assume there are enough entries in the list
case input {
[value, ..rest] ->
decode(apply(inner, value), rest)
[] ->
Error(Nil)
}
}
End(value) -> Ok(value)
}
}
pub fn run_test() {
let parser = Pop(Pop(Pop(End(fn(a) { fn(b) { fn(c) { tuple(a, b)} }}))))
parser == End(tuple("", ""))
let tuple("a", "b") = decode(parser, ["a", "b", "c"])
should.equal(3,5)
}
@CrowdHailer
Copy link
Author

This causes a panic in the compiler

Compiling experiments_test
[src/typ.rs:2745] &other = App {
    public: true,
    module: [],
    name: "Result",
    args: [
        Tuple {
            elems: [
                App {
                    public: true,
                    module: [],
                    name: "String",
                    args: [],
                },
                App {
                    public: true,
                    module: [],
                    name: "String",
                    args: [],
                },
            ],
        },
        App {
            public: true,
            module: [],
            name: "Int",
            args: [],
        },
    ],
}
thread 'main' panicked at 'not implemented', src/typ.rs:2746:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment