Skip to content

Commit

Permalink
WIP: Fix parser for mlton
Browse files Browse the repository at this point in the history
  • Loading branch information
marzipankaiser committed Oct 19, 2023
1 parent 2a8df7f commit 89addab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
20 changes: 10 additions & 10 deletions examples/casestudies/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def or[R] { p: => R } { q: => R } =
def opt[R] { p: => R }: Option[R] / Parser =
or { Some(p()) } { None() }
def many { p: => Unit }: Unit / Parser =
or { some { p() } } { () }
def many[R] { p: => R }: List[R] / Parser =
or { some[R] { p() } } { Nil() }
def some { p: => Unit }: Unit / Parser =
{ p(); many { p() } }
def some[R] { p: => R }: List[R] / Parser =
{ Cons(p(), many { p() }) }
```

## Example: A Simple Expression Language
Expand Down Expand Up @@ -171,19 +171,19 @@ The following example implements an example
```
It uses local (mutable) variables to count the number of leafs as semantic action.
```
def parseCalls(): Int / Parser =
def parseCalls(): Int / Parser = {
or { number(); 1 } {
var count = 1;
ident();
punct("(");
count = count + parseCalls();
many {
val count = parseCalls() +
sum(many {
punct(",");
count = count + parseCalls()
};
parseCalls()
});
punct(")");
count
}
}
```
Notice how the user defined combinator `many` feels like a built-in control operator
`while`.
Expand Down
5 changes: 5 additions & 0 deletions libraries/jit/immutable/list.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def any(l: List[Boolean]): Boolean = l match {
case Nil() => false
case Cons(v,rest) => if (v) { true } else { any(rest) }
}
def sum(l: List[Int]): Int = {
var res = 0
l.foreach { i => res = res + i }
res
}

// elements that satisfy the predicate go into the left list
def partition[A](l: List[A]) { pred: A => Boolean }: (List[A], List[A]) = {
Expand Down
5 changes: 5 additions & 0 deletions libraries/ml/immutable/list.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def any(l: List[Boolean]): Boolean = l match {
case Nil() => false
case Cons(v,rest) => if (v) { true } else { any(rest) }
}
def sum(l: List[Int]): Int = {
var res = 0
l.foreach { i => res = res + i }
res
}

def nonEmpty[A](l: List[A]): Boolean = l match {
case Nil() => false
Expand Down

0 comments on commit 89addab

Please sign in to comment.