Created
May 6, 2020 14:02
-
-
Save CrowdHailer/2ff967e2003268455d130b3702e0513a to your computer and use it in GitHub Desktop.
Trying to make a type safe parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This causes a panic in the compiler