Created
December 4, 2019 18:55
-
-
Save cristianoc/418323d6d7151616a6db603b20eac39f to your computer and use it in GitHub Desktop.
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
and parseInt = (p: Parser.t) => { | |
let res = | |
switch (p.token) { | |
| Int(n) => n | |
| _ => | |
Parser.err(p, "integer expected"); | |
(-1); | |
}; | |
Parser.next(p); | |
res; | |
} | |
// Annotating parseList is an error: can't be analyzed directly as it takes a parameter f | |
[@progress] | |
and parseList: 'a. (Parser.t, ~f: Parser.t => 'a) => list('a) = | |
(p: Parser.t, ~f) => | |
if (p.token == Asterisk) { | |
[]; | |
} else { | |
let item = f(p); | |
let l = parseList(p, ~f); | |
[item, ...l]; | |
} | |
[@progress] | |
and parseExpression = (p: Parser.t) => { | |
switch (p.token) { | |
| Lparen => | |
Parser.next(p); | |
let e1 = parseExpression(p); | |
Parser.expect(p, Plus); | |
let e2 = parseExpression(p); | |
Parser.expect(p, Lparen); | |
Expr.Plus(e1, e2); | |
| _ => Expr.Int(parseInt(p)) | |
}; | |
} | |
[@progress] | |
and parseListExpression = p => parseList(p, ~f=parseExpression); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment